From fd69eb2b35864ad22752a470a46b13b098286b59 Mon Sep 17 00:00:00 2001 From: Chia-I Wu Date: Mon, 18 Aug 2014 14:39:31 +0800 Subject: icd: add more utils for working with floats --- icd/common/CMakeLists.txt | 2 +- icd/common/icd-utils.c | 83 +++++++++++++++++++++++++++++++++++++++++++++++ icd/common/icd-utils.h | 25 ++++++++++++++ 3 files changed, 109 insertions(+), 1 deletion(-) create mode 100644 icd/common/icd-utils.c diff --git a/icd/common/CMakeLists.txt b/icd/common/CMakeLists.txt index 7c8e8352..ba49a230 100644 --- a/icd/common/CMakeLists.txt +++ b/icd/common/CMakeLists.txt @@ -9,5 +9,5 @@ add_custom_command(OUTPUT icd-dispatch-entrypoints.c DEPENDS ${PROJECT_SOURCE_DIR}/xgl-generate.py ${PROJECT_SOURCE_DIR}/xgl.py) -add_library(icd OBJECT icd.c icd-dispatch-entrypoints.c icd-dispatch-table.h icd-format.c) +add_library(icd OBJECT icd.c icd-dispatch-entrypoints.c icd-dispatch-table.h icd-format.c icd-utils.c) set_target_properties(icd PROPERTIES POSITION_INDEPENDENT_CODE ON) diff --git a/icd/common/icd-utils.c b/icd/common/icd-utils.c new file mode 100644 index 00000000..2c0b8707 --- /dev/null +++ b/icd/common/icd-utils.c @@ -0,0 +1,83 @@ +/* + * XGL + * + * Copyright (C) 2014 LunarG, Inc. + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + * DEALINGS IN THE SOFTWARE. + */ + +#include "icd-utils.h" + +/* stolen from Mesa */ +uint16_t u_float_to_half(float f) +{ + union fi { + float f; + uint32_t ui; + }; + + uint32_t sign_mask = 0x80000000; + uint32_t round_mask = ~0xfff; + uint32_t f32inf = 0xff << 23; + uint32_t f16inf = 0x1f << 23; + uint32_t sign; + union fi magic; + union fi f32; + uint16_t f16; + + magic.ui = 0xf << 23; + + f32.f = f; + + /* Sign */ + sign = f32.ui & sign_mask; + f32.ui ^= sign; + + if (f32.ui == f32inf) { + /* Inf */ + f16 = 0x7c00; + } else if (f32.ui > f32inf) { + /* NaN */ + f16 = 0x7e00; + } else { + /* Number */ + f32.ui &= round_mask; + f32.f *= magic.f; + f32.ui -= round_mask; + + /* + * Clamp to max finite value if overflowed. + * OpenGL has completely undefined rounding behavior for float to + * half-float conversions, and this matches what is mandated for float + * to fp11/fp10, which recommend round-to-nearest-finite too. + * (d3d10 is deeply unhappy about flushing such values to infinity, and + * while it also mandates round-to-zero it doesn't care nearly as much + * about that.) + */ + if (f32.ui > f16inf) + f32.ui = f16inf - 1; + + f16 = f32.ui >> 13; + } + + /* Sign */ + f16 |= sign >> 16; + + return f16; +} diff --git a/icd/common/icd-utils.h b/icd/common/icd-utils.h index c6008520..26f911e2 100644 --- a/icd/common/icd-utils.h +++ b/icd/common/icd-utils.h @@ -25,6 +25,8 @@ #ifndef ICD_UTILS_H #define ICD_UTILS_H +#include +#include #include #include "icd.h" @@ -34,6 +36,9 @@ (void) sizeof(char[1 - 2 * !(expr)]); \ } while (0) +#define U_CLAMP(val, min, max) \ + ((val) < (min) ? (min) : (val) > (max)? (max) : (val)) + /** * Return true if val is power of two, or zero. */ @@ -53,4 +58,24 @@ static inline unsigned int u_minify(unsigned int val, unsigned level) return (val >> level) ? val >> level : 1; } +static inline uint32_t u_fui(float f) +{ + union { + float f; + uint32_t ui; + } u = { .f = f }; + + return u.ui; +} + +static inline int u_iround(float f) +{ + if (f >= 0.0f) + return (int) (f + 0.5f); + else + return (int) (f - 0.5f); +} + +uint16_t u_float_to_half(float f); + #endif /* ICD_UTILS_H */ -- cgit v1.2.3