/*1* Copyright (C) 2011 Marek Olšák <[email protected]>2*3* Permission is hereby granted, free of charge, to any person obtaining a4* copy of this software and associated documentation files (the "Software"),5* to deal in the Software without restriction, including without limitation6* the rights to use, copy, modify, merge, publish, distribute, sublicense,7* and/or sell copies of the Software, and to permit persons to whom the8* Software is furnished to do so, subject to the following conditions:9*10* The above copyright notice and this permission notice (including the next11* paragraph) shall be included in all copies or substantial portions of the12* Software.13*14* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR15* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,16* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL17* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER18* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING19* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER20* DEALINGS IN THE SOFTWARE.21*/2223/* Copied from EXT_texture_shared_exponent and edited, getting rid of24* expensive float math bits too. */2526#ifndef RGB9E5_H27#define RGB9E5_H2829#include <assert.h>30#include <stdint.h>3132#include "c99_math.h"3334#define RGB9E5_EXPONENT_BITS 535#define RGB9E5_MANTISSA_BITS 936#define RGB9E5_EXP_BIAS 1537#define RGB9E5_MAX_VALID_BIASED_EXP 313839#define MAX_RGB9E5_EXP (RGB9E5_MAX_VALID_BIASED_EXP - RGB9E5_EXP_BIAS)40#define RGB9E5_MANTISSA_VALUES (1<<RGB9E5_MANTISSA_BITS)41#define MAX_RGB9E5_MANTISSA (RGB9E5_MANTISSA_VALUES-1)42#define MAX_RGB9E5 (((float)MAX_RGB9E5_MANTISSA)/RGB9E5_MANTISSA_VALUES * (1<<MAX_RGB9E5_EXP))4344static inline int rgb9e5_ClampRange(float x)45{46union { float f; uint32_t u; } f, max;47f.f = x;48max.f = MAX_RGB9E5;4950if (f.u > 0x7f800000)51/* catches neg, NaNs */52return 0;53else if (f.u >= max.u)54return max.u;55else56return f.u;57}5859static inline uint32_t float3_to_rgb9e5(const float rgb[3])60{61int rm, gm, bm, exp_shared;62uint32_t revdenom_biasedexp;63union { float f; uint32_t u; } rc, bc, gc, maxrgb, revdenom;6465rc.u = rgb9e5_ClampRange(rgb[0]);66gc.u = rgb9e5_ClampRange(rgb[1]);67bc.u = rgb9e5_ClampRange(rgb[2]);68maxrgb.u = MAX3(rc.u, gc.u, bc.u);6970/*71* Compared to what the spec suggests, instead of conditionally adjusting72* the exponent after the fact do it here by doing the equivalent of +0.5 -73* the int add will spill over into the exponent in this case.74*/75maxrgb.u += maxrgb.u & (1 << (23-9));76exp_shared = MAX2((maxrgb.u >> 23), -RGB9E5_EXP_BIAS - 1 + 127) +771 + RGB9E5_EXP_BIAS - 127;78revdenom_biasedexp = 127 - (exp_shared - RGB9E5_EXP_BIAS -79RGB9E5_MANTISSA_BITS) + 1;80revdenom.u = revdenom_biasedexp << 23;81assert(exp_shared <= RGB9E5_MAX_VALID_BIASED_EXP);8283/*84* The spec uses strict round-up behavior (d3d10 disagrees, but in any case85* must match what is done above for figuring out exponent).86* We avoid the doubles ((int) rc * revdenom + 0.5) by doing the rounding87* ourselves (revdenom was adjusted by +1, above).88*/89rm = (int) (rc.f * revdenom.f);90gm = (int) (gc.f * revdenom.f);91bm = (int) (bc.f * revdenom.f);92rm = (rm & 1) + (rm >> 1);93gm = (gm & 1) + (gm >> 1);94bm = (bm & 1) + (bm >> 1);9596assert(rm <= MAX_RGB9E5_MANTISSA);97assert(gm <= MAX_RGB9E5_MANTISSA);98assert(bm <= MAX_RGB9E5_MANTISSA);99assert(rm >= 0);100assert(gm >= 0);101assert(bm >= 0);102103return (exp_shared << 27) | (bm << 18) | (gm << 9) | rm;104}105106static inline void rgb9e5_to_float3(uint32_t rgb, float retval[3])107{108int exponent;109union { float f; uint32_t u; } scale;110111exponent = (rgb >> 27) - RGB9E5_EXP_BIAS - RGB9E5_MANTISSA_BITS;112scale.u = (exponent + 127) << 23;113114retval[0] = ( rgb & 0x1ff) * scale.f;115retval[1] = ((rgb >> 9) & 0x1ff) * scale.f;116retval[2] = ((rgb >> 18) & 0x1ff) * scale.f;117}118119#endif120121122