1 /*---------------------------------------------------------------------------*
2 Project: TwlSDK - MATH - include
3 File: math/math.h
4
5 Copyright 2003-2008 Nintendo. All rights reserved.
6
7 These coded instructions, statements, and computer programs contain
8 proprietary information of Nintendo of America Inc. and/or Nintendo
9 Company Ltd., and are protected by Federal copyright law. They may
10 not be disclosed to third parties or copied or duplicated in any form,
11 in whole or in part, without the prior written consent of Nintendo.
12
13 $Date:: $
14 $Rev:$
15 $Author:$
16 *---------------------------------------------------------------------------*/
17
18 #ifndef NITRO_MATH_MATH_H_
19 #define NITRO_MATH_MATH_H_
20
21 #include <nitro/misc.h>
22 #include <nitro/types.h>
23
24 #ifdef __cplusplus
25 extern "C" {
26 #endif
27
28 //----------------------------------------------------------------------------
29 // Type Definitions
30 //----------------------------------------------------------------------------
31
32 //----------------------------------------------------------------------------
33 // Function Declarations
34 //----------------------------------------------------------------------------
35
36 //----------------------------------------------------------------------------
37 // Inline Function Implementation
38 //----------------------------------------------------------------------------
39
40 /*---------------------------------------------------------------------------*
41 Name: MATH_ABS
42
43 Description: Macro that returns absolute value.
44 Be careful about side effects, since each argument is subjected to multiple valuations.
45
46 Arguments: a
47
48 Returns: If a < 0 -a. Otherwise, a
49 *---------------------------------------------------------------------------*/
50 #define MATH_ABS(a) ( ( (a) < 0 ) ? (-(a)) : (a) )
51
52 /*---------------------------------------------------------------------------*
53 Name: MATH_IAbs
54
55 Description: Inline function that returns absolute value.
56 Since this is implemented as an inline function, there are no side effects.
57
58 Arguments: a
59
60 Returns: If a < 0 -a. Otherwise, a
61 *---------------------------------------------------------------------------*/
MATH_IAbs(int a)62 SDK_INLINE int MATH_IAbs(int a)
63 {
64 return (a < 0) ? -a : a;
65 }
66
67
68 /*---------------------------------------------------------------------------*
69 Name: MATH_CLAMP
70
71 Description: Macro that gets values in the range from "low" to "high."
72 Be careful about side effects, since each argument is subjected to multiple valuations.
73
74 Arguments: x
75 low: Maximum value
76 hight: Minimum value
77
78 Returns: If x < low, low. If x > high, high. Otherwise x.
79 *---------------------------------------------------------------------------*/
80 #define MATH_CLAMP(x, low, high) ( ( (x) > (high) ) ? (high) : ( ( (x) < (low) ) ? (low) : (x) ) )
81
82
83 /*---------------------------------------------------------------------------*
84 Name: MATH_MIN
85
86 Description: Compares two arguments and returns the smaller one.
87 This is implemented as a macro, so it can be used in the type that defines the inequality operator.
88 Be careful about side effects, since each argument is subjected to multiple valuations.
89
90 Arguments: a, b
91
92 Returns: If a < b, a. Otherwise, b.
93 *---------------------------------------------------------------------------*/
94 #define MATH_MIN(a,b) (((a) <= (b)) ? (a) : (b))
95
96
97 /*---------------------------------------------------------------------------*
98 Name: MATH_IMin
99
100 Description: Compares two int-type integer arguments and returns the smaller one.
101 Since this is implemented as an inline function, there are no side effects.
102
103 Arguments: a, b: int type integers
104
105 Returns: If a <= b, a. Otherwise, b.
106 *---------------------------------------------------------------------------*/
MATH_IMin(int a,int b)107 SDK_INLINE int MATH_IMin(int a, int b)
108 {
109 return (a <= b) ? a : b;
110 }
111
112 /*---------------------------------------------------------------------------*
113 Name: MATH_MAX
114
115 Description: Compares two arguments and returns the larger one.
116 This is implemented as a macro, so it can be used in the type that defines the inequality operator.
117 Be careful about side effects, since each argument is subjected to multiple valuations.
118
119 Arguments: a, b
120
121 Returns: If a >= b, a. Otherwise, b.
122 *---------------------------------------------------------------------------*/
123 #define MATH_MAX(a,b) (((a) >= (b)) ? (a) : (b))
124
125 /*---------------------------------------------------------------------------*
126 Name: MATH_IMax
127
128 Description: Compares two int-type integer arguments and returns the larger one.
129 Since this is implemented as an inline function, there are no side effects.
130
131 Arguments: a, b: int type integers
132
133 Returns: If a >= b, a. Otherwise, b.
134 *---------------------------------------------------------------------------*/
MATH_IMax(int a,int b)135 SDK_INLINE int MATH_IMax(int a, int b)
136 {
137 return (a >= b) ? a : b;
138 }
139
140 /*---------------------------------------------------------------------------*
141 Name: MATH_DIVUP
142
143 Description: This macro divides by base and takes the ceiling of the result.
144
145 Arguments: x: Numeric value
146 base: Base that is a power of 2
147
148 Returns: A number that is the result of dividing x by base and taking the ceiling of the quotient.
149 *---------------------------------------------------------------------------*/
150 #define MATH_DIVUP(x, base) (((x) + ((base)-1)) / (base))
151
152 /*---------------------------------------------------------------------------*
153 Name: MATH_ROUNDUP
154
155 Description: Macro that returns the rounded-up value (ceiling).
156
157 Arguments: x
158 base: Base of the power of 2
159
160 Returns: Smallest multiple of "base" that is greater than or equal to x
161 *---------------------------------------------------------------------------*/
162 #define MATH_ROUNDUP(x, base) (((x) + ((base)-1)) & ~((base)-1))
163
164 /*---------------------------------------------------------------------------*
165 Name: MATH_ROUNDDOWN
166
167 Description: Macro that returns the rounded-down value (floor).
168
169 Arguments: x
170 base: Base of the power of 2
171
172 Returns: Largest multiple of "base" that is less than or equal to x
173 *---------------------------------------------------------------------------*/
174 #define MATH_ROUNDDOWN(x, base) ((x) & ~((base)-1))
175
176 /*---------------------------------------------------------------------------*
177 Name: MATH_ROUNDUP32
178
179 Description: Macro that returns the value rounded-up to a multiple of 32.
180
181 Arguments: x
182
183 Returns: Smallest multiple of 32 that is greater than or equal to x
184 *---------------------------------------------------------------------------*/
185 #define MATH_ROUNDUP32(x) MATH_ROUNDUP(x, 32)
186
187 /*---------------------------------------------------------------------------*
188 Name: MATH_ROUNDDOWN32
189
190 Description: Macro that returns the value rounded-down to a multiple of 32.
191
192 Arguments: x
193
194 Returns: Largest multiple of 32 that is less than or equal to x
195 *---------------------------------------------------------------------------*/
196 #define MATH_ROUNDDOWN32(x) MATH_ROUNDDOWN(x, 32)
197
198 /*---------------------------------------------------------------------------*
199 Name: MATH_CountLeadingZeros
200
201 Description: Determines how many bits from the top have the value 0 in a binary 32-bit expression.
202 In the ARM9 ARM code, this is a single command.
203
204 Arguments: x
205
206 Returns: The number of contiguous 0 bits from the top
207 *---------------------------------------------------------------------------*/
208 u32 MATH_CountLeadingZerosFunc(u32 x);
209
210 #if !defined(PLATFORM_INTRINSIC_FUNCTION_BIT_CLZ32)
211
212 /* clz is available in ARM mode only */
213 #ifdef SDK_ARM9
214 #if defined(SDK_CW) || defined(SDK_RX) || defined(__MWERKS__)
215 #pragma thumb off
MATH_CountLeadingZerosInline(u32 x)216 SDK_INLINE u32 MATH_CountLeadingZerosInline(u32 x)
217 {
218 asm
219 {
220 clz x, x}
221 return x;
222 }
223
224 #pragma thumb reset
225 #elif defined(SDK_ADS)
226 TO BE DEFINED
227 #elif defined(SDK_GCC)
228 TO BE DEFINED
229 #endif
230 #endif
231 #endif /* PLATFORM_INTRINSIC_FUNCTION_BIT_CLZ32 */
232
233 #ifndef MATH_CountLeadingZeros
234 #if defined(PLATFORM_INTRINSIC_FUNCTION_BIT_CLZ32)
235 #define MATH_CountLeadingZeros(x) PLATFORM_INTRINSIC_FUNCTION_BIT_CLZ32(x)
236 #elif defined(SDK_ARM9) && defined(SDK_CODE_ARM)
237 #define MATH_CountLeadingZeros(x) MATH_CountLeadingZerosInline(x)
238 #else // not (ARM9 && CODE_ARM)
239 #define MATH_CountLeadingZeros(x) MATH_CountLeadingZerosFunc(x)
240 #endif // ARM9 && CODE_ARM
241 #endif
242
243 /*---------------------------------------------------------------------------*
244 Name: MATH_CLZ
245
246 Description: This is MATH_CountLeadingZeros under a different name.
247 Determines how many bits from the top have the value 0 in a binary 32-bit expression.
248 In the ARM9 ARM code, this is a single command.
249
250 Arguments: x
251
252 Returns: The number of contiguous 0 bits from the top
253 *---------------------------------------------------------------------------*/
254 #define MATH_CLZ(x) MATH_CountLeadingZeros(x)
255 /*---------------------------------------------------------------------------*
256 Name: MATH_ILog2
257
258 Description: Gets the integer portion of the base-2 logarithm log2(x) of the u32 x argument.
259 In the special case where x == 0, the function returns -1.
260 In the ARM9 ARM code, this is two commands using the CLZ command.
261
262 Arguments: x: u32
263
264 Returns: log2(x) when x > 0, or -1 when x == 0
265 *---------------------------------------------------------------------------*/
MATH_ILog2(u32 x)266 SDK_INLINE int MATH_ILog2(u32 x)
267 {
268 return (int)(31 - MATH_CountLeadingZeros(x));
269 }
270
271 /*---------------------------------------------------------------------------*
272 Name: MATH_CountPopulation
273
274 Description: Determines the number of '1' bits in a binary 32-bit expression.
275
276 Arguments: x
277
278 Returns: The number of '1' bits in the binary expression
279 *---------------------------------------------------------------------------*/
280 u8 MATH_CountPopulation(u32 x);
281
282
283 /*---------------------------------------------------------------------------*
284 Name: MATH_CountTrailingZeros
285
286 Description: Determines how many bits from the bottom have the value 0 in a binary 32-bit expression.
287 Uses the MATH_CountLeadingZeros function.
288
289 Arguments: x: u32 value used for determination
290
291 Returns: The number of contiguous 0 bits from the lower bit.
292 *---------------------------------------------------------------------------*/
MATH_CountTrailingZeros(u32 x)293 SDK_INLINE u32 MATH_CountTrailingZeros(u32 x)
294 {
295 return (u32)(32 - MATH_CountLeadingZeros((u32)(~x & (x - 1))));
296 }
297
298 /*---------------------------------------------------------------------------*
299 Name: MATH_CTZ
300
301 Description: This is MATH_CountTrailingZeros under a different name.
302 Determines how many bits from the bottom have the value 0 in a binary 32-bit expression.
303 Uses the MATH_CountLeadingZeros function.
304
305 Arguments: x: u32 value used for determination
306
307 Returns: The number of contiguous 0 bits from the lower bit.
308 *---------------------------------------------------------------------------*/
309 #define MATH_CTZ(x) MATH_CountTrailingZeros(x)
310
311 /*---------------------------------------------------------------------------*
312 Name: MATH_GetLeastSignificantBit
313
314 Description: Determines the lowest '1' bit in a binary 32bit expression.
315
316 Arguments: x: u32 value used for determination
317
318 Returns: A u32 value expressing the lowest '1' bit.
319 *---------------------------------------------------------------------------*/
MATH_GetLeastSignificantBit(u32 x)320 SDK_INLINE u32 MATH_GetLeastSignificantBit(u32 x)
321 {
322 return (u32)(x & -(s32)x);
323 }
324
325 /*---------------------------------------------------------------------------*
326 Name: MATH_LSB
327
328 Description: Another name for MATH_GetLeastSignificantBit.
329 Determines the lowest '1' bit in a binary 32-bit expression.
330
331 Arguments: x: u32 value used for determination
332
333 Returns: A u32 value expressing the lowest '1' bit.
334 *---------------------------------------------------------------------------*/
335 #define MATH_LSB(x) MATH_GetLeastSignificantBit(x)
336
337 /*---------------------------------------------------------------------------*
338 Name: MATH_GetMostSignificantBit
339
340 Description: Determines the highest '1' bit in a binary 32-bit expression.
341
342 Arguments: x: u32 value used for determination
343
344 Returns: A u32 value expressing the topmost '1' bit.
345 *---------------------------------------------------------------------------*/
MATH_GetMostSignificantBit(u32 x)346 SDK_INLINE u32 MATH_GetMostSignificantBit(u32 x)
347 {
348 return (u32)(x & ((s32)0x80000000 >> MATH_CountLeadingZeros(x)));
349 }
350
351 /*---------------------------------------------------------------------------*
352 Name: MATH_MSB
353
354 Description: Another name for MATH_GetMostSignificantBit.
355 Determines the highest '1' bit in a binary 32-bit expression.
356
357 Arguments: x: u32 value used for determination
358
359 Returns: A u32 value expressing the highest '1' bit.
360 *---------------------------------------------------------------------------*/
361 #define MATH_MSB(x) MATH_GetMostSignificantBit(x)
362
363
364 #ifdef __cplusplus
365 }/* extern "C" */
366 #endif
367
368 /* NITRO_MATH_MATH_H_ */
369 #endif
370