SkFloatBits.h 2.6 KB

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