]> rtime.felk.cvut.cz Git - l4.git/blob - l4/pkg/l4re-core/uclibc/lib/contrib/uclibc/libc/stdlib/lldiv.c
Update
[l4.git] / l4 / pkg / l4re-core / uclibc / lib / contrib / uclibc / libc / stdlib / lldiv.c
1 /* `long long int' divison with remainder.
2    Copyright (C) 1992, 1996, 1997 Free Software Foundation, Inc.
3    This file is part of the GNU C Library.
4
5    The GNU C Library is free software; you can redistribute it and/or
6    modify it under the terms of the GNU Lesser General Public
7    License as published by the Free Software Foundation; either
8    version 2.1 of the License, or (at your option) any later version.
9
10    The GNU C Library is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    Lesser General Public License for more details.
14
15    You should have received a copy of the GNU Lesser General Public
16    License along with the GNU C Library; if not, see
17    <http://www.gnu.org/licenses/>.  */
18
19 #include <features.h>
20 #include <stdlib.h>
21
22
23 /* Return the `lldiv_t' representation of NUMER over DENOM.  */
24 lldiv_t
25 lldiv (long long int numer, long long int denom)
26 {
27     lldiv_t result;
28
29     result.quot = numer / denom;
30     result.rem = numer % denom;
31
32     /* The ANSI standard says that |QUOT| <= |NUMER / DENOM|, where
33        NUMER / DENOM is to be computed in infinite precision.  In
34        other words, we should always truncate the quotient towards
35        zero, never -infinity.  Machine division and remainer may
36        work either way when one or both of NUMER or DENOM is
37        negative.  If only one is negative and QUOT has been
38        truncated towards -infinity, REM will have the same sign as
39        DENOM and the opposite sign of NUMER; if both are negative
40        and QUOT has been truncated towards -infinity, REM will be
41        positive (will have the opposite sign of NUMER).  These are
42        considered `wrong'.  If both are NUM and DENOM are positive,
43        RESULT will always be positive.  This all boils down to: if
44        NUMER >= 0, but REM < 0, we got the wrong answer.  In that
45        case, to get the right answer, add 1 to QUOT and subtract
46        DENOM from REM.  */
47
48     if (numer >= 0 && result.rem < 0)
49     {
50         ++result.quot;
51         result.rem -= denom;
52     }
53
54     return result;
55 }
56
57 #if __WORDSIZE != 64
58 #undef imaxdiv
59 strong_alias(lldiv,imaxdiv)
60 #endif