usr/src/lib/libc/port/gen/div.c
author Jon Tibble <meths@btinternet.com>
Thu, 09 Dec 2010 22:32:39 +0100
changeset 13255 4afa820d78b9
parent 6812 febeba71273d
permissions -rw-r--r--
298 SPARC build fails in smt_pause.o 478 Build needs fixing for pkgdepend flag day Reviewed by: [email protected] Reviewed by: [email protected] Reviewed by: [email protected] Approved by: [email protected]
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
     1
/*
6812
febeba71273d PSARC 2008/309 expunge synonyms.h
raf
parents: 0
diff changeset
     2
 * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
0
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
     3
 * Use is subject to license terms.
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
     4
 */
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
     5
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
     6
/*
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
     7
 * Copyright (c) 1990, 1993
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
     8
 *	The Regents of the University of California.  All rights reserved.
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
     9
 *
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
    10
 * This code is derived from software contributed to Berkeley by
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
    11
 * Chris Torek.
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
    12
 *
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
    13
 * Redistribution and use in source and binary forms, with or without
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
    14
 * modification, are permitted provided that the following conditions
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
    15
 * are met:
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
    16
 * 1. Redistributions of source code must retain the above copyright
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
    17
 *    notice, this list of conditions and the following disclaimer.
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
    18
 * 2. Redistributions in binary form must reproduce the above copyright
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
    19
 *    notice, this list of conditions and the following disclaimer in the
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
    20
 *    documentation and/or other materials provided with the distribution.
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
    21
 * 3. All advertising materials mentioning features or use of this software
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
    22
 *    must display the following acknowledgement:
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
    23
 *	This product includes software developed by the University of
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
    24
 *	California, Berkeley and its contributors.
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
    25
 * 4. Neither the name of the University nor the names of its contributors
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
    26
 *    may be used to endorse or promote products derived from this software
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
    27
 *    without specific prior written permission.
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
    28
 *
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
    29
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
    30
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
    31
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
    32
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
    33
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
    34
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
    35
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
    36
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
    37
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
    38
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
    39
 * SUCH DAMAGE.
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
    40
 */
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
    41
6812
febeba71273d PSARC 2008/309 expunge synonyms.h
raf
parents: 0
diff changeset
    42
#pragma ident	"%Z%%M%	%I%	%E% SMI"
febeba71273d PSARC 2008/309 expunge synonyms.h
raf
parents: 0
diff changeset
    43
febeba71273d PSARC 2008/309 expunge synonyms.h
raf
parents: 0
diff changeset
    44
#include "lint.h"
0
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
    45
#include <sys/types.h>
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
    46
#include <stdlib.h>
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
    47
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
    48
div_t
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
    49
div(num, denom)
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
    50
	int num, denom;
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
    51
{
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
    52
	div_t r;
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
    53
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
    54
	r.quot = num / denom;
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
    55
	r.rem = num % denom;
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
    56
	/*
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
    57
	 * The ANSI standard says that |r.quot| <= |n/d|, where
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
    58
	 * n/d is to be computed in infinite precision.  In other
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
    59
	 * words, we should always truncate the quotient towards
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
    60
	 * 0, never -infinity.
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
    61
	 *
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
    62
	 * Machine division and remainer may work either way when
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
    63
	 * one or both of n or d is negative.  If only one is
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
    64
	 * negative and r.quot has been truncated towards -inf,
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
    65
	 * r.rem will have the same sign as denom and the opposite
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
    66
	 * sign of num; if both are negative and r.quot has been
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
    67
	 * truncated towards -inf, r.rem will be positive (will
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
    68
	 * have the opposite sign of num).  These are considered
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
    69
	 * `wrong'.
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
    70
	 *
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
    71
	 * If both are num and denom are positive, r will always
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
    72
	 * be positive.
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
    73
	 *
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
    74
	 * This all boils down to:
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
    75
	 *	if num >= 0, but r.rem < 0, we got the wrong answer.
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
    76
	 * In that case, to get the right answer, add 1 to r.quot and
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
    77
	 * subtract denom from r.rem.
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
    78
	 */
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
    79
	if (num >= 0 && r.rem < 0) {
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
    80
		r.quot++;
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
    81
		r.rem -= denom;
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
    82
	}
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
    83
	return (r);
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
    84
}
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
    85
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
    86
ldiv_t
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
    87
ldiv(num, denom)
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
    88
	long num, denom;
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
    89
{
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
    90
	ldiv_t r;
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
    91
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
    92
	/* see div() for comments */
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
    93
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
    94
	r.quot = num / denom;
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
    95
	r.rem = num % denom;
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
    96
	if (num >= 0 && r.rem < 0) {
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
    97
		r.quot++;
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
    98
		r.rem -= denom;
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
    99
	}
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
   100
	return (r);
68f95e015346 OpenSolaris Launch
stevel@tonic-gate
parents:
diff changeset
   101
}