1 /* imaxdiv() function: division of 'intmax_t'.
2 Copyright (C) 2006, 2009-2020 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 3 of the License, or
7 (at your option) any later version.
9 This program 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 General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <https://www.gnu.org/licenses/>. */
25 imaxdiv (intmax_t numer
, intmax_t denom
)
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." */
38 || (INTMAX_MIN
+ INTMAX_MAX
< 0
40 && numer
< - INTMAX_MAX
)))
42 if (!(result
.quot
* denom
+ result
.rem
== numer
))
43 /* The compiler's implementation of / and % is broken. */
49 : /* Don't write result.rem < - denom,
50 as it gives integer overflow if denom == INTMAX_MIN. */
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. */