Debian Codegraph @

ext/uvector/uvectorP.h

raw
1 /*
2 * uvectorP.h - internal macros for the uniform vector module
3 *
4 * Copyright (c) 2000-2024 Shiro Kawai <[email protected]>
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 *
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 *
17 * 3. Neither the name of the authors nor the names of its contributors
18 * may be used to endorse or promote products derived from this
19 * software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
27 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
28 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */
33
34 #ifndef GAUCHE_UVECTOR_P_H
35 #define GAUCHE_UVECTOR_P_H
36
37 /*--------------------------------------------------------
38 * inline functions used privately
39 */
40
41 /*-----------------------------------------------------------
42 * range checks
43 */
44 static void range_error(const char *type, ScmObj obj)
45 {
46 if (SCM_INTP(obj) && SCM_INT_VALUE(obj) == 0) {
47 Scm_Error("value out of domain for %svector", type);
48 } else {
49 Scm_Error("value out of domain for %svector: %S", type, obj);
50 }
51 }
52
53 static inline long range_s8hi(long val, int clamp)
54 {
55 if (!(clamp & SCM_CLAMP_HI)) range_error("s8", Scm_MakeInteger(val));
56 return 127;
57 }
58
59 static inline long range_s8lo(long val, int clamp)
60 {
61 if (!(clamp & SCM_CLAMP_LO)) range_error("s8", Scm_MakeInteger(val));
62 return -128;
63 }
64
65 static inline long clamp_s8(long val, int clamp)
66 {
67 if (val > 127) return range_s8hi(val, clamp);
68 if (val < -128) return range_s8lo(val, clamp);
69 return val;
70 }
71
72 static inline long range_u8hi(long val, int clamp)
73 {
74 if (!(clamp & SCM_CLAMP_HI)) range_error("u8", Scm_MakeInteger(val));
75 return 255;
76 }
77
78 static inline long range_u8lo(long val, int clamp)
79 {
80 if (!(clamp & SCM_CLAMP_LO)) range_error("u8", Scm_MakeInteger(val));
81 return 0;
82 }
83
84 static inline long clamp_u8(long val, int clamp)
85 {
86 if (val > 255) return range_u8hi(val, clamp);
87 if (val < 0) return range_u8lo(val, clamp);
88 return val;
89 }
90
91 static inline long range_s16hi(long val, int clamp)
92 {
93 if (!(clamp & SCM_CLAMP_HI)) range_error("s16", Scm_MakeInteger(val));
94 return 32767;
95 }
96
97 static inline long range_s16lo(long val, int clamp)
98 {
99 if (!(clamp & SCM_CLAMP_LO)) range_error("s16", Scm_MakeInteger(val));
100 return -32768;
101 }
102
103 static inline long clamp_s16(long val, int clamp)
104 {
105 if (val > 32767) return range_s16hi(val, clamp);
106 if (val < -32768) return range_s16lo(val, clamp);
107 return val;
108 }
109
110 static inline long range_u16hi(long val, int clamp)
111 {
112 if (!(clamp & SCM_CLAMP_HI)) range_error("u16", Scm_MakeInteger(val));
113 return 65535;
114 }
115
116 static inline long range_u16lo(long val, int clamp)
117 {
118 if (!(clamp & SCM_CLAMP_LO)) range_error("u16", Scm_MakeInteger(val));
119 return 0;
120 }
121
122 static inline long clamp_u16(long val, int clamp)
123 {
124 if (val > 65535) return range_u16hi(val, clamp);
125 if (val < 0) return range_u16lo(val, clamp);
126 return val;
127 }
128
129 static inline long range_s32hi(long val, int clamp)
130 {
131 if (!(clamp & SCM_CLAMP_HI)) range_error("s32", Scm_MakeInteger(val));
132 return 2147483647L;
133 }
134
135 static inline long range_s32lo(long val, int clamp)
136 {
137 if (!(clamp & SCM_CLAMP_LO)) range_error("s32", Scm_MakeInteger(val));
138 return -2147483647L-1;
139 }
140
141 #if SIZEOF_LONG == 4
142 #define clamp_s32(val, clamp) (val)
143 #else
144 static inline long clamp_s32(long val, int clamp)
145 {
146 if (val > 2147483647L) return range_s32hi(val, clamp);
147 if (val < -2147483648L) return range_s32lo(val, clamp);
148 return val;
149 }
150 #endif
151
152 static inline long range_u32hi(u_long val, int clamp)
153 {
154 if (!(clamp & SCM_CLAMP_HI)) range_error("u32", Scm_MakeIntegerU(val));
155 return 4294967295UL;
156 }
157
158 static inline long range_u32lo(u_long val, int clamp)
159 {
160 if (!(clamp & SCM_CLAMP_LO)) range_error("u32", Scm_MakeIntegerU(val));
161 return 0;
162 }
163
164 #if SIZEOF_LONG == 4
165 #define clamp_u32(val, clamp) (val)
166 #else
167 static inline long clamp_u32(u_long val, int clamp)
168 {
169 if (val > 4294967295UL) return range_u32hi(val, clamp);
170 return val;
171 }
172 #endif
173
174 static inline int64_t range_s64hi(int64_t val, int clamp)
175 {
176 if (!(clamp & SCM_CLAMP_HI)) range_error("s64", Scm_MakeInteger64(val));
177 val = INT64_MAX;
178 return val;
179 }
180
181 static inline int64_t range_s64lo(int64_t val, int clamp)
182 {
183 if (!(clamp & SCM_CLAMP_LO)) range_error("s64", Scm_MakeInteger64(val));
184 val = INT64_MIN;
185 return val;
186 }
187
188 static inline uint64_t range_u64hi(uint64_t val, int clamp)
189 {
190 if (!(clamp & SCM_CLAMP_HI)) range_error("u64", Scm_MakeIntegerU64(val));
191 val = UINT64_MAX;
192 return val;
193 }
194
195 static inline uint64_t range_u64lo(uint64_t val, int clamp)
196 {
197 if (!(clamp & SCM_CLAMP_LO)) range_error("u64", Scm_MakeIntegerU64(val));
198 val = 0;
199 return val;
200 }
201
202 /*
203 * 'option' argument for Scm_UVectorSwapBytes.
204 */
205
206 enum {
207 SWAPB_STD, /* simple swapping */
208 SWAPB_ARM_LE, /* arm-little-endian <-> little-endian */
209 SWAPB_ARM_BE /* arm-little-endian <-> big-endian */
210 };
211
212
213 #endif /* GAUCHE_UVECTOR_P_H */