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