| 1 | // |
| 2 | // Copyright (c) 2013 The ANGLE Project Authors. All rights reserved. |
| 3 | // Use of this source code is governed by a BSD-style license that can be |
| 4 | // found in the LICENSE file. |
| 5 | // |
| 6 | |
| 7 | // mathutil.cpp: Math and bit manipulation functions. |
| 8 | |
| 9 | #include "common/mathutil.h" |
| 10 | |
| 11 | #include <math.h> |
| 12 | #include <algorithm> |
| 13 | |
| 14 | namespace gl |
| 15 | { |
| 16 | |
| 17 | namespace |
| 18 | { |
| 19 | |
| 20 | struct RGB9E5Data |
| 21 | { |
| 22 | unsigned int R : 9; |
| 23 | unsigned int G : 9; |
| 24 | unsigned int B : 9; |
| 25 | unsigned int E : 5; |
| 26 | }; |
| 27 | |
| 28 | // B is the exponent bias (15) |
| 29 | constexpr int g_sharedexp_bias = 15; |
| 30 | |
| 31 | // N is the number of mantissa bits per component (9) |
| 32 | constexpr int g_sharedexp_mantissabits = 9; |
| 33 | |
| 34 | // Emax is the maximum allowed biased exponent value (31) |
| 35 | constexpr int g_sharedexp_maxexponent = 31; |
| 36 | |
| 37 | constexpr float g_sharedexp_max = |
| 38 | ((static_cast<float>(1 << g_sharedexp_mantissabits) - 1) / |
| 39 | static_cast<float>(1 << g_sharedexp_mantissabits)) * |
| 40 | static_cast<float>(1 << (g_sharedexp_maxexponent - g_sharedexp_bias)); |
| 41 | |
| 42 | } // anonymous namespace |
| 43 | |
| 44 | unsigned int convertRGBFloatsTo999E5(float red, float green, float blue) |
| 45 | { |
| 46 | const float red_c = std::max<float>(0, std::min(g_sharedexp_max, red)); |
| 47 | const float green_c = std::max<float>(0, std::min(g_sharedexp_max, green)); |
| 48 | const float blue_c = std::max<float>(0, std::min(g_sharedexp_max, blue)); |
| 49 | |
| 50 | const float max_c = std::max<float>(std::max<float>(red_c, green_c), blue_c); |
| 51 | const float exp_p = |
| 52 | std::max<float>(-g_sharedexp_bias - 1, floor(log(max_c))) + 1 + g_sharedexp_bias; |
| 53 | const int max_s = static_cast<int>( |
| 54 | floor((max_c / (pow(2.0f, exp_p - g_sharedexp_bias - g_sharedexp_mantissabits))) + 0.5f)); |
| 55 | const int exp_s = |
| 56 | static_cast<int>((max_s < pow(2.0f, g_sharedexp_mantissabits)) ? exp_p : exp_p + 1); |
| 57 | |
| 58 | RGB9E5Data output; |
| 59 | output.R = static_cast<unsigned int>( |
| 60 | floor((red_c / (pow(2.0f, exp_s - g_sharedexp_bias - g_sharedexp_mantissabits))) + 0.5f)); |
| 61 | output.G = static_cast<unsigned int>( |
| 62 | floor((green_c / (pow(2.0f, exp_s - g_sharedexp_bias - g_sharedexp_mantissabits))) + 0.5f)); |
| 63 | output.B = static_cast<unsigned int>( |
| 64 | floor((blue_c / (pow(2.0f, exp_s - g_sharedexp_bias - g_sharedexp_mantissabits))) + 0.5f)); |
| 65 | output.E = exp_s; |
| 66 | |
| 67 | return bitCast<unsigned int>(output); |
| 68 | } |
| 69 | |
| 70 | void convert999E5toRGBFloats(unsigned int input, float *red, float *green, float *blue) |
| 71 | { |
| 72 | const RGB9E5Data *inputData = reinterpret_cast<const RGB9E5Data *>(&input); |
| 73 | |
| 74 | *red = |
| 75 | inputData->R * pow(2.0f, (int)inputData->E - g_sharedexp_bias - g_sharedexp_mantissabits); |
| 76 | *green = |
| 77 | inputData->G * pow(2.0f, (int)inputData->E - g_sharedexp_bias - g_sharedexp_mantissabits); |
| 78 | *blue = |
| 79 | inputData->B * pow(2.0f, (int)inputData->E - g_sharedexp_bias - g_sharedexp_mantissabits); |
| 80 | } |
| 81 | |
| 82 | int BitCountPolyfill(uint32_t bits) |
| 83 | { |
| 84 | int ones = 0; |
| 85 | while (bits) |
| 86 | { |
| 87 | ones += bool(bits & 1); |
| 88 | bits >>= 1; |
| 89 | } |
| 90 | return ones; |
| 91 | } |
| 92 | |
| 93 | } // namespace gl |
| 94 | |