glm/glm/gtx/integer.inl

88 lines
2.0 KiB
Plaintext
Raw Normal View History

2010-04-29 10:54:07 +00:00
///////////////////////////////////////////////////////////////////////////////////////////////////
2011-01-20 11:40:14 +00:00
// OpenGL Mathematics Copyright (c) 2005 - 2011 G-Truc Creation (www.g-truc.net)
2010-04-29 10:54:07 +00:00
///////////////////////////////////////////////////////////////////////////////////////////////////
// Created : 2005-12-24
// Updated : 2006-12-06
// Licence : This source is under MIT License
// File : glm/gtx/integer.inl
///////////////////////////////////////////////////////////////////////////////////////////////////
namespace glm{
2011-06-07 10:28:11 +00:00
2011-06-10 15:45:17 +00:00
// pow
GLM_FUNC_QUALIFIER int pow(int x, int y)
{
if(y == 0)
return 1;
int result = x;
for(int i = 1; i < y; ++i)
result *= x;
return result;
}
2010-04-29 10:54:07 +00:00
2011-06-10 15:45:17 +00:00
// sqrt: From Christopher J. Musial, An integer square root, Graphics Gems, 1990, page 387
GLM_FUNC_QUALIFIER int sqrt(int x)
{
if(x <= 1) return x;
2010-04-29 10:54:07 +00:00
2011-06-10 15:45:17 +00:00
int NextTrial = x >> 1;
int CurrentAnswer;
2010-04-29 10:54:07 +00:00
2011-06-10 15:45:17 +00:00
do
{
CurrentAnswer = NextTrial;
NextTrial = (NextTrial + x / NextTrial) >> 1;
} while(NextTrial < CurrentAnswer);
2010-04-29 10:54:07 +00:00
2011-06-10 15:45:17 +00:00
return CurrentAnswer;
}
2010-04-29 10:54:07 +00:00
2011-06-10 15:45:17 +00:00
// mod
GLM_FUNC_QUALIFIER int mod(int x, int y)
{
return x - y * (x / y);
}
2010-04-29 10:54:07 +00:00
2011-06-10 15:45:17 +00:00
// factorial (!12 max, integer only)
template <typename genType>
GLM_FUNC_QUALIFIER genType factorial(genType const & x)
{
genType Temp = x;
genType Result;
for(Result = 1; Temp > 1; --Temp)
Result *= Temp;
return Result;
}
2010-04-29 10:54:07 +00:00
2011-06-10 15:45:17 +00:00
template <typename valType>
GLM_FUNC_QUALIFIER detail::tvec2<valType> factorial(
detail::tvec2<valType> const & x)
{
return detail::tvec2<valType>(
factorial(x.x),
factorial(x.y));
}
2010-04-29 10:54:07 +00:00
2011-06-10 15:45:17 +00:00
template <typename valType>
GLM_FUNC_QUALIFIER detail::tvec3<valType> factorial(
detail::tvec3<valType> const & x)
{
return detail::tvec3<valType>(
factorial(x.x),
factorial(x.y),
factorial(x.z));
}
2010-04-29 10:54:07 +00:00
2011-06-10 15:45:17 +00:00
template <typename valType>
GLM_FUNC_QUALIFIER detail::tvec4<valType> factorial(
detail::tvec4<valType> const & x)
{
return detail::tvec4<valType>(
factorial(x.x),
factorial(x.y),
factorial(x.z),
factorial(x.w));
}
2010-04-29 10:54:07 +00:00
}//namespace glm