Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* |
michael@0 | 2 | * Copyright 2011 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 | #include "SkFloatBits.h" |
michael@0 | 9 | #include "SkMathPriv.h" |
michael@0 | 10 | |
michael@0 | 11 | /****************************************************************************** |
michael@0 | 12 | SkFloatBits_toInt[Floor, Round, Ceil] are identical except for what they |
michael@0 | 13 | do right before they return ... >> exp; |
michael@0 | 14 | Floor - adds nothing |
michael@0 | 15 | Round - adds 1 << (exp - 1) |
michael@0 | 16 | Ceil - adds (1 << exp) - 1 |
michael@0 | 17 | |
michael@0 | 18 | Floor and Cast are very similar, but Cast applies its sign after all other |
michael@0 | 19 | computations on value. Also, Cast does not need to check for negative zero, |
michael@0 | 20 | as that value (0x80000000) "does the right thing" for Ceil. Note that it |
michael@0 | 21 | doesn't for Floor/Round/Ceil, hence the explicit check. |
michael@0 | 22 | ******************************************************************************/ |
michael@0 | 23 | |
michael@0 | 24 | #define EXP_BIAS (127+23) |
michael@0 | 25 | #define MATISSA_MAGIC_BIG (1 << 23) |
michael@0 | 26 | |
michael@0 | 27 | static inline int unpack_exp(uint32_t packed) { |
michael@0 | 28 | return (packed << 1 >> 24); |
michael@0 | 29 | } |
michael@0 | 30 | |
michael@0 | 31 | #if 0 |
michael@0 | 32 | // the ARM compiler generates an extra BIC, so I use the dirty version instead |
michael@0 | 33 | static inline int unpack_matissa(uint32_t packed) { |
michael@0 | 34 | // we could mask with 0x7FFFFF, but that is harder for ARM to encode |
michael@0 | 35 | return (packed & ~0xFF000000) | MATISSA_MAGIC_BIG; |
michael@0 | 36 | } |
michael@0 | 37 | #endif |
michael@0 | 38 | |
michael@0 | 39 | // returns the low 24-bits, so we need to OR in the magic_bit afterwards |
michael@0 | 40 | static inline int unpack_matissa_dirty(uint32_t packed) { |
michael@0 | 41 | return packed & ~0xFF000000; |
michael@0 | 42 | } |
michael@0 | 43 | |
michael@0 | 44 | // same as (int)float |
michael@0 | 45 | int32_t SkFloatBits_toIntCast(int32_t packed) { |
michael@0 | 46 | int exp = unpack_exp(packed) - EXP_BIAS; |
michael@0 | 47 | int value = unpack_matissa_dirty(packed) | MATISSA_MAGIC_BIG; |
michael@0 | 48 | |
michael@0 | 49 | if (exp >= 0) { |
michael@0 | 50 | if (exp > 7) { // overflow |
michael@0 | 51 | value = SK_MaxS32; |
michael@0 | 52 | } else { |
michael@0 | 53 | value <<= exp; |
michael@0 | 54 | } |
michael@0 | 55 | } else { |
michael@0 | 56 | exp = -exp; |
michael@0 | 57 | if (exp > 25) { // underflow |
michael@0 | 58 | exp = 25; |
michael@0 | 59 | } |
michael@0 | 60 | value >>= exp; |
michael@0 | 61 | } |
michael@0 | 62 | return SkApplySign(value, SkExtractSign(packed)); |
michael@0 | 63 | } |
michael@0 | 64 | |
michael@0 | 65 | // same as (int)floor(float) |
michael@0 | 66 | int32_t SkFloatBits_toIntFloor(int32_t packed) { |
michael@0 | 67 | // curse you negative 0 |
michael@0 | 68 | if ((packed << 1) == 0) { |
michael@0 | 69 | return 0; |
michael@0 | 70 | } |
michael@0 | 71 | |
michael@0 | 72 | int exp = unpack_exp(packed) - EXP_BIAS; |
michael@0 | 73 | int value = unpack_matissa_dirty(packed) | MATISSA_MAGIC_BIG; |
michael@0 | 74 | |
michael@0 | 75 | if (exp >= 0) { |
michael@0 | 76 | if (exp > 7) { // overflow |
michael@0 | 77 | value = SK_MaxS32; |
michael@0 | 78 | } else { |
michael@0 | 79 | value <<= exp; |
michael@0 | 80 | } |
michael@0 | 81 | // apply the sign after we check for overflow |
michael@0 | 82 | return SkApplySign(value, SkExtractSign(packed)); |
michael@0 | 83 | } else { |
michael@0 | 84 | // apply the sign before we right-shift |
michael@0 | 85 | value = SkApplySign(value, SkExtractSign(packed)); |
michael@0 | 86 | exp = -exp; |
michael@0 | 87 | if (exp > 25) { // underflow |
michael@0 | 88 | exp = 25; |
michael@0 | 89 | } |
michael@0 | 90 | // int add = 0; |
michael@0 | 91 | return value >> exp; |
michael@0 | 92 | } |
michael@0 | 93 | } |
michael@0 | 94 | |
michael@0 | 95 | // same as (int)floor(float + 0.5) |
michael@0 | 96 | int32_t SkFloatBits_toIntRound(int32_t packed) { |
michael@0 | 97 | // curse you negative 0 |
michael@0 | 98 | if ((packed << 1) == 0) { |
michael@0 | 99 | return 0; |
michael@0 | 100 | } |
michael@0 | 101 | |
michael@0 | 102 | int exp = unpack_exp(packed) - EXP_BIAS; |
michael@0 | 103 | int value = unpack_matissa_dirty(packed) | MATISSA_MAGIC_BIG; |
michael@0 | 104 | |
michael@0 | 105 | if (exp >= 0) { |
michael@0 | 106 | if (exp > 7) { // overflow |
michael@0 | 107 | value = SK_MaxS32; |
michael@0 | 108 | } else { |
michael@0 | 109 | value <<= exp; |
michael@0 | 110 | } |
michael@0 | 111 | // apply the sign after we check for overflow |
michael@0 | 112 | return SkApplySign(value, SkExtractSign(packed)); |
michael@0 | 113 | } else { |
michael@0 | 114 | // apply the sign before we right-shift |
michael@0 | 115 | value = SkApplySign(value, SkExtractSign(packed)); |
michael@0 | 116 | exp = -exp; |
michael@0 | 117 | if (exp > 25) { // underflow |
michael@0 | 118 | exp = 25; |
michael@0 | 119 | } |
michael@0 | 120 | int add = 1 << (exp - 1); |
michael@0 | 121 | return (value + add) >> exp; |
michael@0 | 122 | } |
michael@0 | 123 | } |
michael@0 | 124 | |
michael@0 | 125 | // same as (int)ceil(float) |
michael@0 | 126 | int32_t SkFloatBits_toIntCeil(int32_t packed) { |
michael@0 | 127 | // curse you negative 0 |
michael@0 | 128 | if ((packed << 1) == 0) { |
michael@0 | 129 | return 0; |
michael@0 | 130 | } |
michael@0 | 131 | |
michael@0 | 132 | int exp = unpack_exp(packed) - EXP_BIAS; |
michael@0 | 133 | int value = unpack_matissa_dirty(packed) | MATISSA_MAGIC_BIG; |
michael@0 | 134 | |
michael@0 | 135 | if (exp >= 0) { |
michael@0 | 136 | if (exp > 7) { // overflow |
michael@0 | 137 | value = SK_MaxS32; |
michael@0 | 138 | } else { |
michael@0 | 139 | value <<= exp; |
michael@0 | 140 | } |
michael@0 | 141 | // apply the sign after we check for overflow |
michael@0 | 142 | return SkApplySign(value, SkExtractSign(packed)); |
michael@0 | 143 | } else { |
michael@0 | 144 | // apply the sign before we right-shift |
michael@0 | 145 | value = SkApplySign(value, SkExtractSign(packed)); |
michael@0 | 146 | exp = -exp; |
michael@0 | 147 | if (exp > 25) { // underflow |
michael@0 | 148 | exp = 25; |
michael@0 | 149 | } |
michael@0 | 150 | int add = (1 << exp) - 1; |
michael@0 | 151 | return (value + add) >> exp; |
michael@0 | 152 | } |
michael@0 | 153 | } |
michael@0 | 154 | |
michael@0 | 155 | float SkIntToFloatCast(int32_t value) { |
michael@0 | 156 | if (0 == value) { |
michael@0 | 157 | return 0; |
michael@0 | 158 | } |
michael@0 | 159 | |
michael@0 | 160 | int shift = EXP_BIAS; |
michael@0 | 161 | |
michael@0 | 162 | // record the sign and make value positive |
michael@0 | 163 | int sign = SkExtractSign(value); |
michael@0 | 164 | value = SkApplySign(value, sign); |
michael@0 | 165 | |
michael@0 | 166 | if (value >> 24) { // value is too big (has more than 24 bits set) |
michael@0 | 167 | int bias = 8 - SkCLZ(value); |
michael@0 | 168 | SkDebugf("value = %d, bias = %d\n", value, bias); |
michael@0 | 169 | SkASSERT(bias > 0 && bias < 8); |
michael@0 | 170 | value >>= bias; // need to round? |
michael@0 | 171 | shift += bias; |
michael@0 | 172 | } else { |
michael@0 | 173 | int zeros = SkCLZ(value << 8); |
michael@0 | 174 | SkASSERT(zeros >= 0 && zeros <= 23); |
michael@0 | 175 | value <<= zeros; |
michael@0 | 176 | shift -= zeros; |
michael@0 | 177 | } |
michael@0 | 178 | |
michael@0 | 179 | // now value is left-aligned to 24 bits |
michael@0 | 180 | SkASSERT((value >> 23) == 1); |
michael@0 | 181 | SkASSERT(shift >= 0 && shift <= 255); |
michael@0 | 182 | |
michael@0 | 183 | SkFloatIntUnion data; |
michael@0 | 184 | data.fSignBitInt = (sign << 31) | (shift << 23) | (value & ~MATISSA_MAGIC_BIG); |
michael@0 | 185 | return data.fFloat; |
michael@0 | 186 | } |
michael@0 | 187 | |
michael@0 | 188 | float SkIntToFloatCast_NoOverflowCheck(int32_t value) { |
michael@0 | 189 | if (0 == value) { |
michael@0 | 190 | return 0; |
michael@0 | 191 | } |
michael@0 | 192 | |
michael@0 | 193 | int shift = EXP_BIAS; |
michael@0 | 194 | |
michael@0 | 195 | // record the sign and make value positive |
michael@0 | 196 | int sign = SkExtractSign(value); |
michael@0 | 197 | value = SkApplySign(value, sign); |
michael@0 | 198 | |
michael@0 | 199 | int zeros = SkCLZ(value << 8); |
michael@0 | 200 | value <<= zeros; |
michael@0 | 201 | shift -= zeros; |
michael@0 | 202 | |
michael@0 | 203 | SkFloatIntUnion data; |
michael@0 | 204 | data.fSignBitInt = (sign << 31) | (shift << 23) | (value & ~MATISSA_MAGIC_BIG); |
michael@0 | 205 | return data.fFloat; |
michael@0 | 206 | } |