Source-Changes-HG archive
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]
[src/trunk]: src/lib/libc/quad Corrected an off-by-one error (lib/6314 Torbjo...
details: https://anonhg.NetBSD.org/src/rev/e275d9fbdd16
branches: trunk
changeset: 467635:e275d9fbdd16
user: kristerw <kristerw%NetBSD.org@localhost>
date: Fri Mar 26 21:04:24 1999 +0000
description:
Corrected an off-by-one error (lib/6314 Torbjorn Granlund)
diffstat:
lib/libc/quad/fixunsdfdi.c | 38 +++++++++++---------------------------
1 files changed, 11 insertions(+), 27 deletions(-)
diffs (67 lines):
diff -r c32689835114 -r e275d9fbdd16 lib/libc/quad/fixunsdfdi.c
--- a/lib/libc/quad/fixunsdfdi.c Fri Mar 26 20:53:12 1999 +0000
+++ b/lib/libc/quad/fixunsdfdi.c Fri Mar 26 21:04:24 1999 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: fixunsdfdi.c,v 1.4 1997/07/13 20:01:45 christos Exp $ */
+/* $NetBSD: fixunsdfdi.c,v 1.5 1999/03/26 21:04:24 kristerw Exp $ */
/*-
* Copyright (c) 1992, 1993
@@ -42,7 +42,7 @@
#if 0
static char sccsid[] = "@(#)fixunsdfdi.c 8.1 (Berkeley) 6/4/93";
#else
-__RCSID("$NetBSD: fixunsdfdi.c,v 1.4 1997/07/13 20:01:45 christos Exp $");
+__RCSID("$NetBSD: fixunsdfdi.c,v 1.5 1999/03/26 21:04:24 kristerw Exp $");
#endif
#endif /* LIBC_SCCS and not lint */
@@ -61,8 +61,8 @@
__fixunsdfdi(x)
double x;
{
- double toppart;
union uu t;
+ unsigned long tmp;
if (x < 0)
return (UQUAD_MAX); /* ??? should be 0? ERANGE??? */
@@ -74,30 +74,14 @@
return (UQUAD_MAX);
#endif
/*
- * 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..ULONG_MAX]. For paranoia, we assume [LONG_MIN..
- * 2*ULONG_MAX] instead.
+ * 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.
*/
- t.ul[H] = (unsigned long)toppart;
- t.ul[L] = 0;
- x -= (double)t.uq;
- if (x < 0) {
- t.ul[H]--;
- x += ULONG_MAX;
- }
- if (x > ULONG_MAX) {
- t.ul[H]++;
- x -= ULONG_MAX;
- }
- t.ul[L] = (u_long)x;
+ tmp = x / ONE;
+ t.ul[L] = (unsigned long) (x - tmp * ONE);
+ t.ul[H] = tmp;
return (t.uq);
}
Home |
Main Index |
Thread Index |
Old Index