Home | History | Annotate | Download | only in common
      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 #include <algorithm>
     11 #include <math.h>
     12 
     13 namespace gl
     14 {
     15 
     16 struct RGB9E5Data
     17 {
     18     unsigned int R : 9;
     19     unsigned int G : 9;
     20     unsigned int B : 9;
     21     unsigned int E : 5;
     22 };
     23 
     24 // B is the exponent bias (15)
     25 static const int g_sharedexp_bias = 15;
     26 
     27 // N is the number of mantissa bits per component (9)
     28 static const int g_sharedexp_mantissabits = 9;
     29 
     30 // Emax is the maximum allowed biased exponent value (31)
     31 static const int g_sharedexp_maxexponent = 31;
     32 
     33 static const float g_sharedexp_max = ((pow(2.0f, g_sharedexp_mantissabits) - 1) /
     34                                        pow(2.0f, g_sharedexp_mantissabits)) *
     35                                      pow(2.0f, g_sharedexp_maxexponent - g_sharedexp_bias);
     36 
     37 unsigned int convertRGBFloatsTo999E5(float red, float green, float blue)
     38 {
     39     const float red_c = std::max<float>(0, std::min(g_sharedexp_max, red));
     40     const float green_c = std::max<float>(0, std::min(g_sharedexp_max, green));
     41     const float blue_c = std::max<float>(0, std::min(g_sharedexp_max, blue));
     42 
     43     const float max_c = std::max<float>(std::max<float>(red_c, green_c), blue_c);
     44     const float exp_p = std::max<float>(-g_sharedexp_bias - 1, floor(log(max_c))) + 1 + g_sharedexp_bias;
     45     const int max_s = floor((max_c / (pow(2.0f, exp_p - g_sharedexp_bias - g_sharedexp_mantissabits))) + 0.5f);
     46     const int exp_s = (max_s < pow(2.0f, g_sharedexp_mantissabits)) ? exp_p : exp_p + 1;
     47 
     48     RGB9E5Data output;
     49     output.R = floor((red_c / (pow(2.0f, exp_s - g_sharedexp_bias - g_sharedexp_mantissabits))) + 0.5f);
     50     output.G = floor((green_c / (pow(2.0f, exp_s - g_sharedexp_bias - g_sharedexp_mantissabits))) + 0.5f);
     51     output.B = floor((blue_c / (pow(2.0f, exp_s - g_sharedexp_bias - g_sharedexp_mantissabits))) + 0.5f);
     52     output.E = exp_s;
     53 
     54     return *reinterpret_cast<unsigned int*>(&output);
     55 }
     56 
     57 void convert999E5toRGBFloats(unsigned int input, float *red, float *green, float *blue)
     58 {
     59     const RGB9E5Data *inputData = reinterpret_cast<const RGB9E5Data*>(&input);
     60 
     61     *red = inputData->R * pow(2.0f, (int)inputData->E - g_sharedexp_bias - g_sharedexp_mantissabits);
     62     *green = inputData->G * pow(2.0f, (int)inputData->E - g_sharedexp_bias - g_sharedexp_mantissabits);
     63     *blue = inputData->B * pow(2.0f, (int)inputData->E - g_sharedexp_bias - g_sharedexp_mantissabits);
     64 }
     65 
     66 }
     67