xref: /freebsd/lib/libc/stdlib/lldiv.c (revision d915a14ef094c8dfc1a5aee70e135abfec01d0f1)
17a4a6327SMike Barcroft /*-
2*d915a14eSPedro F. Giffuni  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3*d915a14eSPedro F. Giffuni  *
47a4a6327SMike Barcroft  * Copyright (c) 2001 Mike Barcroft <mike@FreeBSD.org>
57a4a6327SMike Barcroft  * All rights reserved.
67a4a6327SMike Barcroft  *
77a4a6327SMike Barcroft  * Redistribution and use in source and binary forms, with or without
87a4a6327SMike Barcroft  * modification, are permitted provided that the following conditions
97a4a6327SMike Barcroft  * are met:
107a4a6327SMike Barcroft  * 1. Redistributions of source code must retain the above copyright
117a4a6327SMike Barcroft  *    notice, this list of conditions and the following disclaimer.
127a4a6327SMike Barcroft  * 2. Redistributions in binary form must reproduce the above copyright
137a4a6327SMike Barcroft  *    notice, this list of conditions and the following disclaimer in the
147a4a6327SMike Barcroft  *    documentation and/or other materials provided with the distribution.
157a4a6327SMike Barcroft  *
167a4a6327SMike Barcroft  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
177a4a6327SMike Barcroft  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
187a4a6327SMike Barcroft  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
197a4a6327SMike Barcroft  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
207a4a6327SMike Barcroft  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
217a4a6327SMike Barcroft  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
227a4a6327SMike Barcroft  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
237a4a6327SMike Barcroft  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
247a4a6327SMike Barcroft  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
257a4a6327SMike Barcroft  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
267a4a6327SMike Barcroft  * SUCH DAMAGE.
277a4a6327SMike Barcroft  */
287a4a6327SMike Barcroft 
297a4a6327SMike Barcroft #include <sys/cdefs.h>
307a4a6327SMike Barcroft __FBSDID("$FreeBSD$");
317a4a6327SMike Barcroft 
327a4a6327SMike Barcroft #include <stdlib.h>
337a4a6327SMike Barcroft 
347a4a6327SMike Barcroft /* See comments in div.c for implementation details. */
357a4a6327SMike Barcroft lldiv_t
367a4a6327SMike Barcroft lldiv(long long numer, long long denom)
377a4a6327SMike Barcroft {
387a4a6327SMike Barcroft 	lldiv_t retval;
397a4a6327SMike Barcroft 
407a4a6327SMike Barcroft 	retval.quot = numer / denom;
417a4a6327SMike Barcroft 	retval.rem = numer % denom;
4269c526f4SAndrey A. Chernov #if !defined(__STDC_VERSION__) || (__STDC_VERSION__ < 199901L)
437a4a6327SMike Barcroft 	if (numer >= 0 && retval.rem < 0) {
447a4a6327SMike Barcroft 		retval.quot++;
457a4a6327SMike Barcroft 		retval.rem -= denom;
467a4a6327SMike Barcroft 	}
4769c526f4SAndrey A. Chernov #endif
487a4a6327SMike Barcroft 	return (retval);
497a4a6327SMike Barcroft }
50