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: 29760 $
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     template <typename T>
Abs(T x)24     inline T Abs(T x)
25     {
26         return (x >= 0) ? x: -x;
27     }
28 
29     template <typename T, typename U, typename S>
Max(T a,U b)30     inline S Max(T a, U b)
31     {
32         return (a >= b) ? a: b;
33     }
34 
35     template <typename T>
Max(T a,T b)36     inline T Max(T a, T b)
37     {
38         return (a >= b) ? a: b;
39     }
40 
41     template <typename T, typename U, typename S>
Min(T a,U b)42     inline S Min(T a, U b)
43     {
44         return (a <= b) ? a: b;
45     }
46 
47     template <typename T>
Min(T a,T b)48     inline T Min(T a, T b)
49     {
50         return (a <= b) ? a: b;
51     }
52 
53     template <typename T, typename U, typename R, typename S>
Max(T a,U b,R c)54     inline S Max(T a, U b, R c)
55     {
56         return (a >= b) ? ((a >= c) ? a: c) : ((b >= c) ? b: c);
57     }
58 
59     template <typename T>
Max(T a,T b,T c)60     inline T Max(T a, T b, T c)
61     {
62         return (a >= b) ? ((a >= c) ? a: c) : ((b >= c) ? b: c);
63     }
64 
65     template <typename T, typename U, typename R, typename S>
Min(T a,U b,R c)66     inline S Min(T a, U b, R c)
67     {
68         return (a <= b) ? ((a <= c) ? a: c) : ((b <= c) ? b: c);
69     }
70 
71     template <typename T>
Min(T a,T b,T c)72     inline T Min(T a, T b, T c)
73     {
74         return (a <= b) ? ((a <= c) ? a: c) : ((b <= c) ? b: c);
75     }
76 
77     template <typename T>
Clamp(T x,T low,T high)78     inline T Clamp(T x, T low, T high)
79     {
80         return (x >= high) ? high : ((x <= low) ? low: x);
81     }
82 
83     template <typename T>
RoundUp(T x,u32 base)84     inline T RoundUp(T x, u32 base)
85     {
86         return static_cast<T>( (x + (base - 1)) & ~(base - 1) );
87     }
88 
89     template <>
RoundUp(void * x,u32 base)90     inline void* RoundUp(void* x, u32 base)
91     {
92         return reinterpret_cast<void*>( RoundUp(reinterpret_cast<uptr>(x), base) );
93     }
94 
95     template <>
RoundUp(const void * x,u32 base)96     inline const void* RoundUp(const void* x, u32 base)
97     {
98         return reinterpret_cast<const void*>( RoundUp(reinterpret_cast<uptr>(x), base) );
99     }
100 
101     template <typename T>
RoundDown(T x,u32 base)102     inline T RoundDown(T x, u32 base)
103     {
104         return static_cast<T>( x & ~(base - 1) );
105     }
106 
107     template <>
RoundDown(void * x,u32 base)108     inline void* RoundDown(void* x, u32 base)
109     {
110         return reinterpret_cast<void*>( RoundDown(reinterpret_cast<uptr>(x), base) );
111     }
112 
113     template <>
RoundDown(const void * x,u32 base)114     inline const void* RoundDown(const void* x, u32 base)
115     {
116         return reinterpret_cast<const void*>( RoundDown(reinterpret_cast<uptr>(x), base) );
117     }
118 
119     template <typename T>
DivUp(T x,T y)120     inline T DivUp(T x, T y)
121     {
122         return static_cast<T>( (x + (y - 1)) / y );
123     }
124 
125     template <typename T>
ExtractBits(bit32 v,int pos,int len)126     inline T ExtractBits(bit32 v, int pos, int len)
127     {
128         return static_cast<T>( v & (((1u << len) - 1) << pos) );
129     }
130 
131     template <typename T>
ExtractBits(bit64 v,int pos,int len)132     inline T ExtractBits(bit64 v, int pos, int len)
133     {
134         return static_cast<T>( v & (((1ull << len) - 1) << pos) );
135     }
136 
137     template <typename T>
GetBits(bit32 v,int pos,int len)138     inline T GetBits(bit32 v, int pos, int len)
139     {
140         return static_cast<T>( (v >> pos) & ((1u << len) - 1) );
141     }
142 
143     template <typename T>
GetBits(bit64 v,int pos,int len)144     inline T GetBits(bit64 v, int pos, int len)
145     {
146         return static_cast<T>( (v >> pos) & ((1ull << len) - 1) );
147     }
148 
149     template <typename T>
MakeBits(T v,int width,int shift)150     inline bit32 MakeBits(T v, int width, int shift)
151     {
152         return (static_cast<bit32>(v) & ((1u << width) - 1)) << shift;
153     }
154 }}
155 
156 
157 #endif  // ifndef NN_MATH_MATH_UTILITY_H_
158