diff options
author | mtklein <mtklein@chromium.org> | 2014-09-30 08:08:59 -0700 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2014-09-30 08:08:59 -0700 |
commit | 1b466f7e21004257afeb35cd008768e3b541dca9 (patch) | |
tree | ecf3bf5758f284a96c88c0e7b2ab4fd055da42de /src | |
parent | e8db3ef2a2161084fa8eb1fd1d54c35bfff52d6c (diff) |
Archive SkFloat
https://crrev.com/610153002/ reminded me that it existed, but it's entirely
unused and made moot by hardware FP. Might as well trim a few K off libskia.
BUG=skia:
R=reed@google.com, mtklein@google.com, tfarina@chromium.org
Author: mtklein@chromium.org
Review URL: https://codereview.chromium.org/619563003
Diffstat (limited to 'src')
-rw-r--r-- | src/core/SkFloat.cpp | 387 | ||||
-rw-r--r-- | src/core/SkFloat.h | 107 | ||||
-rw-r--r-- | src/core/SkGraphics.cpp | 1 |
3 files changed, 0 insertions, 495 deletions
diff --git a/src/core/SkFloat.cpp b/src/core/SkFloat.cpp deleted file mode 100644 index 9ec0a7a081..0000000000 --- a/src/core/SkFloat.cpp +++ /dev/null @@ -1,387 +0,0 @@ - -/* - * 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. - */ - - -#include "SkFloat.h" -#include "SkMathPriv.h" - -#define EXP_BIAS (127+23) - -static int get_unsigned_exp(uint32_t packed) -{ - return (packed << 1 >> 24); -} - -static unsigned get_unsigned_value(uint32_t packed) -{ - return (packed << 9 >> 9) | (1 << 23); -} - -static int get_signed_value(int32_t packed) -{ - return SkApplySign(get_unsigned_value(packed), SkExtractSign(packed)); -} - -///////////////////////////////////////////////////////////////////////// - -int SkFloat::GetShift(int32_t packed, int shift) -{ - if (packed == 0) - return 0; - - int exp = get_unsigned_exp(packed) - EXP_BIAS - shift; - int value = get_unsigned_value(packed); - - if (exp >= 0) - { - if (exp > 8) // overflow - value = SK_MaxS32; - else - value <<= exp; - } - else - { - exp = -exp; - if (exp > 23) // underflow - value = 0; - else - value >>= exp; - } - return SkApplySign(value, SkExtractSign(packed)); -} - -///////////////////////////////////////////////////////////////////////////////////// - -int32_t SkFloat::SetShift(int value, int shift) -{ - if (value == 0) - return 0; - - // 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); - SkASSERT(bias > 0 && bias < 8); - value >>= bias; - 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); - - shift += EXP_BIAS; - if (shift < 0) // underflow - return 0; - else - { - if (shift > 255) // overflow - { - shift = 255; - value = 0x00FFFFFF; - } - int32_t packed = sign << 31; // set the sign-bit - packed |= shift << 23; // store the packed exponent - packed |= ((unsigned)(value << 9) >> 9); // clear 24th bit of value (its implied) - -#ifdef SK_DEBUG - { - int n; - - n = SkExtractSign(packed); - SkASSERT(n == sign); - n = get_unsigned_exp(packed); - SkASSERT(n == shift); - n = get_unsigned_value(packed); - SkASSERT(n == value); - } -#endif - return packed; - } -} - -int32_t SkFloat::Neg(int32_t packed) -{ - if (packed) - packed = packed ^ (1 << 31); - return packed; -} - -int32_t SkFloat::Add(int32_t packed_a, int32_t packed_b) -{ - if (packed_a == 0) - return packed_b; - if (packed_b == 0) - return packed_a; - - int exp_a = get_unsigned_exp(packed_a); - int exp_b = get_unsigned_exp(packed_b); - int exp_diff = exp_a - exp_b; - - int shift_a = 0, shift_b = 0; - int exp; - - if (exp_diff >= 0) - { - if (exp_diff > 24) // B is too small to contribute - return packed_a; - shift_b = exp_diff; - exp = exp_a; - } - else - { - exp_diff = -exp_diff; - if (exp_diff > 24) // A is too small to contribute - return packed_b; - shift_a = exp_diff; - exp = exp_b; - } - - int value_a = get_signed_value(packed_a) >> shift_a; - int value_b = get_signed_value(packed_b) >> shift_b; - - return SkFloat::SetShift(value_a + value_b, exp - EXP_BIAS); -} - -static inline int32_t mul24(int32_t a, int32_t b) { - int64_t tmp = (sk_64_mul(a, b) + (1 << 23)) >> 24; - return sk_64_asS32(tmp); -} - -int32_t SkFloat::Mul(int32_t packed_a, int32_t packed_b) -{ - if (packed_a == 0 || packed_b == 0) - return 0; - - int exp_a = get_unsigned_exp(packed_a); - int exp_b = get_unsigned_exp(packed_b); - - int value_a = get_signed_value(packed_a); - int value_b = get_signed_value(packed_b); - - return SkFloat::SetShift(mul24(value_a, value_b), exp_a + exp_b - 2*EXP_BIAS + 24); -} - -int32_t SkFloat::MulInt(int32_t packed, int n) -{ - return Mul(packed, SetShift(n, 0)); -} - -int32_t SkFloat::Div(int32_t packed_n, int32_t packed_d) -{ - SkASSERT(packed_d != 0); - - if (packed_n == 0) - return 0; - - int exp_n = get_unsigned_exp(packed_n); - int exp_d = get_unsigned_exp(packed_d); - - int value_n = get_signed_value(packed_n); - int value_d = get_signed_value(packed_d); - - return SkFloat::SetShift(SkDivBits(value_n, value_d, 24), exp_n - exp_d - 24); -} - -int32_t SkFloat::DivInt(int32_t packed, int n) -{ - return Div(packed, SetShift(n, 0)); -} - -int32_t SkFloat::Invert(int32_t packed) -{ - return Div(packed, SetShift(1, 0)); -} - -int32_t SkFloat::Sqrt(int32_t packed) -{ - if (packed < 0) - { - SkDEBUGFAIL("can't sqrt a negative number"); - return 0; - } - - int exp = get_unsigned_exp(packed); - int value = get_unsigned_value(packed); - - int nexp = exp - EXP_BIAS; - int root = SkSqrtBits(value << (nexp & 1), 26); - nexp >>= 1; - return SkFloat::SetShift(root, nexp - 11); -} - -#if defined _WIN32 && _MSC_VER >= 1300 // disable warning : unreachable code -#pragma warning ( push ) -#pragma warning ( disable : 4702 ) -#endif - -int32_t SkFloat::CubeRoot(int32_t packed) -{ - sk_throw(); - return 0; -} - -#if defined _WIN32 && _MSC_VER >= 1300 -#pragma warning ( pop ) -#endif - -static inline int32_t clear_high_bit(int32_t n) -{ - return ((uint32_t)(n << 1)) >> 1; -} - -static inline int int_sign(int32_t a, int32_t b) -{ - return a > b ? 1 : (a < b ? -1 : 0); -} - -int SkFloat::Cmp(int32_t packed_a, int32_t packed_b) -{ - packed_a = SkApplySign(clear_high_bit(packed_a), SkExtractSign(packed_a)); - packed_b = SkApplySign(clear_high_bit(packed_b), SkExtractSign(packed_b)); - - return int_sign(packed_a, packed_b); -} - -///////////////////////////////////////////////////////////////////////////////////// -///////////////////////////////////////////////////////////////////////////////////// - -#ifdef SK_DEBUG - -#include "SkRandom.h" -#include "SkFloatingPoint.h" - -void SkFloat::UnitTest() -{ -#if 0 // def SK_SUPPORT_UNITTEST - SkFloat a, b, c, d; - int n; - - a.setZero(); - n = a.getInt(); - SkASSERT(n == 0); - - b.setInt(5); - n = b.getInt(); - SkASSERT(n == 5); - - c.setInt(-3); - n = c.getInt(); - SkASSERT(n == -3); - - d.setAdd(c, b); - SkDebugf("SkFloat: %d + %d = %d\n", c.getInt(), b.getInt(), d.getInt()); - - SkRandom rand; - - int i; - for (i = 0; i < 1000; i++) - { - float fa, fb; - int aa = rand.nextS() >> 14; - int bb = rand.nextS() >> 14; - a.setInt(aa); - b.setInt(bb); - SkASSERT(a.getInt() == aa); - SkASSERT(b.getInt() == bb); - - c.setAdd(a, b); - int cc = c.getInt(); - SkASSERT(cc == aa + bb); - - c.setSub(a, b); - cc = c.getInt(); - SkASSERT(cc == aa - bb); - - aa >>= 5; - bb >>= 5; - a.setInt(aa); - b.setInt(bb); - c.setMul(a, b); - cc = c.getInt(); - SkASSERT(cc == aa * bb); - ///////////////////////////////////// - - aa = rand.nextS() >> 11; - a.setFixed(aa); - cc = a.getFixed(); - SkASSERT(aa == cc); - - bb = rand.nextS() >> 11; - b.setFixed(bb); - cc = b.getFixed(); - SkASSERT(bb == cc); - - cc = SkFixedMul(aa, bb); - c.setMul(a, b); - SkFixed dd = c.getFixed(); - int diff = cc - dd; - SkASSERT(SkAbs32(diff) <= 1); - - fa = (float)aa / 65536.0f; - fb = (float)bb / 65536.0f; - a.assertEquals(fa); - b.assertEquals(fb); - fa = a.getFloat(); - fb = b.getFloat(); - - c.assertEquals(fa * fb, 1); - - c.setDiv(a, b); - cc = SkFixedDiv(aa, bb); - dd = c.getFixed(); - diff = cc - dd; - SkASSERT(SkAbs32(diff) <= 3); - - c.assertEquals(fa / fb, 1); - - SkASSERT((aa == bb) == (a == b)); - SkASSERT((aa != bb) == (a != b)); - SkASSERT((aa < bb) == (a < b)); - SkASSERT((aa <= bb) == (a <= b)); - SkASSERT((aa > bb) == (a > b)); - SkASSERT((aa >= bb) == (a >= b)); - - if (aa < 0) - { - aa = -aa; - fa = -fa; - } - a.setFixed(aa); - c.setSqrt(a); - cc = SkFixedSqrt(aa); - dd = c.getFixed(); - SkASSERT(dd == cc); - - c.assertEquals(sk_float_sqrt(fa), 2); - - // cuberoot -#if 0 - a.setInt(1); - a.cubeRoot(); - a.assertEquals(1.0f, 0); - a.setInt(8); - a.cubeRoot(); - a.assertEquals(2.0f, 0); - a.setInt(27); - a.cubeRoot(); - a.assertEquals(3.0f, 0); -#endif - } -#endif -} - -#endif diff --git a/src/core/SkFloat.h b/src/core/SkFloat.h deleted file mode 100644 index 0d4f9b41a7..0000000000 --- a/src/core/SkFloat.h +++ /dev/null @@ -1,107 +0,0 @@ - -/* - * 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 SkFloat_DEFINED -#define SkFloat_DEFINED - -#include "SkFixed.h" - -class SkFloat { -public: - SkFloat() {} - - void setZero() { fPacked = 0; } -// void setShift(int value, int shift) { fPacked = SetShift(value, shift); } - void setInt(int value) { fPacked = SetShift(value, 0); } - void setFixed(SkFixed value) { fPacked = SetShift(value, -16); } - -// int getShift(int shift) const { return GetShift(fPacked, shift); } - int getInt() const { return GetShift(fPacked, 0); } - SkFixed getFixed() const { return GetShift(fPacked, -16); } - - void abs() { fPacked = Abs(fPacked); } - void negate() { fPacked = Neg(fPacked); } - - void shiftLeft(int bits) { fPacked = Shift(fPacked, bits); } - void setShiftLeft(const SkFloat& a, int bits) { fPacked = Shift(a.fPacked, bits); } - - void shiftRight(int bits) { fPacked = Shift(fPacked, -bits); } - void setShiftRight(const SkFloat& a, int bits) { fPacked = Shift(a.fPacked, -bits); } - - void add(const SkFloat& a) { fPacked = Add(fPacked, a.fPacked); } - void setAdd(const SkFloat& a, const SkFloat& b) { fPacked = Add(a.fPacked, b.fPacked); } - - void sub(const SkFloat& a) { fPacked = Add(fPacked, Neg(a.fPacked)); } - void setSub(const SkFloat& a, const SkFloat& b) { fPacked = Add(a.fPacked, Neg(b.fPacked)); } - - void mul(const SkFloat& a) { fPacked = Mul(fPacked, a.fPacked); } - void setMul(const SkFloat& a, const SkFloat& b) { fPacked = Mul(a.fPacked, b.fPacked); } - - void div(const SkFloat& a) { fPacked = Div(fPacked, a.fPacked); } - void setDiv(const SkFloat& a, const SkFloat& b) { fPacked = Div(a.fPacked, b.fPacked); } - - void sqrt() { fPacked = Sqrt(fPacked); } - void setSqrt(const SkFloat& a) { fPacked = Sqrt(a.fPacked); } - void cubeRoot() { fPacked = CubeRoot(fPacked); } - void setCubeRoot(const SkFloat& a) { fPacked = CubeRoot(a.fPacked); } - - friend bool operator==(const SkFloat& a, const SkFloat& b) { return a.fPacked == b.fPacked; } - friend bool operator!=(const SkFloat& a, const SkFloat& b) { return a.fPacked != b.fPacked; } - friend bool operator<(const SkFloat& a, const SkFloat& b) { return Cmp(a.fPacked, b.fPacked) < 0; } - friend bool operator<=(const SkFloat& a, const SkFloat& b) { return Cmp(a.fPacked, b.fPacked) <= 0; } - friend bool operator>(const SkFloat& a, const SkFloat& b) { return Cmp(a.fPacked, b.fPacked) > 0; } - friend bool operator>=(const SkFloat& a, const SkFloat& b) { return Cmp(a.fPacked, b.fPacked) >= 0; } - -#ifdef SK_DEBUG - static void UnitTest(); - - void assertEquals(float f, int tolerance = 0) - { - union { - float fFloat; - int32_t fPacked; - } tmp; - - tmp.fFloat = f; - int d = tmp.fPacked - fPacked; - SkASSERT(SkAbs32(d) <= tolerance); - } - float getFloat() const - { - union { - float fFloat; - int32_t fPacked; - } tmp; - - tmp.fPacked = fPacked; - return tmp.fFloat; - } -#endif - -private: - int32_t fPacked; - -public: - static int GetShift(int32_t packed, int shift); - static int32_t SetShift(int value, int shift); - static int32_t Neg(int32_t); - static int32_t Abs(int32_t packed) { return (uint32_t)(packed << 1) >> 1; } - static int32_t Shift(int32_t, int bits); - static int32_t Add(int32_t, int32_t); - static int32_t Mul(int32_t, int32_t); - static int32_t MulInt(int32_t, int); - static int32_t Div(int32_t, int32_t); - static int32_t DivInt(int32_t, int); - static int32_t Invert(int32_t); - static int32_t Sqrt(int32_t); - static int32_t CubeRoot(int32_t); - static int Cmp(int32_t, int32_t); -}; - -#endif diff --git a/src/core/SkGraphics.cpp b/src/core/SkGraphics.cpp index 2256e7f1b6..7acee85c4d 100644 --- a/src/core/SkGraphics.cpp +++ b/src/core/SkGraphics.cpp @@ -11,7 +11,6 @@ #include "SkBlitter.h" #include "SkCanvas.h" -#include "SkFloat.h" #include "SkGeometry.h" #include "SkMath.h" #include "SkMatrix.h" |