gfx/skia/trunk/include/core/SkFloatBits.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 /*
michael@0 3 * Copyright 2008 The Android Open Source Project
michael@0 4 *
michael@0 5 * Use of this source code is governed by a BSD-style license that can be
michael@0 6 * found in the LICENSE file.
michael@0 7 */
michael@0 8
michael@0 9
michael@0 10 #ifndef SkFloatBits_DEFINED
michael@0 11 #define SkFloatBits_DEFINED
michael@0 12
michael@0 13 #include "SkTypes.h"
michael@0 14
michael@0 15 /** Convert a sign-bit int (i.e. float interpreted as int) into a 2s compliement
michael@0 16 int. This also converts -0 (0x80000000) to 0. Doing this to a float allows
michael@0 17 it to be compared using normal C operators (<, <=, etc.)
michael@0 18 */
michael@0 19 static inline int32_t SkSignBitTo2sCompliment(int32_t x) {
michael@0 20 if (x < 0) {
michael@0 21 x &= 0x7FFFFFFF;
michael@0 22 x = -x;
michael@0 23 }
michael@0 24 return x;
michael@0 25 }
michael@0 26
michael@0 27 /** Convert a 2s compliment int to a sign-bit (i.e. int interpreted as float).
michael@0 28 This undoes the result of SkSignBitTo2sCompliment().
michael@0 29 */
michael@0 30 static inline int32_t Sk2sComplimentToSignBit(int32_t x) {
michael@0 31 int sign = x >> 31;
michael@0 32 // make x positive
michael@0 33 x = (x ^ sign) - sign;
michael@0 34 // set the sign bit as needed
michael@0 35 x |= sign << 31;
michael@0 36 return x;
michael@0 37 }
michael@0 38
michael@0 39 /** Given the bit representation of a float, return its value cast to an int.
michael@0 40 If the value is out of range, or NaN, return return +/- SK_MaxS32
michael@0 41 */
michael@0 42 int32_t SkFloatBits_toIntCast(int32_t floatBits);
michael@0 43
michael@0 44 /** Given the bit representation of a float, return its floor as an int.
michael@0 45 If the value is out of range, or NaN, return return +/- SK_MaxS32
michael@0 46 */
michael@0 47 SK_API int32_t SkFloatBits_toIntFloor(int32_t floatBits);
michael@0 48
michael@0 49 /** Given the bit representation of a float, return it rounded to an int.
michael@0 50 If the value is out of range, or NaN, return return +/- SK_MaxS32
michael@0 51 */
michael@0 52 SK_API int32_t SkFloatBits_toIntRound(int32_t floatBits);
michael@0 53
michael@0 54 /** Given the bit representation of a float, return its ceiling as an int.
michael@0 55 If the value is out of range, or NaN, return return +/- SK_MaxS32
michael@0 56 */
michael@0 57 SK_API int32_t SkFloatBits_toIntCeil(int32_t floatBits);
michael@0 58
michael@0 59
michael@0 60 union SkFloatIntUnion {
michael@0 61 float fFloat;
michael@0 62 int32_t fSignBitInt;
michael@0 63 };
michael@0 64
michael@0 65 // Helper to see a float as its bit pattern (w/o aliasing warnings)
michael@0 66 static inline int32_t SkFloat2Bits(float x) {
michael@0 67 SkFloatIntUnion data;
michael@0 68 data.fFloat = x;
michael@0 69 return data.fSignBitInt;
michael@0 70 }
michael@0 71
michael@0 72 // Helper to see a bit pattern as a float (w/o aliasing warnings)
michael@0 73 static inline float SkBits2Float(int32_t floatAsBits) {
michael@0 74 SkFloatIntUnion data;
michael@0 75 data.fSignBitInt = floatAsBits;
michael@0 76 return data.fFloat;
michael@0 77 }
michael@0 78
michael@0 79 /** Return the float as a 2s compliment int. Just to be used to compare floats
michael@0 80 to each other or against positive float-bit-constants (like 0). This does
michael@0 81 not return the int equivalent of the float, just something cheaper for
michael@0 82 compares-only.
michael@0 83 */
michael@0 84 static inline int32_t SkFloatAs2sCompliment(float x) {
michael@0 85 return SkSignBitTo2sCompliment(SkFloat2Bits(x));
michael@0 86 }
michael@0 87
michael@0 88 /** Return the 2s compliment int as a float. This undos the result of
michael@0 89 SkFloatAs2sCompliment
michael@0 90 */
michael@0 91 static inline float Sk2sComplimentAsFloat(int32_t x) {
michael@0 92 return SkBits2Float(Sk2sComplimentToSignBit(x));
michael@0 93 }
michael@0 94
michael@0 95 /** Return x cast to a float (i.e. (float)x)
michael@0 96 */
michael@0 97 float SkIntToFloatCast(int x);
michael@0 98 float SkIntToFloatCast_NoOverflowCheck(int x);
michael@0 99
michael@0 100 /** Return the float cast to an int.
michael@0 101 If the value is out of range, or NaN, return +/- SK_MaxS32
michael@0 102 */
michael@0 103 static inline int32_t SkFloatToIntCast(float x) {
michael@0 104 return SkFloatBits_toIntCast(SkFloat2Bits(x));
michael@0 105 }
michael@0 106
michael@0 107 /** Return the floor of the float as an int.
michael@0 108 If the value is out of range, or NaN, return +/- SK_MaxS32
michael@0 109 */
michael@0 110 static inline int32_t SkFloatToIntFloor(float x) {
michael@0 111 return SkFloatBits_toIntFloor(SkFloat2Bits(x));
michael@0 112 }
michael@0 113
michael@0 114 /** Return the float rounded to an int.
michael@0 115 If the value is out of range, or NaN, return +/- SK_MaxS32
michael@0 116 */
michael@0 117 static inline int32_t SkFloatToIntRound(float x) {
michael@0 118 return SkFloatBits_toIntRound(SkFloat2Bits(x));
michael@0 119 }
michael@0 120
michael@0 121 /** Return the ceiling of the float as an int.
michael@0 122 If the value is out of range, or NaN, return +/- SK_MaxS32
michael@0 123 */
michael@0 124 static inline int32_t SkFloatToIntCeil(float x) {
michael@0 125 return SkFloatBits_toIntCeil(SkFloat2Bits(x));
michael@0 126 }
michael@0 127
michael@0 128 // Scalar wrappers for float-bit routines
michael@0 129
michael@0 130 #define SkScalarAs2sCompliment(x) SkFloatAs2sCompliment(x)
michael@0 131 #define Sk2sComplimentAsScalar(x) Sk2sComplimentAsFloat(x)
michael@0 132
michael@0 133 #endif

mercurial