1b8e80941Smrg/* 2b8e80941Smrg * Copyright (c) 2012-2015 Etnaviv Project 3b8e80941Smrg * 4b8e80941Smrg * Permission is hereby granted, free of charge, to any person obtaining a 5b8e80941Smrg * copy of this software and associated documentation files (the "Software"), 6b8e80941Smrg * to deal in the Software without restriction, including without limitation 7b8e80941Smrg * the rights to use, copy, modify, merge, publish, distribute, sub license, 8b8e80941Smrg * and/or sell copies of the Software, and to permit persons to whom the 9b8e80941Smrg * Software is furnished to do so, subject to the following conditions: 10b8e80941Smrg * 11b8e80941Smrg * The above copyright notice and this permission notice (including the 12b8e80941Smrg * next paragraph) shall be included in all copies or substantial portions 13b8e80941Smrg * of the Software. 14b8e80941Smrg * 15b8e80941Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16b8e80941Smrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17b8e80941Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL 18b8e80941Smrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19b8e80941Smrg * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 20b8e80941Smrg * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 21b8e80941Smrg * DEALINGS IN THE SOFTWARE. 22b8e80941Smrg */ 23b8e80941Smrg 24b8e80941Smrg/* Misc util */ 25b8e80941Smrg#ifndef H_ETNA_UTIL 26b8e80941Smrg#define H_ETNA_UTIL 27b8e80941Smrg 28b8e80941Smrg#include <math.h> 29b8e80941Smrg 30b8e80941Smrg/* for conditionally setting boolean flag(s): */ 31b8e80941Smrg#define COND(bool, val) ((bool) ? (val) : 0) 32b8e80941Smrg 33b8e80941Smrg/* align to a value divisable by granularity >= value, works only for powers of two */ 34b8e80941Smrgstatic inline uint32_t 35b8e80941Smrgetna_align_up(uint32_t value, uint32_t granularity) 36b8e80941Smrg{ 37b8e80941Smrg return (value + (granularity - 1)) & (~(granularity - 1)); 38b8e80941Smrg} 39b8e80941Smrg 40b8e80941Smrgstatic inline uint32_t 41b8e80941Smrgetna_bits_ones(unsigned num) 42b8e80941Smrg{ 43b8e80941Smrg return (1 << num) - 1; 44b8e80941Smrg} 45b8e80941Smrg 46b8e80941Smrg/* clamped float [0.0 .. 1.0] -> [0 .. 255] */ 47b8e80941Smrgstatic inline uint8_t 48b8e80941Smrgetna_cfloat_to_uint8(float f) 49b8e80941Smrg{ 50b8e80941Smrg if (f <= 0.0f) 51b8e80941Smrg return 0; 52b8e80941Smrg 53b8e80941Smrg if (f >= (1.0f - 1.0f / 256.0f)) 54b8e80941Smrg return 255; 55b8e80941Smrg 56b8e80941Smrg return f * 256.0f; 57b8e80941Smrg} 58b8e80941Smrg 59b8e80941Smrg/* clamped float [0.0 .. 1.0] -> [0 .. (1<<bits)-1] */ 60b8e80941Smrgstatic inline uint32_t 61b8e80941Smrgetna_cfloat_to_uintN(float f, int bits) 62b8e80941Smrg{ 63b8e80941Smrg if (f <= 0.0f) 64b8e80941Smrg return 0; 65b8e80941Smrg 66b8e80941Smrg if (f >= (1.0f - 1.0f / (1 << bits))) 67b8e80941Smrg return (1 << bits) - 1; 68b8e80941Smrg 69b8e80941Smrg return f * (1 << bits); 70b8e80941Smrg} 71b8e80941Smrg 72b8e80941Smrg/* 1/log10(2) */ 73b8e80941Smrg#define RCPLOG2 (1.4426950408889634f) 74b8e80941Smrg 75b8e80941Smrg/* float to fixp 5.5 */ 76b8e80941Smrgstatic inline uint32_t 77b8e80941Smrgetna_float_to_fixp55(float f) 78b8e80941Smrg{ 79b8e80941Smrg if (f >= 15.953125f) 80b8e80941Smrg return 511; 81b8e80941Smrg 82b8e80941Smrg if (f < -16.0f) 83b8e80941Smrg return 512; 84b8e80941Smrg 85b8e80941Smrg return (int32_t)(f * 32.0f + 0.5f); 86b8e80941Smrg} 87b8e80941Smrg 88b8e80941Smrg/* texture size to log2 in fixp 5.5 format */ 89b8e80941Smrgstatic inline uint32_t 90b8e80941Smrgetna_log2_fixp55(unsigned width) 91b8e80941Smrg{ 92b8e80941Smrg return etna_float_to_fixp55(logf((float)width) * RCPLOG2); 93b8e80941Smrg} 94b8e80941Smrg 95b8e80941Smrg/* float to fixp 16.16 */ 96b8e80941Smrgstatic inline uint32_t 97b8e80941Smrgetna_f32_to_fixp16(float f) 98b8e80941Smrg{ 99b8e80941Smrg if (f >= (32768.0f - 1.0f / 65536.0f)) 100b8e80941Smrg return 0x7fffffff; 101b8e80941Smrg 102b8e80941Smrg if (f < -32768.0f) 103b8e80941Smrg return 0x80000000; 104b8e80941Smrg 105b8e80941Smrg return (int32_t)(f * 65536.0f + 0.5f); 106b8e80941Smrg} 107b8e80941Smrg 108b8e80941Smrg#endif 109