51
62
namespace OpenBabel
53
64
#define MAXPRIMES 256
54
static int primes[MAXPRIMES] = {
55
1, 2, 3, 5, 7, 11, 13, 17, 19, 23,
56
29, 31, 37, 41, 43, 47, 53, 59, 61, 67,
57
71, 73, 79, 83, 89, 97, 101, 103, 107, 109,
58
113, 127, 131, 137, 139, 149, 151, 157, 163, 167,
59
173, 179, 181, 191, 193, 197, 199, 211, 223, 227,
60
229, 233, 239, 241, 251, 257, 263, 269, 271, 277,
61
281, 283, 293, 307, 311, 313, 317, 331, 337, 347,
62
349, 353, 359, 367, 373, 379, 383, 389, 397, 401,
63
409, 419, 421, 431, 433, 439, 443, 449, 457, 461,
64
463, 467, 479, 487, 491, 499, 503, 509, 521, 523,
65
541, 547, 557, 563, 569, 571, 577, 587, 593, 599,
66
601, 607, 613, 617, 619, 631, 641, 643, 647, 653,
67
659, 661, 673, 677, 683, 691, 701, 709, 719, 727,
68
733, 739, 743, 751, 757, 761, 769, 773, 787, 797,
69
809, 811, 821, 823, 827, 829, 839, 853, 857, 859,
70
863, 877, 881, 883, 887, 907, 911, 919, 929, 937,
71
941, 947, 953, 967, 971, 977, 983, 991, 997, 1009,
72
1013, 1019, 1021, 1031, 1033, 1039, 1049, 1051, 1061, 1063,
73
1069, 1087, 1091, 1093, 1097, 1103, 1109, 1117, 1123, 1129,
74
1151, 1153, 1163, 1171, 1181, 1187, 1193, 1201, 1213, 1217,
75
1223, 1229, 1231, 1237, 1249, 1259, 1277, 1279, 1283, 1289,
76
1291, 1297, 1301, 1303, 1307, 1319, 1321, 1327, 1361, 1367,
77
1373, 1381, 1399, 1409, 1423, 1427, 1429, 1433, 1439, 1447,
78
1451, 1453, 1459, 1471, 1481, 1483, 1487, 1489, 1493, 1499,
79
1511, 1523, 1531, 1543, 1549, 1553, 1559, 1567, 1571, 1579,
80
1583, 1597, 1601, 1607, 1609, 1613
65
static int primes[MAXPRIMES] = {
66
1, 2, 3, 5, 7, 11, 13, 17, 19, 23,
67
29, 31, 37, 41, 43, 47, 53, 59, 61, 67,
68
71, 73, 79, 83, 89, 97, 101, 103, 107, 109,
69
113, 127, 131, 137, 139, 149, 151, 157, 163, 167,
70
173, 179, 181, 191, 193, 197, 199, 211, 223, 227,
71
229, 233, 239, 241, 251, 257, 263, 269, 271, 277,
72
281, 283, 293, 307, 311, 313, 317, 331, 337, 347,
73
349, 353, 359, 367, 373, 379, 383, 389, 397, 401,
74
409, 419, 421, 431, 433, 439, 443, 449, 457, 461,
75
463, 467, 479, 487, 491, 499, 503, 509, 521, 523,
76
541, 547, 557, 563, 569, 571, 577, 587, 593, 599,
77
601, 607, 613, 617, 619, 631, 641, 643, 647, 653,
78
659, 661, 673, 677, 683, 691, 701, 709, 719, 727,
79
733, 739, 743, 751, 757, 761, 769, 773, 787, 797,
80
809, 811, 821, 823, 827, 829, 839, 853, 857, 859,
81
863, 877, 881, 883, 887, 907, 911, 919, 929, 937,
82
941, 947, 953, 967, 971, 977, 983, 991, 997, 1009,
83
1013, 1019, 1021, 1031, 1033, 1039, 1049, 1051, 1061, 1063,
84
1069, 1087, 1091, 1093, 1097, 1103, 1109, 1117, 1123, 1129,
85
1151, 1153, 1163, 1171, 1181, 1187, 1193, 1201, 1213, 1217,
86
1223, 1229, 1231, 1237, 1249, 1259, 1277, 1279, 1283, 1289,
87
1291, 1297, 1301, 1303, 1307, 1319, 1321, 1327, 1361, 1367,
88
1373, 1381, 1399, 1409, 1423, 1427, 1429, 1433, 1439, 1447,
89
1451, 1453, 1459, 1471, 1481, 1483, 1487, 1489, 1493, 1499,
90
1511, 1523, 1531, 1543, 1549, 1553, 1559, 1567, 1571, 1579,
91
1583, 1597, 1601, 1607, 1609, 1613
83
static unsigned int isqrt( unsigned int val )
94
static unsigned int isqrt( unsigned int val )
85
96
register unsigned int temp;
86
97
register unsigned int rem;
87
98
register int i,result;
90
101
while( !(val&((unsigned int)3<<30)) && i )
98
109
rem = (val>>30)-1;
105
116
rem = (rem<<2) | (val>>30);
109
120
temp = result<<1;
123
static int IsOddPrime( unsigned int x )
134
static int IsOddPrime( unsigned int x )
125
136
register unsigned int root;
126
137
register unsigned int i;
129
140
for( i=2; i<MAXPRIMES-1; i++ )
131
142
if( (x%primes[i]) == 0 )
133
144
if( (unsigned int) primes[i] >= root )
137
148
for( i=primes[MAXPRIMES-1]; i<=root; i+=2 )
143
static int RelativelyPrime( unsigned int x, unsigned int y )
154
static int RelativelyPrime( unsigned int x, unsigned int y )
145
156
if( BothEven(x,y) )
154
165
while( IsEven(x) );
169
180
while( IsEven(x) );
178
189
while( IsEven(y) );
181
192
return( x == 1 );
184
void DoubleAdd( DoubleType *x, unsigned int y )
195
void DoubleAdd( DoubleType *x, unsigned int y )
191
void DoubleMultiply( unsigned int x, unsigned int y, DoubleType *z )
202
void DoubleMultiply( unsigned int x, unsigned int y, DoubleType *z )
193
204
register unsigned int x0, x1, x2, x3;
194
205
register unsigned int hx, lx;
195
206
register unsigned int hy, ly;
207
218
x1 += HiPart(x0);
212
223
z->hi = HiPart(x1) + x3;
213
224
z->lo = (LoPart(x1)<<16) + LoPart(x0);
216
static int LeadingZeros( unsigned int x )
227
static int LeadingZeros( unsigned int x )
218
229
static int table[256] = {
219
0,1,2,2,3,3,3,3,4,4,4,4,4,4,4,4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
220
6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
221
7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
222
7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
223
8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
224
8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
225
8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
226
8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8
230
0,1,2,2,3,3,3,3,4,4,4,4,4,4,4,4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
231
6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
232
7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
233
7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
234
8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
235
8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
236
8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
237
8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8
229
240
if( x >= (1<<16) )
231
242
if( x >= (1<<24) )
233
244
return 8-table[x>>24];
236
return 16-table[x>>16];
247
return 16-table[x>>16];
238
249
else if( x >= (1<<8) )
240
251
return 24-table[x>>8];
246
unsigned int DoubleModulus( DoubleType *n, unsigned int d )
257
unsigned int DoubleModulus( DoubleType *n, unsigned int d )
248
259
register unsigned int d1, d0;
249
260
register unsigned int r1, r0;
250
261
register unsigned int m,s;
252
263
s = LeadingZeros(d);
256
267
n->hi = (n->hi<<s) | (n->lo>>(32-s));
257
268
n->lo = n->lo << s;