2 * zmath - extended precision integral arithmetic primitives
4 * Copyright (C) 1999-2007 David I. Bell 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.1 $
23 * @(#) $Id: zmath.c,v 30.1 2007/03/16 11:09:46 chongo Exp $
24 * @(#) $Source: /usr/local/src/bin/calc/RCS/zmath.c,v $
26 * Under source code control: 1990/02/15 01:48:28
27 * File existed as early as: before 1990
29 * Share and enjoy! :-) http://www.isthe.com/chongo/tech/comp/calc/
36 HALF _zeroval_
[] = { 0 };
37 HALF _oneval_
[] = { 1 };
38 HALF _twoval_
[] = { 2 };
39 HALF _threeval_
[] = { 3 };
40 HALF _fourval_
[] = { 4 };
41 HALF _fiveval_
[] = { 5 };
42 HALF _sixval_
[] = { 6 };
43 HALF _sevenval_
[] = { 7 };
44 HALF _eightval_
[] = { 8 };
45 HALF _nineval_
[] = { 9 };
46 HALF _tenval_
[] = { 10 };
47 HALF _elevenval_
[] = { 11 };
48 HALF _twelveval_
[] = { 12 };
49 HALF _thirteenval_
[] = { 13 };
50 HALF _fourteenval_
[] = { 14 };
51 HALF _fifteenval_
[] = { 15 };
52 HALF _sixteenval_
[] = { 16 };
53 HALF _seventeenval_
[] = { 17 };
54 HALF _eightteenval_
[] = { 18 };
55 HALF _nineteenval_
[] = { 19 };
56 HALF _twentyval_
[] = { 20 };
57 HALF _sqbaseval_
[] = { 0, 1 };
58 HALF _pow4baseval_
[] = { 0, 0, 1 };
59 HALF _pow8baseval_
[] = { 0, 0, 0, 0, 1 };
62 { _zeroval_
, 1, 0}, { _oneval_
, 1, 0}, { _twoval_
, 1, 0},
63 { _threeval_
, 1, 0}, { _fourval_
, 1, 0}, { _fiveval_
, 1, 0},
64 { _sixval_
, 1, 0}, { _sevenval_
, 1, 0}, { _eightval_
, 1, 0},
65 { _nineval_
, 1, 0}, { _tenval_
, 1, 0}, { _elevenval_
, 1, 0},
66 { _twelveval_
, 1, 0}, { _thirteenval_
, 1, 0}, { _fourteenval_
, 1, 0},
67 { _fifteenval_
, 1, 0}, { _sixteenval_
, 1, 0}, { _seventeenval_
, 1, 0},
68 { _eightteenval_
, 1, 0}, { _nineteenval_
, 1, 0}, { _twentyval_
, 1, 0}
71 ZVALUE _zero_
= { _zeroval_
, 1, 0};
72 ZVALUE _one_
= { _oneval_
, 1, 0 };
73 ZVALUE _two_
= { _twoval_
, 1, 0 };
74 ZVALUE _ten_
= { _tenval_
, 1, 0 };
75 ZVALUE _sqbase_
= { _sqbaseval_
, 2, 0 };
76 ZVALUE _pow4base_
= { _pow4baseval_
, 4, 0 };
77 ZVALUE _pow8base_
= { _pow8baseval_
, 4, 0 };
78 ZVALUE _neg_one_
= { _oneval_
, 1, 1 };
84 ZVALUE _b32_
= { _sqbaseval_
, 2, 0 };
85 ZVALUE _b64_
= { _pow4baseval_
, 3, 0 };
87 ZVALUE _b32_
= { _pow4baseval_
, 3, 0 };
88 ZVALUE _b64_
= { _pow8baseval_
, 5, 0 };
90 -=@
=- BASEB
not 16 or 32 -=@
=-
95 * highhalf[i] - masks off the upper i bits of a HALF
96 * rhighhalf[i] - masks off the upper BASEB-i bits of a HALF
97 * lowhalf[i] - masks off the upper i bits of a HALF
98 * rlowhalf[i] - masks off the upper BASEB-i bits of a HALF
99 * bitmask[i] - (1 << i) for 0 <= i <= BASEB*2
101 HALF highhalf
[BASEB
+1] = {
104 0x80000000, 0xC0000000, 0xE0000000, 0xF0000000,
105 0xF8000000, 0xFC000000, 0xFE000000, 0xFF000000,
106 0xFF800000, 0xFFC00000, 0xFFE00000, 0xFFF00000,
107 0xFFF80000, 0xFFFC0000, 0xFFFE0000, 0xFFFF0000,
108 0xFFFF8000, 0xFFFFC000, 0xFFFFE000, 0xFFFFF000,
109 0xFFFFF800, 0xFFFFFC00, 0xFFFFFE00, 0xFFFFFF00,
110 0xFFFFFF80, 0xFFFFFFC0, 0xFFFFFFE0, 0xFFFFFFF0,
111 0xFFFFFFF8, 0xFFFFFFFC, 0xFFFFFFFE, 0xFFFFFFFF
114 0x8000, 0xC000, 0xE000, 0xF000,
115 0xF800, 0xFC00, 0xFE00, 0xFF00,
116 0xFF80, 0xFFC0, 0xFFE0, 0xFFF0,
117 0xFFF8, 0xFFFC, 0xFFFE, 0xFFFF
119 -=@
=- BASEB
not 16 or 32 -=@
=-
122 HALF rhighhalf
[BASEB
+1] = {
124 0xFFFFFFFF, 0xFFFFFFFE, 0xFFFFFFFC, 0xFFFFFFF8,
125 0xFFFFFFF0, 0xFFFFFFE0, 0xFFFFFFC0, 0xFFFFFF80,
126 0xFFFFFF00, 0xFFFFFE00, 0xFFFFFC00, 0xFFFFF800,
127 0xFFFFF000, 0xFFFFE000, 0xFFFFC000, 0xFFFF8000,
128 0xFFFF0000, 0xFFFE0000, 0xFFFC0000, 0xFFF80000,
129 0xFFF00000, 0xFFE00000, 0xFFC00000, 0xFF800000,
130 0xFF000000, 0xFE000000, 0xFC000000, 0xF8000000,
131 0xF0000000, 0xE0000000, 0xC0000000, 0x80000000,
134 0xFFFF, 0xFFFE, 0xFFFC, 0xFFF8,
135 0xFFF0, 0xFFE0, 0xFFC0, 0xFF80,
136 0xFF00, 0xFE00, 0xFC00, 0xF800,
137 0xF000, 0xE000, 0xC000, 0x8000,
140 -=@
=- BASEB
not 16 or 32 -=@
=-
143 HALF lowhalf
[BASEB
+1] = {
146 0x1F, 0x3F, 0x7F, 0xFF,
147 0x1FF, 0x3FF, 0x7FF, 0xFFF,
148 0x1FFF, 0x3FFF, 0x7FFF, 0xFFFF
150 ,0x1FFFF, 0x3FFFF, 0x7FFFF, 0xFFFFF,
151 0x1FFFFF, 0x3FFFFF, 0x7FFFFF, 0xFFFFFF,
152 0x1FFFFFF, 0x3FFFFFF, 0x7FFFFFF, 0xFFFFFFF,
153 0x1FFFFFFF, 0x3FFFFFFF, 0x7FFFFFFF, 0xFFFFFFFF
156 HALF rlowhalf
[BASEB
+1] = {
158 0xFFFFFFFF, 0x7FFFFFFF, 0x3FFFFFFF, 0x1FFFFFFF,
159 0xFFFFFFF, 0x7FFFFFF, 0x3FFFFFF, 0x1FFFFFF,
160 0xFFFFFF, 0x7FFFFF, 0x3FFFFF, 0x1FFFFF,
161 0xFFFFF, 0x7FFFF, 0x3FFFF, 0x1FFFF,
163 0xFFFF, 0x7FFF, 0x3FFF, 0x1FFF,
164 0xFFF, 0x7FF, 0x3FF, 0x1FF,
165 0xFF, 0x7F, 0x3F, 0x1F,
169 HALF bitmask
[(2*BASEB
)+1] = {
171 0x00000001, 0x00000002, 0x00000004, 0x00000008,
172 0x00000010, 0x00000020, 0x00000040, 0x00000080,
173 0x00000100, 0x00000200, 0x00000400, 0x00000800,
174 0x00001000, 0x00002000, 0x00004000, 0x00008000,
175 0x00010000, 0x00020000, 0x00040000, 0x00080000,
176 0x00100000, 0x00200000, 0x00400000, 0x00800000,
177 0x01000000, 0x02000000, 0x04000000, 0x08000000,
178 0x10000000, 0x20000000, 0x40000000, 0x80000000,
179 0x00000001, 0x00000002, 0x00000004, 0x00000008,
180 0x00000010, 0x00000020, 0x00000040, 0x00000080,
181 0x00000100, 0x00000200, 0x00000400, 0x00000800,
182 0x00001000, 0x00002000, 0x00004000, 0x00008000,
183 0x00010000, 0x00020000, 0x00040000, 0x00080000,
184 0x00100000, 0x00200000, 0x00400000, 0x00800000,
185 0x01000000, 0x02000000, 0x04000000, 0x08000000,
186 0x10000000, 0x20000000, 0x40000000, 0x80000000,
189 0x0001, 0x0002, 0x0004, 0x0008,
190 0x0010, 0x0020, 0x0040, 0x0080,
191 0x0100, 0x0200, 0x0400, 0x0800,
192 0x1000, 0x2000, 0x4000, 0x8000,
193 0x0001, 0x0002, 0x0004, 0x0008,
194 0x0010, 0x0020, 0x0040, 0x0080,
195 0x0100, 0x0200, 0x0400, 0x0800,
196 0x1000, 0x2000, 0x4000, 0x8000,
199 -=@
=- BASEB
not 16 or 32 -=@
=-
201 }; /* actual rotation thru 8 cycles */
203 BOOL _math_abort_
; /* nonzero to abort calculations */
207 * popcnt - popcnt[x] number of 1 bits in 0 <= x < 256
210 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4,
211 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
212 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
213 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
214 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
215 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
216 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
217 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
218 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
219 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
220 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
221 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
222 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
223 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
224 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
225 4, 5, 5, 6, 5, 6, 6, 7, 5, 6, 6, 7, 6, 7, 7, 8
231 STATIC
long nalloc
= 0;
232 STATIC
long nfree
= 0;
242 math_error("Calculation aborted");
245 hp
= (HALF
*) malloc((len
+1) * sizeof(HALF
));
247 math_error("Not enough memory");
261 if ((h
!= _zeroval_
) && (h
!= _oneval_
)) {
271 fprintf(stderr
, "nalloc: %ld nfree: %ld kept: %ld\n",
272 nalloc
, nfree
, nalloc
- nfree
);
278 * Convert a normal integer to a number.
281 itoz(long i
, ZVALUE
*res
)
295 if (i
< 0) { /* fix most negative number */
304 len
= 1 + (((FULL
) i
) >= BASE
);
306 res
->v
= alloc((LEN
)len
);
307 res
->v
[0] = (HALF
) (i
+ diddle
);
309 res
->v
[1] = (HALF
) (i
/ BASE
);
314 * Convert a number to a normal integer, as far as possible.
315 * If the number is out of range, the largest number is returned.
324 return (z
.sign
? -i
: i
);
327 return (z
.sign
? -i
: i
);
332 * Convert a normal unsigned integer to a number.
335 utoz(FULL i
, ZVALUE
*res
)
349 len
= 1 + (((FULL
) i
) >= BASE
);
351 res
->v
= alloc((LEN
)len
);
354 res
->v
[1] = (HALF
) (i
/ BASE
);
359 * Convert a normal signed integer to a number.
362 stoz(SFULL i
, ZVALUE
*res
)
376 if (i
< 0) { /* fix most negative number */
385 len
= 1 + (((FULL
) i
) >= BASE
);
387 res
->v
= alloc((LEN
)len
);
388 res
->v
[0] = (HALF
) (i
+ diddle
);
390 res
->v
[1] = (HALF
) (i
/ BASE
);
395 * Convert a number to a unsigned normal integer, as far as possible.
396 * If the number is out of range, the largest number is returned.
397 * The absolute value of z is converted.
410 * Convert a number to a signed normal integer, as far as possible.
412 * If the number is too large to fit into an integer, than the largest
413 * positive or largest negative integer is returned depending on the sign.
418 FULL val
; /* absolute value of the return value */
420 /* negative value processing */
429 return (SFULL
)(-val
);
432 /* positive value processing */
434 return (SFULL
)MAXFULL
;
438 return (SFULL
)MAXFULL
;
445 * Make a copy of an integer value
448 zcopy(ZVALUE z
, ZVALUE
*res
)
452 if (zisabsleone(z
)) { /* zero or plus or minus one are easy */
453 res
->v
= (z
.v
[0] ? _oneval_
: _zeroval_
);
456 res
->v
= alloc(z
.len
);
462 * Add together two integers.
465 zadd(ZVALUE z1
, ZVALUE z2
, ZVALUE
*res
)
473 if (z1
.sign
&& !z2
.sign
) {
478 if (z2
.sign
&& !z1
.sign
) {
483 if (z2
.len
> z1
.len
) {
484 pd
= z1
.v
; z1
.v
= z2
.v
; z2
.v
= pd
;
485 len
= z1
.len
; z1
.len
= z2
.len
; z2
.len
= (LEN
)len
;
487 dest
.len
= z1
.len
+ 1;
488 dest
.v
= alloc(dest
.len
);
496 sival
.ivalue
= ((FULL
) *p1
++) + ((FULL
) *p2
++) + carry
;
497 /* ignore Saber-C warning #112 - get ushort from uint */
498 /* ok to ignore on name zadd`sival */
500 carry
= sival
.sihigh
;
502 len
= z1
.len
- z2
.len
;
504 sival
.ivalue
= ((FULL
) *p1
++) + carry
;
506 carry
= sival
.sihigh
;
515 * Subtract two integers.
518 zsub(ZVALUE z1
, ZVALUE z2
, ZVALUE
*res
)
520 register HALF
*h1
, *h2
, *hd
;
526 if (z1
.sign
!= z2
.sign
) {
536 while ((len1
> 0) && ((FULL
)*--h1
== (FULL
)*--h2
)) {
544 carry
= ((FULL
)*h1
< (FULL
)*h2
);
546 carry
= (len1
< len2
);
557 dest
.sign
= !dest
.sign
;
559 hd
= alloc((LEN
)len1
);
561 dest
.len
= (LEN
)len1
;
564 while (--len2
>= 0) {
565 /* ignore Saber-C warning #112 - get ushort from uint */
566 /* ok to ignore on name zsub`sival */
567 sival
.ivalue
= (BASE1
- ((FULL
) *h1
++)) + *h2
++ + carry
;
568 *hd
++ = (HALF
)(BASE1
- sival
.silow
);
569 carry
= sival
.sihigh
;
571 while (--len1
>= 0) {
572 sival
.ivalue
= (BASE1
- ((FULL
) *h1
++)) + carry
;
573 *hd
++ = (HALF
)(BASE1
- sival
.silow
);
574 carry
= sival
.sihigh
;
583 * Multiply an integer by a small number.
586 zmuli(ZVALUE z
, long n
, ZVALUE
*res
)
588 register HALF
*h1
, *sd
;
596 if ((n
== 0) || ziszero(z
)) {
608 #if LONG_BITS > BASEB
609 low
= ((FULL
) n
) & BASE1
;
610 high
= ((FULL
) n
) >> BASEB
;
615 dest
.len
= z
.len
+ 2;
616 dest
.v
= alloc(dest
.len
);
619 * Multiply by the low digit.
626 /* ignore Saber-C warning #112 - get ushort from uint */
627 /* ok to ignore on name zmuli`sival */
628 sival
.ivalue
= ((FULL
) *h1
++) * low
+ carry
;
630 carry
= sival
.sihigh
;
634 * If there was only one digit, then we are all done except
635 * for trimming the number if there was no last carry.
645 * Need to multiply by the high digit and add it into the
646 * previous value. Clear the final word of rubbish first.
654 sival
.ivalue
= ((FULL
) *h1
++) * high
+ ((FULL
) *sd
) + carry
;
656 carry
= sival
.sihigh
;
665 * Divide two numbers by their greatest common divisor.
666 * This is useful for reducing the numerator and denominator of
667 * a fraction to its lowest terms.
670 zreduce(ZVALUE z1
, ZVALUE z2
, ZVALUE
*z1res
, ZVALUE
*z2res
)
674 if (zisabsleone(z1
) || zisabsleone(z2
))
682 zequo(z1
, tmp
, z1res
);
683 zequo(z2
, tmp
, z2res
);
691 * Compute the quotient and remainder for division of an integer by an
692 * integer, rounding criteria determined by rnd. Returns the sign of
696 zdiv(ZVALUE z1
, ZVALUE z2
, ZVALUE
*quo
, ZVALUE
*rem
, long rnd
)
698 register HALF
*a
, *b
;
700 HALF
*A
, *B
, *a1
, *b0
;
703 LEN m
, n
, len
, i
, p
, j1
, j2
, k
;
707 math_error("Division by zero in zdiv");
716 memcpy(A
, z1
.v
, m
* sizeof(HALF
));
717 for (i
= m
; i
<= n
; i
++)
724 memcpy(A
, z1
.v
, m
* sizeof(HALF
));
727 len
= m
- n
+ 1; /* quotient length will be len or len +/- 1 */
728 a1
= A
+ n
; /* start of digits for quotient */
731 while (!*b0
) { /* b0: working start for divisor */
747 f
= f
<< BASEB
| *--a
;
748 a
[1] = (HALF
)(f
/ u
);
758 k
++; /* k: number of bits in top divisor digit */
761 h
= j1
? ((FULL
) B
[n
- 1] << j1
| B
[n
- 2] >> k
) : B
[n
-1];
762 onebit
= (BOOL
)((B
[n
- 2] >> (k
- 1)) & 1);
766 f
= (FULL
) A
[m
] << j2
| (FULL
) A
[m
- 1] << j1
;
767 if (j1
) f
|= A
[m
- 2] >> k
;
771 if (onebit
&& x
> TOPHALF
+ f
% h
)
779 f
= (FULL
) *a
+ u
+ x
* *b
++;
781 u
= (HALF
) (f
>> BASEB
);
784 A
[m
] = (HALF
) (~x
+ !s
);
787 f
= (FULL
) *a
- u
- x
* *b
++;
789 u
= -(HALF
)(f
>> BASEB
);
792 A
[m
] = (HALF
)(x
+ s
);
798 done
: while (m
> 0 && A
[m
- 1] == 0)
800 if (m
== 0 && s
== 0) {
803 if (a1
[len
- 1] == 0)
810 memcpy(quo
->v
, a1
, len
* sizeof(HALF
));
811 quo
->sign
= z1
.sign
^ z2
.sign
;
817 adjust
= (((*a1
^ rnd
) & 1) ? TRUE
: FALSE
);
819 adjust
= (((rnd
& 1) ^ z1
.sign
^ z2
.sign
) ? TRUE
: FALSE
);
821 adjust
^= z1
.sign
^ z2
.sign
;
824 if (rnd
& 16) { /* round-off case */
832 g
= (FULL
) *--a
+ (*--b
>> 1 | f
);
833 f
= *b
& 1 ? TOPHALF
: 0;
837 if (g
== BASE
&& f
== 0) {
838 while ((--i
> 0) && ((*--a
| *--b
) == 0));
840 } else if (g
>= BASE
) {
845 g
= (FULL
) *--a
- (*--b
>> 1 | f
);
846 f
= *b
& 1 ? TOPHALF
: 0;
850 if (g
> 0 && g
< BASE
)
852 else if (g
== 0 && f
== 0)
861 while (i
> 0 && *a
== BASE1
) {
877 while (A
[m
- 1] == 0)
886 f
= (FULL
) *b
++ - *a
- (FULL
) u
;
888 u
= -(HALF
)(f
>> BASEB
);
891 while (A
[m
- 1] == 0)
900 f
= (FULL
) *b
++ + *a
+ (f
>> BASEB
);
909 memcpy(rem
->v
, A
, m
* sizeof(HALF
));
910 rem
->sign
= z1
.sign
^ adjust
;
911 val
= rem
->sign
? -1 : 1;
912 if (a1
[len
- 1] == 0)
919 memcpy(quo
->v
, a1
, len
* sizeof(HALF
));
920 quo
->sign
= z1
.sign
^ z2
.sign
;
928 * Compute and store at a specified location the integer quotient
929 * of two integers, the type of rounding being determined by rnd.
930 * Returns the sign of z1/z2 - calculated quotient.
933 zquo(ZVALUE z1
, ZVALUE z2
, ZVALUE
*res
, long rnd
)
938 val
= zdiv(z1
, z2
, res
, &tmp
, rnd
);
947 * Compute and store at a specified location the remainder for
948 * division of an integer by an integer, the type of rounding
949 * used being determined by rnd. Returns the sign of the remainder.
952 zmod(ZVALUE z1
, ZVALUE z2
, ZVALUE
*res
, long rnd
)
957 val
= zdiv(z1
, z2
, &tmp
, res
, rnd
);
964 * Computes the quotient z1/z2 on the assumption that this is exact.
965 * There is no thorough check on the exactness of the division
966 * so this should not be called unless z1/z2 is an integer.
969 zequo(ZVALUE z1
, ZVALUE z2
, ZVALUE
*res
)
971 LEN i
, j
, k
, len
, m
, n
, o
, p
;
972 HALF
*a
, *a0
, *A
, *b
, *B
, u
, v
, w
, x
;
980 math_error("Division by zero");
987 if (zhighbit(z1
) < zhighbit(z2
)) {
988 math_error("Bad call to zequo");
999 len
= m
- n
+ 1; /* Maximum length of quotient */
1002 memcpy(A
, z1
.v
+ o
, len
* sizeof(HALF
));
1010 f
= f
<< BASEB
| *--a
;
1026 while (u
) { /* To find w = inverse of v modulo BASE */
1039 while (!*++a0
&& p
> 1)
1045 x
= k
? w
* (*a0
>> k
| a0
[1] << j
) : w
* *a0
;
1055 f
= (FULL
) *a
- g
* *b
++ - (FULL
) u
;
1057 u
= -(HALF
)(f
>> BASEB
);
1064 u
= -(HALF
)(f
>> BASEB
);
1074 u
= (HALF
)(w
* *a0
) >> k
;
1075 x
= (HALF
)(((FULL
) z1
.v
[z1
.len
- 1] << BASEB
1077 /((FULL
) B
[n
-1] << BASEB
| B
[n
-2]));
1078 if ((x
^ u
) & 1) x
--;
1082 if (!A
[len
- 1]) len
--;
1085 res
->sign
= z1
.sign
!= z2
.sign
;
1091 * Return the quotient and remainder of an integer divided by a small
1092 * number. A nonzero remainder is only meaningful when both numbers
1096 zdivi(ZVALUE z
, long n
, ZVALUE
*res
)
1106 math_error("Division by zero");
1122 * If the division is by a large number, then call the normal
1128 divval
[0] = (HALF
) n
;
1129 #if LONG_BITS > BASEB
1130 divval
[1] = (HALF
)(((FULL
) n
) >> BASEB
);
1135 zdiv(z
, div
, res
, &dest
, 0);
1141 * Division is by a small number, so we can be quick about it.
1146 dest
.v
= alloc(len
);
1151 val
= ((val
<< BASEB
) + ((FULL
) *--h1
));
1152 *--sd
= (HALF
)(val
/ n
);
1163 * Calculate the mod of an integer by a small number.
1164 * This is only defined for positive moduli.
1167 zmodi(ZVALUE z
, long n
)
1177 math_error("Division by zero");
1181 math_error("Non-positive modulus");
1184 if (ziszero(z
) || (n
== 1))
1189 * If the modulus is by a large number, then call the normal
1195 divval
[0] = (HALF
) n
;
1196 #if LONG_BITS > BASEB
1197 divval
[1] = (HALF
)(((FULL
) n
) >> BASEB
);
1202 zmod(z
, div
, &temp
, 0);
1208 * The modulus is by a small number, so we can do this quickly.
1214 val
= ((val
<< BASEB
) + ((FULL
) *--h1
)) % n
;
1222 * Return whether or not one number exactly divides another one.
1223 * Returns TRUE if division occurs with no remainder.
1224 * z1 is the number to be divided by z2.
1228 zdivides(ZVALUE z1
, ZVALUE z2
)
1232 HALF
*a
, *a0
, *A
, *b
, *B
, *c
, *d
;
1236 if (zisunit(z2
) || ziszero(z1
)) return TRUE
;
1237 if (ziszero(z2
)) return FALSE
;
1241 if (m
< n
) return FALSE
;
1247 if (*c
++) return FALSE
;
1256 while (!(v
& 1)) { /* Counting and checking zero bits */
1257 if (u
& 1) return FALSE
;
1263 if (n
== 1 && v
== 1) return TRUE
;
1264 if (!*c
) { /* Removing any further zeros */
1270 if (m
< n
) return FALSE
;
1273 B
= alloc((LEN
)n
); /* Array for shifted z2 */
1279 f
= f
<< BASEB
| *--d
;
1280 *--b
= (HALF
)(f
>> j
);
1288 while (x
) { /* Finding minv(*B, BASE) */
1296 A
= alloc((LEN
)(m
+ 2)); /* Main working array */
1297 memcpy(A
, c
, m
* sizeof(HALF
));
1301 k
= m
- n
+ 1; /* Length of presumed quotient */
1313 f
= (FULL
) *a
- (FULL
) x
* *b
++ - u
;
1315 u
= -(HALF
)(f
>> BASEB
);
1319 u
= -(HALF
)(f
>> BASEB
);
1321 while (*a
== 0) *a
++ = BASE1
;
1330 while (--n
&& !*--a
);
1340 * Compute the bitwise OR of two integers
1343 zor(ZVALUE z1
, ZVALUE z2
, ZVALUE
*res
)
1345 register HALF
*sp
, *dp
;
1347 ZVALUE bz
, lz
, dest
;
1349 if (z1
.len
>= z2
.len
) {
1357 dest
.v
= alloc(dest
.len
);
1370 * Compute the bitwise AND of two integers
1373 zand(ZVALUE z1
, ZVALUE z2
, ZVALUE
*res
)
1379 len
= ((z1
.len
<= z2
.len
) ? z1
.len
: z2
.len
);
1382 while ((len
> 1) && ((*h1
& *h2
) == 0)) {
1388 dest
.v
= alloc(len
);
1394 *hd
++ = (*h1
++ & *h2
++);
1400 * Compute the bitwise XOR of two integers.
1403 zxor(ZVALUE z1
, ZVALUE z2
, ZVALUE
*res
)
1413 if (z1
.len
< z2
.len
) {
1418 } else if (z1
.len
== z2
.len
) {
1419 while (len
> 1 && z1
.v
[len
-1] == z2
.v
[len
-1])
1425 dest
.v
= alloc(len
);
1429 *dp
++ = *h1
++ ^ *h2
++;
1437 * Compute the bitwise ANDNOT of two integers.
1440 zandnot(ZVALUE z1
, ZVALUE z2
, ZVALUE
*res
)
1447 if (z2
.len
>= len
) {
1448 while (len
> 1 && (z1
.v
[len
-1] & ~z2
.v
[len
-1]) == 0)
1457 dest
.v
= alloc(len
);
1463 *dp
++ = *h1
++ & ~*h2
++;
1471 * Shift a number left (or right) by the specified number of bits.
1472 * Positive shift means to the left. When shifting right, rightmost
1473 * bits are lost. The sign of the number is preserved.
1476 zshift(ZVALUE z
, long n
, ZVALUE
*res
)
1479 LEN hc
; /* number of halfwords shift is by */
1490 * If shift value is negative, then shift right.
1491 * Check for large shifts, and handle word-sized shifts quickly.
1495 if ((n
< 0) || (n
>= (z
.len
* BASEB
))) {
1499 hc
= (LEN
)(n
/ BASEB
);
1504 ans
.v
= alloc(ans
.len
);
1519 * Shift value is positive, so shift leftwards.
1520 * Check specially for a shift of the value 1, since this is common.
1521 * Also handle word-sized shifts quickly.
1528 hc
= (LEN
)(n
/ BASEB
);
1530 ans
.len
= z
.len
+ hc
+ 1;
1531 ans
.v
= alloc(ans
.len
);
1534 memset((char *) ans
.v
, 0, hc
* sizeof(HALF
));
1535 memcpy((char *) (ans
.v
+ hc
),
1536 (char *) z
.v
, z
.len
* sizeof(HALF
));
1537 ans
.v
[ans
.len
- 1] = 0;
1551 * Return the position of the lowest bit which is set in the binary
1552 * representation of a number (counting from zero). This is the highest
1553 * power of two which evenly divides the number.
1564 for (zp
= z
.v
; *zp
== 0; zp
++)
1569 /* ignore Saber-C warning #530 about empty while statement */
1570 /* ok to ignore in proc zlowbit */
1571 while ((*(bitval
++) & dataval
) == 0) {
1573 return (n
*BASEB
)+(bitval
-bitmask
-1);
1578 * Return the position of the highest bit which is set in the binary
1579 * representation of a number (counting from zero). This is the highest power
1580 * of two which is less than or equal to the number (which is assumed nonzero).
1588 dataval
= z
.v
[z
.len
-1];
1592 bitval
= bitmask
+BASEB
;
1594 /* ignore Saber-C warning #530 about empty while statement */
1595 /* ok to ignore in proc zhighbit */
1596 while ((*(--bitval
) & dataval
) == 0) {
1599 return (z
.len
*BASEB
)+(LEN
)(bitval
-bitmask
-BASEB
);
1604 * Return whether or not the specifed bit number is set in a number.
1605 * Rightmost bit of a number is bit 0.
1608 zisset(ZVALUE z
, long n
)
1610 if ((n
< 0) || ((n
/ BASEB
) >= z
.len
))
1612 return ((z
.v
[n
/ BASEB
] & (((HALF
) 1) << (n
% BASEB
))) != 0);
1617 * Check whether or not a number has exactly one bit set, and
1618 * thus is an exact power of two. Returns TRUE if so.
1626 if (ziszero(z
) || zisneg(z
))
1632 if (*hp
++ || *hp
++ || *hp
++ || *hp
++)
1639 return ((*hp
& -*hp
) == *hp
); /* NEEDS 2'S COMPLEMENT */
1644 * Check whether or not a number has all of its bits set below some
1645 * bit position, and thus is one less than an exact power of two.
1646 * Returns TRUE if so.
1649 zisallbits(ZVALUE z
)
1655 if (ziszero(z
) || zisneg(z
))
1661 if ((*hp
++ != BASE1
) || (*hp
++ != BASE1
) ||
1662 (*hp
++ != BASE1
) || (*hp
++ != BASE1
))
1669 digit
= (HALF
)(*hp
+ 1);
1670 return ((digit
& -digit
) == digit
); /* NEEDS 2'S COMPLEMENT */
1675 * Return the number whose binary representation contains only one bit which
1676 * is in the specified position (counting from zero). This is equivilant
1677 * to raising two to the given power.
1680 zbitvalue(long n
, ZVALUE
*res
)
1686 z
.len
= (LEN
)((n
/ BASEB
) + 1);
1689 z
.v
[z
.len
-1] = (((HALF
) 1) << (n
% BASEB
));
1695 * Compare a number against zero.
1696 * Returns the sgn function of the number (-1, 0, or 1).
1718 * Return the sign of z1 - z2, i.e. 1 if the first integer is greater,
1719 * 0 if they are equal, -1 otherwise.
1722 zrel(ZVALUE z1
, ZVALUE z2
)
1729 if (z1
.sign
< z2
.sign
)
1731 if (z2
.sign
< z1
.sign
)
1735 if (z1
.len
!= z2
.len
)
1736 return (z1
.len
> z2
.len
) ? sign
: -sign
;
1747 return (*h1
> *h2
) ? sign
: -sign
;
1753 * Return the sign of abs(z1) - abs(z2), i.e. 1 if the first integer
1754 * has greater absolute value, 0 is they have equal absolute value,
1758 zabsrel(ZVALUE z1
, ZVALUE z2
)
1763 if (z1
.len
!= z2
.len
)
1764 return (z1
.len
> z2
.len
) ? 1 : -1;
1775 return (*h1
> *h2
) ? 1 : -1;
1781 * Compare two numbers to see if they are equal or not.
1782 * Returns TRUE if they differ.
1785 zcmp(ZVALUE z1
, ZVALUE z2
)
1787 register HALF
*h1
, *h2
;
1790 if ((z1
.sign
!= z2
.sign
) || (z1
.len
!= z2
.len
) || (*z1
.v
!= *z2
.v
))
1804 * Utility to calculate the gcd of two FULL integers.
1807 uugcd(FULL f1
, FULL f2
)
1821 * Utility to calculate the gcd of two small integers.
1824 iigcd(long i1
, long i2
)
1828 f1
= (FULL
) ((i1
>= 0) ? i1
: -i1
);
1829 f2
= (FULL
) ((i2
>= 0) ? i2
: -i2
);
1845 h
= z
->v
+ z
->len
- 1;
1847 while (*h
== 0 && len
> 1) {
1856 * Utility routine to shift right.
1858 * NOTE: The ZVALUE length is not adjusted instead, the value is
1859 * zero padded from the left. One may need to call ztrim()
1860 * or use zshift() instead.
1863 zshiftr(ZVALUE z
, long n
)
1865 register HALF
*h
, *lim
;
1872 lim
= z
.v
+ z
.len
- len
;
1887 maskt
= (((FULL
) *--h
) << (BASEB
- n
)) & BASE1
;
1888 *h
= ((*h
>> n
) | (HALF
)mask
);
1896 * Utility routine to shift left.
1898 * NOTE: The ZVALUE length is not adjusted. The bits in the upper
1899 * HALF are simply tossed. You may want to use zshift() instead.
1902 zshiftl(ZVALUE z
, long n
)
1910 h
= z
.v
+ z
.len
- 1;
1926 i
= (((FULL
) *h
) << n
) | mask
;
1941 * popcnt - count the number of 0 or 1 bits in an integer
1943 * We ignore all 0 bits above the highest bit.
1946 zpopcnt(ZVALUE z
, int bitval
)
1948 long cnt
= 0; /* number of times found */
1949 HALF h
; /* HALF to count */
1960 for (i
=0; i
< z
.len
; ++i
) {
1961 /* count each octet */
1962 for (h
= z
.v
[i
]; h
; h
>>= 8) {
1963 cnt
+= (long)popcnt
[h
& 0xff];
1973 * count each HALF up until the last
1975 for (i
=0; i
< z
.len
-1; ++i
) {
1977 /* count each octet */
1979 for (h
= z
.v
[i
]; h
; h
>>= 8) {
1980 cnt
-= (long)popcnt
[h
& 0xff];
1985 * count the last octet up until the highest 1 bit
1987 for (h
= z
.v
[z
.len
-1]; h
; h
>>=1) {
1988 /* count each 0 bit */
1989 if ((h
& 0x1) == 0) {