785a5b941a
- remove dead code - rewrite float -> int converters The strategy for the new converters is: - convert input to double - floor/ceil/round in double space - pin that double to [SK_MinS32, SK_MaxS32] - truncate that double to int32_t This simpler strategy does not work: - floor/ceil/round in float space - pin that float to [SK_MinS32, SK_MaxS32] - truncate that float to int32_t SK_MinS32 and SK_MaxS32 are not representable as floats: they round to the nearest float, ±2^31, which makes the pin insufficient for floats near SK_MinS32 (-2^31+1) or SK_MaxS32 (+2^31-1). float only has 24 bits of precision, and we need 31. double can represent all integers up to 50-something bits. An alternative is to pin in float to ±2147483520, the last exactly representable float before SK_MaxS32 (127 too small). Our tests test that we round as floor(x+0.5), which can return different numbers than round(x) for negative x. So this CL explicitly uses floor(x+0.5). I've updated the tests with ±inf and ±NaN, and tried to make them a little clearer, especially using SK_MinS32 instead of -SK_MaxS32. I have not timed anything here. I have never seen any of these methods in a profile. BUG=skia: GOLD_TRYBOT_URL= https://gold.skia.org/search?issue=2012333003 Review-Url: https://codereview.chromium.org/2012333003
105 lines
2.9 KiB
C
105 lines
2.9 KiB
C
/*
|
|
* Copyright 2008 The Android Open Source Project
|
|
*
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
* found in the LICENSE file.
|
|
*/
|
|
|
|
|
|
#ifndef SkFloatBits_DEFINED
|
|
#define SkFloatBits_DEFINED
|
|
|
|
#include "SkTypes.h"
|
|
#include <math.h>
|
|
|
|
/** Convert a sign-bit int (i.e. float interpreted as int) into a 2s compliement
|
|
int. This also converts -0 (0x80000000) to 0. Doing this to a float allows
|
|
it to be compared using normal C operators (<, <=, etc.)
|
|
*/
|
|
static inline int32_t SkSignBitTo2sCompliment(int32_t x) {
|
|
if (x < 0) {
|
|
x &= 0x7FFFFFFF;
|
|
x = -x;
|
|
}
|
|
return x;
|
|
}
|
|
|
|
/** Convert a 2s compliment int to a sign-bit (i.e. int interpreted as float).
|
|
This undoes the result of SkSignBitTo2sCompliment().
|
|
*/
|
|
static inline int32_t Sk2sComplimentToSignBit(int32_t x) {
|
|
int sign = x >> 31;
|
|
// make x positive
|
|
x = (x ^ sign) - sign;
|
|
// set the sign bit as needed
|
|
x |= SkLeftShift(sign, 31);
|
|
return x;
|
|
}
|
|
|
|
union SkFloatIntUnion {
|
|
float fFloat;
|
|
int32_t fSignBitInt;
|
|
};
|
|
|
|
// Helper to see a float as its bit pattern (w/o aliasing warnings)
|
|
static inline int32_t SkFloat2Bits(float x) {
|
|
SkFloatIntUnion data;
|
|
data.fFloat = x;
|
|
return data.fSignBitInt;
|
|
}
|
|
|
|
// Helper to see a bit pattern as a float (w/o aliasing warnings)
|
|
static inline float SkBits2Float(int32_t floatAsBits) {
|
|
SkFloatIntUnion data;
|
|
data.fSignBitInt = floatAsBits;
|
|
return data.fFloat;
|
|
}
|
|
|
|
/** Return the float as a 2s compliment int. Just to be used to compare floats
|
|
to each other or against positive float-bit-constants (like 0). This does
|
|
not return the int equivalent of the float, just something cheaper for
|
|
compares-only.
|
|
*/
|
|
static inline int32_t SkFloatAs2sCompliment(float x) {
|
|
return SkSignBitTo2sCompliment(SkFloat2Bits(x));
|
|
}
|
|
|
|
/** Return the 2s compliment int as a float. This undos the result of
|
|
SkFloatAs2sCompliment
|
|
*/
|
|
static inline float Sk2sComplimentAsFloat(int32_t x) {
|
|
return SkBits2Float(Sk2sComplimentToSignBit(x));
|
|
}
|
|
|
|
static inline int32_t pin_double_to_int(double x) {
|
|
return (int32_t)SkTPin<double>(x, SK_MinS32, SK_MaxS32);
|
|
}
|
|
|
|
/** Return the floor of the float as an int.
|
|
If the value is out of range, or NaN, return +/- SK_MaxS32
|
|
*/
|
|
static inline int32_t SkFloatToIntFloor(float x) {
|
|
return pin_double_to_int(floor(x));
|
|
}
|
|
|
|
/** Return the float rounded to an int.
|
|
If the value is out of range, or NaN, return +/- SK_MaxS32
|
|
*/
|
|
static inline int32_t SkFloatToIntRound(float x) {
|
|
return pin_double_to_int(floor((double)x + 0.5));
|
|
}
|
|
|
|
/** Return the ceiling of the float as an int.
|
|
If the value is out of range, or NaN, return +/- SK_MaxS32
|
|
*/
|
|
static inline int32_t SkFloatToIntCeil(float x) {
|
|
return pin_double_to_int(ceil(x));
|
|
}
|
|
|
|
// Scalar wrappers for float-bit routines
|
|
|
|
#define SkScalarAs2sCompliment(x) SkFloatAs2sCompliment(x)
|
|
#define Sk2sComplimentAsScalar(x) Sk2sComplimentAsFloat(x)
|
|
|
|
#endif
|