summaryrefslogtreecommitdiffstats
path: root/lib/libc/quad/qdivrem.c
diff options
context:
space:
mode:
authorotto <otto@openbsd.org>2004-04-27 17:46:46 +0000
committerotto <otto@openbsd.org>2004-04-27 17:46:46 +0000
commit99ae6da7b858227f6f2d3e8bf2bc3ae3e8dc902f (patch)
tree1e8c75d2d5dda9d3c6463cc88fa8f87f423635fc /lib/libc/quad/qdivrem.c
parentunbreak^2 (diff)
downloadwireguard-openbsd-99ae6da7b858227f6f2d3e8bf2bc3ae3e8dc902f.tar.xz
wireguard-openbsd-99ae6da7b858227f6f2d3e8bf2bc3ae3e8dc902f.zip
A quad is two ints, not two longs. Also fix some problems with
conversions from floating point to quad. Problem reported by Marcus Holland-Moritz. From NetBSD. ok millert@
Diffstat (limited to 'lib/libc/quad/qdivrem.c')
-rw-r--r--lib/libc/quad/qdivrem.c75
1 files changed, 38 insertions, 37 deletions
diff --git a/lib/libc/quad/qdivrem.c b/lib/libc/quad/qdivrem.c
index 6c9e522b2a7..ac666c25707 100644
--- a/lib/libc/quad/qdivrem.c
+++ b/lib/libc/quad/qdivrem.c
@@ -32,7 +32,7 @@
*/
#if defined(LIBC_SCCS) && !defined(lint)
-static char rcsid[] = "$OpenBSD: qdivrem.c,v 1.4 2003/06/02 20:18:36 millert Exp $";
+static char rcsid[] = "$OpenBSD: qdivrem.c,v 1.5 2004/04/27 17:46:46 otto Exp $";
#endif /* LIBC_SCCS and not lint */
/*
@@ -42,25 +42,25 @@ static char rcsid[] = "$OpenBSD: qdivrem.c,v 1.4 2003/06/02 20:18:36 millert Exp
#include "quad.h"
-#define B ((long)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_long)(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 ULONG_MAX == 0xffffffff && USHRT_MAX >= 0xffff
+#if UINT_MAX == 0xffffffffU && USHRT_MAX >= 0xffff
typedef unsigned short digit;
#else
-typedef u_long digit;
+typedef u_int digit;
#endif
-static void shl(digit *p, int len, int sh);
+static void shl __P((digit *p, int len, int sh));
/*
* __qdivrem(u, v, rem) returns u/v and, optionally, sets *rem to u%v.
*
* We do this in base 2-sup-HALF_BITS, so that all intermediate products
- * fit within u_long. As a consequence, the maximum length dividend and
+ * fit within u_int. As a consequence, the maximum length dividend and
* divisor are 4 `digits' in this base (they are shorter if they have
* leading zeros).
*/
@@ -70,8 +70,8 @@ __qdivrem(uq, vq, arq)
{
union uu tmp;
digit *u, *v, *q;
- register digit v1, v2;
- u_long qhat, rhat, t;
+ digit v1, v2;
+ u_int qhat, rhat, t;
int m, n, d, j, i;
digit uspace[5], vspace[5], qspace[5];
@@ -111,18 +111,18 @@ __qdivrem(uq, vq, arq)
*/
tmp.uq = uq;
u[0] = 0;
- u[1] = HHALF(tmp.ul[H]);
- u[2] = LHALF(tmp.ul[H]);
- u[3] = HHALF(tmp.ul[L]);
- u[4] = LHALF(tmp.ul[L]);
+ 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] = HHALF(tmp.ul[H]);
- v[2] = LHALF(tmp.ul[H]);
- v[3] = HHALF(tmp.ul[L]);
- v[4] = LHALF(tmp.ul[L]);
+ 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_long rbj; /* r*B+u[j] (not root boy jim) */
+ u_int rbj; /* r*B+u[j] (not root boy jim) */
digit q1, q2, q3, q4;
/*
@@ -134,13 +134,13 @@ __qdivrem(uq, vq, arq)
* We unroll this completely here.
*/
t = v[2]; /* nonzero, by definition */
- q1 = u[1] / t;
+ q1 = (digit)(u[1] / t);
rbj = COMBINE(u[1] % t, u[2]);
- q2 = rbj / t;
+ q2 = (digit)(rbj / t);
rbj = COMBINE(rbj % t, u[3]);
- q3 = rbj / t;
+ q3 = (digit)(rbj / t);
rbj = COMBINE(rbj % t, u[4]);
- q4 = rbj / t;
+ q4 = (digit)(rbj / t);
if (arq)
*arq = rbj % t;
tmp.ul[H] = COMBINE(q1, q2);
@@ -180,7 +180,7 @@ __qdivrem(uq, vq, arq)
v1 = v[1]; /* for D3 -- note that v[1..n] are constant */
v2 = v[2]; /* for D3 */
do {
- register digit uj0, uj1, uj2;
+ digit uj0, uj1, uj2;
/*
* D3: Calculate qhat (\^q, in TeX notation).
@@ -198,9 +198,9 @@ __qdivrem(uq, vq, arq)
rhat = uj1;
goto qhat_too_big;
} else {
- u_long n = COMBINE(uj0, uj1);
- qhat = n / v1;
- rhat = n % v1;
+ u_int nn = COMBINE(uj0, uj1);
+ qhat = nn / v1;
+ rhat = nn % v1;
}
while (v2 * qhat > COMBINE(rhat, uj2)) {
qhat_too_big:
@@ -216,11 +216,11 @@ __qdivrem(uq, vq, arq)
*/
for (t = 0, i = n; i > 0; i--) {
t = u[i + j] - v[i] * qhat - t;
- u[i + j] = LHALF(t);
+ u[i + j] = (digit)LHALF(t);
t = (B - HHALF(t)) & (B - 1);
}
t = u[j] - t;
- u[j] = LHALF(t);
+ u[j] = (digit)LHALF(t);
/*
* D5: test remainder.
* There is a borrow if and only if HHALF(t) is nonzero;
@@ -231,12 +231,12 @@ __qdivrem(uq, vq, arq)
qhat--;
for (t = 0, i = n; i > 0; i--) { /* D6: add back. */
t += u[i + j] + v[i];
- u[i + j] = LHALF(t);
+ u[i + j] = (digit)LHALF(t);
t = HHALF(t);
}
- u[j] = LHALF(u[j] + t);
+ u[j] = (digit)LHALF(u[j] + t);
}
- q[j] = qhat;
+ q[j] = (digit)qhat;
} while (++j <= m); /* D7: loop on j. */
/*
@@ -247,8 +247,8 @@ __qdivrem(uq, vq, arq)
if (arq) {
if (d) {
for (i = m + n; i > m; --i)
- u[i] = (u[i] >> d) |
- LHALF(u[i - 1] << (HALF_BITS - d));
+ 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]);
@@ -267,11 +267,12 @@ __qdivrem(uq, vq, arq)
* We may assume len >= 0. NOTE THAT THIS WRITES len+1 DIGITS.
*/
static void
-shl(register digit *p, register int len, register int sh)
+shl(digit *p, int len, int sh)
{
- register int i;
+ int i;
for (i = 0; i < len; i++)
- p[i] = LHALF(p[i] << sh) | (p[i + 1] >> (HALF_BITS - sh));
- p[i] = LHALF(p[i] << sh);
+ 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));
}