root/maint/gnulib/lib/imaxdiv.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. imaxdiv

   1 /* imaxdiv() function: division of 'intmax_t'.
   2    Copyright (C) 2006, 2009-2021 Free Software Foundation, Inc.
   3 
   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.
   8 
   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.
  13 
  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/>.  */
  16 
  17 #include <config.h>
  18 
  19 /* Specification.  */
  20 #include <inttypes.h>
  21 
  22 #include <stdlib.h>
  23 
  24 imaxdiv_t
  25 imaxdiv (intmax_t numer, intmax_t denom)
     /* [previous][next][first][last][top][bottom][index][help] */
  26 {
  27   imaxdiv_t result;
  28 
  29   result.quot = numer / denom;
  30   result.rem = numer % denom;
  31 
  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)))
  41     {
  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 ();
  60     }
  61 
  62   return result;
  63 }

/* [previous][next][first][last][top][bottom][index][help] */