/*---------------------------------------------------------------------------* Project: Horizon File: math_Utility.h Copyright (C)2009-2010 Nintendo Co., Ltd. All rights reserved. These coded instructions, statements, and computer programs contain proprietary information of Nintendo of America Inc. and/or Nintendo Company Ltd., and are protected by Federal copyright law. They may not be disclosed to third parties or copied or duplicated in any form, in whole or in part, without the prior written consent of Nintendo. $Revision: 29760 $ *---------------------------------------------------------------------------*/ #ifndef NN_MATH_MATH_UTILITY_H_ #define NN_MATH_MATH_UTILITY_H_ #include namespace nn { namespace math { template inline T Abs(T x) { return (x >= 0) ? x: -x; } template inline S Max(T a, U b) { return (a >= b) ? a: b; } template inline T Max(T a, T b) { return (a >= b) ? a: b; } template inline S Min(T a, U b) { return (a <= b) ? a: b; } template inline T Min(T a, T b) { return (a <= b) ? a: b; } template inline S Max(T a, U b, R c) { return (a >= b) ? ((a >= c) ? a: c) : ((b >= c) ? b: c); } template inline T Max(T a, T b, T c) { return (a >= b) ? ((a >= c) ? a: c) : ((b >= c) ? b: c); } template inline S Min(T a, U b, R c) { return (a <= b) ? ((a <= c) ? a: c) : ((b <= c) ? b: c); } template inline T Min(T a, T b, T c) { return (a <= b) ? ((a <= c) ? a: c) : ((b <= c) ? b: c); } template inline T Clamp(T x, T low, T high) { return (x >= high) ? high : ((x <= low) ? low: x); } template inline T RoundUp(T x, u32 base) { return static_cast( (x + (base - 1)) & ~(base - 1) ); } template <> inline void* RoundUp(void* x, u32 base) { return reinterpret_cast( RoundUp(reinterpret_cast(x), base) ); } template <> inline const void* RoundUp(const void* x, u32 base) { return reinterpret_cast( RoundUp(reinterpret_cast(x), base) ); } template inline T RoundDown(T x, u32 base) { return static_cast( x & ~(base - 1) ); } template <> inline void* RoundDown(void* x, u32 base) { return reinterpret_cast( RoundDown(reinterpret_cast(x), base) ); } template <> inline const void* RoundDown(const void* x, u32 base) { return reinterpret_cast( RoundDown(reinterpret_cast(x), base) ); } template inline T DivUp(T x, T y) { return static_cast( (x + (y - 1)) / y ); } template inline T ExtractBits(bit32 v, int pos, int len) { return static_cast( v & (((1u << len) - 1) << pos) ); } template inline T ExtractBits(bit64 v, int pos, int len) { return static_cast( v & (((1ull << len) - 1) << pos) ); } template inline T GetBits(bit32 v, int pos, int len) { return static_cast( (v >> pos) & ((1u << len) - 1) ); } template inline T GetBits(bit64 v, int pos, int len) { return static_cast( (v >> pos) & ((1ull << len) - 1) ); } template inline bit32 MakeBits(T v, int width, int shift) { return (static_cast(v) & ((1u << width) - 1)) << shift; } }} #endif // ifndef NN_MATH_MATH_UTILITY_H_