blob: 6a03cb476d7c7da5b3facbc409f32692314cc114 [file] [log] [blame]
shannonwoods@chromium.org92b9cd52013-05-30 00:14:48 +00001#include "precompiled.h"
2//
3// Copyright (c) 2013 The ANGLE Project Authors. All rights reserved.
4// Use of this source code is governed by a BSD-style license that can be
5// found in the LICENSE file.
6//
7
8// mathutil.cpp: Math and bit manipulation functions.
9
10#include "common/mathutil.h"
11
12namespace gl
13{
14
15struct RGB9E5Data
16{
17 unsigned int R : 9;
18 unsigned int G : 9;
19 unsigned int B : 9;
20 unsigned int E : 5;
21};
22
23// B is the exponent bias (15)
24static const int g_sharedexp_bias = 15;
25
26// N is the number of mantissa bits per component (9)
27static const int g_sharedexp_mantissabits = 9;
28
29// Emax is the maximum allowed biased exponent value (31)
30static const int g_sharedexp_maxexponent = 31;
31
32static const float g_sharedexp_max = ((pow(2.0f, g_sharedexp_mantissabits) - 1) /
33 pow(2.0f, g_sharedexp_mantissabits)) *
34 pow(2.0f, g_sharedexp_maxexponent - g_sharedexp_bias);
35
36unsigned int convertRGBFloatsTo999E5(float red, float green, float blue)
37{
38 const float red_c = std::max<float>(0, std::min(g_sharedexp_max, red));
39 const float green_c = std::max<float>(0, std::min(g_sharedexp_max, green));
40 const float blue_c = std::max<float>(0, std::min(g_sharedexp_max, blue));
41
42 const float max_c = std::max<float>(std::max<float>(red_c, green_c), blue_c);
43 const float exp_p = std::max<float>(-g_sharedexp_bias - 1, floor(log(max_c))) + 1 + g_sharedexp_bias;
44 const int max_s = floor((max_c / (pow(2.0f, exp_p - g_sharedexp_bias - g_sharedexp_mantissabits))) + 0.5f);
45 const int exp_s = (max_s < pow(2.0f, g_sharedexp_mantissabits)) ? exp_p : exp_p + 1;
46
47 RGB9E5Data output;
48 output.R = floor((red_c / (pow(2.0f, exp_s - g_sharedexp_bias - g_sharedexp_mantissabits))) + 0.5f);
49 output.G = floor((green_c / (pow(2.0f, exp_s - g_sharedexp_bias - g_sharedexp_mantissabits))) + 0.5f);
50 output.B = floor((blue_c / (pow(2.0f, exp_s - g_sharedexp_bias - g_sharedexp_mantissabits))) + 0.5f);
51 output.E = exp_s;
52
53 return *reinterpret_cast<unsigned int*>(&output);
54}
55
56void convert999E5toRGBFloats(unsigned int input, float *red, float *green, float *blue)
57{
58 const RGB9E5Data *inputData = reinterpret_cast<const RGB9E5Data*>(&input);
59
60 *red = inputData->R * pow(2.0f, (int)inputData->E - g_sharedexp_bias - g_sharedexp_mantissabits);
61 *green = inputData->G * pow(2.0f, (int)inputData->E - g_sharedexp_bias - g_sharedexp_mantissabits);
62 *blue = inputData->B * pow(2.0f, (int)inputData->E - g_sharedexp_bias - g_sharedexp_mantissabits);
63}
64
65}