prime.c 7.53 KB
Newer Older
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
/*========================================================================
               Copyright (C) 1996-2002 by Jorn Lind-Nielsen
                            All rights reserved

    Permission is hereby granted, without written agreement and without
    license or royalty fees, to use, reproduce, prepare derivative
    works, distribute, and display this software and its documentation
    for any purpose, provided that (1) the above copyright notice and
    the following two paragraphs appear in all copies of the source code
    and (2) redistributions, including without limitation binaries,
    reproduce these notices in the supporting documentation. Substantial
    modifications to this software may be copyrighted by their authors
    and need not follow the licensing terms described here, provided
    that the new terms are clearly indicated in all files where they apply.

    IN NO EVENT SHALL JORN LIND-NIELSEN, OR DISTRIBUTORS OF THIS
    SOFTWARE BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, SPECIAL,
    INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OF THIS
    SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE AUTHORS OR ANY OF THE
    ABOVE PARTIES HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

    JORN LIND-NIELSEN SPECIFICALLY DISCLAIM ANY WARRANTIES, INCLUDING,
    BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
    FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
    ON AN "AS IS" BASIS, AND THE AUTHORS AND DISTRIBUTORS HAVE NO
    OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR
    MODIFICATIONS.
========================================================================*/

/*************************************************************************
31
  $Header: /Volumes/CVS/repository/spot/spot/buddy/src/prime.c,v 1.2 2003/05/05 13:45:08 aduret Exp $
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
32
33
34
35
36
37
38
39
40
41
42
43
44
45
  FILE:  prime.c
  DESCR: Prime number calculations
  AUTH:  Jorn Lind
  DATE:  (C) feb 2001
*************************************************************************/
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include "prime.h"


#define Random(i) ( (rand() % (i)) + 1 )
#define isEven(src) (!((src) & 0x1))
#define hasFactor(src,n) ( (((src)!=(n)) && ((src)%(n) == 0)) )
46
#define BitIsSet(src,b) ( ((src) & (1U<<(b))) != 0 )
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117

#define CHECKTIMES 20

#if defined(BUDDYUINT64)
 typedef BUDDYUINT64 UINT64;
 #define BUILTIN64
#elif defined(__GNUC__) || defined(__KCC)
 typedef long long UINT64;
 #define BUILTIN64
#elif defined(_MSV_VER)
 typedef unsigned _int64 UINT64;
 #define BUILTIN64
#else
 typedef struct __UINT64
 {
   unsigned int hi;
   unsigned int lo;
 } UINT64;

 #define MAX(a,b) ((a) > (b) ? (a) : (b))
 #define GETCARRY(a,b) ( ((a)+(b)) < MAX((a),(b)) ? 1 : 0 )
#endif


#ifndef BUILTIN64
/*************************************************************************
   64 bit unsigned int arithmetics
*************************************************************************/

static UINT64 u64_mul(unsigned int x, unsigned int y)
{
  UINT64 res;
  unsigned int yh = 0;
  unsigned int yl = y;
  int i;

  res.lo = res.hi = 0;

  for (i=0 ; i<32 ; ++i)
  {
    if (x & 0x1)
    {
      unsigned int carry = GETCARRY(res.lo,yl);
      res.lo += yl;
      res.hi += yh + carry;
    }

    yh = (yh << 1) | (yl & 0x80000000 ? 1 : 0);
    yl = (yl << 1);

    x >>= 1;
  }

  return res;
}


static void u64_shl(UINT64* a, unsigned int *carryOut)
{
  *carryOut = (*carryOut << 1) | (a->hi & 0x80000000 ? 0x1 : 0x0);
  a->hi     = (a->hi << 1) | (a->lo & 0x80000000 ? 0x1 : 0x0);
  a->lo     = (a->lo << 1);
}


static unsigned int u64_mod(UINT64 dividend, unsigned int divisor)
{
  unsigned int remainder = 0;
  int i;

  u64_shl(&dividend, &remainder);
118

Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
  for (i=0 ; i<64 ; ++i)
  {
    if (remainder >= divisor)
      remainder -= divisor;

    u64_shl(&dividend, &remainder);
  }

  return remainder >> 1;
}
#endif /* BUILTIN64 */

#ifdef BUILTIN64
#define u64_mulmod(a,b,c) ((unsigned int)( ((UINT64)a*(UINT64)b)%(UINT64)c ));
#else
#define u64_mulmod(a,b,c) u64_mod( u64_mul((a),(b)), (c) );
#endif


/*************************************************************************
  Miller Rabin check
*************************************************************************/

static unsigned int numberOfBits(unsigned int src)
{
  unsigned int b;

  if (src == 0)
    return 0;
148

Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
  for (b=(sizeof(unsigned int)*8)-1 ; b>0 ; --b)
    if (BitIsSet(src,b))
      return b+1;

  return 1;
}



static int isWitness(unsigned int witness, unsigned int src)
{
  unsigned int bitNum = numberOfBits(src-1)-1;
  unsigned int d = 1;
  int i;

  for (i=bitNum ; i>=0 ; --i)
  {
    unsigned int x = d;

    d = u64_mulmod(d,d,src);
169

Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
170
171
    if (d == 1  &&  x != 1  &&  x != src-1)
      return 1;
172

Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
    if (BitIsSet(src-1,i))
      d = u64_mulmod(d,witness,src);
  }

  return d != 1;
}


static int isMillerRabinPrime(unsigned int src)
{
  int n;

  for (n=0 ; n<CHECKTIMES ; ++n)
  {
    unsigned int witness = Random(src-1);

    if (isWitness(witness,src))
      return 0;
  }

  return 1;
}


/*************************************************************************
  Basic prime searching stuff
*************************************************************************/

static int hasEasyFactors(unsigned int src)
{
  return hasFactor(src, 3)
      || hasFactor(src, 5)
      || hasFactor(src, 7)
      || hasFactor(src, 11)
      || hasFactor(src, 13);
}


static int isPrime(unsigned int src)
{
  if (hasEasyFactors(src))
    return 0;

  return isMillerRabinPrime(src);
}


/*************************************************************************
  External interface
*************************************************************************/

unsigned int bdd_prime_gte(unsigned int src)
{
  if (isEven(src))
    ++src;

  while (!isPrime(src))
    src += 2;

  return src;
}


unsigned int bdd_prime_lte(unsigned int src)
{
  if (isEven(src))
     --src;

  while (!isPrime(src))
     src -= 2;

  return src;
}

247
248
249
250
251
252
253
254
255
256
257
258
259
unsigned int bdd_nextpower(unsigned int v)
{
  v--;
  v |= v >> 1;
  v |= v >> 2;
  v |= v >> 4;
  v |= v >> 8;
  v |= v >> 16;
#if INT_MAX > 0x7FFFFFFF
  v |= v >> 32;
#endif
  return v + 1;
}
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295


/*************************************************************************
   Testing
*************************************************************************/

#if 0
int main()
{
  printf("Nb0 = %u\n", numberOfBits(0));
  printf("Nb1 = %u\n", numberOfBits(1));
  printf("Nb2 = %u\n", numberOfBits(2));
  printf("Nb3 = %u\n", numberOfBits(3));
  printf("Nb5 = %u\n", numberOfBits(5));
  printf("Nb9 = %u\n", numberOfBits(9));
  printf("Nb15 = %u\n", numberOfBits(15));
  printf("Nb17 = %u\n", numberOfBits(17));
  return 0;
}
#endif


#if 0
void testMul(unsigned int a, unsigned int b)
{
  UINT64 x = u64_mul(a,b);
  long long z1 = (long long)a * (long long)b;
  long long z2 = ((long long)x.hi << 32) + (long long)x.lo;
  if (z1 != z2)
    printf("%d * %d = %lld,%lld\n", a, b, z1, z2);
}


void testMod(unsigned int a, unsigned int b, unsigned int c)
{
  UINT64 x = u64_mul(a,b);
296

Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
297
298
299
300
  long long z1 = (long long)a * (long long)b;
  long long z2 = ((long long)x.hi << 32) + (long long)x.lo;
  unsigned int m1 = z1 % c;
  unsigned int m2 = u64_mod(x,c);
301

Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
  if (z1 != z2)
    printf("%d * %d = %lld,%lld\n", a, b, z1, z2);

  if (m1 != m2)
    printf("%llu %% %u = %u,%u\n", z1, c, m1, m2);
}
#endif

#if 0
int main()
{
  int n;

  srand(time(NULL));

  for (n=0 ; n<1000 ; ++n)
  {
    unsigned int x = Random(10000)+2;
    int a = bdd_prime_lte(x);
    int b=_bdd_prime_lte(x);
    /*printf("%d: %d, %d  ", x, );*/
    if (a != b)
      printf("ERROR");
    /*printf("\n");*/
  }
327

Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
328
329
330
331
332
333
  return 0;
}
#endif


/* EOF */