From 0054d4bb540493bf87d38fec54ad16471334453e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Kijewski?= <rene.kijewski@fu-berlin.de> Date: Tue, 11 Feb 2014 15:01:09 +0100 Subject: [PATCH] Adapt quad lib to RIOT coding conventions --- sys/quad_math/adddi3.c | 19 +- sys/quad_math/anddi3.c | 19 +- sys/quad_math/ashldi3.c | 36 +-- sys/quad_math/ashrdi3.c | 58 ++--- sys/quad_math/cmpdi2.c | 20 +- sys/quad_math/divdi3.c | 39 ++-- sys/quad_math/fixdfdi.c | 30 +-- sys/quad_math/fixsfdi.c | 28 +-- sys/quad_math/fixunsdfdi.c | 60 ++--- sys/quad_math/fixunssfdi.c | 98 ++++---- sys/quad_math/floatdidf.c | 50 ++-- sys/quad_math/floatdisf.c | 54 ++--- sys/quad_math/floatunsdidf.c | 19 +- sys/quad_math/iordi3.c | 19 +- sys/quad_math/lshldi3.c | 36 +-- sys/quad_math/lshrdi3.c | 36 +-- sys/quad_math/moddi3.c | 40 ++-- sys/quad_math/muldi3.c | 279 +++++++++++----------- sys/quad_math/negdi2.c | 17 +- sys/quad_math/notdi2.c | 17 +- sys/quad_math/qdivrem.c | 436 +++++++++++++++++++---------------- sys/quad_math/quad.h | 46 ++-- sys/quad_math/subdi3.c | 19 +- sys/quad_math/ucmpdi2.c | 20 +- sys/quad_math/udivdi3.c | 10 +- sys/quad_math/umoddi3.c | 14 +- sys/quad_math/xordi3.c | 19 +- 27 files changed, 805 insertions(+), 733 deletions(-) diff --git a/sys/quad_math/adddi3.c b/sys/quad_math/adddi3.c index 70e236a5d3..12af0f4ad8 100644 --- a/sys/quad_math/adddi3.c +++ b/sys/quad_math/adddi3.c @@ -1,7 +1,7 @@ -/* $OpenBSD: adddi3.c,v 1.6 2005/08/08 08:05:35 espie Exp $ */ +/* $OpenBSD: adddi3.c,v 1.6 2005/08/08 08:05:35 espie Exp $ */ /*- * Copyright (c) 1992, 1993 - * The Regents of the University of California. All rights reserved. + * The Regents of the University of California. All rights reserved. * * This software was developed by the Computer Systems Engineering group * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and @@ -39,14 +39,13 @@ * u_int addition x+y occurs if and only if the sum x+y is less than * either x or y (the choice to compare with x or y is arbitrary). */ -quad_t -__adddi3(quad_t a, quad_t b) +quad_t __adddi3(quad_t a, quad_t b) { - union uu aa, bb, sum; + union uu aa, bb, sum; - aa.q = a; - bb.q = b; - sum.ul[L] = aa.ul[L] + bb.ul[L]; - sum.ul[H] = aa.ul[H] + bb.ul[H] + (sum.ul[L] < bb.ul[L]); - return (sum.q); + aa.q = a; + bb.q = b; + sum.ul[L] = aa.ul[L] + bb.ul[L]; + sum.ul[H] = aa.ul[H] + bb.ul[H] + (sum.ul[L] < bb.ul[L]); + return sum.q; } diff --git a/sys/quad_math/anddi3.c b/sys/quad_math/anddi3.c index 2512ad783d..06074aff2c 100644 --- a/sys/quad_math/anddi3.c +++ b/sys/quad_math/anddi3.c @@ -1,7 +1,7 @@ -/* $OpenBSD: anddi3.c,v 1.5 2005/08/08 08:05:35 espie Exp $ */ +/* $OpenBSD: anddi3.c,v 1.5 2005/08/08 08:05:35 espie Exp $ */ /*- * Copyright (c) 1992, 1993 - * The Regents of the University of California. All rights reserved. + * The Regents of the University of California. All rights reserved. * * This software was developed by the Computer Systems Engineering group * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and @@ -37,14 +37,13 @@ /* * Return a & b, in quad. */ -quad_t -__anddi3(quad_t a, quad_t b) +quad_t __anddi3(quad_t a, quad_t b) { - union uu aa, bb; + union uu aa, bb; - aa.q = a; - bb.q = b; - aa.ul[0] &= bb.ul[0]; - aa.ul[1] &= bb.ul[1]; - return (aa.q); + aa.q = a; + bb.q = b; + aa.ul[0] &= bb.ul[0]; + aa.ul[1] &= bb.ul[1]; + return aa.q; } diff --git a/sys/quad_math/ashldi3.c b/sys/quad_math/ashldi3.c index d21f54f87b..d9e78bb453 100644 --- a/sys/quad_math/ashldi3.c +++ b/sys/quad_math/ashldi3.c @@ -1,7 +1,7 @@ -/* $OpenBSD: ashldi3.c,v 1.6 2005/08/08 08:05:35 espie Exp $ */ +/* $OpenBSD: ashldi3.c,v 1.6 2005/08/08 08:05:35 espie Exp $ */ /*- * Copyright (c) 1992, 1993 - * The Regents of the University of California. All rights reserved. + * The Regents of the University of California. All rights reserved. * * This software was developed by the Computer Systems Engineering group * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and @@ -38,21 +38,23 @@ * Shift a (signed) quad value left (arithmetic shift left). * This is the same as logical shift left! */ -quad_t -__ashldi3(quad_t a, qshift_t shift) +quad_t __ashldi3(quad_t a, qshift_t shift) { - union uu aa; + union uu aa; - if (shift == 0) - return(a); - aa.q = a; - if (shift >= INT_BITS) { - aa.ul[H] = aa.ul[L] << (shift - INT_BITS); - aa.ul[L] = 0; - } else { - aa.ul[H] = (aa.ul[H] << shift) | - (aa.ul[L] >> (INT_BITS - shift)); - aa.ul[L] <<= shift; - } - return (aa.q); + if (shift == 0) { + return a; + } + + aa.q = a; + + if (shift >= INT_BITS) { + aa.ul[H] = aa.ul[L] << (shift - INT_BITS); + aa.ul[L] = 0; + } else { + aa.ul[H] = (aa.ul[H] << shift) | (aa.ul[L] >> (INT_BITS - shift)); + aa.ul[L] <<= shift; + } + + return aa.q; } diff --git a/sys/quad_math/ashrdi3.c b/sys/quad_math/ashrdi3.c index d99643bac5..f74991cd05 100644 --- a/sys/quad_math/ashrdi3.c +++ b/sys/quad_math/ashrdi3.c @@ -1,7 +1,7 @@ -/* $OpenBSD: ashrdi3.c,v 1.6 2005/08/08 08:05:35 espie Exp $ */ +/* $OpenBSD: ashrdi3.c,v 1.6 2005/08/08 08:05:35 espie Exp $ */ /*- * Copyright (c) 1992, 1993 - * The Regents of the University of California. All rights reserved. + * The Regents of the University of California. All rights reserved. * * This software was developed by the Computer Systems Engineering group * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and @@ -37,34 +37,34 @@ /* * Shift a (signed) quad value right (arithmetic shift right). */ -quad_t -__ashrdi3(quad_t a, qshift_t shift) +quad_t __ashrdi3(quad_t a, qshift_t shift) { - union uu aa; + union uu aa; - if (shift == 0) - return(a); - aa.q = a; - if (shift >= INT_BITS) { - int s; + if (shift == 0) { + return a; + } - /* - * Smear bits rightward using the machine's right-shift - * method, whether that is sign extension or zero fill, - * to get the `sign word' s. Note that shifting by - * INT_BITS is undefined, so we shift (INT_BITS-1), - * then 1 more, to get our answer. - */ - /* LINTED inherits machine dependency */ - s = (aa.sl[H] >> (INT_BITS - 1)) >> 1; - /* LINTED inherits machine dependency*/ - aa.ul[L] = aa.sl[H] >> (shift - INT_BITS); - aa.ul[H] = s; - } else { - aa.ul[L] = (aa.ul[L] >> shift) | - (aa.ul[H] << (INT_BITS - shift)); - /* LINTED inherits machine dependency */ - aa.sl[H] >>= shift; - } - return (aa.q); + aa.q = a; + + if (shift >= INT_BITS) { + /* + * Smear bits rightward using the machine's right-shift + * method, whether that is sign extension or zero fill, + * to get the `sign word' s. Note that shifting by + * INT_BITS is undefined, so we shift (INT_BITS-1), + * then 1 more, to get our answer. + */ + /* LINTED inherits machine dependency */ + int s = (aa.sl[H] >> (INT_BITS - 1)) >> 1; + /* LINTED inherits machine dependency*/ + aa.ul[L] = aa.sl[H] >> (shift - INT_BITS); + aa.ul[H] = s; + } else { + aa.ul[L] = (aa.ul[L] >> shift) | (aa.ul[H] << (INT_BITS - shift)); + /* LINTED inherits machine dependency */ + aa.sl[H] >>= shift; + } + + return aa.q; } diff --git a/sys/quad_math/cmpdi2.c b/sys/quad_math/cmpdi2.c index 5333bc6fe9..3aa54684a7 100644 --- a/sys/quad_math/cmpdi2.c +++ b/sys/quad_math/cmpdi2.c @@ -1,7 +1,7 @@ -/* $OpenBSD: cmpdi2.c,v 1.5 2005/08/08 08:05:35 espie Exp $ */ +/* $OpenBSD: cmpdi2.c,v 1.5 2005/08/08 08:05:35 espie Exp $ */ /*- * Copyright (c) 1992, 1993 - * The Regents of the University of California. All rights reserved. + * The Regents of the University of California. All rights reserved. * * This software was developed by the Computer Systems Engineering group * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and @@ -39,13 +39,15 @@ * Both a and b are considered signed---which means only the high word is * signed. */ -int -__cmpdi2(quad_t a, quad_t b) +int __cmpdi2(quad_t a, quad_t b) { - union uu aa, bb; + union uu aa, bb; - aa.q = a; - bb.q = b; - return (aa.sl[H] < bb.sl[H] ? 0 : aa.sl[H] > bb.sl[H] ? 2 : - aa.ul[L] < bb.ul[L] ? 0 : aa.ul[L] > bb.ul[L] ? 2 : 1); + aa.q = a; + bb.q = b; + return aa.sl[H] < bb.sl[H] ? 0 + : aa.sl[H] > bb.sl[H] ? 2 + : aa.ul[L] < bb.ul[L] ? 0 + : aa.ul[L] > bb.ul[L] ? 2 + : 1; } diff --git a/sys/quad_math/divdi3.c b/sys/quad_math/divdi3.c index 0c194fcba8..e7212a74e8 100644 --- a/sys/quad_math/divdi3.c +++ b/sys/quad_math/divdi3.c @@ -1,7 +1,7 @@ -/* $OpenBSD: divdi3.c,v 1.6 2005/08/08 08:05:35 espie Exp $ */ +/* $OpenBSD: divdi3.c,v 1.6 2005/08/08 08:05:35 espie Exp $ */ /*- * Copyright (c) 1992, 1993 - * The Regents of the University of California. All rights reserved. + * The Regents of the University of California. All rights reserved. * * This software was developed by the Computer Systems Engineering group * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and @@ -38,22 +38,29 @@ * Divide two signed quads. * ??? if -1/2 should produce -1 on this machine, this code is wrong */ -quad_t -__divdi3(quad_t a, quad_t b) +quad_t __divdi3(quad_t a, quad_t b) { - u_quad_t ua, ub, uq; - int neg = 0; + u_quad_t ua, ub, uq; + int neg = 0; - ua = a; - ub = b; + ua = a; + ub = b; - if (a < 0) - ua = -ua, neg ^= 1; - if (b < 0) - ub = -ub, neg ^= 1; + if (a < 0) { + ua = -ua; + neg = !neg; + } - uq = __qdivrem(ua, ub, (u_quad_t *)0); - if (neg) - uq = - uq; - return uq; + if (b < 0) { + ub = -ub; + neg = !neg; + } + + uq = __qdivrem(ua, ub, NULL); + + if (neg) { + uq = -uq; + } + + return uq; } diff --git a/sys/quad_math/fixdfdi.c b/sys/quad_math/fixdfdi.c index 861cf354ab..799547108e 100644 --- a/sys/quad_math/fixdfdi.c +++ b/sys/quad_math/fixdfdi.c @@ -1,7 +1,7 @@ -/* $OpenBSD: fixdfdi.c,v 1.5 2005/08/08 08:05:35 espie Exp $ */ +/* $OpenBSD: fixdfdi.c,v 1.5 2005/08/08 08:05:35 espie Exp $ */ /*- * Copyright (c) 1992, 1993 - * The Regents of the University of California. All rights reserved. + * The Regents of the University of California. All rights reserved. * * This software was developed by the Computer Systems Engineering group * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and @@ -38,17 +38,19 @@ * Convert double to (signed) quad. * We clamp anything that is out of range. */ -quad_t -__fixdfdi(double x) +quad_t __fixdfdi(double x) { - if (x < 0) - if (x <= QUAD_MIN) - return (QUAD_MIN); - else - return ((quad_t)-(u_quad_t)-x); - else - if (x >= QUAD_MAX) - return (QUAD_MAX); - else - return ((quad_t)(u_quad_t)x); + if (x < 0) { + if (x <= QUAD_MIN) { + return QUAD_MIN; + } else { + return (quad_t) -(u_quad_t) -x; + } + } else { + if (x >= QUAD_MAX) { + return QUAD_MAX; + } else { + return (quad_t) (u_quad_t) x; + } + } } diff --git a/sys/quad_math/fixsfdi.c b/sys/quad_math/fixsfdi.c index fb28d46923..d7d006eaf5 100644 --- a/sys/quad_math/fixsfdi.c +++ b/sys/quad_math/fixsfdi.c @@ -1,4 +1,4 @@ -/* $OpenBSD: fixsfdi.c,v 1.6 2005/08/08 08:05:35 espie Exp $ */ +/* $OpenBSD: fixsfdi.c,v 1.6 2005/08/08 08:05:35 espie Exp $ */ /*- * Copyright (c) 1992 The Regents of the University of California. * All rights reserved. @@ -40,17 +40,19 @@ * * N.B.: must use new ANSI syntax (sorry). */ -quad_t -__fixsfdi(float x) +quad_t __fixsfdi(float x) { - if (x < 0) - if (x <= QUAD_MIN) - return (QUAD_MIN); - else - return ((quad_t)-(u_quad_t)-x); - else - if (x >= QUAD_MAX) - return (QUAD_MAX); - else - return ((quad_t)(u_quad_t)x); + if (x < 0) { + if (x <= QUAD_MIN) { + return QUAD_MIN; + } else { + return (quad_t) -(u_quad_t) -x; + } + } else { + if (x >= QUAD_MAX) { + return QUAD_MAX; + } else { + return (quad_t) (u_quad_t) x; + } + } } diff --git a/sys/quad_math/fixunsdfdi.c b/sys/quad_math/fixunsdfdi.c index ed04c130d9..a7d34a4120 100644 --- a/sys/quad_math/fixunsdfdi.c +++ b/sys/quad_math/fixunsdfdi.c @@ -1,7 +1,7 @@ -/* $OpenBSD: fixunsdfdi.c,v 1.7 2005/08/08 08:05:35 espie Exp $ */ +/* $OpenBSD: fixunsdfdi.c,v 1.7 2005/08/08 08:05:35 espie Exp $ */ /*- * Copyright (c) 1992, 1993 - * The Regents of the University of California. All rights reserved. + * The Regents of the University of California. All rights reserved. * * This software was developed by the Computer Systems Engineering group * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and @@ -34,39 +34,43 @@ #include "quad.h" -#define ONE_FOURTH ((int)1 << (INT_BITS - 2)) -#define ONE_HALF (ONE_FOURTH * 2.0) -#define ONE (ONE_FOURTH * 4.0) +#define ONE_FOURTH ((int) 1 << (INT_BITS - 2)) +#define ONE_HALF (ONE_FOURTH * 2.0) +#define ONE (ONE_FOURTH * 4.0) /* * Convert double to (unsigned) quad. * Not sure what to do with negative numbers---for now, anything out * of range becomes UQUAD_MAX. */ -u_quad_t -__fixunsdfdi(double x) +u_quad_t __fixunsdfdi(double x) { - union uu t; - unsigned int tmp; + union uu t; + unsigned int tmp; - if (x < 0) - return (UQUAD_MAX); /* ??? should be 0? ERANGE??? */ -#ifdef notdef /* this falls afoul of a GCC bug */ - if (x >= UQUAD_MAX) - return (UQUAD_MAX); -#else /* so we wire in 2^64-1 instead */ - if (x >= 18446744073709551615.0) /* XXX */ - return (UQUAD_MAX); + if (x < 0) { + return (UQUAD_MAX); /* ??? should be 0? ERANGE??? */ + } + +#ifdef notdef /* this falls afoul of a GCC bug */ + if (x >= UQUAD_MAX) { + return (UQUAD_MAX); + } +#else /* so we wire in 2^64-1 instead */ + if (x >= 18446744073709551615.0) { /* XXX */ + return (UQUAD_MAX); + } #endif - /* - * Now we know that 0 <= x <= 18446744073709549568. The upper - * limit is one ulp less than 18446744073709551615 tested for above. - * Dividing this by 2^32 will *not* round irrespective of any - * rounding modes (except if the result is an IEEE denorm). - * Furthermore, the quotient will fit into a 32-bit integer. - */ - tmp = x / ONE; - t.ul[L] = (unsigned int) (x - tmp * ONE); - t.ul[H] = tmp; - return (t.uq); + + /* + * Now we know that 0 <= x <= 18446744073709549568. The upper + * limit is one ulp less than 18446744073709551615 tested for above. + * Dividing this by 2^32 will *not* round irrespective of any + * rounding modes (except if the result is an IEEE denorm). + * Furthermore, the quotient will fit into a 32-bit integer. + */ + tmp = x / ONE; + t.ul[L] = (unsigned int) (x - tmp * ONE); + t.ul[H] = tmp; + return (t.uq); } diff --git a/sys/quad_math/fixunssfdi.c b/sys/quad_math/fixunssfdi.c index 359779e0cd..b3badd4beb 100644 --- a/sys/quad_math/fixunssfdi.c +++ b/sys/quad_math/fixunssfdi.c @@ -1,7 +1,7 @@ -/* $OpenBSD: fixunssfdi.c,v 1.5 2005/08/08 08:05:35 espie Exp $ */ +/* $OpenBSD: fixunssfdi.c,v 1.5 2005/08/08 08:05:35 espie Exp $ */ /*- * Copyright (c) 1992, 1993 - * The Regents of the University of California. All rights reserved. + * The Regents of the University of California. All rights reserved. * * This software was developed by the Computer Systems Engineering group * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and @@ -34,9 +34,9 @@ #include "quad.h" -#define ONE_FOURTH ((int)1 << (INT_BITS - 2)) -#define ONE_HALF (ONE_FOURTH * 2.0) -#define ONE (ONE_FOURTH * 4.0) +#define ONE_FOURTH ((int)1 << (INT_BITS - 2)) +#define ONE_HALF (ONE_FOURTH * 2.0) +#define ONE (ONE_FOURTH * 4.0) /* * Convert float to (unsigned) quad. We do most of our work in double, @@ -47,47 +47,55 @@ * * N.B.: must use new ANSI syntax (sorry). */ -u_quad_t -__fixunssfdi(float f) +u_quad_t __fixunssfdi(float f) { - double x, toppart; - union uu t; + double x, toppart; + union uu t; + + if (f < 0) { + return (UQUAD_MAX); /* ??? should be 0? ERANGE??? */ + } + +#ifdef notdef /* this falls afoul of a GCC bug */ + if (f >= UQUAD_MAX) { + return UQUAD_MAX; + } +#else /* so we wire in 2^64-1 instead */ + if (f >= 18446744073709551615.0) { /* XXX */ + return (UQUAD_MAX); + } - if (f < 0) - return (UQUAD_MAX); /* ??? should be 0? ERANGE??? */ -#ifdef notdef /* this falls afoul of a GCC bug */ - if (f >= UQUAD_MAX) - return (UQUAD_MAX); -#else /* so we wire in 2^64-1 instead */ - if (f >= 18446744073709551615.0) /* XXX */ - return (UQUAD_MAX); #endif - x = f; - /* - * Get the upper part of the result. Note that the divide - * may round up; we want to avoid this if possible, so we - * subtract `1/2' first. - */ - toppart = (x - ONE_HALF) / ONE; - /* - * Now build a u_quad_t out of the top part. The difference - * between x and this is the bottom part (this may introduce - * a few fuzzy bits, but what the heck). With any luck this - * difference will be nonnegative: x should wind up in the - * range [0..UINT_MAX]. For paranoia, we assume [INT_MIN.. - * 2*UINT_MAX] instead. - */ - t.ul[H] = (unsigned int)toppart; - t.ul[L] = 0; - x -= (double)t.uq; - if (x < 0) { - t.ul[H]--; - x += UINT_MAX; - } - if (x > UINT_MAX) { - t.ul[H]++; - x -= UINT_MAX; - } - t.ul[L] = (u_int)x; - return (t.uq); + + x = f; + /* + * Get the upper part of the result. Note that the divide + * may round up; we want to avoid this if possible, so we + * subtract `1/2' first. + */ + toppart = (x - ONE_HALF) / ONE; + /* + * Now build a u_quad_t out of the top part. The difference + * between x and this is the bottom part (this may introduce + * a few fuzzy bits, but what the heck). With any luck this + * difference will be nonnegative: x should wind up in the + * range [0..UINT_MAX]. For paranoia, we assume [INT_MIN.. + * 2*UINT_MAX] instead. + */ + t.ul[H] = (unsigned int) toppart; + t.ul[L] = 0; + x -= (double) t.uq; + + if (x < 0) { + t.ul[H]--; + x += UINT_MAX; + } + + if (x > UINT_MAX) { + t.ul[H]++; + x -= UINT_MAX; + } + + t.ul[L] = (u_int)x; + return t.uq; } diff --git a/sys/quad_math/floatdidf.c b/sys/quad_math/floatdidf.c index 5cdc8393f2..0b0aaf02a8 100644 --- a/sys/quad_math/floatdidf.c +++ b/sys/quad_math/floatdidf.c @@ -1,7 +1,7 @@ -/* $OpenBSD: floatdidf.c,v 1.6 2005/08/08 08:05:35 espie Exp $ */ +/* $OpenBSD: floatdidf.c,v 1.6 2005/08/08 08:05:35 espie Exp $ */ /*- * Copyright (c) 1992, 1993 - * The Regents of the University of California. All rights reserved. + * The Regents of the University of California. All rights reserved. * * This software was developed by the Computer Systems Engineering group * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and @@ -37,30 +37,32 @@ /* * Convert (signed) quad to double. */ -double -__floatdidf(quad_t x) +double __floatdidf(quad_t x) { - double d; - union uu u; - int neg; + double d; + union uu u; + int neg; - /* - * Get an unsigned number first, by negating if necessary. - */ - if (x < 0) - u.q = -x, neg = 1; - else - u.q = x, neg = 0; + /* + * Get an unsigned number first, by negating if necessary. + */ + if (x < 0) { + u.q = -x; + neg = 1; + } else { + u.q = x; + neg = 0; + } - /* - * Now u.ul[H] has the factor of 2^32 (or whatever) and u.ul[L] - * has the units. Ideally we could just set d, add INT_BITS to - * its exponent, and then add the units, but this is portable - * code and does not know how to get at an exponent. Machine- - * specific code may be able to do this more efficiently. - */ - d = (double)u.ul[H] * (((int)1 << (INT_BITS - 2)) * 4.0); - d += u.ul[L]; + /* + * Now u.ul[H] has the factor of 2^32 (or whatever) and u.ul[L] + * has the units. Ideally we could just set d, add INT_BITS to + * its exponent, and then add the units, but this is portable + * code and does not know how to get at an exponent. Machine- + * specific code may be able to do this more efficiently. + */ + d = (double) u.ul[H] * (((int) 1 << (INT_BITS - 2)) * 4.0); + d += u.ul[L]; - return (neg ? -d : d); + return neg ? -d : d; } diff --git a/sys/quad_math/floatdisf.c b/sys/quad_math/floatdisf.c index 6134787f13..2d9c3f8ea4 100644 --- a/sys/quad_math/floatdisf.c +++ b/sys/quad_math/floatdisf.c @@ -1,7 +1,7 @@ -/* $OpenBSD: floatdisf.c,v 1.6 2005/08/08 08:05:35 espie Exp $ */ +/* $OpenBSD: floatdisf.c,v 1.6 2005/08/08 08:05:35 espie Exp $ */ /*- * Copyright (c) 1992, 1993 - * The Regents of the University of California. All rights reserved. + * The Regents of the University of California. All rights reserved. * * This software was developed by the Computer Systems Engineering group * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and @@ -37,32 +37,34 @@ /* * Convert (signed) quad to float. */ -float -__floatdisf(quad_t x) +float __floatdisf(quad_t x) { - float f; - union uu u; - int neg; + float f; + union uu u; + int neg; - /* - * Get an unsigned number first, by negating if necessary. - */ - if (x < 0) - u.q = -x, neg = 1; - else - u.q = x, neg = 0; + /* + * Get an unsigned number first, by negating if necessary. + */ + if (x < 0) { + u.q = -x; + neg = 1; + } else { + u.q = x; + neg = 0; + } - /* - * Now u.ul[H] has the factor of 2^32 (or whatever) and u.ul[L] - * has the units. Ideally we could just set f, add INT_BITS to - * its exponent, and then add the units, but this is portable - * code and does not know how to get at an exponent. Machine- - * specific code may be able to do this more efficiently. - * - * Using double here may be excessive paranoia. - */ - f = (double)u.ul[H] * (((int)1 << (INT_BITS - 2)) * 4.0); - f += u.ul[L]; + /* + * Now u.ul[H] has the factor of 2^32 (or whatever) and u.ul[L] + * has the units. Ideally we could just set f, add INT_BITS to + * its exponent, and then add the units, but this is portable + * code and does not know how to get at an exponent. Machine- + * specific code may be able to do this more efficiently. + * + * Using double here may be excessive paranoia. + */ + f = (double) u.ul[H] * (((int) 1 << (INT_BITS - 2)) * 4.0); + f += u.ul[L]; - return (neg ? -f : f); + return neg ? -f : f; } diff --git a/sys/quad_math/floatunsdidf.c b/sys/quad_math/floatunsdidf.c index 361b61a203..d3cbdc6412 100644 --- a/sys/quad_math/floatunsdidf.c +++ b/sys/quad_math/floatunsdidf.c @@ -1,7 +1,7 @@ -/* $OpenBSD: floatunsdidf.c,v 1.6 2005/08/08 08:05:35 espie Exp $ */ +/* $OpenBSD: floatunsdidf.c,v 1.6 2005/08/08 08:05:35 espie Exp $ */ /*- * Copyright (c) 1992, 1993 - * The Regents of the University of California. All rights reserved. + * The Regents of the University of California. All rights reserved. * * This software was developed by the Computer Systems Engineering group * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and @@ -38,14 +38,13 @@ * Convert (unsigned) quad to double. * This is exactly like floatdidf.c except that negatives never occur. */ -double -__floatunsdidf(u_quad_t x) +double __floatunsdidf(u_quad_t x) { - double d; - union uu u; + double d; + union uu u; - u.uq = x; - d = (double)u.ul[H] * (((int)1 << (INT_BITS - 2)) * 4.0); - d += u.ul[L]; - return (d); + u.uq = x; + d = (double) u.ul[H] * (((int) 1 << (INT_BITS - 2)) * 4.0); + d += u.ul[L]; + return d; } diff --git a/sys/quad_math/iordi3.c b/sys/quad_math/iordi3.c index 62aef5ecbb..70eb5e0a55 100644 --- a/sys/quad_math/iordi3.c +++ b/sys/quad_math/iordi3.c @@ -1,7 +1,7 @@ -/* $OpenBSD: iordi3.c,v 1.5 2005/08/08 08:05:35 espie Exp $ */ +/* $OpenBSD: iordi3.c,v 1.5 2005/08/08 08:05:35 espie Exp $ */ /*- * Copyright (c) 1992, 1993 - * The Regents of the University of California. All rights reserved. + * The Regents of the University of California. All rights reserved. * * This software was developed by the Computer Systems Engineering group * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and @@ -37,14 +37,13 @@ /* * Return a | b, in quad. */ -quad_t -__iordi3(quad_t a, quad_t b) +quad_t __iordi3(quad_t a, quad_t b) { - union uu aa, bb; + union uu aa, bb; - aa.q = a; - bb.q = b; - aa.ul[0] |= bb.ul[0]; - aa.ul[1] |= bb.ul[1]; - return (aa.q); + aa.q = a; + bb.q = b; + aa.ul[0] |= bb.ul[0]; + aa.ul[1] |= bb.ul[1]; + return aa.q; } diff --git a/sys/quad_math/lshldi3.c b/sys/quad_math/lshldi3.c index f750044bb9..6afb6125a3 100644 --- a/sys/quad_math/lshldi3.c +++ b/sys/quad_math/lshldi3.c @@ -1,7 +1,7 @@ -/* $OpenBSD: lshldi3.c,v 1.6 2005/08/08 08:05:35 espie Exp $ */ +/* $OpenBSD: lshldi3.c,v 1.6 2005/08/08 08:05:35 espie Exp $ */ /*- * Copyright (c) 1992, 1993 - * The Regents of the University of California. All rights reserved. + * The Regents of the University of California. All rights reserved. * * This software was developed by the Computer Systems Engineering group * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and @@ -38,21 +38,23 @@ * Shift an (unsigned) quad value left (logical shift left). * This is the same as arithmetic shift left! */ -quad_t -__lshldi3(quad_t a, qshift_t shift) +quad_t __lshldi3(quad_t a, qshift_t shift) { - union uu aa; + union uu aa; - if (shift == 0) - return(a); - aa.q = a; - if (shift >= INT_BITS) { - aa.ul[H] = aa.ul[L] << (shift - INT_BITS); - aa.ul[L] = 0; - } else { - aa.ul[H] = (aa.ul[H] << shift) | - (aa.ul[L] >> (INT_BITS - shift)); - aa.ul[L] <<= shift; - } - return (aa.q); + if (shift == 0) { + return a; + } + + aa.q = a; + + if (shift >= INT_BITS) { + aa.ul[H] = aa.ul[L] << (shift - INT_BITS); + aa.ul[L] = 0; + } else { + aa.ul[H] = (aa.ul[H] << shift) | (aa.ul[L] >> (INT_BITS - shift)); + aa.ul[L] <<= shift; + } + + return aa.q; } diff --git a/sys/quad_math/lshrdi3.c b/sys/quad_math/lshrdi3.c index 1dded26d40..e2a5c3f0ad 100644 --- a/sys/quad_math/lshrdi3.c +++ b/sys/quad_math/lshrdi3.c @@ -1,7 +1,7 @@ -/* $OpenBSD: lshrdi3.c,v 1.6 2005/08/08 08:05:35 espie Exp $ */ +/* $OpenBSD: lshrdi3.c,v 1.6 2005/08/08 08:05:35 espie Exp $ */ /*- * Copyright (c) 1992, 1993 - * The Regents of the University of California. All rights reserved. + * The Regents of the University of California. All rights reserved. * * This software was developed by the Computer Systems Engineering group * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and @@ -37,21 +37,23 @@ /* * Shift an (unsigned) quad value right (logical shift right). */ -quad_t -__lshrdi3(quad_t a, qshift_t shift) +quad_t __lshrdi3(quad_t a, qshift_t shift) { - union uu aa; + union uu aa; - if (shift == 0) - return(a); - aa.q = a; - if (shift >= INT_BITS) { - aa.ul[L] = aa.ul[H] >> (shift - INT_BITS); - aa.ul[H] = 0; - } else { - aa.ul[L] = (aa.ul[L] >> shift) | - (aa.ul[H] << (INT_BITS - shift)); - aa.ul[H] >>= shift; - } - return (aa.q); + if (shift == 0) { + return a; + } + + aa.q = a; + + if (shift >= INT_BITS) { + aa.ul[L] = aa.ul[H] >> (shift - INT_BITS); + aa.ul[H] = 0; + } else { + aa.ul[L] = (aa.ul[L] >> shift) | (aa.ul[H] << (INT_BITS - shift)); + aa.ul[H] >>= shift; + } + + return aa.q; } diff --git a/sys/quad_math/moddi3.c b/sys/quad_math/moddi3.c index 36c49485ae..8b0708ac3d 100644 --- a/sys/quad_math/moddi3.c +++ b/sys/quad_math/moddi3.c @@ -1,7 +1,7 @@ -/* $OpenBSD: moddi3.c,v 1.6 2005/08/08 08:05:35 espie Exp $ */ +/* $OpenBSD: moddi3.c,v 1.6 2005/08/08 08:05:35 espie Exp $ */ /*- * Copyright (c) 1992, 1993 - * The Regents of the University of California. All rights reserved. + * The Regents of the University of California. All rights reserved. * * This software was developed by the Computer Systems Engineering group * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and @@ -37,23 +37,29 @@ /* * Return remainder after dividing two signed quads. * - * XXX we assume a % b < 0 iff a < 0, but this is actually machine-dependent. + * XXX we assume a % b < 0 iff a < 0, but this is actually machine-dependent. */ -quad_t -__moddi3(quad_t a, quad_t b) +quad_t __moddi3(quad_t a, quad_t b) { - u_quad_t ua, ub, ur; - int neg = 0; + u_quad_t ua, ub, ur; + int neg = 0; - ua = a; - ub = b; + ua = a; + ub = b; - if (a < 0) - ua = -ua, neg ^= 1; - if (b < 0) - ub = -ub; - (void)__qdivrem(ua, ub, &ur); - if (neg) - ur = -ur; - return (ur); + if (a < 0) { + ua = -ua; + neg = 1; + } + if (b < 0) { + ub = -ub; + } + + (void) __qdivrem(ua, ub, &ur); + + if (neg) { + ur = -ur; + } + + return ur; } diff --git a/sys/quad_math/muldi3.c b/sys/quad_math/muldi3.c index 3cce20fff8..9d616113d2 100644 --- a/sys/quad_math/muldi3.c +++ b/sys/quad_math/muldi3.c @@ -1,7 +1,7 @@ -/* $OpenBSD: muldi3.c,v 1.5 2005/08/08 08:05:35 espie Exp $ */ +/* $OpenBSD: muldi3.c,v 1.5 2005/08/08 08:05:35 espie Exp $ */ /*- * Copyright (c) 1992, 1993 - * The Regents of the University of California. All rights reserved. + * The Regents of the University of California. All rights reserved. * * This software was developed by the Computer Systems Engineering group * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and @@ -40,30 +40,30 @@ * Our algorithm is based on the following. Split incoming quad values * u and v (where u,v >= 0) into * - * u = 2^n u1 * u0 (n = number of bits in `u_int', usu. 32) + * u = 2^n u1 * u0 (n = number of bits in `u_int', usu. 32) * - * and + * and * - * v = 2^n v1 * v0 + * v = 2^n v1 * v0 * * Then * - * uv = 2^2n u1 v1 + 2^n u1 v0 + 2^n v1 u0 + u0 v0 - * = 2^2n u1 v1 + 2^n (u1 v0 + v1 u0) + u0 v0 + * uv = 2^2n u1 v1 + 2^n u1 v0 + 2^n v1 u0 + u0 v0 + * = 2^2n u1 v1 + 2^n (u1 v0 + v1 u0) + u0 v0 * * Now add 2^n u1 v1 to the first term and subtract it from the middle, * and add 2^n u0 v0 to the last term and subtract it from the middle. * This gives: * - * uv = (2^2n + 2^n) (u1 v1) + - * (2^n) (u1 v0 - u1 v1 + u0 v1 - u0 v0) + - * (2^n + 1) (u0 v0) + * uv = (2^2n + 2^n) (u1 v1) + + * (2^n) (u1 v0 - u1 v1 + u0 v1 - u0 v0) + + * (2^n + 1) (u0 v0) * * Factoring the middle a bit gives us: * - * uv = (2^2n + 2^n) (u1 v1) + [u1v1 = high] - * (2^n) (u1 - u0) (v0 - v1) + [(u1-u0)... = mid] - * (2^n + 1) (u0 v0) [u0v0 = low] + * uv = (2^2n + 2^n) (u1 v1) + [u1v1 = high] + * (2^n) (u1 - u0) (v0 - v1) + [(u1-u0)... = mid] + * (2^n + 1) (u0 v0) [u0v0 = low] * * The terms (u1 v1), (u1 - u0) (v0 - v1), and (u0 v0) can all be done * in just half the precision of the original. (Note that either or both @@ -81,11 +81,11 @@ * we are not interested in uv, but rather in (uv mod 2^2n). This * makes some of the terms above vanish, and we get: * - * (2^n)(high) + (2^n)(mid) + (2^n + 1)(low) + * (2^n)(high) + (2^n)(mid) + (2^n + 1)(low) * * or * - * (2^n)(high + mid + low) + low + * (2^n)(high + mid + low) + low * * Furthermore, `high' and `mid' can be computed mod 2^n, as any factor * of 2^n in either one will also vanish. Only `low' need be computed @@ -93,68 +93,73 @@ */ static quad_t __lmulq(u_int, u_int); -quad_t -__muldi3(a, b) - quad_t a, b; +quad_t __muldi3(quad_t a, quad_t b) { - union uu u, v, low, prod; - u_int high, mid, udiff, vdiff; - int negall, negmid; -#define u1 u.ul[H] -#define u0 u.ul[L] -#define v1 v.ul[H] -#define v0 v.ul[L] - - /* - * Get u and v such that u, v >= 0. When this is finished, - * u1, u0, v1, and v0 will be directly accessible through the - * int fields. - */ - if (a >= 0) - u.q = a, negall = 0; - else - u.q = -a, negall = 1; - if (b >= 0) - v.q = b; - else - v.q = -b, negall ^= 1; - - if (u1 == 0 && v1 == 0) { - /* - * An (I hope) important optimization occurs when u1 and v1 - * are both 0. This should be common since most numbers - * are small. Here the product is just u0*v0. - */ - prod.q = __lmulq(u0, v0); - } else { - /* - * Compute the three intermediate products, remembering - * whether the middle term is negative. We can discard - * any upper bits in high and mid, so we can use native - * u_int * u_int => u_int arithmetic. - */ - low.q = __lmulq(u0, v0); - - if (u1 >= u0) - negmid = 0, udiff = u1 - u0; - else - negmid = 1, udiff = u0 - u1; - if (v0 >= v1) - vdiff = v0 - v1; - else - vdiff = v1 - v0, negmid ^= 1; - mid = udiff * vdiff; - - high = u1 * v1; - - /* - * Assemble the final product. - */ - prod.ul[H] = high + (negmid ? -mid : mid) + low.ul[L] + - low.ul[H]; - prod.ul[L] = low.ul[L]; - } - return (negall ? -prod.q : prod.q); + union uu u, v, low, prod; + u_int high, mid, udiff, vdiff; + int negall, negmid; +#define u1 u.ul[H] +#define u0 u.ul[L] +#define v1 v.ul[H] +#define v0 v.ul[L] + + /* + * Get u and v such that u, v >= 0. When this is finished, + * u1, u0, v1, and v0 will be directly accessible through the + * int fields. + */ + if (a >= 0) { + u.q = a, negall = 0; + } else { + u.q = -a, negall = 1; + } + + if (b >= 0) { + v.q = b; + } else { + v.q = -b, negall ^= 1; + } + + if (u1 == 0 && v1 == 0) { + /* + * An (I hope) important optimization occurs when u1 and v1 + * are both 0. This should be common since most numbers + * are small. Here the product is just u0*v0. + */ + prod.q = __lmulq(u0, v0); + } else { + /* + * Compute the three intermediate products, remembering + * whether the middle term is negative. We can discard + * any upper bits in high and mid, so we can use native + * u_int * u_int => u_int arithmetic. + */ + low.q = __lmulq(u0, v0); + + if (u1 >= u0) { + negmid = 0, udiff = u1 - u0; + } else { + negmid = 1, udiff = u0 - u1; + } + + if (v0 >= v1) { + vdiff = v0 - v1; + } else { + vdiff = v1 - v0, negmid ^= 1; + } + + mid = udiff * vdiff; + + high = u1 * v1; + + /* + * Assemble the final product. + */ + prod.ul[H] = high + (negmid ? -mid : mid) + low.ul[L] + low.ul[H]; + prod.ul[L] = low.ul[L]; + } + + return negall ? -prod.q : prod.q; #undef u1 #undef u0 #undef v1 @@ -174,66 +179,72 @@ __muldi3(a, b) * * Note that, for u_int l, the quad-precision result * - * l << N + * l << N * * splits into high and low ints as HHALF(l) and LHUP(l) respectively. */ -static quad_t -__lmulq(u_int u, u_int v) +static quad_t __lmulq(u_int u, u_int v) { - u_int u1, u0, v1, v0, udiff, vdiff, high, mid, low; - u_int prodh, prodl, was; - union uu prod; - int neg; - - u1 = HHALF(u); - u0 = LHALF(u); - v1 = HHALF(v); - v0 = LHALF(v); - - low = u0 * v0; - - /* This is the same small-number optimization as before. */ - if (u1 == 0 && v1 == 0) - return (low); - - if (u1 >= u0) - udiff = u1 - u0, neg = 0; - else - udiff = u0 - u1, neg = 1; - if (v0 >= v1) - vdiff = v0 - v1; - else - vdiff = v1 - v0, neg ^= 1; - mid = udiff * vdiff; - - high = u1 * v1; - - /* prod = (high << 2N) + (high << N); */ - prodh = high + HHALF(high); - prodl = LHUP(high); - - /* if (neg) prod -= mid << N; else prod += mid << N; */ - if (neg) { - was = prodl; - prodl -= LHUP(mid); - prodh -= HHALF(mid) + (prodl > was); - } else { - was = prodl; - prodl += LHUP(mid); - prodh += HHALF(mid) + (prodl < was); - } - - /* prod += low << N */ - was = prodl; - prodl += LHUP(low); - prodh += HHALF(low) + (prodl < was); - /* ... + low; */ - if ((prodl += low) < low) - prodh++; - - /* return 4N-bit product */ - prod.ul[H] = prodh; - prod.ul[L] = prodl; - return (prod.q); + u_int u1, u0, v1, v0, udiff, vdiff, high, mid, low; + u_int prodh, prodl, was; + union uu prod; + int neg; + + u1 = HHALF(u); + u0 = LHALF(u); + v1 = HHALF(v); + v0 = LHALF(v); + + low = u0 * v0; + + /* This is the same small-number optimization as before. */ + if (u1 == 0 && v1 == 0) { + return low; + } + + if (u1 >= u0) { + udiff = u1 - u0, neg = 0; + } else { + udiff = u0 - u1, neg = 1; + } + + if (v0 >= v1) { + vdiff = v0 - v1; + } else { + vdiff = v1 - v0, neg ^= 1; + } + + mid = udiff * vdiff; + + high = u1 * v1; + + /* prod = (high << 2N) + (high << N); */ + prodh = high + HHALF(high); + prodl = LHUP(high); + + /* if (neg) prod -= mid << N; else prod += mid << N; */ + if (neg) { + was = prodl; + prodl -= LHUP(mid); + prodh -= HHALF(mid) + (prodl > was); + } else { + was = prodl; + prodl += LHUP(mid); + prodh += HHALF(mid) + (prodl < was); + } + + /* prod += low << N */ + was = prodl; + prodl += LHUP(low); + prodh += HHALF(low) + (prodl < was); + + /* ... + low; */ + if ((prodl += low) < low) { + prodh++; + } + + /* return 4N-bit product */ + prod.ul[H] = prodh; + prod.ul[L] = prodl; + return prod.q; } diff --git a/sys/quad_math/negdi2.c b/sys/quad_math/negdi2.c index 669ce0832f..fbd8e68203 100644 --- a/sys/quad_math/negdi2.c +++ b/sys/quad_math/negdi2.c @@ -1,7 +1,7 @@ -/* $OpenBSD: negdi2.c,v 1.5 2005/08/08 08:05:35 espie Exp $ */ +/* $OpenBSD: negdi2.c,v 1.5 2005/08/08 08:05:35 espie Exp $ */ /*- * Copyright (c) 1992, 1993 - * The Regents of the University of California. All rights reserved. + * The Regents of the University of California. All rights reserved. * * This software was developed by the Computer Systems Engineering group * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and @@ -37,13 +37,12 @@ /* * Return -a (or, equivalently, 0 - a), in quad. See subdi3.c. */ -quad_t -__negdi2(quad_t a) +quad_t __negdi2(quad_t a) { - union uu aa, res; + union uu aa, res; - aa.q = a; - res.ul[L] = -aa.ul[L]; - res.ul[H] = -aa.ul[H] - (res.ul[L] > 0); - return (res.q); + aa.q = a; + res.ul[L] = -aa.ul[L]; + res.ul[H] = -aa.ul[H] - (res.ul[L] > 0); + return res.q; } diff --git a/sys/quad_math/notdi2.c b/sys/quad_math/notdi2.c index 47f23ab452..ccac89cc87 100644 --- a/sys/quad_math/notdi2.c +++ b/sys/quad_math/notdi2.c @@ -1,7 +1,7 @@ -/* $OpenBSD: notdi2.c,v 1.5 2005/08/08 08:05:35 espie Exp $ */ +/* $OpenBSD: notdi2.c,v 1.5 2005/08/08 08:05:35 espie Exp $ */ /*- * Copyright (c) 1992, 1993 - * The Regents of the University of California. All rights reserved. + * The Regents of the University of California. All rights reserved. * * This software was developed by the Computer Systems Engineering group * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and @@ -38,13 +38,12 @@ * Return ~a. For some reason gcc calls this `one's complement' rather * than `not'. */ -quad_t -__one_cmpldi2(quad_t a) +quad_t __one_cmpldi2(quad_t a) { - union uu aa; + union uu aa; - aa.q = a; - aa.ul[0] = ~aa.ul[0]; - aa.ul[1] = ~aa.ul[1]; - return (aa.q); + aa.q = a; + aa.ul[0] = ~aa.ul[0]; + aa.ul[1] = ~aa.ul[1]; + return aa.q; } diff --git a/sys/quad_math/qdivrem.c b/sys/quad_math/qdivrem.c index 81d87ec4b3..21de613d65 100644 --- a/sys/quad_math/qdivrem.c +++ b/sys/quad_math/qdivrem.c @@ -1,7 +1,7 @@ -/* $OpenBSD: qdivrem.c,v 1.7 2005/08/08 08:05:35 espie Exp $ */ +/* $OpenBSD: qdivrem.c,v 1.7 2005/08/08 08:05:35 espie Exp $ */ /*- * Copyright (c) 1992, 1993 - * The Regents of the University of California. All rights reserved. + * The Regents of the University of California. All rights reserved. * * This software was developed by the Computer Systems Engineering group * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and @@ -39,10 +39,10 @@ #include "quad.h" -#define B ((int)1 << HALF_BITS) /* digit base */ +#define B ((int)1 << HALF_BITS) /* digit base */ /* Combine two `digits' to make a single two-digit number. */ -#define COMBINE(a, b) (((u_int)(a) << HALF_BITS) | (b)) +#define COMBINE(a, b) (((u_int)(a) << HALF_BITS) | (b)) /* select a type for digits in base B: use unsigned short if they fit */ #if UINT_MAX == 0xffffffffU && USHRT_MAX >= 0xffff @@ -64,197 +64,229 @@ static void shl(digit *p, int len, int sh); u_quad_t __qdivrem(u_quad_t uq, u_quad_t vq, u_quad_t *arq) { - union uu tmp; - digit *u, *v, *q; - digit v1, v2; - u_int qhat, rhat, t; - int m, n, d, j, i; - digit uspace[5], vspace[5], qspace[5]; - - /* - * Take care of special cases: divide by zero, and u < v. - */ - if (vq == 0) { - /* divide by zero. */ - static volatile const unsigned int zero = 0; - - tmp.ul[H] = tmp.ul[L] = 1 / zero; - if (arq) - *arq = uq; - return (tmp.q); - } - if (uq < vq) { - if (arq) - *arq = uq; - return (0); - } - u = &uspace[0]; - v = &vspace[0]; - q = &qspace[0]; - - /* - * Break dividend and divisor into digits in base B, then - * count leading zeros to determine m and n. When done, we - * will have: - * u = (u[1]u[2]...u[m+n]) sub B - * v = (v[1]v[2]...v[n]) sub B - * v[1] != 0 - * 1 < n <= 4 (if n = 1, we use a different division algorithm) - * m >= 0 (otherwise u < v, which we already checked) - * m + n = 4 - * and thus - * m = 4 - n <= 2 - */ - tmp.uq = uq; - u[0] = 0; - u[1] = (digit)HHALF(tmp.ul[H]); - u[2] = (digit)LHALF(tmp.ul[H]); - u[3] = (digit)HHALF(tmp.ul[L]); - u[4] = (digit)LHALF(tmp.ul[L]); - tmp.uq = vq; - v[1] = (digit)HHALF(tmp.ul[H]); - v[2] = (digit)LHALF(tmp.ul[H]); - v[3] = (digit)HHALF(tmp.ul[L]); - v[4] = (digit)LHALF(tmp.ul[L]); - for (n = 4; v[1] == 0; v++) { - if (--n == 1) { - u_int rbj; /* r*B+u[j] (not root boy jim) */ - digit q1, q2, q3, q4; - - /* - * Change of plan, per exercise 16. - * r = 0; - * for j = 1..4: - * q[j] = floor((r*B + u[j]) / v), - * r = (r*B + u[j]) % v; - * We unroll this completely here. - */ - t = v[2]; /* nonzero, by definition */ - q1 = (digit)(u[1] / t); - rbj = COMBINE(u[1] % t, u[2]); - q2 = (digit)(rbj / t); - rbj = COMBINE(rbj % t, u[3]); - q3 = (digit)(rbj / t); - rbj = COMBINE(rbj % t, u[4]); - q4 = (digit)(rbj / t); - if (arq) - *arq = rbj % t; - tmp.ul[H] = COMBINE(q1, q2); - tmp.ul[L] = COMBINE(q3, q4); - return (tmp.q); - } - } - - /* - * By adjusting q once we determine m, we can guarantee that - * there is a complete four-digit quotient at &qspace[1] when - * we finally stop. - */ - for (m = 4 - n; u[1] == 0; u++) - m--; - for (i = 4 - m; --i >= 0;) - q[i] = 0; - q += 4 - m; - - /* - * Here we run Program D, translated from MIX to C and acquiring - * a few minor changes. - * - * D1: choose multiplier 1 << d to ensure v[1] >= B/2. - */ - d = 0; - for (t = v[1]; t < B / 2; t <<= 1) - d++; - if (d > 0) { - shl(&u[0], m + n, d); /* u <<= d */ - shl(&v[1], n - 1, d); /* v <<= d */ - } - /* - * D2: j = 0. - */ - j = 0; - v1 = v[1]; /* for D3 -- note that v[1..n] are constant */ - v2 = v[2]; /* for D3 */ - do { - digit uj0, uj1, uj2; - - /* - * D3: Calculate qhat (\^q, in TeX notation). - * Let qhat = min((u[j]*B + u[j+1])/v[1], B-1), and - * let rhat = (u[j]*B + u[j+1]) mod v[1]. - * While rhat < B and v[2]*qhat > rhat*B+u[j+2], - * decrement qhat and increase rhat correspondingly. - * Note that if rhat >= B, v[2]*qhat < rhat*B. - */ - uj0 = u[j + 0]; /* for D3 only -- note that u[j+...] change */ - uj1 = u[j + 1]; /* for D3 only */ - uj2 = u[j + 2]; /* for D3 only */ - if (uj0 == v1) { - qhat = B; - rhat = uj1; - goto qhat_too_big; - } else { - u_int nn = COMBINE(uj0, uj1); - qhat = nn / v1; - rhat = nn % v1; - } - while (v2 * qhat > COMBINE(rhat, uj2)) { - qhat_too_big: - qhat--; - if ((rhat += v1) >= B) - break; - } - /* - * D4: Multiply and subtract. - * The variable `t' holds any borrows across the loop. - * We split this up so that we do not require v[0] = 0, - * and to eliminate a final special case. - */ - for (t = 0, i = n; i > 0; i--) { - t = u[i + j] - v[i] * qhat - t; - u[i + j] = (digit)LHALF(t); - t = (B - HHALF(t)) & (B - 1); - } - t = u[j] - t; - u[j] = (digit)LHALF(t); - /* - * D5: test remainder. - * There is a borrow if and only if HHALF(t) is nonzero; - * in that (rare) case, qhat was too large (by exactly 1). - * Fix it by adding v[1..n] to u[j..j+n]. - */ - if (HHALF(t)) { - qhat--; - for (t = 0, i = n; i > 0; i--) { /* D6: add back. */ - t += u[i + j] + v[i]; - u[i + j] = (digit)LHALF(t); - t = HHALF(t); - } - u[j] = (digit)LHALF(u[j] + t); - } - q[j] = (digit)qhat; - } while (++j <= m); /* D7: loop on j. */ - - /* - * If caller wants the remainder, we have to calculate it as - * u[m..m+n] >> d (this is at most n digits and thus fits in - * u[m+1..m+n], but we may need more source digits). - */ - if (arq) { - if (d) { - for (i = m + n; i > m; --i) - u[i] = (digit)(((u_int)u[i] >> d) | - LHALF((u_int)u[i - 1] << (HALF_BITS - d))); - u[i] = 0; - } - tmp.ul[H] = COMBINE(uspace[1], uspace[2]); - tmp.ul[L] = COMBINE(uspace[3], uspace[4]); - *arq = tmp.q; - } - - tmp.ul[H] = COMBINE(qspace[1], qspace[2]); - tmp.ul[L] = COMBINE(qspace[3], qspace[4]); - return (tmp.q); + union uu tmp; + digit *u, *v, *q; + digit v1, v2; + u_int qhat, rhat, t; + int m, n, d, j, i; + digit uspace[5], vspace[5], qspace[5]; + + /* + * Take care of special cases: divide by zero, and u < v. + */ + if (vq == 0) { + /* divide by zero. */ + static volatile const unsigned int zero = 0; + + tmp.ul[H] = tmp.ul[L] = 1 / zero; + + if (arq) { + *arq = uq; + } + + return tmp.q; + } + + if (uq < vq) { + if (arq) { + *arq = uq; + } + + return 0; + } + + u = &uspace[0]; + v = &vspace[0]; + q = &qspace[0]; + + /* + * Break dividend and divisor into digits in base B, then + * count leading zeros to determine m and n. When done, we + * will have: + * u = (u[1]u[2]...u[m+n]) sub B + * v = (v[1]v[2]...v[n]) sub B + * v[1] != 0 + * 1 < n <= 4 (if n = 1, we use a different division algorithm) + * m >= 0 (otherwise u < v, which we already checked) + * m + n = 4 + * and thus + * m = 4 - n <= 2 + */ + tmp.uq = uq; + u[0] = 0; + u[1] = (digit) HHALF(tmp.ul[H]); + u[2] = (digit) LHALF(tmp.ul[H]); + u[3] = (digit) HHALF(tmp.ul[L]); + u[4] = (digit) LHALF(tmp.ul[L]); + tmp.uq = vq; + v[1] = (digit) HHALF(tmp.ul[H]); + v[2] = (digit) LHALF(tmp.ul[H]); + v[3] = (digit) HHALF(tmp.ul[L]); + v[4] = (digit) LHALF(tmp.ul[L]); + + for (n = 4; v[1] == 0; v++) { + if (--n == 1) { + u_int rbj; /* r*B+u[j] (not root boy jim) */ + digit q1, q2, q3, q4; + + /* + * Change of plan, per exercise 16. + * r = 0; + * for j = 1..4: + * q[j] = floor((r*B + u[j]) / v), + * r = (r*B + u[j]) % v; + * We unroll this completely here. + */ + t = v[2]; /* nonzero, by definition */ + q1 = (digit) (u[1] / t); + rbj = COMBINE(u[1] % t, u[2]); + q2 = (digit) (rbj / t); + rbj = COMBINE(rbj % t, u[3]); + q3 = (digit) (rbj / t); + rbj = COMBINE(rbj % t, u[4]); + q4 = (digit) (rbj / t); + + if (arq) { + *arq = rbj % t; + } + + tmp.ul[H] = COMBINE(q1, q2); + tmp.ul[L] = COMBINE(q3, q4); + return tmp.q; + } + } + + /* + * By adjusting q once we determine m, we can guarantee that + * there is a complete four-digit quotient at &qspace[1] when + * we finally stop. + */ + for (m = 4 - n; u[1] == 0; u++) { + m--; + } + + for (i = 4 - m; --i >= 0;) { + q[i] = 0; + } + + q += 4 - m; + + /* + * Here we run Program D, translated from MIX to C and acquiring + * a few minor changes. + * + * D1: choose multiplier 1 << d to ensure v[1] >= B/2. + */ + d = 0; + + for (t = v[1]; t < B / 2; t <<= 1) { + d++; + } + + if (d > 0) { + shl(&u[0], m + n, d); /* u <<= d */ + shl(&v[1], n - 1, d); /* v <<= d */ + } + + /* + * D2: j = 0. + */ + j = 0; + v1 = v[1]; /* for D3 -- note that v[1..n] are constant */ + v2 = v[2]; /* for D3 */ + + do { + digit uj0, uj1, uj2; + + /* + * D3: Calculate qhat (\^q, in TeX notation). + * Let qhat = min((u[j]*B + u[j+1])/v[1], B-1), and + * let rhat = (u[j]*B + u[j+1]) mod v[1]. + * While rhat < B and v[2]*qhat > rhat*B+u[j+2], + * decrement qhat and increase rhat correspondingly. + * Note that if rhat >= B, v[2]*qhat < rhat*B. + */ + uj0 = u[j + 0]; /* for D3 only -- note that u[j+...] change */ + uj1 = u[j + 1]; /* for D3 only */ + uj2 = u[j + 2]; /* for D3 only */ + + if (uj0 == v1) { + qhat = B; + rhat = uj1; + goto qhat_too_big; + } else { + u_int nn = COMBINE(uj0, uj1); + qhat = nn / v1; + rhat = nn % v1; + } + + while (v2 * qhat > COMBINE(rhat, uj2)) { +qhat_too_big: + qhat--; + + if ((rhat += v1) >= B) { + break; + } + } + + /* + * D4: Multiply and subtract. + * The variable `t' holds any borrows across the loop. + * We split this up so that we do not require v[0] = 0, + * and to eliminate a final special case. + */ + for (t = 0, i = n; i > 0; i--) { + t = u[i + j] - v[i] * qhat - t; + u[i + j] = (digit) LHALF(t); + t = (B - HHALF(t)) & (B - 1); + } + + t = u[j] - t; + u[j] = (digit) LHALF(t); + + /* + * D5: test remainder. + * There is a borrow if and only if HHALF(t) is nonzero; + * in that (rare) case, qhat was too large (by exactly 1). + * Fix it by adding v[1..n] to u[j..j+n]. + */ + if (HHALF(t)) { + qhat--; + + for (t = 0, i = n; i > 0; i--) { /* D6: add back. */ + t += u[i + j] + v[i]; + u[i + j] = (digit) LHALF(t); + t = HHALF(t); + } + + u[j] = (digit) LHALF(u[j] + t); + } + + q[j] = (digit)qhat; + } while (++j <= m); /* D7: loop on j. */ + + /* + * If caller wants the remainder, we have to calculate it as + * u[m..m+n] >> d (this is at most n digits and thus fits in + * u[m+1..m+n], but we may need more source digits). + */ + if (arq) { + if (d) { + for (i = m + n; i > m; --i) { + u[i] = (digit)(((u_int)u[i] >> d) | LHALF((u_int)u[i - 1] << (HALF_BITS - d))); + } + + u[i] = 0; + } + + tmp.ul[H] = COMBINE(uspace[1], uspace[2]); + tmp.ul[L] = COMBINE(uspace[3], uspace[4]); + *arq = tmp.q; + } + + tmp.ul[H] = COMBINE(qspace[1], qspace[2]); + tmp.ul[L] = COMBINE(qspace[3], qspace[4]); + return tmp.q; } /* @@ -262,13 +294,13 @@ __qdivrem(u_quad_t uq, u_quad_t vq, u_quad_t *arq) * `fall out' the left (there never will be any such anyway). * We may assume len >= 0. NOTE THAT THIS WRITES len+1 DIGITS. */ -static void -shl(digit *p, int len, int sh) +static void shl(digit *p, int len, int sh) { - int i; + int i; + + for (i = 0; i < len; i++) { + p[i] = (digit) (LHALF((u_int)p[i] << sh) | ((u_int)p[i + 1] >> (HALF_BITS - sh))); + } - for (i = 0; i < len; i++) - p[i] = (digit)(LHALF((u_int)p[i] << sh) | - ((u_int)p[i + 1] >> (HALF_BITS - sh))); - p[i] = (digit)(LHALF((u_int)p[i] << sh)); + p[i] = (digit)(LHALF((u_int)p[i] << sh)); } diff --git a/sys/quad_math/quad.h b/sys/quad_math/quad.h index 03199559c7..2060b0d622 100644 --- a/sys/quad_math/quad.h +++ b/sys/quad_math/quad.h @@ -1,6 +1,6 @@ /*- * Copyright (c) 1992, 1993 - * The Regents of the University of California. All rights reserved. + * The Regents of the University of California. All rights reserved. * * This software was developed by the Computer Systems Engineering group * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and @@ -30,7 +30,7 @@ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * - * $OpenBSD: quad.h,v 1.7 2009/11/07 23:09:35 jsg Exp $ + * $OpenBSD: quad.h,v 1.7 2009/11/07 23:09:35 jsg Exp $ */ /* @@ -59,14 +59,18 @@ typedef char ___QUAD_ASSERT__2COMPLEMENT[-1234 == (~1234 + 1) ? +1 : -1]; typedef long long quad_t; typedef unsigned long long u_quad_t; +/* + * Define high and low parts of a quad_t. + */ + #if BYTE_ORDER == LITTLE_ENDIAN -# define _QUAD_LOWWORD (0) -# define _QUAD_HIGHWORD (1) +# define L (0) +# define H (1) #elif BYTE_ORDER == BIG_ENDIAN -# define _QUAD_LOWWORD (1) -# define _QUAD_HIGHWORD (0) +# define L (1) +# define H (0) #else -# error "BYTE_ORDER must be either LITTLE_ENDIAN or BIG_ENDIAN!" +# error "BYTE_ORDER must be either LITTLE_ENDIAN or BIG_ENDIAN!" #endif #define QUAD_MIN (LLONG_MIN) @@ -78,26 +82,20 @@ typedef unsigned long long u_quad_t; * one or more of the following formats. */ union uu { - quad_t q; /* as a (signed) quad */ - u_quad_t uq; /* as an unsigned quad */ - int sl[2]; /* as two signed ints */ - u_int ul[2]; /* as two unsigned ints */ + quad_t q; /* as a (signed) quad */ + u_quad_t uq; /* as an unsigned quad */ + int sl[2]; /* as two signed ints */ + u_int ul[2]; /* as two unsigned ints */ }; -/* - * Define high and low parts of a quad_t. - */ -#define H _QUAD_HIGHWORD -#define L _QUAD_LOWWORD - /* * Total number of bits in a quad_t and in the pieces that make it up. * These are used for shifting, and also below for halfword extraction * and assembly. */ -#define QUAD_BITS (sizeof(quad_t) * CHAR_BIT) -#define INT_BITS (sizeof(int) * CHAR_BIT) -#define HALF_BITS (sizeof(int) * CHAR_BIT / 2) +#define QUAD_BITS (sizeof(quad_t) * CHAR_BIT) +#define INT_BITS (sizeof(int) * CHAR_BIT) +#define HALF_BITS (sizeof(int) * CHAR_BIT / 2) /* * Extract high and low shortwords from longword, and move low shortword of @@ -108,11 +106,11 @@ union uu { * and lower halves, and to reassemble a product as a quad_t, shifted left * (sizeof(int)*CHAR_BIT/2). */ -#define HHALF(x) ((u_int)(x) >> HALF_BITS) -#define LHALF(x) ((u_int)(x) & (((int)1 << HALF_BITS) - 1)) -#define LHUP(x) ((u_int)(x) << HALF_BITS) +#define HHALF(x) ((u_int) (x) >> HALF_BITS) +#define LHALF(x) ((u_int) (x) & (((int) 1 << HALF_BITS) - 1)) +#define LHUP(x) ((u_int) (x) << HALF_BITS) -typedef unsigned int qshift_t; +typedef unsigned int qshift_t; quad_t __adddi3(quad_t, quad_t); quad_t __anddi3(quad_t, quad_t); diff --git a/sys/quad_math/subdi3.c b/sys/quad_math/subdi3.c index 909ef4c708..2d4e925601 100644 --- a/sys/quad_math/subdi3.c +++ b/sys/quad_math/subdi3.c @@ -1,7 +1,7 @@ -/* $OpenBSD: subdi3.c,v 1.6 2005/08/08 08:05:35 espie Exp $ */ +/* $OpenBSD: subdi3.c,v 1.6 2005/08/08 08:05:35 espie Exp $ */ /*- * Copyright (c) 1992, 1993 - * The Regents of the University of California. All rights reserved. + * The Regents of the University of California. All rights reserved. * * This software was developed by the Computer Systems Engineering group * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and @@ -38,14 +38,13 @@ * Subtract two quad values. This is trivial since a one-bit carry * from a single u_int difference x-y occurs if and only if (x-y) > x. */ -quad_t -__subdi3(quad_t a, quad_t b) +quad_t __subdi3(quad_t a, quad_t b) { - union uu aa, bb, diff; + union uu aa, bb, diff; - aa.q = a; - bb.q = b; - diff.ul[L] = aa.ul[L] - bb.ul[L]; - diff.ul[H] = aa.ul[H] - bb.ul[H] - (diff.ul[L] > aa.ul[L]); - return (diff.q); + aa.q = a; + bb.q = b; + diff.ul[L] = aa.ul[L] - bb.ul[L]; + diff.ul[H] = aa.ul[H] - bb.ul[H] - (diff.ul[L] > aa.ul[L]); + return diff.q; } diff --git a/sys/quad_math/ucmpdi2.c b/sys/quad_math/ucmpdi2.c index 2b97460e95..4e21b85a67 100644 --- a/sys/quad_math/ucmpdi2.c +++ b/sys/quad_math/ucmpdi2.c @@ -1,7 +1,7 @@ -/* $OpenBSD: ucmpdi2.c,v 1.5 2005/08/08 08:05:35 espie Exp $ */ +/* $OpenBSD: ucmpdi2.c,v 1.5 2005/08/08 08:05:35 espie Exp $ */ /*- * Copyright (c) 1992, 1993 - * The Regents of the University of California. All rights reserved. + * The Regents of the University of California. All rights reserved. * * This software was developed by the Computer Systems Engineering group * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and @@ -38,13 +38,15 @@ * Return 0, 1, or 2 as a <, =, > b respectively. * Neither a nor b are considered signed. */ -int -__ucmpdi2(u_quad_t a, u_quad_t b) +int __ucmpdi2(u_quad_t a, u_quad_t b) { - union uu aa, bb; + union uu aa, bb; - aa.uq = a; - bb.uq = b; - return (aa.ul[H] < bb.ul[H] ? 0 : aa.ul[H] > bb.ul[H] ? 2 : - aa.ul[L] < bb.ul[L] ? 0 : aa.ul[L] > bb.ul[L] ? 2 : 1); + aa.uq = a; + bb.uq = b; + return aa.ul[H] < bb.ul[H] ? 0 + : aa.ul[H] > bb.ul[H] ? 2 + : aa.ul[L] < bb.ul[L] ? 0 + : aa.ul[L] > bb.ul[L] ? 2 + : 1; } diff --git a/sys/quad_math/udivdi3.c b/sys/quad_math/udivdi3.c index b702aab6fb..530c486417 100644 --- a/sys/quad_math/udivdi3.c +++ b/sys/quad_math/udivdi3.c @@ -1,7 +1,7 @@ -/* $OpenBSD: udivdi3.c,v 1.5 2005/08/08 08:05:35 espie Exp $ */ +/* $OpenBSD: udivdi3.c,v 1.5 2005/08/08 08:05:35 espie Exp $ */ /*- * Copyright (c) 1992, 1993 - * The Regents of the University of California. All rights reserved. + * The Regents of the University of California. All rights reserved. * * This software was developed by the Computer Systems Engineering group * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and @@ -37,9 +37,7 @@ /* * Divide two unsigned quads. */ -u_quad_t -__udivdi3(u_quad_t a, u_quad_t b) +u_quad_t __udivdi3(u_quad_t a, u_quad_t b) { - - return (__qdivrem(a, b, (u_quad_t *)0)); + return __qdivrem(a, b, NULL); } diff --git a/sys/quad_math/umoddi3.c b/sys/quad_math/umoddi3.c index 1eab2fc5b4..77c87c1f16 100644 --- a/sys/quad_math/umoddi3.c +++ b/sys/quad_math/umoddi3.c @@ -1,7 +1,7 @@ -/* $OpenBSD: umoddi3.c,v 1.5 2005/08/08 08:05:35 espie Exp $ */ +/* $OpenBSD: umoddi3.c,v 1.5 2005/08/08 08:05:35 espie Exp $ */ /*- * Copyright (c) 1992, 1993 - * The Regents of the University of California. All rights reserved. + * The Regents of the University of California. All rights reserved. * * This software was developed by the Computer Systems Engineering group * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and @@ -37,11 +37,9 @@ /* * Return remainder after dividing two unsigned quads. */ -u_quad_t -__umoddi3(u_quad_t a, u_quad_t b) +u_quad_t __umoddi3(u_quad_t a, u_quad_t b) { - u_quad_t r; - - (void)__qdivrem(a, b, &r); - return (r); + u_quad_t r; + (void) __qdivrem(a, b, &r); + return r; } diff --git a/sys/quad_math/xordi3.c b/sys/quad_math/xordi3.c index 9619eadd9b..cb9b9e28df 100644 --- a/sys/quad_math/xordi3.c +++ b/sys/quad_math/xordi3.c @@ -1,7 +1,7 @@ -/* $OpenBSD: xordi3.c,v 1.5 2005/08/08 08:05:35 espie Exp $ */ +/* $OpenBSD: xordi3.c,v 1.5 2005/08/08 08:05:35 espie Exp $ */ /*- * Copyright (c) 1992, 1993 - * The Regents of the University of California. All rights reserved. + * The Regents of the University of California. All rights reserved. * * This software was developed by the Computer Systems Engineering group * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and @@ -37,14 +37,13 @@ /* * Return a ^ b, in quad. */ -quad_t -__xordi3(quad_t a, quad_t b) +quad_t __xordi3(quad_t a, quad_t b) { - union uu aa, bb; + union uu aa, bb; - aa.q = a; - bb.q = b; - aa.ul[0] ^= bb.ul[0]; - aa.ul[1] ^= bb.ul[1]; - return (aa.q); + aa.q = a; + bb.q = b; + aa.ul[0] ^= bb.ul[0]; + aa.ul[1] ^= bb.ul[1]; + return aa.q; } -- GitLab