2 * zfunc - extended precision integral arithmetic non-primitive routines
4 * Copyright (C) 1999-2007 David I. Bell, Landon Curt Noll and Ernest Bowen
6 * Primary author: David I. Bell
8 * Calc is open software; you can redistribute it and/or modify it under
9 * the terms of the version 2.1 of the GNU Lesser General Public License
10 * as published by the Free Software Foundation.
12 * Calc is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
15 * Public License for more details.
17 * A copy of version 2.1 of the GNU Lesser General Public License is
18 * distributed with calc under the filename COPYING-LGPL. You should have
19 * received a copy with calc; if not, write to Free Software Foundation, Inc.
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 * @(#) $Revision: 30.3 $
23 * @(#) $Id: zfunc.c,v 30.3 2013/08/11 08:41:38 chongo Exp $
24 * @(#) $Source: /usr/local/src/bin/calc/RCS/zfunc.c,v $
26 * Under source code control: 1990/02/15 01:48:27
27 * File existed as early as: before 1990
29 * Share and enjoy! :-) http://www.isthe.com/chongo/tech/comp/calc/
35 ZVALUE _tenpowers_
[TEN_MAX
+1]; /* table of 10^2^n */
37 STATIC
long *power10
= NULL
;
38 STATIC
int max_power10_exp
= 0;
44 * or: unsigned long long x
45 * or: long x and x >= 0
46 * or: long long x and x >= 0
48 * If issq_mod4k[x & 0xfff] == 0, then x cannot be a perfect square
49 * else x might be a perfect square.
51 STATIC USB8 issq_mod4k
[1<<12] = {
52 1,1,0,0,1,0,0,0,0,1,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,
53 0,1,0,0,1,0,0,0,0,1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,
54 1,1,0,0,1,0,0,0,0,1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,
55 0,1,0,0,1,0,0,0,0,1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,1,0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0,0,1,0,0,0,0,1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,
179 0,1,0,0,1,0,0,0,0,1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,
184 * Compute the factorial of a number.
187 zfact(ZVALUE z
, ZVALUE
*dest
)
189 long ptwo
; /* count of powers of two */
190 long n
; /* current multiplication value */
191 long m
; /* reduced multiplication value */
192 long mul
; /* collected value to multiply by */
196 math_error("Negative argument for factorial");
200 math_error("Very large factorial");
208 * Multiply numbers together, but squeeze out all powers of two.
209 * We will put them back in at the end. Also collect multiple
210 * numbers together until there is a risk of overflow.
213 for (m
= n
; ((m
& 0x1) == 0); m
>>= 1)
215 if (mul
<= MAXLONG
/m
) {
219 zmuli(res
, mul
, &temp
);
225 * Multiply by the remaining value, then scale result by
226 * the proper power of two.
229 zmuli(res
, mul
, &temp
);
233 zshift(res
, ptwo
, &temp
);
240 * Compute the permutation function M! / (M - N)!.
243 zperm(ZVALUE z1
, ZVALUE z2
, ZVALUE
*res
)
246 ZVALUE cur
, tmp
, ans
;
248 if (zisneg(z1
) || zisneg(z2
)) {
249 math_error("Negative argument for permutation");
252 if (zrel(z1
, z2
) < 0) {
253 math_error("Second arg larger than first in permutation");
257 math_error("Very large permutation");
262 zsub(z1
, _one_
, &cur
);
263 while (--count
> 0) {
264 zmul(ans
, cur
, &tmp
);
267 zsub(cur
, _one_
, &tmp
);
276 * docomb evaluates binomial coefficient when z1 >= 0, z2 >= 0
279 docomb(ZVALUE z1
, ZVALUE z2
, ZVALUE
*res
)
282 ZVALUE mul
, div
, temp
;
290 if (zrel(z2
, z1
) > 0)
294 if (zge31b(z2
) && zge31b(temp
)) {
298 if (zrel(temp
, z2
) < 0)
299 count
= ztofull(temp
);
312 for (i
= 2; i
<= count
; i
++) {
314 dh
[0] = (HALF
)(i
& BASE1
);
315 dh
[1] = (HALF
)(i
>> BASEB
);
316 div
.len
= 1 + (dh
[1] != 0);
320 zsub(mul
, _one_
, &temp
);
323 zmul(ans
, mul
, &temp
);
325 zquo(temp
, div
, &ans
, 0);
334 * Compute the combinatorial function M! / ( N! * (M - N)! ).
335 * Returns 0 if result is 0
339 * -2 if too complicated
340 * 3 result stored at res
343 zcomb(ZVALUE z1
, ZVALUE z2
, ZVALUE
*res
)
348 if (z2
.sign
|| (!z1
.sign
&& zrel(z2
, z1
) > 0))
354 zsub(z1
, _one_
, &z3
);
357 r
= docomb(z4
, z2
, res
);
372 return docomb(z1
, z2
, res
);
377 * Compute the Jacobi function (m / n) for odd n.
379 * The property of the Jacobi function is: If n>2 is prime then
381 * the result is 1 if m == x^2 (mod n) for some x.
382 * otherwise the result is -1.
384 * If n is not prime, then the result does not prove that n is not prine
385 * when the value of the Jacobi is 1.
387 * Jacobi evaluation of (m / n), where n > 0 is odd AND m > 0 is odd:
389 * rule 0: (0 / n) == 0
390 * rule 1: (1 / n) == 1
391 * rule 2: (m / n) == (a / n) if m == a % n
392 * rule 3: (m / n) == (2*m / n) if n == 1 % 8 OR n == 7 % 8
393 * rule 4: (m / n) == -(2*m / n) if n != 1 & 8 AND n != 7 % 8
394 * rule 5: (m / n) == (n / m) if m == 3 % 4 AND n == 3 % 4
395 * rule 6: (m / n) == -(n / m) if m != 3 % 4 OR n != 3 % 4
397 * NOTE: This function returns 0 in invalid Jacobi parameters:
398 * m < 0 OR n is even OR n < 1.
401 zjacobi(ZVALUE z1
, ZVALUE z2
)
408 if (ziszero(z1
) || zisneg(z1
))
410 if (ziseven(z2
) || zisneg(z2
))
413 /* assume a value of 1 unless we find otherwise */
430 zshift(p
, -lowbit
, &tmp
);
433 if ((lowbit
& 1) && (((*q
.v
& 0x7) == 3) ||
434 ((*q
.v
& 0x7) == 5)))
442 if ((*p
.v
& *q
.v
& 0x3) == 3)
452 * Return the Fibonacci number F(n).
453 * This is evaluated by recursively using the formulas:
454 * F(2N+1) = F(N+1)^2 + F(N)^2
456 * F(2N) = F(N+1)^2 - F(N-1)^2
459 zfib(ZVALUE z
, ZVALUE
*res
)
463 ZVALUE fnm1
, fn
, fnp1
; /* consecutive fibonacci values */
468 math_error("Very large Fibonacci number");
476 sign
= z
.sign
&& ((n
& 0x1) == 0);
479 res
->sign
= (BOOL
)sign
;
504 zadd(fnm1
, fn
, &fnp1
);
506 zsub(fnp1
, fn
, &fnm1
);
513 res
->sign
= (BOOL
)sign
;
518 * Compute the result of raising one number to the power of another
519 * The second number is assumed to be non-negative.
520 * It cannot be too large except for trivial cases.
523 zpowi(ZVALUE z1
, ZVALUE z2
, ZVALUE
*res
)
525 int sign
; /* final sign of number */
526 unsigned long power
; /* power to raise to */
527 FULL bit
; /* current bit value */
528 long twos
; /* count of times 2 is in result */
531 sign
= (z1
.sign
&& zisodd(z2
));
534 if (ziszero(z2
) && !ziszero(z1
)) { /* number raised to power 0 */
538 if (zisabsleone(z1
)) { /* 0, 1, or -1 raised to a power */
540 ans
.sign
= (BOOL
)sign
;
547 math_error("Raising to very large power");
550 power
= ztoulong(z2
);
551 if (zistwo(z1
)) { /* two raised to a power */
552 zbitvalue((long) power
, res
);
556 * See if this is a power of ten
558 if (zistiny(z1
) && (*z1
.v
== 10)) {
559 ztenpow((long) power
, res
);
560 res
->sign
= (BOOL
)sign
;
564 * Handle low powers specially
567 switch ((int) power
) {
570 ans
.v
= alloc(ans
.len
);
572 ans
.sign
= (BOOL
)sign
;
582 res
->sign
= (BOOL
)sign
;
592 * Shift out all powers of twos so the multiplies are smaller.
593 * We will shift back the right amount when done.
598 ans
.v
= alloc(z1
.len
);
608 * Compute the power by squaring and multiplying.
609 * This uses the left to right method of power raising.
612 while ((bit
& power
) == 0)
617 zmul(ans
, z1
, &temp
);
627 zmul(ans
, z1
, &temp
);
634 * Scale back up by proper power of two
637 zshift(ans
, twos
, &temp
);
642 ans
.sign
= (BOOL
)sign
;
648 * Compute ten to the specified power
649 * This saves some work since the squares of ten are saved.
652 ztenpow(long power
, ZVALUE
*res
)
663 _tenpowers_
[0] = _ten_
;
664 for (i
= 0; power
; i
++) {
665 if (_tenpowers_
[i
].len
== 0) {
667 zsquare(_tenpowers_
[i
-1], &_tenpowers_
[i
]);
669 math_error("cannot compute 10^2^(TEN_MAX+1)");
674 zmul(ans
, _tenpowers_
[i
], &temp
);
685 * Calculate modular inverse suppressing unnecessary divisions.
686 * This is based on the Euclidean algorithm for large numbers.
687 * (Algorithm X from Knuth Vol 2, section 4.5.2. and exercise 17)
688 * Returns TRUE if there is no solution because the numbers
689 * are not relatively prime.
692 zmodinv(ZVALUE u
, ZVALUE v
, ZVALUE
*res
)
694 FULL q1
, q2
, ui3
, vi3
, uh
, vh
, A
, B
, C
, D
, T
;
695 ZVALUE u2
, u3
, v2
, v3
, qz
, tmp1
, tmp2
, tmp3
;
698 if (zisneg(u
) || (zrel(u
, v
) >= 0))
707 * Loop here while the size of the numbers remain above
708 * the size of a HALF. Throughout this loop u3 >= v3.
710 while ((u3
.len
> 1) && !ziszero(v3
)) {
712 #if LONG_BITS == BASEB
713 uh
= u3
.v
[u3
.len
- 1];
714 if (v3
.len
== u3
.len
)
715 vh
= v3
.v
[v3
.len
- 1];
717 uh
= (((FULL
) u3
.v
[u3
.len
- 1]) << BASEB
) + u3
.v
[u3
.len
- 2];
718 if ((v3
.len
+ 1) >= u3
.len
)
719 vh
= v3
.v
[v3
.len
- 1];
720 if (v3
.len
== u3
.len
)
721 vh
= (vh
<< BASEB
) + v3
.v
[v3
.len
- 2];
729 * Calculate successive quotients of the continued fraction
730 * expansion using only single precision arithmetic until
731 * greater precision is required.
733 while ((vh
+ C
) && (vh
+ D
)) {
734 q1
= (uh
+ A
) / (vh
+ C
);
735 q2
= (uh
+ B
) / (vh
+ D
);
750 * If B is zero, then we made no progress because
751 * the calculation requires a very large quotient.
752 * So we must do this step of the calculation in
756 zquo(u3
, v3
, &qz
, 0);
758 zsub(u2
, tmp1
, &tmp2
);
764 zsub(u3
, tmp1
, &tmp2
);
773 * Apply the calculated A,B,C,D numbers to the current
774 * values to update them as if the full precision
775 * calculations had been carried out.
777 zmuli(u2
, (long) A
, &tmp1
);
778 zmuli(v2
, (long) B
, &tmp2
);
779 zadd(tmp1
, tmp2
, &tmp3
);
782 zmuli(u2
, (long) C
, &tmp1
);
783 zmuli(v2
, (long) D
, &tmp2
);
787 zadd(tmp1
, tmp2
, &v2
);
790 zmuli(u3
, (long) A
, &tmp1
);
791 zmuli(v3
, (long) B
, &tmp2
);
792 zadd(tmp1
, tmp2
, &tmp3
);
795 zmuli(u3
, (long) C
, &tmp1
);
796 zmuli(v3
, (long) D
, &tmp2
);
800 zadd(tmp1
, tmp2
, &v3
);
806 * Here when the remaining numbers become single precision in size.
807 * Finish the procedure using single precision calculations.
809 if (ziszero(v3
) && !zisone(u3
)) {
822 zmuli(v2
, (long) q1
, &tmp1
);
823 zsub(u2
, tmp1
, &tmp2
);
848 * Compute the greatest common divisor of a pair of integers.
851 zgcd(ZVALUE z1
, ZVALUE z2
, ZVALUE
*res
)
854 LEN len
, l
, m
, n
, o
, p
, q
;
856 HALF
*a
, *a0
, *A
, *b
, *b0
, *B
, *c
, *d
;
861 if (zisunit(z1
) || zisunit(z2
)) {
867 if (ziszero(z1
) || !zcmp(z1
, z2
)) {
877 while (!(z1
.v
[o
] | z2
.v
[o
])) o
++; /* Count common zero digits */
884 u
= *c
| *d
; /* Count common zero bits */
892 while (!*c
) { /* Removing zero digits */
903 u
= *d
; /* Count zero bits for *d */
914 memcpy(A
, c
, m
* sizeof(HALF
)); /* Copy c[] to A[] */
916 /* Copy d[] to B[], shifting if necessary */
923 f
= f
<< BASEB
| *--d
;
924 *--b
= (HALF
) (f
>> q
);
926 if (B
[n
-1] == 0) n
--;
928 else memcpy(B
, d
, n
* sizeof(HALF
));
930 if (n
== 1) { /* One digit case; use Euclid's algorithm */
935 if (m
== 1) { /* a has one digit */
937 if (v
> 1) { /* Euclid's algorithm */
942 f
= (FULL
) u
<< BASEB
| *--b
;
945 while (u
) { w
= v
% u
; v
= u
; u
= w
; }
951 gcd
.v
= alloc(len
+ 1);
952 /* Common zero digits */
953 if (o
) memset(gcd
.v
, 0, o
* sizeof(HALF
));
954 /* Left shift for common zero bits */
961 f
= f
>> BASEB
| (FULL
) *b
++ << p
;
964 if (f
>>= BASEB
) {len
++; *a
= (HALF
) f
;}
966 memcpy(gcd
.v
+ o
, b0
, n
* sizeof(HALF
));
976 u
= B
[n
-1]; /* Bit count for b */
984 while (m
) { /* START OF MAIN LOOP */
985 if (m
- n
< 2 || needw
) {
989 while (!(u
& v
)) { /* count zero bits for *a0 */
994 if (q
) { /* right-justify a */
999 f
= f
<< BASEB
| *--a
;
1000 *a
= (HALF
) (f
>> q
);
1002 if (!a0
[m
-1]) m
--; /* top digit vanishes */
1008 j
= (m
- 1) * BASEB
;
1009 while (u
>>= 1) j
++; /* counting bits for a */
1011 if (h
< 0) { /* swapping to get h > 0 */
1023 if (needw
) { /* find w = minv(*b0, h0) */
1030 if (u
& x
) { u
-= v
* x
; w
|= x
;}
1036 if (h
< BASEB
) g
&= (1 << h
) - 1;
1045 if (g
> 1) { /* a - g * b case */
1048 f
= (FULL
) *a
- g
* *b
++ - f
;
1062 while (m
&& !*a0
) { /* Removing trailing zeros */
1066 if (f
) { /* a - g * b < 0 */
1067 while (m
> 1 && a0
[m
-1] == BASE1
) m
--;
1076 } else { /* abs(a - b) case */
1077 while (i
&& *a
++ == *b
++) i
--;
1079 if (m
== n
) { /* a and b same length */
1080 if (i
) { /* a not equal to b */
1081 while (m
&& a0
[m
-1] == b0
[m
-1]) m
--;
1082 if (a0
[m
-1] < b0
[m
-1]) {
1083 /* Swapping since a < b */
1094 f
= (FULL
) *a
- *b
++ - f
;
1100 } else { /* a has more digits than b */
1106 f
= (FULL
) *a
- *b
++ - f
;
1111 if (f
) { while (!*a
) *a
++ = BASE1
;
1117 while (m
&& !*a0
) { /* Removing trailing zeros */
1122 while (m
&& !a0
[m
-1]) m
--; /* Removing leading zeros */
1124 if (m
== 1) { /* a has one digit */
1126 if (v
> 1) { /* Euclid's algorithm */
1131 f
= (FULL
) u
<< BASEB
| *--b
;
1134 while (u
) { w
= v
% u
; v
= u
; u
= w
; }
1140 gcd
.v
= alloc(len
+ 1);
1141 if (o
) memset(gcd
.v
, 0, o
* sizeof(HALF
)); /* Common zero digits */
1142 if (p
) { /* Left shift for common zero bits */
1148 f
= (FULL
) *b
++ << p
| f
;
1153 len
++; *a
= (HALF
) f
;
1156 memcpy(gcd
.v
+ o
, b0
, n
* sizeof(HALF
));
1167 * Compute the lcm of two integers (least common multiple).
1168 * This is done using the formula: gcd(a,b) * lcm(a,b) = a * b.
1171 zlcm(ZVALUE z1
, ZVALUE z2
, ZVALUE
*res
)
1173 ZVALUE temp1
, temp2
;
1175 zgcd(z1
, z2
, &temp1
);
1176 zequo(z1
, temp1
, &temp2
);
1178 zmul(temp2
, z2
, res
);
1184 * Return whether or not two numbers are relatively prime to each other.
1187 zrelprime(ZVALUE z1
, ZVALUE z2
)
1189 FULL rem1
, rem2
; /* remainders */
1195 if (ziseven(z1
) && ziseven(z2
)) /* false if both even */
1197 if (zisunit(z1
) || zisunit(z2
)) /* true if either is a unit */
1199 if (ziszero(z1
) || ziszero(z2
)) /* false if either is zero */
1201 if (zistwo(z1
) || zistwo(z2
)) /* true if either is two */
1204 * Try reducing each number by the product of the first few odd primes
1205 * to see if any of them are a common factor.
1207 rem1
= zmodi(z1
, (FULL
)3 * 5 * 7 * 11 * 13);
1208 rem2
= zmodi(z2
, (FULL
)3 * 5 * 7 * 11 * 13);
1209 if (((rem1
% 3) == 0) && ((rem2
% 3) == 0))
1211 if (((rem1
% 5) == 0) && ((rem2
% 5) == 0))
1213 if (((rem1
% 7) == 0) && ((rem2
% 7) == 0))
1215 if (((rem1
% 11) == 0) && ((rem2
% 11) == 0))
1217 if (((rem1
% 13) == 0) && ((rem2
% 13) == 0))
1220 * Try a new batch of primes now
1222 rem1
= zmodi(z1
, (FULL
)17 * 19 * 23);
1223 rem2
= zmodi(z2
, (FULL
)17 * 19 * 23);
1224 if (((rem1
% 17) == 0) && ((rem2
% 17) == 0))
1226 if (((rem1
% 19) == 0) && ((rem2
% 19) == 0))
1228 if (((rem1
% 23) == 0) && ((rem2
% 23) == 0))
1231 * Yuk, we must actually compute the gcd to know the answer
1234 result
= zisunit(rem
);
1241 * Compute the integer floor of the log of an integer to a specified base.
1242 * The signs of the integers and base are ignored.
1243 * Example: zlog(123456, 10) = 5.
1246 zlog(ZVALUE z
, ZVALUE base
)
1248 ZVALUE
*zp
; /* current square */
1249 long power
; /* current power */
1250 ZVALUE temp
; /* temporary */
1251 ZVALUE squares
[32]; /* table of squares of base */
1259 * Make sure that the numbers are nonzero and the base is > 1
1261 if (ziszero(z
) || ziszero(base
) || zisone(base
)) {
1262 math_error("Zero or too small argument argument for zlog!!!");
1267 * Some trivial cases.
1269 power
= zrel(z
, base
);
1273 /* base - power of two */
1274 if (zisonebit(base
))
1275 return (zhighbit(z
) / zlowbit(base
));
1278 if (base
.len
== 1 && base
.v
[0] == 10)
1279 return zlog10(z
, NULL
);
1281 * Now loop by squaring the base each time, and see whether or
1282 * not each successive square is still smaller than the number.
1286 while (zp
->len
* 2 - 1 <= z
.len
&& zrel(z
, *zp
) > 0) {
1287 /* while square not too large */
1288 zsquare(*zp
, zp
+ 1);
1293 * Now back down the squares,
1296 for (; zp
> squares
; zp
--) {
1297 if (zrel(z
, *zp
) >= 0) {
1298 zquo(z
, *zp
, &temp
, 0);
1307 if (zrel(z
, *zp
) >= 0)
1316 * Return the integral log base 10 of a number.
1318 * If was_10_power != NULL, then this flag is set to TRUE if the
1319 * value was a power of 10, FALSE otherwise.
1322 zlog10(ZVALUE z
, BOOL
*was_10_power
)
1324 ZVALUE
*zp
; /* current square */
1325 long power
; /* current power */
1326 ZVALUE temp
; /* temporary */
1327 ZVALUE pow10
; /* power of 10 */
1328 FLAG rel
; /* relationship */
1332 math_error("Zero argument argument for zlog10");
1336 /* Ignore sign of z */
1339 /* preload power10 table if missing */
1340 if (power10
== NULL
) {
1343 /* determine power10 table size */
1344 for (v
=1, max_power10_exp
=0;
1345 v
<= (long)(MAXLONG
/10L);
1346 v
*= 10L, ++max_power10_exp
) {
1349 /* create power10 table */
1350 power10
= malloc(sizeof(long) * (max_power10_exp
+1));
1351 if (power10
== NULL
) {
1352 math_error("cannot malloc power10 table");
1356 /* load power10 table */
1357 for (i
=0, v
= 1L; i
<= max_power10_exp
; ++i
, v
*= 10L) {
1362 /* assume not a power of ten unless we find out otherwise */
1363 if (was_10_power
!= NULL
) {
1364 *was_10_power
= FALSE
;
1367 /* quick exit for small values */
1368 if (! zgtmaxlong(z
)) {
1369 long value
= ztolong(z
);
1371 for (i
=0; i
<= max_power10_exp
; ++i
) {
1372 if (value
== power10
[i
]) {
1373 if (was_10_power
!= NULL
) {
1374 *was_10_power
= TRUE
;
1377 } else if (value
< power10
[i
]) {
1384 * Loop by squaring the base each time, and see whether or
1385 * not each successive square is still smaller than the number.
1387 zp
= &_tenpowers_
[0];
1389 while (((zp
->len
* 2) - 1) <= z
.len
) { /* while square not too large */
1390 if (zp
>= &_tenpowers_
[TEN_MAX
]) {
1391 math_error("Maximum storable power of 10 reached!");
1395 zsquare(*zp
, zp
+ 1);
1400 * Now back down the squares, and multiply them together to see
1401 * exactly how many times the base can be raised by.
1403 /* find the tenpower table entry < z */
1407 /* quick exit - we match a tenpower entry */
1408 if (was_10_power
!= NULL
) {
1409 *was_10_power
= TRUE
;
1411 return (1L << (zp
- _tenpowers_
));
1413 } while (rel
> 0 && --zp
>= _tenpowers_
);
1414 if (zp
< _tenpowers_
) {
1415 math_error("fell off bottom of tenpower table!");
1419 /* the tenpower value is now our starting comparison value */
1421 power
= (1L << (zp
- _tenpowers_
));
1423 /* try to build up a power of 10 from tenpower table entries */
1424 while (--zp
>= _tenpowers_
) {
1426 /* try the next lower tenpower value */
1427 zmul(pow10
, *zp
, &temp
);
1428 rel
= zrel(temp
, z
);
1430 /* exact power of 10 match */
1431 power
+= (1L << (zp
- _tenpowers_
));
1432 if (was_10_power
!= NULL
) {
1433 *was_10_power
= TRUE
;
1437 /* ignore this entry if we went too large */
1438 } else if (rel
> 0) {
1441 /* otherwise increase power and keep going */
1443 power
+= (1L << (zp
- _tenpowers_
));
1453 * Return the number of times that one number will divide another.
1454 * This works similarly to zlog, except that divisions must be exact.
1455 * For example, zdivcount(540, 3) = 3, since 3^3 divides 540 but 3^4 won't.
1458 zdivcount(ZVALUE z1
, ZVALUE z2
)
1460 long count
; /* number of factors removed */
1461 ZVALUE tmp
; /* ignored return value */
1463 if (ziszero(z1
) || ziszero(z2
) || zisunit(z2
))
1465 count
= zfacrem(z1
, z2
, &tmp
);
1472 * Remove all occurrences of the specified factor from a number.
1473 * Also returns the number of factors removed as a function return value.
1474 * Example: zfacrem(540, 3, &x) returns 3 and sets x to 20.
1477 zfacrem(ZVALUE z1
, ZVALUE z2
, ZVALUE
*rem
)
1479 register ZVALUE
*zp
; /* current square */
1480 long count
; /* total count of divisions */
1481 long worth
; /* worth of current square */
1482 long lowbit
; /* for zlowbit(z2) */
1483 ZVALUE temp1
, temp2
, temp3
; /* temporaries */
1484 ZVALUE squares
[32]; /* table of squares of factor */
1489 * Reject trivial cases.
1491 if ((z1
.len
< z2
.len
) || (zisodd(z1
) && ziseven(z2
)) ||
1492 ziszero(z2
) || zisone(z2
) ||
1493 ((z1
.len
== z2
.len
) && (z1
.v
[z1
.len
-1] < z2
.v
[z2
.len
-1]))) {
1494 rem
->v
= alloc(z1
.len
);
1501 * Handle any power of two special.
1503 if (zisonebit(z2
)) {
1504 lowbit
= zlowbit(z2
);
1505 count
= zlowbit(z1
) / lowbit
;
1506 rem
->v
= alloc(z1
.len
);
1510 zshiftr(*rem
, count
* lowbit
);
1515 * See if the factor goes in even once.
1517 zdiv(z1
, z2
, &temp1
, &temp2
, 0);
1518 if (!ziszero(temp2
)) {
1521 rem
->v
= alloc(z1
.len
);
1530 * Now loop by squaring the factor each time, and see whether
1531 * or not each successive square will still divide the number.
1537 while (((zp
->len
* 2) - 1) <= z1
.len
) { /* while square not too large */
1538 zsquare(*zp
, &temp1
);
1539 zdiv(z1
, temp1
, &temp2
, &temp3
, 0);
1540 if (!ziszero(temp3
)) {
1555 * Now back down the list of squares, and see if the lower powers
1556 * will divide any more times.
1559 * We prevent the zp pointer from walking behind squares
1560 * by stopping one short of the end and running the loop one
1563 * We could stop the loop with just zp >= squares, but stopping
1564 * short and running the loop one last time manually helps make
1565 * code checkers such as insure happy.
1567 for (; zp
> squares
; zp
--, worth
/= 2) {
1568 if (zp
->len
<= z1
.len
) {
1569 zdiv(z1
, *zp
, &temp1
, &temp2
, 0);
1570 if (ziszero(temp2
)) {
1582 /* run the loop manually one last time */
1583 if (zp
== squares
) {
1584 if (zp
->len
<= z1
.len
) {
1585 zdiv(z1
, *zp
, &temp1
, &temp2
, 0);
1586 if (ziszero(temp2
)) {
1605 * Keep dividing a number by the gcd of it with another number until the
1606 * result is relatively prime to the second number. Returns the number
1607 * of divisions made, and if this is positive, stores result at res.
1610 zgcdrem(ZVALUE z1
, ZVALUE z2
, ZVALUE
*res
)
1613 long count
, onecount
;
1616 if (ziszero(z1
) || ziszero(z2
)) {
1617 math_error("Zero argument in call to zgcdrem!!!");
1621 * Begin by taking the gcd for the first time.
1622 * If the number is already relatively prime, then we are done.
1628 if (zisonebit(z2
)) {
1632 zshift(z1
, -sh
, res
);
1633 return 1 + (sh
- 1)/zlowbit(z2
);
1635 if (zisonebit(z1
)) {
1642 zgcd(z1
, z2
, &tmp1
);
1643 if (zisunit(tmp1
) || ziszero(tmp1
))
1645 zequo(z1
, tmp1
, &tmp2
);
1650 * Now keep alternately taking the gcd and removing factors until
1651 * the gcd becomes one.
1653 while (!zisunit(z2
)) {
1654 onecount
= zfacrem(z1
, z2
, &tmp1
);
1660 zgcd(z1
, z2
, &tmp1
);
1670 * Return the number of digits (base 10) in a number, ignoring the sign.
1678 if (!zge16b(z1
)) { /* do small numbers ourself */
1681 while (*z1
.v
>= (HALF
)val
) {
1687 return (zlog10(z1
, NULL
) + 1);
1692 * Return the single digit at the specified decimal place of a number,
1693 * where 0 means the rightmost digit. Example: zdigit(1234, 1) = 3.
1696 zdigit(ZVALUE z1
, long n
)
1702 if (ziszero(z1
) || (n
< 0) || (n
/ BASEDIG
>= z1
.len
))
1705 return zmodi(z1
, 10L);
1707 return zmodi(z1
, 100L) / 10;
1709 return zmodi(z1
, 1000L) / 100;
1711 return zmodi(z1
, 10000L) / 1000;
1713 zquo(z1
, tmp1
, &tmp2
, 0);
1714 res
= zmodi(tmp2
, 10L);
1722 * z is to be a nonnegative integer
1723 * If z is the square of a integer stores at dest the square root of z;
1724 * otherwise stores at z an integer differing from the square root
1725 * by less than 1. Returns the sign of the true square root minus
1726 * the calculated integer. Type of rounding is determined by
1727 * rnd as follows: rnd = 0 gives round down, rnd = 1
1728 * rounds up, rnd = 8 rounds to even integer, rnd = 9 rounds to odd
1729 * integer, rnd = 16 rounds to nearest integer.
1732 zsqrt(ZVALUE z
, ZVALUE
*dest
, long rnd
)
1734 HALF
*a
, *A
, *b
, *a0
, u
;
1735 int i
, j
, j1
, j2
, k
, k1
, m
, m0
, m1
, n
, n0
, o
;
1736 FULL d
, e
, f
, g
, h
, s
, t
, x
, topbit
;
1742 math_error("Square root of negative number");
1751 m
= m0
+ o
; /* m is smallest even number >= z.len */
1768 memcpy(A
, z
.v
, m0
* sizeof(HALF
));
1773 f
= (FULL
) A
[1] << j
| A
[0] >> k
;
1776 g
= (FULL
) A
[0] << (j
+ BASEB
);
1777 d
= e
= topbit
= (FULL
)1 << (k
- 1);
1780 f
= (FULL
) A
[m
-1] << (j
+ BASEB
) | (FULL
) A
[m
-2] << j
|
1783 f
= (FULL
) A
[m
-1] << BASEB
| A
[m
-2];
1784 g
= (FULL
) A
[m
-3] << (j
+ BASEB
) | (FULL
) A
[m
-4] << j
;
1785 d
= e
= topbit
= (FULL
)1 << (BASEB
+ k
- 1);
1802 while (d
&& (f
& topbit
) == s
) {
1820 if (t
== 0 && f
< d
)
1836 A
[3] = (HALF
)(e
>> BASEB
);
1838 A
[1] = (HALF
)(f
>> BASEB
);
1843 u
= (HALF
)(s
? BASE1
: 0);
1845 A
[m1
- 1] = (HALF
)(e
>> (BASEB
- 1));
1846 A
[m1
- 2] = ((HALF
)(e
<< 1) | (HALF
)(s
> 0));
1847 A
[m1
- 3] = (HALF
)(f
>> BASEB
);
1848 A
[m1
- 4] = (HALF
)f
;
1853 A
[m1
- 2] = (HALF
)(e
>> (BASEB
- 1));
1854 A
[m1
- 3] = ((HALF
)(e
<< 1) | (HALF
)(s
> 0));
1856 A
[m1
- 5] = (HALF
)(f
>> BASEB
);
1857 A
[m1
- 6] = (HALF
)f
;
1862 onebit
= ((e
& ((FULL
)1 << (k
- 1))) ? TRUE
: FALSE
);
1868 f
= (FULL
) *a
<< j1
| (FULL
) a
[-1] << j2
| a
[-2] >> k1
;
1870 f
= (FULL
) *a
<< BASEB
| a
[-1];
1875 if (onebit
&& x
> 2 * (f
% h
) + 2)
1881 f
= *a
+ x
* (BASE
- x
);
1883 u
= (HALF
)(f
>> BASEB
);
1885 f
= *a
+ x
* *b
++ + u
;
1887 u
= (HALF
)(f
>> BASEB
);
1896 u
= -(HALF
)(f
>> BASEB
);
1898 f
= *a
- x
* *b
++ - u
;
1900 u
= -(HALF
)(f
>> BASEB
);
1907 *a
= ((HALF
)(x
<< 1) | (HALF
)(u
> 0));
1913 while (m
> 1 && *--a
== u
)
1921 if (a
[1] & 1) *a
|= TOPHALF
;
1926 while (m
> 0 && A
[m
- 1] == 0)
1933 memcpy(sqrt
.v
, a0
, n
* sizeof(HALF
));
1947 while (i
> 0 && *--a
== *--b
)
1949 up
= (i
> 0 && *a
> *b
);
1952 while (m
> 1 && A
[m
- 1] == BASE1
)
1960 while (i
> 0 && *--a
+ *--b
== BASE1
)
1962 up
= ((FULL
) *a
+ *b
>= BASE
);
1968 up
= (((rnd
^ *a0
) & 1) ? TRUE
: FALSE
);
1970 up
= ((rnd
& 1) ? TRUE
: FALSE
);
1975 while (i
-- && *a
== BASE1
)
1989 memcpy(sqrt
.v
, a0
, n
* sizeof(HALF
));
1997 * Take an arbitrary root of a number (to the greatest integer).
1998 * This uses the following iteration to get the Kth root of N:
1999 * x = ((K-1) * x + N / x^(K-1)) / K
2002 zroot(ZVALUE z1
, ZVALUE z2
, ZVALUE
*dest
)
2004 ZVALUE ztry
, quo
, old
, temp
, temp2
;
2005 ZVALUE k1
; /* holds k - 1 */
2012 if (sign
&& ziseven(z2
)) {
2013 math_error("Even root of negative number");
2016 if (ziszero(z2
) || zisneg(z2
)) {
2017 math_error("Non-positive root");
2020 if (ziszero(z1
)) { /* root of zero */
2024 if (zisunit(z2
)) { /* first root */
2028 if (zge31b(z2
)) { /* humongous root */
2030 dest
->sign
= (BOOL
)((HALF
)sign
);
2033 k
= (LEN
)ztolong(z2
);
2034 highbit
= zhighbit(z1
);
2035 if (highbit
< k
) { /* too high a root */
2037 dest
->sign
= (BOOL
)((HALF
)sign
);
2040 sival
.ivalue
= k
- 1;
2041 k1
.v
= &sival
.silow
;
2042 /* ignore Saber-C warning #112 - get ushort from uint */
2043 /* ok to ignore on name zroot`sival */
2044 k1
.len
= 1 + (sival
.sihigh
!= 0);
2048 * Allocate the numbers to use for the main loop.
2049 * The size and high bits of the final result are correctly set here.
2050 * Notice that the remainder of the test value is rubbish, but this
2053 highbit
= (highbit
+ k
- 1) / k
;
2054 ztry
.len
= (highbit
/ BASEB
) + 1;
2055 ztry
.v
= alloc(ztry
.len
);
2057 ztry
.v
[ztry
.len
-1] = ((HALF
)1 << (highbit
% BASEB
));
2059 old
.v
= alloc(ztry
.len
);
2064 * Main divide and average loop
2067 zpowi(ztry
, k1
, &temp
);
2068 zquo(z1
, temp
, &quo
, 0);
2070 i
= zrel(ztry
, quo
);
2073 * Current try is less than or equal to the root since
2074 * it is less than the quotient. If the quotient is
2075 * equal to the try, we are all done. Also, if the
2076 * try is equal to the old value, we are done since
2077 * no improvement occurred. If not, save the improved
2078 * value and loop some more.
2080 if ((i
== 0) || (zcmp(old
, ztry
) == 0)) {
2083 ztry
.sign
= (BOOL
)((HALF
)sign
);
2089 zcopyval(ztry
, old
);
2091 /* average current try and quotient for the new try */
2092 zmul(ztry
, k1
, &temp
);
2094 zadd(quo
, temp
, &temp2
);
2097 zquo(temp2
, z2
, &ztry
, 0);
2104 * Test to see if a number is an exact square or not.
2112 /* negative values are never perfect squares */
2117 /* ignore trailing zero words */
2118 while ((z
.len
> 1) && (*z
.v
== 0)) {
2123 /* zero or one is a perfect square */
2124 if (zisabsleone(z
)) {
2128 /* check mod 4096 values */
2129 if (issq_mod4k
[(int)(*z
.v
& 0xfff)] == 0) {
2133 /* must do full square root test now */
2134 n
= !zsqrt(z
, &tmp
, 0);
2136 return (n
? TRUE
: FALSE
);