1 /* Copyright (c) 1995, 1996, 2001 by Arkkra Enterprises */
2 /* All rights reserved */
4 * rational.c functions to do operations on rational numbers
8 * radd(), rsub(), rmul(), rdiv(), rneg(), rinv(), rrai(), rred(),
9 * ator(), rtoa(); also gtrat(), called by macros GT,GE,LT,LE
11 * ratmsg(), add64_64(), mul32_64(), divmod64(), red64_64()
13 * The first group of functions are for the user. The second
14 * are for internal use only.
17 * The functions in this file do operations on rational numbers.
18 * The rational arguments to functions that the users can call
19 * must be in standard form (lowest terms, with positive
20 * denominator) except for rred(). There are checks for division
21 * by zero and for overflow of numerators and denominators.
22 * (The absolute values of each are limited to MAXLONG, defined
23 * in rational.h.) If there is an error, the external int
24 * raterrno is set to RATDIV0 or RATOVER, as the case may be,
25 * and *raterrfuncp is checked. If nonzero, it is assumed to
26 * point at the user's error handler, and it is called with a
27 * parameter equal to raterrno. Otherwise, a message is printed
28 * to stderr. In any case, the answer returned to the user is
29 * 0/1. If there was no error, raterrno is set to RATNOERR.
31 * In general, the functions assume they are being called with
32 * valid parameters. If they are not, results are not guaranteed
33 * to be correct. However, they are defensive enough so that
34 * invalid parameters will not cause a crash in these routines.
35 * They will not always detect invalid parameters, but if they
36 * do, they will use the raterrno/raterrfuncp mechanism described
37 * above, with the value RATPARM.
39 * These routines depend on a INT32B being a 32-bit number,
40 * stored in two's complement form, and UINT32B being the same
41 * for unsigned. See rational.h. Numerators and denominators
42 * are assumed to be INT32B. Furthermore, the number 0x80000000
43 * is not allowed. The routines should work on any machine and
44 * compiler where these requirements are met.
46 * Internally, when 64-bit numbers are used, they are represented
47 * by an array of two INT32B. The 0 subscript contains the low
48 * order bits and the 1 subscript contains the high order bits.
49 * The numbers are usually used as two's complement signed
50 * integers, so the high bit of the 1 subscript is a sign bit.
62 # include "rational.h"
68 * Define SMALL to be a number that uses less than half as many bits as
69 * MAXLONG (15 as compared to 31). Define a SMALLRAT as a rational number
70 * whose numerator (absolute value) and denominator are in that range.
71 * The denominator is assumed to be positive.
74 #define SMALLRAT(q) ((q).n <= SMALL && (q).n >= -SMALL && (q).d <= SMALL)
78 * This macro checks whether a 64-bit integer is actually less than or
79 * equal to MAXLONG in absolute value, 0x7fffffff.
81 #define INT32(n) ((n)[1] == 0 && (n)[0] >= 0 || \
82 (n)[1] == -1 && (n)[0] < 0 && (n)[0] != 0x80000000)
86 * Return whether the first 64-bit number equals the second.
87 * To be equal, both words must be equal.
89 #define EQ64(x, y) ( (x)[1] == (y)[1] && (x)[0] == (y)[0] )
93 * Return whether the first 64-bit number is greater than the second.
94 * If the high order words are equal, use the low order words, unsigned;
95 * otherwise, just use the high order words.
97 #define GT64(x, y) ( \
99 (UINT32B)(x)[0] > (UINT32B)(y)[0] \
106 * Return whether the first 64-bit number is less than the second.
107 * If the high order words are equal, use the low order words, unsigned;
108 * otherwise, just use the high order words.
110 #define LT64(x, y) ( \
112 (UINT32B)(x)[0] < (UINT32B)(y)[0] \
119 * Return whether the first *unsigned* 64-bit number is less than or equal to
120 * the second. If the high order words are equal, use the low order words;
121 * otherwise, just use the high order words.
123 #define LEU64(x, y) ( \
125 (UINT32B)(x)[0] <= (UINT32B)(y)[0] \
127 (UINT32B)(x)[1] <= (UINT32B)(y)[1] \
132 * Negate a 64-bit number.
135 (x)[1] = ~(x)[1]; /* one's complement */ \
136 (x)[0] = -(x)[0]; /* two's complement */ \
137 if ((x)[0] == 0) /* if "carry" must inc high word */ \
143 * Shift a 64-bit number left one bit as unsigned (not that it matters).
145 #define SHL1U64(x) { \
146 (x)[1] <<= 1; /* shift high word */ \
147 if ((x)[0] < 0) /* if high bit of low word is set */ \
148 (x)[1]++; /* shift it into the high word */ \
149 (x)[0] <<= 1; /* shift low word */ \
154 * Shift a 64-bit number right one bit as unsigned.
156 #define SHR1U64(x) { \
157 (x)[0] = (UINT32B)(x)[0] >> 1; /* shift low word */ \
158 if ((x)[1] & 1) /* if low bit of high word set*/\
159 (x)[0] |= 0x80000000; /* shift it into low word */ \
160 (x)[1] = (UINT32B)(x)[1] >> 1; /* shift low word */ \
165 /* declare as static the functions that are only used internally */
167 static void ratmsg(int code);
168 static void add64_64(INT32B a[], INT32B x[], INT32B y[]);
169 static void mul32_64(INT32B a[], INT32B x, INT32B y);
170 static void divmod64(INT32B x[], INT32B y[], INT32B q[], INT32B r[]);
171 static void red64_64(INT32B num[], INT32B den[]);
173 static void ratmsg(), add64_64(), mul32_64(), divmod64(), red64_64();
177 int raterrno; /* set to error type upon return to user */
178 void (*raterrfuncp)(); /* error handler functions to be called */
180 static RATIONAL zero = {0,1};
183 * radd() add two rational numbers
185 * This function adds two rational numbers. They must be in standard
188 * Parameters: x the first number
189 * y the second number
191 * Return value: The sum (x + y), if it can be represented as a
192 * RATIONAL, else 0/1.
194 * Side effects: If radd() succeeds, it sets raterrno to RATNOERR.
195 * Otherwise, the numerator or denominator must have
196 * overflowed, so it sets raterrno to RATOVER and
197 * either prints a message or calls a user-supplied
207 RATIONAL a; /* the answer */
208 INT32B bign[2]; /* 64-bit numerator */
209 INT32B bigd[2]; /* 64-bit denominator */
210 INT32B bigt[2]; /* temp storage */
213 raterrno = RATNOERR; /* no error yet */
216 * If the numbers are small enough, do it the easy way, since there is
217 * then no danger of overflow.
219 if (SMALLRAT(x) && SMALLRAT(y)) {
220 a.n = x.n * y.d + x.d * y.n;
222 rred(&a); /* reduce to standard form */
227 * To avoid overflow during the calculations, use two INT32B to
230 mul32_64(bign, x.n, y.d); /* get first part of numerator */
231 mul32_64(bigt, x.d, y.n); /* get second part of numerator */
232 add64_64(bign, bign, bigt); /* add to get full numerator */
233 mul32_64(bigd, x.d, y.d); /* get denominator */
234 red64_64(bign, bigd); /* reduce */
236 /* overflow if the result can't fit in a RATIONAL */
237 if ( ! INT32(bign) || ! INT32(bigd) ) {
238 ratmsg(RATOVER); /* set raterrno, report error */
242 a.n = bign[0]; /* set answer */
249 * rsub() subtract two rational numbers
251 * This function subtracts two rational numbers. They must be in standard
254 * Parameters: x the first number
255 * y the second number
257 * Return value: The difference (x - y), if it can be represented as a
258 * RATIONAL, else 0/1.
260 * Side effects: If rsub() succeeds, it sets raterrno to RATNOERR.
261 * Otherwise, the numerator or denominator must have
262 * overflowed, so it sets raterrno to RATOVER and
263 * either prints a message or calls a user-supplied
274 * Just negate the second operand and add. We could call rneg() to
275 * negate y, but why waste the time?
282 * rmul() multiply two rational numbers
284 * This function multiplies two rational numbers. They must be in standard
287 * Parameters: x the first number
288 * y the second number
290 * Return value: The product (x * y), if it can be represented as a
291 * RATIONAL, else 0/1.
293 * Side effects: If rsub() succeeds, it sets raterrno to RATNOERR.
294 * Otherwise, the numerator or denominator must have
295 * overflowed, so it sets raterrno to RATOVER and
296 * either prints a message or calls a user-supplied
306 RATIONAL a; /* the answer */
307 INT32B bign[2]; /* 64-bit numerator */
308 INT32B bigd[2]; /* 64-bit denominator */
311 raterrno = RATNOERR; /* no error yet */
314 * If the numbers are small enough, do it the easy way, since there is
315 * then no danger of overflow.
317 if (SMALLRAT(x) && SMALLRAT(y)) {
320 rred(&a); /* reduce to standard form */
325 * To avoid overflow during the calculations, use two INT32B to
328 mul32_64(bign, x.n, y.n); /* get numerator */
329 mul32_64(bigd, x.d, y.d); /* get denominator */
330 red64_64(bign, bigd); /* reduce */
332 /* overflow if the result can't fit in a RATIONAL */
333 if ( ! INT32(bign) || ! INT32(bigd) ) {
334 ratmsg(RATOVER); /* set raterrno, report error */
338 a.n = bign[0]; /* set answer */
345 * rdiv() divide two rational numbers
347 * This function divides two rational numbers. They must be in standard
350 * Parameters: x the first number
351 * y the second number
353 * Return value: The quotient (x / y), if it is defined and can be
354 * represented as a RATIONAL, else 0/1.
356 * Side effects: If rdiv() succeeds, it sets raterrno to RATNOERR.
357 * Otherwise, either the second number was zero or the
358 * numerator or denominator overflowed. In this case,
359 * it sets raterrno to RATDIV0 or RATOVER, respectively,
360 * and either prints a message or calls a user-supplied
370 RATIONAL r; /* reciprocal of y */
373 r = rinv(y); /* first find 1/y */
375 if (raterrno != RATNOERR) /* if y was 0, return failure now */
379 * Return x * r. Whether rmul() succeeds or fails, we still just want
380 * to leave raterrno the same and return what rmul() returns.
386 * rneg() negate a rational number
388 * This function negates a rational number. It must be in standard form.
390 * Parameters: x the number
392 * Return value: The negative (-x).
394 * Side effects: It sets raterrno to RATNOERR.
403 raterrno = RATNOERR; /* no errors are possible */
407 /* answer is already in standard form since x was */
412 * rinv() invert a rational number
414 * This function inverts a rational number. It must be in standard form.
416 * Parameters: x the number
418 * Return value: The reciprocal (1 / x), if it is defined, else 0/1.
420 * Side effects: If rinv() succeeds, it sets raterrno to RATNOERR.
421 * Otherwise, the second number must have been zero,
422 * so it sets raterrno to RATDIV0 and either prints a
423 * message or calls a user-supplied error handler.
432 RATIONAL a; /* the answer */
435 /* check for division by 0 */
437 ratmsg(RATDIV0); /* set raterrno, report error */
441 raterrno = RATNOERR; /* no errors from here on */
443 a.n = x.d; /* flip numerator and denominator */
446 if (a.d < 0) { /* if x was negative, reverse signs */
455 * rrai() raise a rational number to an integral power
457 * This function raises a rational number to an integral power. The
458 * rational number must be in standard form.
460 * Parameters: x the rational number
461 * n the power, an integer
463 * Return value: The result (x to the nth power), if it is defined and
464 * can be represented as a RATIONAL, else 0/1.
466 * Side effects: If rrai() succeeds, it sets raterrno to RATNOERR.
467 * Otherwise, either zero is being raised to a non-
468 * positive power, or the numerator or denominator
469 * overflowed. In this case, it sets raterrno to
470 * RATDIV0 or RATOVER, respectively, and either prints
471 * a message or calls a user-supplied error handler.
481 static RATIONAL one = {1,1};
483 RATIONAL a; /* the answer */
484 register int i; /* loop counter */
487 /* it is undefined to raise zero to a nonpositive power */
488 if (ZE(x) && n <= 0) {
489 ratmsg(RATDIV0); /* set raterrno, report error */
493 raterrno = RATNOERR; /* no error yet */
495 a = one; /* init to 1 */
497 for (i = 0; i < n; i++) {
498 a = rmul(a, x); /* mul again by x */
499 if (raterrno != RATNOERR)
503 for (i = 0; i > n; i--) {
504 a = rdiv(a, x); /* div again by x */
505 if (raterrno != RATNOERR)
514 * rred() reduce a rational number to standard form
516 * This function puts a rational number into standard form; that is,
517 * numerator and denominator will be relatively prime and the denominator
518 * will be positive. On input, they may be any integers whose absolute
519 * values do not exceed MAXLONG.
521 * Parameters: ap pointer to the rational number
523 * Return value: None.
525 * Side effects: If ap->d is 0, the function sets raterrno to RATDIV0,
526 * either prints a message or calls a user-supplied
527 * error handler, and sets *ap to 0/1. Otherwise, it
528 * sets raterrno to RATNOERR and puts *ap in standard form.
534 register RATIONAL *ap;
537 register INT32B b, c, r; /* temp variables for Euclidean algorithm */
538 register int sign; /* answer is pos (1) or neg (-1) */
542 * Since the numerator and denominator can be anything <= MAXLONG,
543 * we must guard against division by 0.
546 ratmsg(RATDIV0); /* set raterrno, report error */
551 raterrno = RATNOERR; /* no errors possible from here on */
553 if (ap->n == 0) { /* if so, answer is "0/1" */
558 /* now figure out sign of answer, and make n & d positive */
559 sign = 1; /* init to positive */
560 if (ap->n < 0) { /* reverse if numerator neg */
564 if (ap->d < 0) { /* reverse if denominator neg */
569 /* now check whether numerator or denominator are equal */
570 if (ap->n == ap->d) { /* if so, answer is +1 or -1 */
576 if (ap->n < ap->d) { /* set so that c > b */
584 /* use Euclidean Algorithm to find greatest common divisor of c & b */
591 /* now c is the greatest common divisor */
593 ap->n /= c; /* divide out greatest common divisor */
594 ap->d /= c; /* divide out greatest common divisor */
596 if (sign < 0) /* put sign in if result should be negative */
603 * ator() convert an ascii string to a rational number
605 * This function takes an ascii string as input and interprets it as
606 * a rational number. White space may precede the number, but the
607 * number may not contain white space. The numerator may be preceded
608 * by a minus sign. The denomintor is optional, but if present, must
609 * not contain a sign. In short, the number must match one of the
610 * following lex regular expressions, which starts where s points and
611 * ends before the first character not matching the pattern:
613 * [ \t\n]*-?[0-9]+\/[0-9]+
614 * Further restrictions are that the absolute values of numerator and
615 * denominator cannot exceed MAXLONG, and the denominator cannot be 0.
616 * If neither pattern is matched, or the further restrictions are
617 * violated, the function sets *rp to 0/1 and returns NULL. Otherwise,
618 * it sets *rp to the result in standard form, and returns a pointer to
619 * the first char after the number found.
621 * Parameters: rp pointer to where the answer goes
622 * s string containing ascii rational number
624 * Return value: If a valid rational number is found, the function
625 * returns a pointer to the next char in the string
626 * following the number. Otherwise it returns NULL.
628 * Side effects: If ator() succeeds, it sets *rp to the result.
629 * Otherwise, it sets it to 0/1.
635 register RATIONAL *rp;
639 register char *p; /* point somewhere in s[] */
640 int sign; /* 1 means positive, -1 negative */
643 /* skip by white space */
644 for (p = s; isspace(*p); p++)
647 /* init sign to positive; then reverse it if a dash is found */
654 /* fail if there are no digits */
655 if ( ! isdigit(*p) ) {
661 * Collect the numerator digits, and defend against overflow.
664 while ( isdigit(*p) ) {
665 if (rp->n > MAXLONG / 10) {
670 if (rp->n > MAXLONG - (*p - '0')) {
677 if (sign < 0) /* make negative if necessary */
682 * If there is to be a denominator, collect its digits. Otherwise,
683 * set it to 1. Defend against overflow.
687 if ( ! isdigit(*p) ) { /* must be digit (no '-' allowed) */
692 while ( isdigit(*p) ) {
693 if (rp->d > MAXLONG / 10) {
698 if (rp->d > MAXLONG - (*p - '0')) {
704 if (rp->d == 0) { /* zero denominator is a failure */
709 rp->d = 1; /* no denominator; assume 1 */
712 rred(rp); /* reduce the fraction */
714 return(p); /* first char after the number */
718 * rtoa() convert a rational number to an ascii string
720 * This function takes a rational number as input converts it into
721 * an ascii string. If the denominator is 1, it will not be printed.
722 * The number must be in standard form.
724 * Parameters: s pointer to where the answer goes
725 * rp pointer to the rational number
727 * Return value: The function returns a pointer to the next char in
728 * the string following the number.
730 * Side effects: The function sets s[] to the result.
740 register INT32B num, den; /* copy of num and den from *rp */
741 register int i; /* index into t[] */
742 char t[12]; /* temp answer string */
745 num = rp->n; /* copy num and den for efficiency */
748 if (num < 0) { /* if num is negative */
749 *s++ = '-'; /* output minus sign */
750 num = -num; /* and make num positive */
754 do { /* calc digits in reverse order */
755 t[i++] = num % 10 + '0';
757 } while (num > 0); /* always loop at least once so 0="0"*/
759 while (--i >= 0) /* copy digits to answer string */
762 if (den != 1) { /* if a denominator is needed */
763 *s++ = '/'; /* fraction bar */
765 do { /* calc digits in reverse order */
766 t[i++] = den % 10 + '0';
770 while (--i >= 0) /* copy digits to answer string */
778 * gtrat() decide whether one rational is greater than another
780 * This function decides whether its first parameter is greater than
781 * its second. It is used by the macros GT, GE, LT, and LE.
782 * The numbers must be in standard form. (Actually, all that is
783 * matters is that the denominators be positive.)
785 * Parameters: x the first rational
786 * y the second rational
788 * Return value: 1 if x > y, otherwise 0
799 INT32B a[2]; /* temp holding areas for 64-bit numbers */
803 /* if no overflow possible, cross-multiply and return truth value */
804 /* note: this depends on positive denominators */
805 if (SMALLRAT(x) && SMALLRAT(y))
806 return(x.n * y.d > x.d * y.n);
809 * The numbers are too big; we have to do it the hard way to avoid
810 * overflow. Cross-multiply. Note: this depends on positive
813 mul32_64(a, x.n, y.d);
814 mul32_64(b, x.d, y.n);
820 * ratmsg() handle rational error of type "code"
822 * This function sets raterrno. Then calls the user's error handler,
823 * if there is one, or else prints a message to standard error.
825 * Parameters: code the error code
827 * Return value: None.
829 * Side effects: raterrno is set; then either a message is printed
830 * to standard error or the user's error handler is
840 raterrno = code; /* set global error flag */
842 if (raterrfuncp == 0) {
843 /* no user trap exists, so print message from here */
846 (void)fputs("rational overflow\n", stderr);
850 (void)fputs("rational division by zero\n", stderr);
854 (void)fputs("invalid number passed to rational number routine\n", stderr);
858 (void)fputs("error in rational routines\n", stderr);
862 /* call user trap function to handle the error */
863 (*raterrfuncp)(code);
868 * add64_64() add 64-bit numbers to get a 64-bit number
870 * This function adds two 64-bit signed numbers to get a 64-bit
871 * signed number. It is assumed that the result will not overflow.
872 * Any of the inputs may be the same arrays.
874 * Parameters: a answer goes here
880 * Side effects: a is set to the result.
891 INT32B t[2]; /* temp storage */
894 /* first add low and high parts separately */
895 /* use temp storage in case a[] is the same array as x[] or y[] */
899 /* figure out if the low part carries into the high part */
900 if (x[0] < 0 && y[0] < 0) { /* both high order bits set */
901 t[1]++; /* must be a carry */
902 } else if (x[0] < 0 || y[0] < 0) { /* exactly one high bit set */
903 if (t[0] >= 0) /* if result high bit clear */
904 t[1]++; /* must be a carry */
907 a[0] = t[0]; /* copy results */
912 * mul32_64() multiply 32-bit numbers to get a 64-bit number
914 * This function multiplies two 32-bit signed numbers to get a 64-bit
915 * signed number. The numbers must not equal 0x80000000. Overflow
918 * Parameters: a answer goes here
919 * x the first 32-bit number
920 * y the second 32-bit number
924 * Side effects: a is set to the result.
935 INT32B t[2]; /* temp storage for inner terms */
936 INT32B xl, xh; /* low and high 16 bits of x */
937 INT32B yl, yh; /* low and high 16 bits of y */
938 int sign; /* sign of the result */
941 /* make both numbers positive and determine the sign of the result */
942 sign = 1; /* start at positive */
952 /* break x and y into high and low pieces */
953 xl = x & 0xffff; /* 0 <= xl <= 0xffff */
954 xh = x >> 16; /* 0 <= xh <= 0x7fff */
955 yl = y & 0xffff; /* 0 <= yl <= 0xffff */
956 yh = y >> 16; /* 0 <= yh <= 0x7fff */
958 /* multiply the outer parts */
959 a[0] = xl * yl; /* 0 <= a[0] <= 0xfffe0001 */
960 a[1] = xh * yh; /* 0 <= a[1] <= 0x3fff0001 */
962 /* multiply the inner parts and break the result in two pieces */
963 t[0] = xl * yh + xh * yl; /* 0 <= t[0] <= 0xfffd0002 */
964 t[1] = (UINT32B)t[0] >> 16; /* 0 <= t[1] <= 0x0000fffd */
965 t[0] <<= 16; /* 0 <= t[0] <= 0xffff0000 */
967 /* add the two partial products */
968 add64_64(a, a, t); /* 0 <= a <= 0x3fffffff00000001 */
970 /* if the answer is supposed to be negative, negate it */
976 * divmod64() find quotient and remainder of two 64-bit numbers
978 * This function takes two 64-bit numbers and divides the first by the
979 * second, to get a quotient and remainder, both 64 bits. It is assumed
980 * that the first number is nonnegative and the second number is positive.
981 * q and r must be different arrays.
983 * Parameters: x first number (dividend)
984 * y second number (divisor)
990 * Side effects: q and r are altered to be the results
991 * x and y are not altered
1003 INT32B s[2]; /* temp storage for divisor */
1004 INT32B t[2]; /* temp storage for scratch */
1005 register int shift; /* how far has y been shifted? */
1008 r[0] = x[0]; /* copy dividend to remainder place */
1010 s[0] = y[0]; /* copy divisor to temp storage */
1013 /* shift divisor left until greater than dividend */
1014 /* compare as unsigned so no problem if it gets shifted into sign bit */
1015 for (shift = 0; LEU64(s, r); shift++)
1018 SHR1U64(s); /* shift it back right one, so <= dividend */
1021 q[0] = 0; /* start quotient at 0 */
1025 * Loop once for each bit shifted.
1027 for ( ; shift >= 0; shift--) {
1029 * If the current divisor does not exceed what's left of the
1030 * dividend, subtract it from it, and record that by setting
1031 * the low order bit of the current quotient.
1033 if ( ! GT64(s, r) ) {
1041 /* shift quotient left and divisor right */
1046 SHR1U64(q); /* shift quotient right */
1050 * red64_64() reduce a 64 bit over 64 bit rational to lowest terms
1052 * This function takes two 64-bit numbers as numerator and denominator
1053 * of a rational number, and reduces them to lowest terms, with the
1054 * denominator positive. If the user called this package correctly, the
1055 * denominator cannot be zero, but to be defensive we check for that, and
1056 * if it happens, set raterrno to RATPARM and either print a message or
1057 * call a user-supplied error handler.
1059 * Parameters: num numerator
1062 * Return value: none
1064 * Side effects: num and den are altered to be the result
1074 INT32B b[2], c[2], r[2]; /* temp variables for Euclidean algorithm */
1075 INT32B junk[2]; /* placeholder for calling divmod64 */
1076 int sign; /* answer is pos (1) or neg (-1) */
1079 if (den[1] == 0 && den[0] == 0) { /* if den == 0 */
1081 * This is an error. The user must have called a routine with
1082 * an invalid number for us to get here, in fact a number with
1083 * a zero denominator, since "den" here always was created as
1084 * the product of denominators. Report the error and return
1087 num[1] = 0; /* set num = 0 */
1089 den[1] = 0; /* set den = 1 */
1091 ratmsg(RATPARM); /* set raterrno, report error */
1095 if (num[1] == 0 && num[0] == 0) { /* if num == 0 */
1096 den[1] = 0; /* set den = 1; answer is 0/1 */
1101 /* now figure out sign of answer, and make num & den positive */
1102 sign = 1; /* init to positive */
1103 if (num[1] < 0) { /* if numerator neg */
1104 sign = -sign; /* reverse the sign */
1107 if (den[1] < 0) { /* if denominator neg */
1108 sign = -sign; /* reverse the sign */
1112 /* now check whether numerator or denominator is larger */
1113 if (EQ64(num, den)) {
1114 num[0] = sign; /* answer is +1 or -1 */
1116 if (sign < 0) /* set high order word to sign bit */
1121 den[1] = 0; /* set den to 1 */
1127 /* set up c and b so that one is num, the other den, and c > b */
1128 if (LT64(num, den)) { /* if num < den */
1129 c[0] = den[0]; /* c = den */
1131 b[0] = num[0]; /* b = num */
1134 c[0] = num[0]; /* c = num */
1136 b[0] = den[0]; /* b = den */
1140 /* use Euclidean Algorithm to find greatest common divisor of c & b */
1142 divmod64(c, b, junk, r); /* r = c % b */
1143 c[0] = b[0]; /* c = b */
1145 b[0] = r[0]; /* b = r */
1147 } while (r[0] != 0 || r[1] != 0); /* while r != 0 */
1149 /* now c is the greatest common divisor of num and den */
1151 /* divide out the greatest common divisor and put the sign in */
1152 divmod64(num, c, num, junk); /* num /= c */
1153 if (sign < 0) /* if should be negative */
1154 NEG64(num); /* negate the numerator */
1155 divmod64(den, c, den, junk); /* den /= c */