source: trunk/libs/newlib/src/newlib/libc/stdlib/lldiv.c @ 543

Last change on this file since 543 was 444, checked in by satin@…, 6 years ago

add newlib,libalmos-mkh, restructure shared_syscalls.h and mini-libc

File size: 3.3 KB
Line 
1/*
2FUNCTION
3<<lldiv>>---divide two long long integers
4
5INDEX
6        lldiv
7
8SYNOPSIS
9        #include <stdlib.h>
10        lldiv_t lldiv(long long <[n]>, long long <[d]>);
11
12DESCRIPTION
13Divide
14@tex
15$n/d$,
16@end tex
17@ifnottex
18<[n]>/<[d]>,
19@end ifnottex
20returning quotient and remainder as two long long integers in a structure
21<<lldiv_t>>.
22
23RETURNS
24The result is represented with the structure
25
26. typedef struct
27. {
28.  long long quot;
29.  long long rem;
30. } lldiv_t;
31
32where the <<quot>> field represents the quotient, and <<rem>> the
33remainder.  For nonzero <[d]>, if `<<<[r]> = ldiv(<[n]>,<[d]>);>>' then
34<[n]> equals `<<<[r]>.rem + <[d]>*<[r]>.quot>>'.
35
36To divide <<long>> rather than <<long long>> values, use the similar
37function <<ldiv>>.
38
39PORTABILITY
40<<lldiv>> is ISO 9899 (C99) compatable.
41
42No supporting OS subroutines are required.
43*/
44
45/*-
46 * Copyright (c) 2001 Mike Barcroft <mike@FreeBSD.org>
47 * All rights reserved.
48 *
49 * Redistribution and use in source and binary forms, with or without
50 * modification, are permitted provided that the following conditions
51 * are met:
52 * 1. Redistributions of source code must retain the above copyright
53 *    notice, this list of conditions and the following disclaimer.
54 * 2. Redistributions in binary form must reproduce the above copyright
55 *    notice, this list of conditions and the following disclaimer in the
56 *    documentation and/or other materials provided with the distribution.
57 *
58 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
59 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
60 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
61 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
62 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
63 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
64 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
65 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
66 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
67 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
68 * SUCH DAMAGE.
69 */
70
71#include <stdlib.h>
72
73/*
74 * The ANSI standard says that |r.quot| <= |n/d|, where
75 * n/d is to be computed in infinite precision.  In other
76 * words, we should always truncate the quotient towards
77 * 0, never -infinity.
78 *
79 * Machine division and remainer may work either way when
80 * one or both of n or d is negative.  If only one is
81 * negative and r.quot has been truncated towards -inf,
82 * r.rem will have the same sign as denom and the opposite
83 * sign of num; if both are negative and r.quot has been
84 * truncated towards -inf, r.rem will be positive (will
85 * have the opposite sign of num).  These are considered
86 * `wrong'.
87 *
88 * If both are num and denom are positive, r will always
89 * be positive.
90 *
91 * This all boils down to:
92 *      if num >= 0, but r.rem < 0, we got the wrong answer.
93 * In that case, to get the right answer, add 1 to r.quot and
94 * subtract denom from r.rem.
95 */
96lldiv_t
97lldiv (long long numer, long long denom)
98{
99        lldiv_t retval;
100
101        retval.quot = numer / denom;
102        retval.rem = numer % denom;
103        if (numer >= 0 && retval.rem < 0) {
104                retval.quot++;
105                retval.rem -= denom;
106        }
107        return (retval);
108}
109
Note: See TracBrowser for help on using the repository browser.