source: trunk/libs/newlib/src/newlib/libc/stdlib/ldiv.c @ 577

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

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

File size: 2.9 KB
Line 
1/*
2FUNCTION
3<<ldiv>>---divide two long integers
4
5INDEX
6        ldiv
7
8SYNOPSIS
9        #include <stdlib.h>
10        ldiv_t ldiv(long <[n]>, 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 integers in a structure <<ldiv_t>>.
21
22RETURNS
23The result is represented with the structure
24
25. typedef struct
26. {
27.  long quot;
28.  long rem;
29. } ldiv_t;
30
31where the <<quot>> field represents the quotient, and <<rem>> the
32remainder.  For nonzero <[d]>, if `<<<[r]> = ldiv(<[n]>,<[d]>);>>' then
33<[n]> equals `<<<[r]>.rem + <[d]>*<[r]>.quot>>'.
34
35To divide <<int>> rather than <<long>> values, use the similar
36function <<div>>.
37
38PORTABILITY
39<<ldiv>> is ANSI.
40
41No supporting OS subroutines are required.
42*/
43
44
45/*
46 * Copyright (c) 1990 Regents of the University of California.
47 * All rights reserved.
48 *
49 * This code is derived from software contributed to Berkeley by
50 * Chris Torek.
51 *
52 * Redistribution and use in source and binary forms, with or without
53 * modification, are permitted provided that the following conditions
54 * are met:
55 * 1. Redistributions of source code must retain the above copyright
56 *    notice, this list of conditions and the following disclaimer.
57 * 2. Redistributions in binary form must reproduce the above copyright
58 *    notice, this list of conditions and the following disclaimer in the
59 *    documentation and/or other materials provided with the distribution.
60 * 3. All advertising materials mentioning features or use of this software
61 *    must display the following acknowledgement:
62 *      This product includes software developed by the University of
63 *      California, Berkeley and its contributors.
64 * 4. Neither the name of the University nor the names of its contributors
65 *    may be used to endorse or promote products derived from this software
66 *    without specific prior written permission.
67 *
68 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
69 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
70 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
71 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
72 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
73 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
74 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
75 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
76 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
77 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
78 * SUCH DAMAGE.
79 */
80
81#include <_ansi.h>
82#include <stdlib.h>             /* ldiv_t */
83
84ldiv_t
85ldiv (long num,
86        long denom)
87{
88        ldiv_t r;
89
90        /* see div.c for comments */
91
92        r.quot = num / denom;
93        r.rem = num % denom;
94        if (num >= 0 && r.rem < 0) {
95                ++r.quot;
96                r.rem -= denom;
97        }
98        else if (num < 0 && r.rem > 0) {
99                --r.quot;
100                r.rem += denom;
101        }
102        return (r);
103}
Note: See TracBrowser for help on using the repository browser.