xref: /openbsd-src/lib/libc/stdlib/lldiv.c (revision 60e29447380c2a7085979385eb4e985f0d44970e)
1*60e29447Sguenther /*	$OpenBSD: lldiv.c,v 1.2 2016/08/14 23:18:03 guenther Exp $	*/
2aa522acbSmillert /*
3aa522acbSmillert  * Copyright (c) 1990 Regents of the University of California.
4aa522acbSmillert  * All rights reserved.
5aa522acbSmillert  *
6aa522acbSmillert  * This code is derived from software contributed to Berkeley by
7aa522acbSmillert  * Chris Torek.
8aa522acbSmillert  *
9aa522acbSmillert  * Redistribution and use in source and binary forms, with or without
10aa522acbSmillert  * modification, are permitted provided that the following conditions
11aa522acbSmillert  * are met:
12aa522acbSmillert  * 1. Redistributions of source code must retain the above copyright
13aa522acbSmillert  *    notice, this list of conditions and the following disclaimer.
14aa522acbSmillert  * 2. Redistributions in binary form must reproduce the above copyright
15aa522acbSmillert  *    notice, this list of conditions and the following disclaimer in the
16aa522acbSmillert  *    documentation and/or other materials provided with the distribution.
17aa522acbSmillert  * 3. Neither the name of the University nor the names of its contributors
18aa522acbSmillert  *    may be used to endorse or promote products derived from this software
19aa522acbSmillert  *    without specific prior written permission.
20aa522acbSmillert  *
21aa522acbSmillert  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22aa522acbSmillert  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23aa522acbSmillert  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24aa522acbSmillert  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25aa522acbSmillert  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26aa522acbSmillert  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27aa522acbSmillert  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28aa522acbSmillert  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29aa522acbSmillert  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30aa522acbSmillert  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31aa522acbSmillert  * SUCH DAMAGE.
32aa522acbSmillert  */
33aa522acbSmillert 
34aa522acbSmillert #include <stdlib.h>		/* lldiv_t */
35aa522acbSmillert 
36aa522acbSmillert lldiv_t
lldiv(long long num,long long denom)37aa522acbSmillert lldiv(long long num, long long denom)
38aa522acbSmillert {
39aa522acbSmillert 	lldiv_t r;
40aa522acbSmillert 
41aa522acbSmillert 	/* see div.c for comments */
42aa522acbSmillert 
43aa522acbSmillert 	r.quot = num / denom;
44aa522acbSmillert 	r.rem = num % denom;
45aa522acbSmillert 	if (num >= 0 && r.rem < 0) {
46aa522acbSmillert 		r.quot++;
47aa522acbSmillert 		r.rem -= denom;
48aa522acbSmillert 	}
49aa522acbSmillert 	return (r);
50aa522acbSmillert }
51*60e29447Sguenther 
52*60e29447Sguenther __weak_alias(qdiv, lldiv);
53