exp2l: Work around a NetBSD 10.0/i386 bug.
[gnulib.git] / lib / imaxdiv.c
blob72006b855a9ac30d934206676b68804fe2fb8048
1 /* imaxdiv() function: division of 'intmax_t'.
2 Copyright (C) 2006, 2009-2024 Free Software Foundation, Inc.
4 This file is free software: you can redistribute it and/or modify
5 it under the terms of the GNU Lesser General Public License as
6 published by the Free Software Foundation, either version 3 of the
7 License, or (at your option) any later version.
9 This file is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public License
15 along with this program. If not, see <https://www.gnu.org/licenses/>. */
17 #include <config.h>
19 /* Specification. */
20 #include <inttypes.h>
22 #include <stdlib.h>
24 imaxdiv_t
25 imaxdiv (intmax_t numer, intmax_t denom)
27 imaxdiv_t result;
29 result.quot = numer / denom;
30 result.rem = numer % denom;
32 /* Verify the requirements of ISO C 99 section 6.5.5 paragraph 6:
33 "When integers are divided, the result of the / operator is the
34 algebraic quotient with any fractional part discarded. (This is
35 often called "truncation toward zero".) If the quotient a/b is
36 representable, the expression (a/b)*b + a%b shall equal a." */
37 if (!(denom == 0
38 || (INTMAX_MIN + INTMAX_MAX < 0
39 && denom == -1
40 && numer < - INTMAX_MAX)))
42 if (!(result.quot * denom + result.rem == numer))
43 /* The compiler's implementation of / and % is broken. */
44 abort ();
45 if (!(numer >= 0
46 ? result.rem >= 0
47 && (denom >= 0
48 ? result.rem < denom
49 : /* Don't write result.rem < - denom,
50 as it gives integer overflow if denom == INTMAX_MIN. */
51 - result.rem > denom)
52 : result.rem <= 0
53 && (denom >= 0
54 ? result.rem > - denom
55 : result.rem > denom)))
56 /* The compiler's implementation of / and % may be ok according to
57 C89, but not to C99. Please report this to <bug-gnulib@ngu.org>.
58 This might be a big portability problem. */
59 abort ();
62 return result;