1 /*---------------------------------------------------------------------------*
2   Project:  Horizon
3   File:     math_Utility.h
4 
5   Copyright (C)2009-2010 Nintendo Co., Ltd.  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   $Revision: 35273 $
14  *---------------------------------------------------------------------------*/
15 
16 #ifndef NN_MATH_MATH_UTILITY_H_
17 #define NN_MATH_MATH_UTILITY_H_
18 
19 #include <nn/types.h>
20 
21 namespace nn { namespace math {
22 
23 /* Please see man pages for details
24 
25 
26 */
27 
28     /* Please see man pages for details
29 
30 
31 
32 
33 
34     */
35     template <typename T>
Abs(T x)36     inline T Abs(T x)
37     {
38         return (x >= 0) ? x: -x;
39     }
40 
41     /* Please see man pages for details
42 
43 
44 
45 
46 
47 
48 
49 
50 
51     */
52     template <typename T, typename U, typename S>
Max(T a,U b)53     inline S Max(T a, U b)
54     {
55         return (a >= b) ? a: b;
56     }
57 
58     /* Please see man pages for details
59 
60 
61 
62 
63 
64 
65 
66     */
67     template <typename T>
Max(T a,T b)68     inline T Max(T a, T b)
69     {
70         return (a >= b) ? a: b;
71     }
72 
73     /* Please see man pages for details
74 
75 
76 
77 
78 
79 
80 
81 
82 
83     */
84     template <typename T, typename U, typename S>
Min(T a,U b)85     inline S Min(T a, U b)
86     {
87         return (a <= b) ? a: b;
88     }
89 
90     /* Please see man pages for details
91 
92 
93 
94 
95 
96 
97 
98     */
99     template <typename T>
Min(T a,T b)100     inline T Min(T a, T b)
101     {
102         return (a <= b) ? a: b;
103     }
104 
105     /* Please see man pages for details
106 
107 
108 
109 
110 
111 
112 
113 
114 
115 
116 
117     */
118     template <typename T, typename U, typename R, typename S>
Max(T a,U b,R c)119     inline S Max(T a, U b, R c)
120     {
121         return (a >= b) ? ((a >= c) ? a: c) : ((b >= c) ? b: c);
122     }
123 
124     /* Please see man pages for details
125 
126 
127 
128 
129 
130 
131 
132 
133     */
134     template <typename T>
Max(T a,T b,T c)135     inline T Max(T a, T b, T c)
136     {
137         return (a >= b) ? ((a >= c) ? a: c) : ((b >= c) ? b: c);
138     }
139 
140     /* Please see man pages for details
141 
142 
143 
144 
145 
146 
147 
148 
149 
150 
151 
152     */
153     template <typename T, typename U, typename R, typename S>
Min(T a,U b,R c)154     inline S Min(T a, U b, R c)
155     {
156         return (a <= b) ? ((a <= c) ? a: c) : ((b <= c) ? b: c);
157     }
158 
159     /* Please see man pages for details
160 
161 
162 
163 
164 
165 
166 
167 
168     */
169     template <typename T>
Min(T a,T b,T c)170     inline T Min(T a, T b, T c)
171     {
172         return (a <= b) ? ((a <= c) ? a: c) : ((b <= c) ? b: c);
173     }
174 
175     /* Please see man pages for details
176 
177 
178 
179 
180 
181 
182 
183 
184 
185     */
186     template <typename T>
Clamp(T x,T low,T high)187     inline T Clamp(T x, T low, T high)
188     {
189         return (x >= high) ? high : ((x <= low) ? low: x);
190     }
191 
192     /* Please see man pages for details
193 
194 
195 
196 
197 
198 
199 
200     */
201     template <typename T>
RoundUp(T x,u32 base)202     inline T RoundUp(T x, u32 base)
203     {
204         return static_cast<T>( (x + (base - 1)) & ~(base - 1) );
205     }
206 
207     /* Please see man pages for details
208 
209 
210 
211 
212 
213 
214     */
215     template <>
RoundUp(void * x,u32 base)216     inline void* RoundUp(void* x, u32 base)
217     {
218         return reinterpret_cast<void*>( RoundUp(reinterpret_cast<uptr>(x), base) );
219     }
220 
221     /* Please see man pages for details
222 
223 
224 
225 
226 
227 
228     */
229     template <>
RoundUp(const void * x,u32 base)230     inline const void* RoundUp(const void* x, u32 base)
231     {
232         return reinterpret_cast<const void*>( RoundUp(reinterpret_cast<uptr>(x), base) );
233     }
234 
235     /* Please see man pages for details
236 
237 
238 
239 
240 
241 
242 
243     */
244     template <typename T>
RoundDown(T x,u32 base)245     inline T RoundDown(T x, u32 base)
246     {
247         return static_cast<T>( x & ~(base - 1) );
248     }
249 
250     /* Please see man pages for details
251 
252 
253 
254 
255 
256 
257     */
258     template <>
RoundDown(void * x,u32 base)259     inline void* RoundDown(void* x, u32 base)
260     {
261         return reinterpret_cast<void*>( RoundDown(reinterpret_cast<uptr>(x), base) );
262     }
263 
264     /* Please see man pages for details
265 
266 
267 
268 
269 
270 
271     */
272     template <>
RoundDown(const void * x,u32 base)273     inline const void* RoundDown(const void* x, u32 base)
274     {
275         return reinterpret_cast<const void*>( RoundDown(reinterpret_cast<uptr>(x), base) );
276     }
277 
278     /* Please see man pages for details
279 
280 
281 
282 
283 
284 
285     */
286     template <typename T>
DivUp(T x,T y)287     inline T DivUp(T x, T y)
288     {
289         return static_cast<T>( (x + (y - 1)) / y );
290     }
291 
292     /* Please see man pages for details
293 
294 
295 
296 
297 
298 
299 
300 
301     */
302     template <typename T>
ExtractBits(bit32 v,int pos,int len)303     inline T ExtractBits(bit32 v, int pos, int len)
304     {
305         return static_cast<T>( v & (((1u << len) - 1) << pos) );
306     }
307 
308     /* Please see man pages for details
309 
310 
311 
312 
313 
314 
315 
316 
317     */
318     template <typename T>
ExtractBits(bit64 v,int pos,int len)319     inline T ExtractBits(bit64 v, int pos, int len)
320     {
321         return static_cast<T>( v & (((1ull << len) - 1) << pos) );
322     }
323 
324     /* Please see man pages for details
325 
326 
327 
328 
329 
330 
331 
332 
333 
334 
335     */
336     template <typename T>
GetBits(bit32 v,int pos,int len)337     inline T GetBits(bit32 v, int pos, int len)
338     {
339         return static_cast<T>( (v >> pos) & ((1u << len) - 1) );
340     }
341 
342     /* Please see man pages for details
343 
344 
345 
346 
347 
348 
349 
350 
351 
352 
353     */
354     template <typename T>
GetBits(bit64 v,int pos,int len)355     inline T GetBits(bit64 v, int pos, int len)
356     {
357         return static_cast<T>( (v >> pos) & ((1ull << len) - 1) );
358     }
359 
360     /* Please see man pages for details
361 
362 
363 
364 
365 
366 
367 
368 
369 
370     */
371     template <typename T>
MakeBits(T v,int width,int shift)372     inline bit32 MakeBits(T v, int width, int shift)
373     {
374         return (static_cast<bit32>(v) & ((1u << width) - 1)) << shift;
375     }
376 
377 /*
378 
379 */
380 }}
381 
382 
383 #endif  // ifndef NN_MATH_MATH_UTILITY_H_
384