/* [<][>][^][v][top][bottom][index][help] */
DEFINITIONS
This source file includes following definitions.
- range_error
- range_s8hi
- range_s8lo
- clamp_s8
- range_u8hi
- range_u8lo
- clamp_u8
- range_s16hi
- range_s16lo
- clamp_s16
- range_u16hi
- range_u16lo
- clamp_u16
- range_s32hi
- range_s32lo
- clamp_s32
- range_u32hi
- range_u32lo
- clamp_u32
- range_s64hi
- range_s64lo
- range_u64hi
- range_u64lo
- s8unbox
- u8unbox
- s16unbox
- u16unbox
1 /*
2 * uvectorP.h - internal macros for the uniform vector module
3 *
4 * Copyright (c) 2000-2005 Shiro Kawai, All rights reserved.
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 * $Id: uvectorP.h,v 1.22 2005/04/12 01:42:24 shirok Exp $
34 */
35
36 #ifndef GAUCHE_UVECTOR_P_H
37 #define GAUCHE_UVECTOR_P_H
38
39 /*--------------------------------------------------------
40 * inline functions used privately
41 */
42
43 /*-----------------------------------------------------------
44 * range checks
45 */
46 static void range_error(const char *type, ScmObj obj)
47 {
48 if (SCM_INTP(obj) && SCM_INT_VALUE(obj) == 0) {
49 Scm_Error("value out of domain for %svector", type);
50 } else {
51 Scm_Error("value out of domain for %svector: %S", type, obj);
52 }
53 }
54
55 static inline long range_s8hi(long val, int clamp)
56 {
57 if (clamp & SCM_CLAMP_HI) return 127;
58 else range_error("s8", Scm_MakeInteger(val));
59 }
60
61 static inline long range_s8lo(long val, int clamp)
62 {
63 if (clamp & SCM_CLAMP_LO) return -128;
64 else range_error("s8", Scm_MakeInteger(val));
65 }
66
67 static inline long clamp_s8(long val, int clamp)
68 {
69 if (val > 127) return range_s8hi(val, clamp);
70 if (val < -128) return range_s8lo(val, clamp);
71 return val;
72 }
73
74 static inline long range_u8hi(long val, int clamp)
75 {
76 if (clamp & SCM_CLAMP_HI) return 255;
77 else range_error("u8", Scm_MakeInteger(val));
78 }
79
80 static inline long range_u8lo(long val, int clamp)
81 {
82 if (clamp & SCM_CLAMP_LO) return 0;
83 else range_error("u8", Scm_MakeInteger(val));
84 }
85
86 static inline long clamp_u8(long val, int clamp)
87 {
88 if (val > 255) return range_u8hi(val, clamp);
89 if (val < 0) return range_u8lo(val, clamp);
90 return val;
91 }
92
93 static inline long range_s16hi(long val, int clamp)
94 {
95 if (clamp & SCM_CLAMP_HI) return 32767;
96 else range_error("s16", Scm_MakeInteger(val));
97 }
98
99 static inline long range_s16lo(long val, int clamp)
100 {
101 if (clamp & SCM_CLAMP_LO) return -32768;
102 else range_error("s16", Scm_MakeInteger(val));
103 }
104
105 static inline long clamp_s16(long val, int clamp)
106 {
107 if (val > 32767) return range_s16hi(val, clamp);
108 if (val < -32768) return range_s16lo(val, clamp);
109 return val;
110 }
111
112 static inline long range_u16hi(long val, int clamp)
113 {
114 if (clamp & SCM_CLAMP_HI) return 65535;
115 else range_error("u16", Scm_MakeInteger(val));
116 }
117
118 static inline long range_u16lo(long val, int clamp)
119 {
120 if (clamp & SCM_CLAMP_LO) return 0;
121 else range_error("u16", Scm_MakeInteger(val));
122 }
123
124 static inline long clamp_u16(long val, int clamp)
125 {
126 if (val > 65535) return range_u16hi(val, clamp);
127 if (val < 0) return range_u16lo(val, clamp);
128 return val;
129 }
130
131 static inline long range_s32hi(long val, int clamp)
132 {
133 if (clamp & SCM_CLAMP_HI) return 2147483647L;
134 else range_error("s32", Scm_MakeInteger(val));
135 }
136
137 static inline long range_s32lo(long val, int clamp)
138 {
139 if (clamp & SCM_CLAMP_LO) return -2147483647L-1;
140 else range_error("s32", Scm_MakeInteger(val));
141 }
142
143 #if SIZEOF_LONG == 4
144 #define clamp_s32(val, clamp) (val)
145 #else
146 static inline long clamp_s32(long val, int clamp)
147 {
148 if (val > 2147483647L) return range_s32hi(val, clamp);
149 if (val < -2147483648L) return range_s32lo(val, clamp);
150 return val;
151 }
152 #endif
153
154 static inline long range_u32hi(u_long val, int clamp)
155 {
156 if (clamp & SCM_CLAMP_HI) return 4294967295UL;
157 else range_error("u32", Scm_MakeIntegerU(val));
158 }
159
160 static inline long range_u32lo(u_long val, int clamp)
161 {
162 if (clamp & SCM_CLAMP_LO) return 0;
163 else range_error("u32", Scm_MakeIntegerU(val));
164 }
165
166 #if SIZEOF_LONG == 4
167 #define clamp_u32(val, clamp) (val)
168 #else
169 static inline long clamp_u32(u_long val, int clamp)
170 {
171 if (val > 4294967295UL) return range_u32hi(val, clamp);
172 return val;
173 }
174 #endif
175
176 static inline ScmInt64 range_s64hi(ScmInt64 val, int clamp)
177 {
178 if (clamp & SCM_CLAMP_HI) {
179 SCM_SET_INT64_MAX(val);
180 return val;
181 }
182 else range_error("s64", Scm_MakeInteger64(val));
183 }
184
185 static inline ScmInt64 range_s64lo(ScmInt64 val, int clamp)
186 {
187 if (clamp & SCM_CLAMP_LO) {
188 SCM_SET_INT64_MIN(val);
189 return val;
190 }
191 else range_error("s64", Scm_MakeInteger64(val));
192 }
193
194 static inline ScmUInt64 range_u64hi(ScmUInt64 val, int clamp)
195 {
196 if (clamp & SCM_CLAMP_HI) {
197 SCM_SET_UINT64_MAX(val);
198 return val;
199 }
200 else range_error("u64", Scm_MakeIntegerU64(val));
201 }
202
203 static inline ScmUInt64 range_u64lo(ScmUInt64 val, int clamp)
204 {
205 if (clamp & SCM_CLAMP_LO) {
206 SCM_SET_INT64_ZERO(val);
207 return val;
208 }
209 else range_error("u64", Scm_MakeIntegerU64(val));
210 }
211
212 /*
213 * boxing, unboxing
214 */
215 static inline signed char s8unbox(ScmObj obj, int clamp)
216 {
217 long val;
218 if (!SCM_INTP(obj)) val = Scm_GetInteger(obj); /* clamped to long */
219 else val = SCM_INT_VALUE(obj);
220 return clamp_s8(val, clamp);
221 }
222
223 static inline unsigned char u8unbox(ScmObj obj, int clamp)
224 {
225 long val;
226 if (!SCM_INTP(obj)) val = Scm_GetInteger(obj); /* clamped to long */
227 else val = SCM_INT_VALUE(obj);
228 return clamp_u8(val, clamp);
229 }
230
231 static inline short s16unbox(ScmObj obj, int clamp)
232 {
233 long val;
234 if (!SCM_INTP(obj)) val = Scm_GetInteger(obj); /* clamped to long */
235 else val = SCM_INT_VALUE(obj);
236 return clamp_s16(val, clamp);
237 return val;
238 }
239
240 static inline unsigned short u16unbox(ScmObj obj, int clamp)
241 {
242 long val;
243 if (!SCM_INTP(obj)) val = Scm_GetInteger(obj); /* clamped to long */
244 else val = SCM_INT_VALUE(obj);
245 return clamp_u16(val, clamp);
246 }
247
248
249
250 #endif /* GAUCHE_UVECTOR_P_H */