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 <machine/trap.h>
33
34 typedef struct {
35 int quot;
36 int rem;
37 } div_t;
38
39 typedef struct {
40 long quot;
41 long rem;
42 } ldiv_t;
43
44 typedef struct {
45 long long quot;
46 long long rem;
47 } lldiv_t;
48
49 static inline div_t
div(int numer,int denom)50 div(int numer, int denom)
51 {
52 div_t retval;
53
54 retval.quot = numer / denom;
55 retval.rem = numer % denom;
56 if (numer >= 0 && retval.rem < 0) {
57 retval.quot++;
58 retval.rem -= denom;
59 }
60 return retval;
61 }
62
63 static inline ldiv_t
ldiv(long numer,long denom)64 ldiv(long numer, long denom)
65 {
66 ldiv_t retval;
67
68 retval.quot = numer / denom;
69 retval.rem = numer % denom;
70 if (numer >= 0 && retval.rem < 0) {
71 retval.quot++;
72 retval.rem -= denom;
73 }
74 return retval;
75 }
76
77 static inline lldiv_t
lldiv(long long numer,long long denom)78 lldiv(long long numer, long long denom)
79 {
80 lldiv_t retval;
81
82 retval.quot = numer / denom;
83 retval.rem = numer % denom;
84 if (numer >= 0 && retval.rem < 0) {
85 retval.quot++;
86 retval.rem -= denom;
87 }
88 return retval;
89 }
90
91 static inline void __attribute__((noreturn, cold))
abort(void)92 abort(void)
93 {
94 ml_fatal_trap(0x0800);
95 }
96
97 #endif
98