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; }