From 11c9808d7ebd71ad15fecd63aba74187ce447edd Mon Sep 17 00:00:00 2001 From: eck Date: Thu, 1 Mar 1990 16:32:22 +0000 Subject: [PATCH] fixed div() and ldiv() --- lang/cem/libcc.ansi/stdlib/div.c | 18 ++++++++++++++++-- lang/cem/libcc.ansi/stdlib/ldiv.c | 18 ++++++++++++++++-- 2 files changed, 32 insertions(+), 4 deletions(-) diff --git a/lang/cem/libcc.ansi/stdlib/div.c b/lang/cem/libcc.ansi/stdlib/div.c index bf27906f1..243079941 100644 --- a/lang/cem/libcc.ansi/stdlib/div.c +++ b/lang/cem/libcc.ansi/stdlib/div.c @@ -6,12 +6,26 @@ #include +static int tmp = -1; + div_t div(register int numer, register int denom) { div_t r; - r.quot = numer / denom; - r.rem = numer % denom; + /* 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; + } return r; } diff --git a/lang/cem/libcc.ansi/stdlib/ldiv.c b/lang/cem/libcc.ansi/stdlib/ldiv.c index 77574648c..25b842cbe 100644 --- a/lang/cem/libcc.ansi/stdlib/ldiv.c +++ b/lang/cem/libcc.ansi/stdlib/ldiv.c @@ -6,12 +6,26 @@ #include +static long tmp = -1; + ldiv_t ldiv(register long numer, register long denom) { ldiv_t r; - r.quot = numer / denom; - r.rem = numer % denom; + /* 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; + } return r; }