Changeset 89943 in webkit for trunk/Source/JavaScriptCore/wtf/MathExtras.h
- Timestamp:
- Jun 28, 2011, 11:26:06 AM (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/Source/JavaScriptCore/wtf/MathExtras.h
r89705 r89943 208 208 inline float grad2rad(float g) { return g * piFloat / 200.0f; } 209 209 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 211 template<typename T> inline T defaultMinimumForClamp() { return std::numeric_limits<T>::min(); } 212 template<> inline float defaultMinimumForClamp() { return -std::numeric_limits<float>::max(); } 213 template<> inline double defaultMinimumForClamp() { return -std::numeric_limits<double>::max(); } 214 template<typename T> inline T defaultMaximumForClamp() { return std::numeric_limits<T>::max(); } 215 216 template<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 } 224 template<> inline long long int clampTo(double, long long int, long long int); // clampTo does not support long long ints. 225 226 inline int clampToInteger(double value) 227 { 228 return clampTo<int>(value); 229 } 230 231 inline float clampToFloat(double value) 232 { 233 return clampTo<float>(value); 234 } 235 236 inline int clampToPositiveInteger(double value) 237 { 238 return clampTo<int>(value, 0); 239 } 240 241 inline int clampToInteger(float value) 242 { 243 return clampTo<int>(value); 266 244 } 267 245 … … 269 247 { 270 248 const unsigned intMax = static_cast<unsigned>(std::numeric_limits<int>::max()); 271 249 272 250 if (x >= intMax) 273 251 return std::numeric_limits<int>::max();
Note:
See TracChangeset
for help on using the changeset viewer.