xref: /xnu-11215/libkern/libcxx_support/stdlib.h (revision 94d3b452)
1 /*
2  * Copyright (c) 2022 Apple Inc. All rights reserved.
3  *
4  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5  *
6  * This file contains Original Code and/or Modifications of Original Code
7  * as defined in and that are subject to the Apple Public Source License
8  * Version 2.0 (the 'License'). You may not use this file except in
9  * compliance with the License. The rights granted to you under the License
10  * may not be used to create, or enable the creation or redistribution of,
11  * unlawful or unlicensed copies of an Apple operating system, or to
12  * circumvent, violate, or enable the circumvention or violation of, any
13  * terms of an Apple operating system software license agreement.
14  *
15  * Please obtain a copy of the License at
16  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17  *
18  * The Original Code and all software distributed under the License are
19  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23  * Please see the License for the specific language governing rights and
24  * limitations under the License.
25  *
26  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27  */
28 
29 #ifndef __STDLIB_H__
30 #define __STDLIB_H__
31 
32 #include <sys/_types/_size_t.h>
33 #include <machine/trap.h>
34 
35 typedef struct {
36 	int quot;
37 	int rem;
38 } div_t;
39 
40 typedef struct {
41 	long quot;
42 	long rem;
43 } ldiv_t;
44 
45 typedef struct {
46 	long long quot;
47 	long long rem;
48 } lldiv_t;
49 
50 static inline div_t
div(int numer,int denom)51 div(int numer, int denom)
52 {
53 	div_t retval;
54 
55 	retval.quot = numer / denom;
56 	retval.rem = numer % denom;
57 	if (numer >= 0 && retval.rem < 0) {
58 		retval.quot++;
59 		retval.rem -= denom;
60 	}
61 	return retval;
62 }
63 
64 static inline ldiv_t
ldiv(long numer,long denom)65 ldiv(long numer, long denom)
66 {
67 	ldiv_t retval;
68 
69 	retval.quot = numer / denom;
70 	retval.rem = numer % denom;
71 	if (numer >= 0 && retval.rem < 0) {
72 		retval.quot++;
73 		retval.rem -= denom;
74 	}
75 	return retval;
76 }
77 
78 static inline lldiv_t
lldiv(long long numer,long long denom)79 lldiv(long long numer, long long denom)
80 {
81 	lldiv_t retval;
82 
83 	retval.quot = numer / denom;
84 	retval.rem = numer % denom;
85 	if (numer >= 0 && retval.rem < 0) {
86 		retval.quot++;
87 		retval.rem -= denom;
88 	}
89 	return retval;
90 }
91 
92 static inline void __attribute__((noreturn, cold))
abort(void)93 abort(void)
94 {
95 	ml_fatal_trap(0x0800);
96 }
97 
98 #endif
99