Update contrib.
3 * <<div>>---divide two integers
8 * div_t div(int <[n]>, int <[d]>);
11 * div_t div(<[n]>, <[d]>)
20 * returning quotient and remainder as two integers in a structure <<div_t>>.
22 * The result is represented with the structure
27 * where the <<quot>> field represents the quotient, and <<rem>> the
28 * remainder. For nonzero <[d]>, if `<<<[r]> = div(<[n]>,<[d]>);>>' then
29 * <[n]> equals `<<<[r]>.rem + <[d]>*<[r]>.quot>>'.
30 * To divide <<long>> rather than <<int>> values, use the similar
34 * No supporting OS subroutines are required.
42 * Copyright (c) 1990 Regents of the University of California.
43 * All rights reserved.
45 * This code is derived from software contributed to Berkeley by
48 * Redistribution and use in source and binary forms, with or without
49 * modification, are permitted provided that the following conditions
51 * 1. Redistributions of source code must retain the above copyright
52 * notice, this list of conditions and the following disclaimer.
53 * 2. Redistributions in binary form must reproduce the above copyright
54 * notice, this list of conditions and the following disclaimer in the
55 * documentation and/or other materials provided with the distribution.
56 * 3. All advertising materials mentioning features or use of this software
57 * must display the following acknowledgement:
58 * This product includes software developed by the University of
59 * California, Berkeley and its contributors.
60 * 4. Neither the name of the University nor the names of its contributors
61 * may be used to endorse or promote products derived from this software
62 * without specific prior written permission.
64 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
65 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
66 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
67 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
68 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
69 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
70 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
71 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
72 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
73 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
78 #include <stdlib.h> /* div_t */
81 Divide two integer values.
82 numer is divided by denom.
83 Quotient and remainder are returned in a div_t structure.
84 @return A div_t structure is returned
86 @param denom Denominator.
88 EXPORT_C div_t div (int num, int denom)
95 * The ANSI standard says that |r.quot| <= |n/d|, where
96 * n/d is to be computed in infinite precision. In other
97 * words, we should always truncate the quotient towards
100 * Machine division and remainer may work either way when
101 * one or both of n or d is negative. If only one is
102 * negative and r.quot has been truncated towards -inf,
103 * r.rem will have the same sign as denom and the opposite
104 * sign of num; if both are negative and r.quot has been
105 * truncated towards -inf, r.rem will be positive (will
106 * have the opposite sign of num). These are considered
109 * If both are num and denom are positive, r will always
112 * This all boils down to:
113 * if num >= 0, but r.rem < 0, we got the wrong answer.
114 * In that case, to get the right answer, add 1 to r.quot and
115 * subtract denom from r.rem.
117 if (num >= 0 && r.rem < 0) {