blob: 20b3f0c113a55bd75270cd0c30ecc899e09108e3 [file] [log] [blame]
shannonwoods@chromium.org92b9cd52013-05-30 00:14:48 +00001//
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"
Geoff Lang17732822013-08-29 13:46:49 -040010#include <algorithm>
Jamie Madill4f267862014-04-17 15:53:37 -040011#include <math.h>
shannonwoods@chromium.org92b9cd52013-05-30 00:14:48 +000012
13namespace gl
14{
15
16struct 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)
25static const int g_sharedexp_bias = 15;
26
27// N is the number of mantissa bits per component (9)
28static const int g_sharedexp_mantissabits = 9;
29
30// Emax is the maximum allowed biased exponent value (31)
31static const int g_sharedexp_maxexponent = 31;
32
33static 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
37unsigned 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
57void 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}