michael@0: /* michael@0: * Copyright 2011 Google Inc. michael@0: * michael@0: * Use of this source code is governed by a BSD-style license that can be michael@0: * found in the LICENSE file. michael@0: */ michael@0: michael@0: #include "SkFloatBits.h" michael@0: #include "SkMathPriv.h" michael@0: michael@0: /****************************************************************************** michael@0: SkFloatBits_toInt[Floor, Round, Ceil] are identical except for what they michael@0: do right before they return ... >> exp; michael@0: Floor - adds nothing michael@0: Round - adds 1 << (exp - 1) michael@0: Ceil - adds (1 << exp) - 1 michael@0: michael@0: Floor and Cast are very similar, but Cast applies its sign after all other michael@0: computations on value. Also, Cast does not need to check for negative zero, michael@0: as that value (0x80000000) "does the right thing" for Ceil. Note that it michael@0: doesn't for Floor/Round/Ceil, hence the explicit check. michael@0: ******************************************************************************/ michael@0: michael@0: #define EXP_BIAS (127+23) michael@0: #define MATISSA_MAGIC_BIG (1 << 23) michael@0: michael@0: static inline int unpack_exp(uint32_t packed) { michael@0: return (packed << 1 >> 24); michael@0: } michael@0: michael@0: #if 0 michael@0: // the ARM compiler generates an extra BIC, so I use the dirty version instead michael@0: static inline int unpack_matissa(uint32_t packed) { michael@0: // we could mask with 0x7FFFFF, but that is harder for ARM to encode michael@0: return (packed & ~0xFF000000) | MATISSA_MAGIC_BIG; michael@0: } michael@0: #endif michael@0: michael@0: // returns the low 24-bits, so we need to OR in the magic_bit afterwards michael@0: static inline int unpack_matissa_dirty(uint32_t packed) { michael@0: return packed & ~0xFF000000; michael@0: } michael@0: michael@0: // same as (int)float michael@0: int32_t SkFloatBits_toIntCast(int32_t packed) { michael@0: int exp = unpack_exp(packed) - EXP_BIAS; michael@0: int value = unpack_matissa_dirty(packed) | MATISSA_MAGIC_BIG; michael@0: michael@0: if (exp >= 0) { michael@0: if (exp > 7) { // overflow michael@0: value = SK_MaxS32; michael@0: } else { michael@0: value <<= exp; michael@0: } michael@0: } else { michael@0: exp = -exp; michael@0: if (exp > 25) { // underflow michael@0: exp = 25; michael@0: } michael@0: value >>= exp; michael@0: } michael@0: return SkApplySign(value, SkExtractSign(packed)); michael@0: } michael@0: michael@0: // same as (int)floor(float) michael@0: int32_t SkFloatBits_toIntFloor(int32_t packed) { michael@0: // curse you negative 0 michael@0: if ((packed << 1) == 0) { michael@0: return 0; michael@0: } michael@0: michael@0: int exp = unpack_exp(packed) - EXP_BIAS; michael@0: int value = unpack_matissa_dirty(packed) | MATISSA_MAGIC_BIG; michael@0: michael@0: if (exp >= 0) { michael@0: if (exp > 7) { // overflow michael@0: value = SK_MaxS32; michael@0: } else { michael@0: value <<= exp; michael@0: } michael@0: // apply the sign after we check for overflow michael@0: return SkApplySign(value, SkExtractSign(packed)); michael@0: } else { michael@0: // apply the sign before we right-shift michael@0: value = SkApplySign(value, SkExtractSign(packed)); michael@0: exp = -exp; michael@0: if (exp > 25) { // underflow michael@0: exp = 25; michael@0: } michael@0: // int add = 0; michael@0: return value >> exp; michael@0: } michael@0: } michael@0: michael@0: // same as (int)floor(float + 0.5) michael@0: int32_t SkFloatBits_toIntRound(int32_t packed) { michael@0: // curse you negative 0 michael@0: if ((packed << 1) == 0) { michael@0: return 0; michael@0: } michael@0: michael@0: int exp = unpack_exp(packed) - EXP_BIAS; michael@0: int value = unpack_matissa_dirty(packed) | MATISSA_MAGIC_BIG; michael@0: michael@0: if (exp >= 0) { michael@0: if (exp > 7) { // overflow michael@0: value = SK_MaxS32; michael@0: } else { michael@0: value <<= exp; michael@0: } michael@0: // apply the sign after we check for overflow michael@0: return SkApplySign(value, SkExtractSign(packed)); michael@0: } else { michael@0: // apply the sign before we right-shift michael@0: value = SkApplySign(value, SkExtractSign(packed)); michael@0: exp = -exp; michael@0: if (exp > 25) { // underflow michael@0: exp = 25; michael@0: } michael@0: int add = 1 << (exp - 1); michael@0: return (value + add) >> exp; michael@0: } michael@0: } michael@0: michael@0: // same as (int)ceil(float) michael@0: int32_t SkFloatBits_toIntCeil(int32_t packed) { michael@0: // curse you negative 0 michael@0: if ((packed << 1) == 0) { michael@0: return 0; michael@0: } michael@0: michael@0: int exp = unpack_exp(packed) - EXP_BIAS; michael@0: int value = unpack_matissa_dirty(packed) | MATISSA_MAGIC_BIG; michael@0: michael@0: if (exp >= 0) { michael@0: if (exp > 7) { // overflow michael@0: value = SK_MaxS32; michael@0: } else { michael@0: value <<= exp; michael@0: } michael@0: // apply the sign after we check for overflow michael@0: return SkApplySign(value, SkExtractSign(packed)); michael@0: } else { michael@0: // apply the sign before we right-shift michael@0: value = SkApplySign(value, SkExtractSign(packed)); michael@0: exp = -exp; michael@0: if (exp > 25) { // underflow michael@0: exp = 25; michael@0: } michael@0: int add = (1 << exp) - 1; michael@0: return (value + add) >> exp; michael@0: } michael@0: } michael@0: michael@0: float SkIntToFloatCast(int32_t value) { michael@0: if (0 == value) { michael@0: return 0; michael@0: } michael@0: michael@0: int shift = EXP_BIAS; michael@0: michael@0: // record the sign and make value positive michael@0: int sign = SkExtractSign(value); michael@0: value = SkApplySign(value, sign); michael@0: michael@0: if (value >> 24) { // value is too big (has more than 24 bits set) michael@0: int bias = 8 - SkCLZ(value); michael@0: SkDebugf("value = %d, bias = %d\n", value, bias); michael@0: SkASSERT(bias > 0 && bias < 8); michael@0: value >>= bias; // need to round? michael@0: shift += bias; michael@0: } else { michael@0: int zeros = SkCLZ(value << 8); michael@0: SkASSERT(zeros >= 0 && zeros <= 23); michael@0: value <<= zeros; michael@0: shift -= zeros; michael@0: } michael@0: michael@0: // now value is left-aligned to 24 bits michael@0: SkASSERT((value >> 23) == 1); michael@0: SkASSERT(shift >= 0 && shift <= 255); michael@0: michael@0: SkFloatIntUnion data; michael@0: data.fSignBitInt = (sign << 31) | (shift << 23) | (value & ~MATISSA_MAGIC_BIG); michael@0: return data.fFloat; michael@0: } michael@0: michael@0: float SkIntToFloatCast_NoOverflowCheck(int32_t value) { michael@0: if (0 == value) { michael@0: return 0; michael@0: } michael@0: michael@0: int shift = EXP_BIAS; michael@0: michael@0: // record the sign and make value positive michael@0: int sign = SkExtractSign(value); michael@0: value = SkApplySign(value, sign); michael@0: michael@0: int zeros = SkCLZ(value << 8); michael@0: value <<= zeros; michael@0: shift -= zeros; michael@0: michael@0: SkFloatIntUnion data; michael@0: data.fSignBitInt = (sign << 31) | (shift << 23) | (value & ~MATISSA_MAGIC_BIG); michael@0: return data.fFloat; michael@0: }