aboutsummaryrefslogtreecommitdiffhomepage
path: root/src
diff options
context:
space:
mode:
authorGravatar mtklein <mtklein@chromium.org>2016-05-27 10:47:31 -0700
committerGravatar Commit bot <commit-bot@chromium.org>2016-05-27 10:47:32 -0700
commit785a5b941a4d74a1f272729fe8dca55c6eda6bb8 (patch)
tree895897fb08b687abaac95e9be208272db32e4342 /src
parent78d58d1084f0390c1c0f9123ac6e48efcd226f39 (diff)
Clean up SkFloatBits
- 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
Diffstat (limited to 'src')
-rw-r--r--src/core/SkFloatBits.cpp205
1 files changed, 0 insertions, 205 deletions
diff --git a/src/core/SkFloatBits.cpp b/src/core/SkFloatBits.cpp
deleted file mode 100644
index ea705513ac..0000000000
--- a/src/core/SkFloatBits.cpp
+++ /dev/null
@@ -1,205 +0,0 @@
-/*
- * Copyright 2011 Google Inc.
- *
- * Use of this source code is governed by a BSD-style license that can be
- * found in the LICENSE file.
- */
-
-#include "SkFloatBits.h"
-#include "SkMathPriv.h"
-
-/******************************************************************************
- SkFloatBits_toInt[Floor, Round, Ceil] are identical except for what they
- do right before they return ... >> exp;
- Floor - adds nothing
- Round - adds 1 << (exp - 1)
- Ceil - adds (1 << exp) - 1
-
- Floor and Cast are very similar, but Cast applies its sign after all other
- computations on value. Also, Cast does not need to check for negative zero,
- as that value (0x80000000) "does the right thing" for Ceil. Note that it
- doesn't for Floor/Round/Ceil, hence the explicit check.
-******************************************************************************/
-
-#define EXP_BIAS (127+23)
-#define MATISSA_MAGIC_BIG (1 << 23)
-
-static inline int unpack_exp(uint32_t packed) {
- return (packed << 1 >> 24);
-}
-
-#if 0
-// the ARM compiler generates an extra BIC, so I use the dirty version instead
-static inline int unpack_matissa(uint32_t packed) {
- // we could mask with 0x7FFFFF, but that is harder for ARM to encode
- return (packed & ~0xFF000000) | MATISSA_MAGIC_BIG;
-}
-#endif
-
-// returns the low 24-bits, so we need to OR in the magic_bit afterwards
-static inline int unpack_matissa_dirty(uint32_t packed) {
- return packed & ~0xFF000000;
-}
-
-// same as (int)float
-int32_t SkFloatBits_toIntCast(int32_t packed) {
- int exp = unpack_exp(packed) - EXP_BIAS;
- int value = unpack_matissa_dirty(packed) | MATISSA_MAGIC_BIG;
-
- if (exp >= 0) {
- if (exp > 7) { // overflow
- value = SK_MaxS32;
- } else {
- value <<= exp;
- }
- } else {
- exp = -exp;
- if (exp > 25) { // underflow
- exp = 25;
- }
- value >>= exp;
- }
- return SkApplySign(value, SkExtractSign(packed));
-}
-
-// same as (int)floor(float)
-int32_t SkFloatBits_toIntFloor(int32_t packed) {
- // curse you negative 0
- if (SkLeftShift(packed, 1) == 0) {
- return 0;
- }
-
- int exp = unpack_exp(packed) - EXP_BIAS;
- int value = unpack_matissa_dirty(packed) | MATISSA_MAGIC_BIG;
-
- if (exp >= 0) {
- if (exp > 7) { // overflow
- value = SK_MaxS32;
- } else {
- value <<= exp;
- }
- // apply the sign after we check for overflow
- return SkApplySign(value, SkExtractSign(packed));
- } else {
- // apply the sign before we right-shift
- value = SkApplySign(value, SkExtractSign(packed));
- exp = -exp;
- if (exp > 25) { // underflow
-#ifdef SK_CPU_FLUSH_TO_ZERO
- // The iOS ARM processor discards small denormalized numbers to go faster.
- // The comparision below empirically causes the result to agree with the
- // tests in MathTest test_float_floor
- if (exp > 149) {
- return 0;
- }
-#else
- exp = 25;
-#endif
- }
- // int add = 0;
- return value >> exp;
- }
-}
-
-// same as (int)floor(float + 0.5)
-int32_t SkFloatBits_toIntRound(int32_t packed) {
- // curse you negative 0
- if (SkLeftShift(packed, 1) == 0) {
- return 0;
- }
-
- int exp = unpack_exp(packed) - EXP_BIAS;
- int value = unpack_matissa_dirty(packed) | MATISSA_MAGIC_BIG;
-
- if (exp >= 0) {
- if (exp > 7) { // overflow
- value = SK_MaxS32;
- } else {
- value <<= exp;
- }
- // apply the sign after we check for overflow
- return SkApplySign(value, SkExtractSign(packed));
- } else {
- // apply the sign before we right-shift
- value = SkApplySign(value, SkExtractSign(packed));
- exp = -exp;
- if (exp > 25) { // underflow
- exp = 25;
- }
- int add = 1 << (exp - 1);
- return (value + add) >> exp;
- }
-}
-
-// same as (int)ceil(float)
-int32_t SkFloatBits_toIntCeil(int32_t packed) {
- // curse you negative 0
- if (SkLeftShift(packed, 1) == 0) {
- return 0;
- }
-
- int exp = unpack_exp(packed) - EXP_BIAS;
- int value = unpack_matissa_dirty(packed) | MATISSA_MAGIC_BIG;
-
- if (exp >= 0) {
- if (exp > 7) { // overflow
- value = SK_MaxS32;
- } else {
- value <<= exp;
- }
- // apply the sign after we check for overflow
- return SkApplySign(value, SkExtractSign(packed));
- } else {
- // apply the sign before we right-shift
- value = SkApplySign(value, SkExtractSign(packed));
- exp = -exp;
- if (exp > 25) { // underflow
-#ifdef SK_CPU_FLUSH_TO_ZERO
- // The iOS ARM processor discards small denormalized numbers to go faster.
- // The comparision below empirically causes the result to agree with the
- // tests in MathTest test_float_ceil
- if (exp > 149) {
- return 0;
- }
- return 0 < value;
-#else
- exp = 25;
-#endif
- }
- int add = (1 << exp) - 1;
- return (value + add) >> exp;
- }
-}
-
-float SkIntToFloatCast(int32_t value) {
- if (0 == value) {
- return 0;
- }
-
- int shift = EXP_BIAS;
-
- // record the sign and make value positive
- int sign = SkExtractSign(value);
- value = SkApplySign(value, sign);
-
- if (value >> 24) { // value is too big (has more than 24 bits set)
- int bias = 8 - SkCLZ(value);
- SkDebugf("value = %d, bias = %d\n", value, bias);
- SkASSERT(bias > 0 && bias < 8);
- value >>= bias; // need to round?
- shift += bias;
- } else {
- int zeros = SkCLZ(value << 8);
- SkASSERT(zeros >= 0 && zeros <= 23);
- value <<= zeros;
- shift -= zeros;
- }
-
- // now value is left-aligned to 24 bits
- SkASSERT((value >> 23) == 1);
- SkASSERT(shift >= 0 && shift <= 255);
-
- SkFloatIntUnion data;
- data.fSignBitInt = SkLeftShift(sign, 31) | SkLeftShift(shift, 23) | (value & ~MATISSA_MAGIC_BIG);
- return data.fFloat;
-}