10Sstevel@tonic-gate /*
2*6812Sraf * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
30Sstevel@tonic-gate * Use is subject to license terms.
40Sstevel@tonic-gate */
50Sstevel@tonic-gate
60Sstevel@tonic-gate /*
70Sstevel@tonic-gate * Copyright (c) 1990, 1993
80Sstevel@tonic-gate * The Regents of the University of California. All rights reserved.
90Sstevel@tonic-gate *
100Sstevel@tonic-gate * This code is derived from software contributed to Berkeley by
110Sstevel@tonic-gate * Chris Torek.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * Redistribution and use in source and binary forms, with or without
140Sstevel@tonic-gate * modification, are permitted provided that the following conditions
150Sstevel@tonic-gate * are met:
160Sstevel@tonic-gate * 1. Redistributions of source code must retain the above copyright
170Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer.
180Sstevel@tonic-gate * 2. Redistributions in binary form must reproduce the above copyright
190Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer in the
200Sstevel@tonic-gate * documentation and/or other materials provided with the distribution.
210Sstevel@tonic-gate * 3. All advertising materials mentioning features or use of this software
220Sstevel@tonic-gate * must display the following acknowledgement:
230Sstevel@tonic-gate * This product includes software developed by the University of
240Sstevel@tonic-gate * California, Berkeley and its contributors.
250Sstevel@tonic-gate * 4. Neither the name of the University nor the names of its contributors
260Sstevel@tonic-gate * may be used to endorse or promote products derived from this software
270Sstevel@tonic-gate * without specific prior written permission.
280Sstevel@tonic-gate *
290Sstevel@tonic-gate * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
300Sstevel@tonic-gate * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
310Sstevel@tonic-gate * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
320Sstevel@tonic-gate * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
330Sstevel@tonic-gate * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
340Sstevel@tonic-gate * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
350Sstevel@tonic-gate * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
360Sstevel@tonic-gate * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
370Sstevel@tonic-gate * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
380Sstevel@tonic-gate * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
390Sstevel@tonic-gate * SUCH DAMAGE.
400Sstevel@tonic-gate */
410Sstevel@tonic-gate
42*6812Sraf #pragma ident "%Z%%M% %I% %E% SMI"
43*6812Sraf
44*6812Sraf #include "lint.h"
450Sstevel@tonic-gate #include <sys/types.h>
460Sstevel@tonic-gate #include <stdlib.h>
470Sstevel@tonic-gate
480Sstevel@tonic-gate div_t
div(num,denom)490Sstevel@tonic-gate div(num, denom)
500Sstevel@tonic-gate int num, denom;
510Sstevel@tonic-gate {
520Sstevel@tonic-gate div_t r;
530Sstevel@tonic-gate
540Sstevel@tonic-gate r.quot = num / denom;
550Sstevel@tonic-gate r.rem = num % denom;
560Sstevel@tonic-gate /*
570Sstevel@tonic-gate * The ANSI standard says that |r.quot| <= |n/d|, where
580Sstevel@tonic-gate * n/d is to be computed in infinite precision. In other
590Sstevel@tonic-gate * words, we should always truncate the quotient towards
600Sstevel@tonic-gate * 0, never -infinity.
610Sstevel@tonic-gate *
620Sstevel@tonic-gate * Machine division and remainer may work either way when
630Sstevel@tonic-gate * one or both of n or d is negative. If only one is
640Sstevel@tonic-gate * negative and r.quot has been truncated towards -inf,
650Sstevel@tonic-gate * r.rem will have the same sign as denom and the opposite
660Sstevel@tonic-gate * sign of num; if both are negative and r.quot has been
670Sstevel@tonic-gate * truncated towards -inf, r.rem will be positive (will
680Sstevel@tonic-gate * have the opposite sign of num). These are considered
690Sstevel@tonic-gate * `wrong'.
700Sstevel@tonic-gate *
710Sstevel@tonic-gate * If both are num and denom are positive, r will always
720Sstevel@tonic-gate * be positive.
730Sstevel@tonic-gate *
740Sstevel@tonic-gate * This all boils down to:
750Sstevel@tonic-gate * if num >= 0, but r.rem < 0, we got the wrong answer.
760Sstevel@tonic-gate * In that case, to get the right answer, add 1 to r.quot and
770Sstevel@tonic-gate * subtract denom from r.rem.
780Sstevel@tonic-gate */
790Sstevel@tonic-gate if (num >= 0 && r.rem < 0) {
800Sstevel@tonic-gate r.quot++;
810Sstevel@tonic-gate r.rem -= denom;
820Sstevel@tonic-gate }
830Sstevel@tonic-gate return (r);
840Sstevel@tonic-gate }
850Sstevel@tonic-gate
860Sstevel@tonic-gate ldiv_t
ldiv(num,denom)870Sstevel@tonic-gate ldiv(num, denom)
880Sstevel@tonic-gate long num, denom;
890Sstevel@tonic-gate {
900Sstevel@tonic-gate ldiv_t r;
910Sstevel@tonic-gate
920Sstevel@tonic-gate /* see div() for comments */
930Sstevel@tonic-gate
940Sstevel@tonic-gate r.quot = num / denom;
950Sstevel@tonic-gate r.rem = num % denom;
960Sstevel@tonic-gate if (num >= 0 && r.rem < 0) {
970Sstevel@tonic-gate r.quot++;
980Sstevel@tonic-gate r.rem -= denom;
990Sstevel@tonic-gate }
1000Sstevel@tonic-gate return (r);
1010Sstevel@tonic-gate }
102