Ignore:
Timestamp:
Jun 28, 2011, 11:26:06 AM (14 years ago)
Author:
[email protected]
Message:

2011-06-28 Luke Macpherson <[email protected]>

Reviewed by Darin Adler.

Clean up integer clamping functions in MathExtras.h and support arbitrary numeric types and limits.
https://bugs.webkit.org/show_bug.cgi?id=63469

  • wtf/MathExtras.h: (defaultMinimumForClamp): Version of std::numeric_limits::min() that returns the largest negative value for floating point types. (defaultMaximumForClamp): Symmetric alias for std::numeric_limits::max() (clampTo): New templated clamping function that supports arbitrary output types. (clampToInteger): Use new clampTo template. (clampToFloat): Use new clampTo template. (clampToPositiveInteger): Use new clampTo template.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/JavaScriptCore/wtf/MathExtras.h

    r89705 r89943  
    208208inline float grad2rad(float g) { return g * piFloat / 200.0f; }
    209209
    210 inline int clampToInteger(double x)
    211 {
    212     const double intMax = static_cast<double>(std::numeric_limits<int>::max());
    213     const double intMin = static_cast<double>(std::numeric_limits<int>::min());
    214    
    215     if (x >= intMax)
    216         return std::numeric_limits<int>::max();
    217     if (x <= intMin)
    218         return std::numeric_limits<int>::min();
    219     return static_cast<int>(x);
    220 }
    221 
    222 inline float clampToFloat(double x)
    223 {
    224     const double floatMax = static_cast<double>(std::numeric_limits<float>::max());
    225     const double floatMin = -static_cast<double>(std::numeric_limits<float>::max());
    226    
    227     if (x >= floatMax)
    228         return std::numeric_limits<float>::max();
    229     if (x <= floatMin)
    230         return -std::numeric_limits<float>::max();
    231     return static_cast<float>(x);
    232 }
    233 
    234 inline int clampToPositiveInteger(double x)
    235 {
    236     const double intMax = static_cast<double>(std::numeric_limits<int>::max());
    237    
    238     if (x >= intMax)
    239         return std::numeric_limits<int>::max();
    240     if (x <= 0)
    241         return 0;
    242     return static_cast<int>(x);
    243 }
    244 
    245 inline int clampToInteger(float x)
    246 {
    247     const float intMax = static_cast<float>(std::numeric_limits<int>::max());
    248     const float intMin = static_cast<float>(std::numeric_limits<int>::min());
    249    
    250     if (x >= intMax)
    251         return std::numeric_limits<int>::max();
    252     if (x <= intMin)
    253         return std::numeric_limits<int>::min();
    254     return static_cast<int>(x);
    255 }
    256 
    257 inline int clampToPositiveInteger(float x)
    258 {
    259     const float intMax = static_cast<float>(std::numeric_limits<int>::max());
    260    
    261     if (x >= intMax)
    262         return std::numeric_limits<int>::max();
    263     if (x <= 0)
    264         return 0;
    265     return static_cast<int>(x);
     210// std::numeric_limits<T>::min() returns the smallest positive value for floating point types
     211template<typename T> inline T defaultMinimumForClamp() { return std::numeric_limits<T>::min(); }
     212template<> inline float defaultMinimumForClamp() { return -std::numeric_limits<float>::max(); }
     213template<> inline double defaultMinimumForClamp() { return -std::numeric_limits<double>::max(); }
     214template<typename T> inline T defaultMaximumForClamp() { return std::numeric_limits<T>::max(); }
     215
     216template<typename T> inline T clampTo(double value, T min = defaultMinimumForClamp<T>(), T max = defaultMaximumForClamp<T>())
     217{
     218    if (value >= static_cast<double>(max))
     219        return max;
     220    if (value <= static_cast<double>(min))
     221        return min;
     222    return static_cast<T>(value);
     223}
     224template<> inline long long int clampTo(double, long long int, long long int); // clampTo does not support long long ints.
     225
     226inline int clampToInteger(double value)
     227{
     228    return clampTo<int>(value);
     229}
     230
     231inline float clampToFloat(double value)
     232{
     233    return clampTo<float>(value);
     234}
     235
     236inline int clampToPositiveInteger(double value)
     237{
     238    return clampTo<int>(value, 0);
     239}
     240
     241inline int clampToInteger(float value)
     242{
     243    return clampTo<int>(value);
    266244}
    267245
     
    269247{
    270248    const unsigned intMax = static_cast<unsigned>(std::numeric_limits<int>::max());
    271    
     249
    272250    if (x >= intMax)
    273251        return std::numeric_limits<int>::max();
Note: See TracChangeset for help on using the changeset viewer.