gfx/skia/trunk/src/utils/SkFloatUtils.h

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

michael@0 1 /*
michael@0 2 * Copyright 2012 Google Inc.
michael@0 3 *
michael@0 4 * Use of this source code is governed by a BSD-style license that can be
michael@0 5 * found in the LICENSE file.
michael@0 6 */
michael@0 7
michael@0 8 #ifndef SkFloatUtils_DEFINED
michael@0 9 #define SkFloatUtils_DEFINED
michael@0 10
michael@0 11 #include "SkTypes.h"
michael@0 12 #include <limits.h>
michael@0 13 #include <float.h>
michael@0 14
michael@0 15 template <size_t size>
michael@0 16 class SkTypeWithSize {
michael@0 17 public:
michael@0 18 // Prevents using SkTypeWithSize<N> with non-specialized N.
michael@0 19 typedef void UInt;
michael@0 20 };
michael@0 21
michael@0 22 template <>
michael@0 23 class SkTypeWithSize<32> {
michael@0 24 public:
michael@0 25 typedef uint32_t UInt;
michael@0 26 };
michael@0 27
michael@0 28 template <>
michael@0 29 class SkTypeWithSize<64> {
michael@0 30 public:
michael@0 31 typedef uint64_t UInt;
michael@0 32 };
michael@0 33
michael@0 34 template <typename RawType>
michael@0 35 struct SkNumericLimits {
michael@0 36 static const int digits = 0;
michael@0 37 };
michael@0 38
michael@0 39 template <>
michael@0 40 struct SkNumericLimits<double> {
michael@0 41 static const int digits = DBL_MANT_DIG;
michael@0 42 };
michael@0 43
michael@0 44 template <>
michael@0 45 struct SkNumericLimits<float> {
michael@0 46 static const int digits = FLT_MANT_DIG;
michael@0 47 };
michael@0 48
michael@0 49 //See
michael@0 50 //http://stackoverflow.com/questions/17333/most-effective-way-for-float-and-double-comparison/3423299#3423299
michael@0 51 //http://code.google.com/p/googletest/source/browse/trunk/include/gtest/internal/gtest-internal.h
michael@0 52 //http://www.cygnus-software.com/papers/comparingfloats/comparingfloats.htm
michael@0 53
michael@0 54 template <typename RawType, unsigned int ULPs>
michael@0 55 class SkFloatingPoint {
michael@0 56 public:
michael@0 57 /** Bits is a unsigned integer the same size as the floating point number. */
michael@0 58 typedef typename SkTypeWithSize<sizeof(RawType) * CHAR_BIT>::UInt Bits;
michael@0 59
michael@0 60 /** # of bits in a number. */
michael@0 61 static const size_t kBitCount = CHAR_BIT * sizeof(RawType);
michael@0 62
michael@0 63 /** # of fraction bits in a number. */
michael@0 64 static const size_t kFractionBitCount = SkNumericLimits<RawType>::digits - 1;
michael@0 65
michael@0 66 /** # of exponent bits in a number. */
michael@0 67 static const size_t kExponentBitCount = kBitCount - 1 - kFractionBitCount;
michael@0 68
michael@0 69 /** The mask for the sign bit. */
michael@0 70 static const Bits kSignBitMask = static_cast<Bits>(1) << (kBitCount - 1);
michael@0 71
michael@0 72 /** The mask for the fraction bits. */
michael@0 73 static const Bits kFractionBitMask =
michael@0 74 ~static_cast<Bits>(0) >> (kExponentBitCount + 1);
michael@0 75
michael@0 76 /** The mask for the exponent bits. */
michael@0 77 static const Bits kExponentBitMask = ~(kSignBitMask | kFractionBitMask);
michael@0 78
michael@0 79 /** How many ULP's (Units in the Last Place) to tolerate when comparing. */
michael@0 80 static const size_t kMaxUlps = ULPs;
michael@0 81
michael@0 82 /**
michael@0 83 * Constructs a FloatingPoint from a raw floating-point number.
michael@0 84 *
michael@0 85 * On an Intel CPU, passing a non-normalized NAN (Not a Number)
michael@0 86 * around may change its bits, although the new value is guaranteed
michael@0 87 * to be also a NAN. Therefore, don't expect this constructor to
michael@0 88 * preserve the bits in x when x is a NAN.
michael@0 89 */
michael@0 90 explicit SkFloatingPoint(const RawType& x) { fU.value = x; }
michael@0 91
michael@0 92 /** Returns the exponent bits of this number. */
michael@0 93 Bits exponent_bits() const { return kExponentBitMask & fU.bits; }
michael@0 94
michael@0 95 /** Returns the fraction bits of this number. */
michael@0 96 Bits fraction_bits() const { return kFractionBitMask & fU.bits; }
michael@0 97
michael@0 98 /** Returns true iff this is NAN (not a number). */
michael@0 99 bool is_nan() const {
michael@0 100 // It's a NAN if both of the folloowing are true:
michael@0 101 // * the exponent bits are all ones
michael@0 102 // * the fraction bits are not all zero.
michael@0 103 return (exponent_bits() == kExponentBitMask) && (fraction_bits() != 0);
michael@0 104 }
michael@0 105
michael@0 106 /**
michael@0 107 * Returns true iff this number is at most kMaxUlps ULP's away from ths.
michael@0 108 * In particular, this function:
michael@0 109 * - returns false if either number is (or both are) NAN.
michael@0 110 * - treats really large numbers as almost equal to infinity.
michael@0 111 * - thinks +0.0 and -0.0 are 0 DLP's apart.
michael@0 112 */
michael@0 113 bool AlmostEquals(const SkFloatingPoint& rhs) const {
michael@0 114 // Any comparison operation involving a NAN must return false.
michael@0 115 if (is_nan() || rhs.is_nan()) return false;
michael@0 116
michael@0 117 const Bits dist = DistanceBetweenSignAndMagnitudeNumbers(fU.bits,
michael@0 118 rhs.fU.bits);
michael@0 119 //SkDEBUGF(("(%f, %f, %d) ", u_.value_, rhs.u_.value_, dist));
michael@0 120 return dist <= kMaxUlps;
michael@0 121 }
michael@0 122
michael@0 123 private:
michael@0 124 /** The data type used to store the actual floating-point number. */
michael@0 125 union FloatingPointUnion {
michael@0 126 /** The raw floating-point number. */
michael@0 127 RawType value;
michael@0 128 /** The bits that represent the number. */
michael@0 129 Bits bits;
michael@0 130 };
michael@0 131
michael@0 132 /**
michael@0 133 * Converts an integer from the sign-and-magnitude representation to
michael@0 134 * the biased representation. More precisely, let N be 2 to the
michael@0 135 * power of (kBitCount - 1), an integer x is represented by the
michael@0 136 * unsigned number x + N.
michael@0 137 *
michael@0 138 * For instance,
michael@0 139 *
michael@0 140 * -N + 1 (the most negative number representable using
michael@0 141 * sign-and-magnitude) is represented by 1;
michael@0 142 * 0 is represented by N; and
michael@0 143 * N - 1 (the biggest number representable using
michael@0 144 * sign-and-magnitude) is represented by 2N - 1.
michael@0 145 *
michael@0 146 * Read http://en.wikipedia.org/wiki/Signed_number_representations
michael@0 147 * for more details on signed number representations.
michael@0 148 */
michael@0 149 static Bits SignAndMagnitudeToBiased(const Bits &sam) {
michael@0 150 if (kSignBitMask & sam) {
michael@0 151 // sam represents a negative number.
michael@0 152 return ~sam + 1;
michael@0 153 } else {
michael@0 154 // sam represents a positive number.
michael@0 155 return kSignBitMask | sam;
michael@0 156 }
michael@0 157 }
michael@0 158
michael@0 159 /**
michael@0 160 * Given two numbers in the sign-and-magnitude representation,
michael@0 161 * returns the distance between them as an unsigned number.
michael@0 162 */
michael@0 163 static Bits DistanceBetweenSignAndMagnitudeNumbers(const Bits &sam1,
michael@0 164 const Bits &sam2) {
michael@0 165 const Bits biased1 = SignAndMagnitudeToBiased(sam1);
michael@0 166 const Bits biased2 = SignAndMagnitudeToBiased(sam2);
michael@0 167 return (biased1 >= biased2) ? (biased1 - biased2) : (biased2 - biased1);
michael@0 168 }
michael@0 169
michael@0 170 FloatingPointUnion fU;
michael@0 171 };
michael@0 172
michael@0 173 #endif

mercurial