Title: [89943] trunk/Source/_javascript_Core
Revision
89943
Author
[email protected]
Date
2011-06-28 11:26:06 -0700 (Tue, 28 Jun 2011)

Log 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.

Modified Paths

Diff

Modified: trunk/Source/_javascript_Core/ChangeLog (89942 => 89943)


--- trunk/Source/_javascript_Core/ChangeLog	2011-06-28 18:24:06 UTC (rev 89942)
+++ trunk/Source/_javascript_Core/ChangeLog	2011-06-28 18:26:06 UTC (rev 89943)
@@ -1,3 +1,24 @@
+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.
+
 2011-06-28  Adam Roben  <[email protected]>
 
         Windows Debug build fix after r89885

Modified: trunk/Source/_javascript_Core/wtf/MathExtras.h (89942 => 89943)


--- trunk/Source/_javascript_Core/wtf/MathExtras.h	2011-06-28 18:24:06 UTC (rev 89942)
+++ trunk/Source/_javascript_Core/wtf/MathExtras.h	2011-06-28 18:26:06 UTC (rev 89943)
@@ -207,68 +207,46 @@
 inline float rad2grad(float r) { return r * 200.0f / piFloat; }
 inline float grad2rad(float g) { return g * piFloat / 200.0f; }
 
-inline int clampToInteger(double x)
+// std::numeric_limits<T>::min() returns the smallest positive value for floating point types
+template<typename T> inline T defaultMinimumForClamp() { return std::numeric_limits<T>::min(); }
+template<> inline float defaultMinimumForClamp() { return -std::numeric_limits<float>::max(); }
+template<> inline double defaultMinimumForClamp() { return -std::numeric_limits<double>::max(); }
+template<typename T> inline T defaultMaximumForClamp() { return std::numeric_limits<T>::max(); }
+
+template<typename T> inline T clampTo(double value, T min = defaultMinimumForClamp<T>(), T max = defaultMaximumForClamp<T>())
 {
-    const double intMax = static_cast<double>(std::numeric_limits<int>::max());
-    const double intMin = static_cast<double>(std::numeric_limits<int>::min());
-    
-    if (x >= intMax)
-        return std::numeric_limits<int>::max();
-    if (x <= intMin)
-        return std::numeric_limits<int>::min();
-    return static_cast<int>(x);
+    if (value >= static_cast<double>(max))
+        return max;
+    if (value <= static_cast<double>(min))
+        return min;
+    return static_cast<T>(value);
 }
+template<> inline long long int clampTo(double, long long int, long long int); // clampTo does not support long long ints.
 
-inline float clampToFloat(double x)
+inline int clampToInteger(double value)
 {
-    const double floatMax = static_cast<double>(std::numeric_limits<float>::max());
-    const double floatMin = -static_cast<double>(std::numeric_limits<float>::max());
-    
-    if (x >= floatMax)
-        return std::numeric_limits<float>::max();
-    if (x <= floatMin)
-        return -std::numeric_limits<float>::max();
-    return static_cast<float>(x);
+    return clampTo<int>(value);
 }
 
-inline int clampToPositiveInteger(double x)
+inline float clampToFloat(double value)
 {
-    const double intMax = static_cast<double>(std::numeric_limits<int>::max());
-    
-    if (x >= intMax)
-        return std::numeric_limits<int>::max();
-    if (x <= 0)
-        return 0;
-    return static_cast<int>(x);
+    return clampTo<float>(value);
 }
 
-inline int clampToInteger(float x)
+inline int clampToPositiveInteger(double value)
 {
-    const float intMax = static_cast<float>(std::numeric_limits<int>::max());
-    const float intMin = static_cast<float>(std::numeric_limits<int>::min());
-    
-    if (x >= intMax)
-        return std::numeric_limits<int>::max();
-    if (x <= intMin)
-        return std::numeric_limits<int>::min();
-    return static_cast<int>(x);
+    return clampTo<int>(value, 0);
 }
 
-inline int clampToPositiveInteger(float x)
+inline int clampToInteger(float value)
 {
-    const float intMax = static_cast<float>(std::numeric_limits<int>::max());
-    
-    if (x >= intMax)
-        return std::numeric_limits<int>::max();
-    if (x <= 0)
-        return 0;
-    return static_cast<int>(x);
+    return clampTo<int>(value);
 }
 
 inline int clampToInteger(unsigned x)
 {
     const unsigned intMax = static_cast<unsigned>(std::numeric_limits<int>::max());
-    
+
     if (x >= intMax)
         return std::numeric_limits<int>::max();
     return static_cast<int>(x);
_______________________________________________
webkit-changes mailing list
[email protected]
http://lists.webkit.org/mailman/listinfo.cgi/webkit-changes

Reply via email to