soui 5.0.0.1
Soui5 Doc
 
Loading...
Searching...
No Matches
SFloatBits.h
1
2/*
3 * Copyright 2008 The Android Open Source Project
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
8
9#ifndef _SFloatBits_DEFINED_
10#define _SFloatBits_DEFINED_
11
12#include <stdint.h>
13
14SNSBEGIN
15/** Convert a sign-bit int (i.e. float interpreted as int) into a 2s compliement
16 int. This also converts -0 (0x80000000) to 0. Doing this to a float allows
17 it to be compared using normal C operators (<, <=, etc.)
18*/
19static inline int32_t SkSignBitTo2sCompliment(int32_t x)
20{
21 if (x < 0)
22 {
23 x &= 0x7FFFFFFF;
24 x = -x;
25 }
26 return x;
27}
28
29/** Convert a 2s compliment int to a sign-bit (i.e. int interpreted as float).
30 This undoes the result of SkSignBitTo2sCompliment().
31 */
32static inline int32_t Sk2sComplimentToSignBit(int32_t x)
33{
34 int sign = x >> 31;
35 // make x positive
36 x = (x ^ sign) - sign;
37 // set the sign bit as needed
38 x |= sign << 31;
39 return x;
40}
41
42union SkFloatIntUnion {
43 float fFloat;
44 int32_t fSignBitInt;
45};
46
47// Helper to see a float as its bit pattern (w/o aliasing warnings)
48static inline int32_t SkFloat2Bits(float x)
49{
50 SkFloatIntUnion data;
51 data.fFloat = x;
52 return data.fSignBitInt;
53}
54
55// Helper to see a bit pattern as a float (w/o aliasing warnings)
56static inline float SkBits2Float(int32_t floatAsBits)
57{
58 SkFloatIntUnion data;
59 data.fSignBitInt = floatAsBits;
60 return data.fFloat;
61}
62
63/** Return the float as a 2s compliment int. Just to be used to compare floats
64 to each other or against positive float-bit-constants (like 0). This does
65 not return the int equivalent of the float, just something cheaper for
66 compares-only.
67 */
68static inline int32_t SkFloatAs2sCompliment(float x)
69{
70 return SkSignBitTo2sCompliment(SkFloat2Bits(x));
71}
72
73// Scalar wrappers for float-bit routines
74
75#define SFloatAs2sCompliment(x) SkFloatAs2sCompliment(x)
76#define Sk2sComplimentAsScalar(x) Sk2sComplimentAsFloat(x)
77
78SNSEND
79#endif