⚠ Archived content — this site is no longer maintained.   Current WebKit documentation is at docs.webkit.org.

Changeset 89943 in webkit


Ignore:
Timestamp:
Jun 28, 2011, 11:26:06 AM (15 years ago)
Author:
macpherson@chromium.org
Message:

2011-06-28 Luke Macpherson <macpherson@chromium.org>

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.
Location:
trunk/Source/JavaScriptCore
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/JavaScriptCore/ChangeLog

    r89927 r89943  
     12011-06-28  Luke Macpherson   <macpherson@chromium.org>
     2
     3        Reviewed by Darin Adler.
     4
     5        Clean up integer clamping functions in MathExtras.h and support arbitrary numeric types and limits.
     6        https://bugs.webkit.org/show_bug.cgi?id=63469
     7
     8        * wtf/MathExtras.h:
     9        (defaultMinimumForClamp):
     10        Version of std::numeric_limits::min() that returns the largest negative value for floating point types.
     11        (defaultMaximumForClamp):
     12        Symmetric alias for std::numeric_limits::max()
     13        (clampTo):
     14        New templated clamping function that supports arbitrary output types.
     15        (clampToInteger):
     16        Use new clampTo template.
     17        (clampToFloat):
     18        Use new clampTo template.
     19        (clampToPositiveInteger):
     20        Use new clampTo template.
     21
    1222011-06-28  Adam Roben  <aroben@apple.com>
    223
  • 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.