From 067ad0cc224e8b3ed50edbe7eb20655c27a4ad5c Mon Sep 17 00:00:00 2001 From: ceriel Date: Tue, 26 Oct 1993 13:58:04 +0000 Subject: [PATCH] Fixed div() and ldiv() for machines with -1/2 = -1 --- lang/cem/libcc.ansi/stdlib/div.c | 22 +++++++--------------- lang/cem/libcc.ansi/stdlib/ldiv.c | 22 +++++++--------------- 2 files changed, 14 insertions(+), 30 deletions(-) diff --git a/lang/cem/libcc.ansi/stdlib/div.c b/lang/cem/libcc.ansi/stdlib/div.c index 79c35a33e..cdb8ecc7e 100644 --- a/lang/cem/libcc.ansi/stdlib/div.c +++ b/lang/cem/libcc.ansi/stdlib/div.c @@ -6,26 +6,18 @@ #include -static int tmp = -1; - div_t div(register int numer, register int denom) { div_t r; - /* The assignment of tmp should not be optimized !! */ - if (tmp == -1) { - tmp = (tmp / 2 == 0); - } - if (numer == 0) { - r.quot = numer / denom; /* might trap if denom == 0 */ - r.rem = numer % denom; - } else if ( !tmp && ((numer < 0) != (denom < 0))) { - r.quot = (numer / denom) + 1; - r.rem = numer - (numer / denom + 1) * denom; - } else { - r.quot = numer / denom; - r.rem = numer % denom; + r.quot = numer / denom; /* might trap if denom == 0 */ + r.rem = numer % denom; + + if (r.rem != 0 && (numer > 0) != (r.rem > 0)) { + r.quot++; + r.rem -= denom; } + return r; } diff --git a/lang/cem/libcc.ansi/stdlib/ldiv.c b/lang/cem/libcc.ansi/stdlib/ldiv.c index 42c761977..4f5a1750b 100644 --- a/lang/cem/libcc.ansi/stdlib/ldiv.c +++ b/lang/cem/libcc.ansi/stdlib/ldiv.c @@ -6,26 +6,18 @@ #include -static long tmp = -1; - ldiv_t ldiv(register long numer, register long denom) { ldiv_t r; - /* The assignment of tmp should not be optimized !! */ - if (tmp == -1) { - tmp = (tmp / 2 == 0); - } - if (numer == 0) { - r.quot = numer / denom; /* might trap if denom == 0 */ - r.rem = numer % denom; - } else if ( !tmp && ((numer < 0) != (denom < 0))) { - r.quot = (numer / denom) + 1; - r.rem = numer - (numer / denom + 1) * denom; - } else { - r.quot = numer / denom; - r.rem = numer % denom; + r.quot = numer / denom; /* might trap if denom == 0 */ + r.rem = numer % denom; + + if (r.rem != 0 && (numer > 0) != (r.rem > 0)) { + r.quot++; + r.rem -= denom; } + return r; }