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