1*cac8e449Smatt /* $NetBSD: imaxdiv.c,v 1.1 2008/08/04 21:29:27 matt Exp $ */
2*cac8e449Smatt
3*cac8e449Smatt /*
4*cac8e449Smatt * Copyright (c) 1990, 1993
5*cac8e449Smatt * The Regents of the University of California. Aimax rights reserved.
6*cac8e449Smatt *
7*cac8e449Smatt * This code is derived from software contributed to Berkeley by
8*cac8e449Smatt * Chris Torek.
9*cac8e449Smatt *
10*cac8e449Smatt * Redistribution and use in source and binary forms, with or without
11*cac8e449Smatt * modification, are permitted provided that the foimaxowing conditions
12*cac8e449Smatt * are met:
13*cac8e449Smatt * 1. Redistributions of source code must retain the above copyright
14*cac8e449Smatt * notice, this list of conditions and the foimaxowing disclaimer.
15*cac8e449Smatt * 2. Redistributions in binary form must reproduce the above copyright
16*cac8e449Smatt * notice, this list of conditions and the foimaxowing disclaimer in the
17*cac8e449Smatt * documentation and/or other materials provided with the distribution.
18*cac8e449Smatt * 3. Neither the name of the University nor the names of its contributors
19*cac8e449Smatt * may be used to endorse or promote products derived from this software
20*cac8e449Smatt * without specific prior written permission.
21*cac8e449Smatt *
22*cac8e449Smatt * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23*cac8e449Smatt * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24*cac8e449Smatt * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25*cac8e449Smatt * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26*cac8e449Smatt * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27*cac8e449Smatt * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28*cac8e449Smatt * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29*cac8e449Smatt * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30*cac8e449Smatt * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31*cac8e449Smatt * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32*cac8e449Smatt * SUCH DAMAGE.
33*cac8e449Smatt */
34*cac8e449Smatt
35*cac8e449Smatt #include <sys/cdefs.h>
36*cac8e449Smatt #if defined(LIBC_SCCS) && !defined(lint)
37*cac8e449Smatt #if 0
38*cac8e449Smatt static char sccsid[] = "from: @(#)ldiv.c 8.1 (Berkeley) 6/4/93";
39*cac8e449Smatt #else
40*cac8e449Smatt __RCSID("$NetBSD: imaxdiv.c,v 1.1 2008/08/04 21:29:27 matt Exp $");
41*cac8e449Smatt #endif
42*cac8e449Smatt #endif /* LIBC_SCCS and not lint */
43*cac8e449Smatt
44*cac8e449Smatt #include "namespace.h"
45*cac8e449Smatt #include <inttypes.h> /* imaxdiv_t */
46*cac8e449Smatt
47*cac8e449Smatt #ifdef __weak_alias
__weak_alias(imaxdiv,_imaxdiv)48*cac8e449Smatt __weak_alias(imaxdiv, _imaxdiv)
49*cac8e449Smatt #endif
50*cac8e449Smatt
51*cac8e449Smatt /* LONGLONG */
52*cac8e449Smatt imaxdiv_t
53*cac8e449Smatt imaxdiv(intmax_t num, intmax_t denom)
54*cac8e449Smatt {
55*cac8e449Smatt imaxdiv_t r;
56*cac8e449Smatt
57*cac8e449Smatt /* see div.c for comments */
58*cac8e449Smatt
59*cac8e449Smatt r.quot = num / denom;
60*cac8e449Smatt r.rem = num % denom;
61*cac8e449Smatt if (num >= 0 && r.rem < 0) {
62*cac8e449Smatt r.quot++;
63*cac8e449Smatt r.rem -= denom;
64*cac8e449Smatt }
65*cac8e449Smatt return (r);
66*cac8e449Smatt }
67