1 /**
2 * \file format_utils.h
3 * A collection of format conversion utility functions.
4 */
5
6 /*
7 * Mesa 3-D graphics library
8 *
9 * Copyright (C) 1999-2006 Brian Paul All Rights Reserved.
10 * Copyright (C) 2014 Intel Corporation All Rights Reserved.
11 *
12 * Permission is hereby granted, free of charge, to any person obtaining a
13 * copy of this software and associated documentation files (the "Software"),
14 * to deal in the Software without restriction, including without limitation
15 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
16 * and/or sell copies of the Software, and to permit persons to whom the
17 * Software is furnished to do so, subject to the following conditions:
18 *
19 * The above copyright notice and this permission notice shall be included
20 * in all copies or substantial portions of the Software.
21 *
22 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
23 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
25 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
26 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
27 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
28 * OTHER DEALINGS IN THE SOFTWARE.
29 */
30
31 #ifndef UTIL_FORMAT_UTILS_H
32 #define UTIL_FORMAT_UTILS_H
33
34 #include "util/half_float.h"
35 #include "util/rounding.h"
36
37 /* Extends an integer of size SRC_BITS to one of size DST_BITS linearly */
38 #define EXTEND_NORMALIZED_INT(X, SRC_BITS, DST_BITS) \
39 (((X) * (int)(u_uintN_max(DST_BITS) / u_uintN_max(SRC_BITS))) + \
40 (((DST_BITS) % (SRC_BITS)) ? ((X) >> ((SRC_BITS) - (DST_BITS) % (SRC_BITS))) : 0))
41
42 static inline float
_mesa_unorm_to_float(unsigned x,unsigned src_bits)43 _mesa_unorm_to_float(unsigned x, unsigned src_bits)
44 {
45 return x * (1.0f / (float)u_uintN_max(src_bits));
46 }
47
48 static inline float
_mesa_snorm_to_float(int x,unsigned src_bits)49 _mesa_snorm_to_float(int x, unsigned src_bits)
50 {
51 if (x <= -u_intN_max(src_bits))
52 return -1.0f;
53 else
54 return x * (1.0f / (float)u_intN_max(src_bits));
55 }
56
57 static inline uint16_t
_mesa_unorm_to_half(unsigned x,unsigned src_bits)58 _mesa_unorm_to_half(unsigned x, unsigned src_bits)
59 {
60 return _mesa_float_to_half(_mesa_unorm_to_float(x, src_bits));
61 }
62
63 static inline uint16_t
_mesa_snorm_to_half(int x,unsigned src_bits)64 _mesa_snorm_to_half(int x, unsigned src_bits)
65 {
66 return _mesa_float_to_half(_mesa_snorm_to_float(x, src_bits));
67 }
68
69 static inline unsigned
_mesa_float_to_unorm(float x,unsigned dst_bits)70 _mesa_float_to_unorm(float x, unsigned dst_bits)
71 {
72 if (x < 0.0f)
73 return 0;
74 else if (x > 1.0f)
75 return u_uintN_max(dst_bits);
76 else
77 return _mesa_i64roundevenf(x * u_uintN_max(dst_bits));
78 }
79
80 static inline unsigned
_mesa_half_to_unorm(uint16_t x,unsigned dst_bits)81 _mesa_half_to_unorm(uint16_t x, unsigned dst_bits)
82 {
83 return _mesa_float_to_unorm(_mesa_half_to_float(x), dst_bits);
84 }
85
86 static inline unsigned
_mesa_unorm_to_unorm(unsigned x,unsigned src_bits,unsigned dst_bits)87 _mesa_unorm_to_unorm(unsigned x, unsigned src_bits, unsigned dst_bits)
88 {
89 if (src_bits < dst_bits) {
90 return EXTEND_NORMALIZED_INT(x, src_bits, dst_bits);
91 } else if (src_bits > dst_bits) {
92 unsigned src_half = (1u << (src_bits - 1)) - 1;
93
94 if (src_bits + dst_bits > sizeof(x) * 8) {
95 assert(src_bits + dst_bits <= sizeof(uint64_t) * 8);
96 return (((uint64_t) x * u_uintN_max(dst_bits) + src_half) /
97 u_uintN_max(src_bits));
98 } else {
99 return (x * u_uintN_max(dst_bits) + src_half) / u_uintN_max(src_bits);
100 }
101 } else {
102 return x;
103 }
104 }
105
106 static inline unsigned
_mesa_snorm_to_unorm(int x,unsigned src_bits,unsigned dst_bits)107 _mesa_snorm_to_unorm(int x, unsigned src_bits, unsigned dst_bits)
108 {
109 if (x < 0)
110 return 0;
111 else
112 return _mesa_unorm_to_unorm(x, src_bits - 1, dst_bits);
113 }
114
115 static inline int
_mesa_float_to_snorm(float x,unsigned dst_bits)116 _mesa_float_to_snorm(float x, unsigned dst_bits)
117 {
118 if (x < -1.0f)
119 return -u_intN_max(dst_bits);
120 else if (x > 1.0f)
121 return u_intN_max(dst_bits);
122 else
123 return _mesa_lroundevenf(x * u_intN_max(dst_bits));
124 }
125
126 static inline int
_mesa_half_to_snorm(uint16_t x,unsigned dst_bits)127 _mesa_half_to_snorm(uint16_t x, unsigned dst_bits)
128 {
129 return _mesa_float_to_snorm(_mesa_half_to_float(x), dst_bits);
130 }
131
132 static inline int
_mesa_unorm_to_snorm(unsigned x,unsigned src_bits,unsigned dst_bits)133 _mesa_unorm_to_snorm(unsigned x, unsigned src_bits, unsigned dst_bits)
134 {
135 return _mesa_unorm_to_unorm(x, src_bits, dst_bits - 1);
136 }
137
138 static inline int
_mesa_snorm_to_snorm(int x,unsigned src_bits,unsigned dst_bits)139 _mesa_snorm_to_snorm(int x, unsigned src_bits, unsigned dst_bits)
140 {
141 if (x < -u_intN_max(src_bits))
142 return -u_intN_max(dst_bits);
143 else if (src_bits < dst_bits)
144 return EXTEND_NORMALIZED_INT(x, src_bits - 1, dst_bits - 1);
145 else
146 return x >> (src_bits - dst_bits);
147 }
148
149 static inline unsigned
_mesa_unsigned_to_unsigned(unsigned src,unsigned dst_size)150 _mesa_unsigned_to_unsigned(unsigned src, unsigned dst_size)
151 {
152 return MIN2(src, u_uintN_max(dst_size));
153 }
154
155 static inline int
_mesa_unsigned_to_signed(unsigned src,unsigned dst_size)156 _mesa_unsigned_to_signed(unsigned src, unsigned dst_size)
157 {
158 return MIN2(src, (unsigned)u_intN_max(dst_size));
159 }
160
161 static inline int
_mesa_signed_to_signed(int src,unsigned dst_size)162 _mesa_signed_to_signed(int src, unsigned dst_size)
163 {
164 return CLAMP(src, u_intN_min(dst_size), u_intN_max(dst_size));
165 }
166
167 static inline unsigned
_mesa_signed_to_unsigned(int src,unsigned dst_size)168 _mesa_signed_to_unsigned(int src, unsigned dst_size)
169 {
170 return CLAMP(src, 0, u_uintN_max(dst_size));
171 }
172
173 static inline unsigned
_mesa_float_to_unsigned(float src,unsigned dst_bits)174 _mesa_float_to_unsigned(float src, unsigned dst_bits)
175 {
176 if (src < 0.0f)
177 return 0;
178 if (src > (float)u_uintN_max(dst_bits))
179 return u_uintN_max(dst_bits);
180 return _mesa_signed_to_unsigned(src, dst_bits);
181 }
182
183 static inline unsigned
_mesa_float_to_signed(float src,unsigned dst_bits)184 _mesa_float_to_signed(float src, unsigned dst_bits)
185 {
186 if (src < (float)(-u_intN_max(dst_bits)))
187 return -u_intN_max(dst_bits);
188 if (src > (float)u_intN_max(dst_bits))
189 return u_intN_max(dst_bits);
190 return _mesa_signed_to_signed(src, dst_bits);
191 }
192
193 static inline unsigned
_mesa_half_to_unsigned(uint16_t src,unsigned dst_bits)194 _mesa_half_to_unsigned(uint16_t src, unsigned dst_bits)
195 {
196 if (_mesa_half_is_negative(src))
197 return 0;
198 return _mesa_unsigned_to_unsigned(_mesa_float_to_half(src), dst_bits);
199 }
200
201 static inline unsigned
_mesa_half_to_signed(uint16_t src,unsigned dst_bits)202 _mesa_half_to_signed(uint16_t src, unsigned dst_bits)
203 {
204 return _mesa_float_to_signed(_mesa_half_to_float(src), dst_bits);
205 }
206
207 #endif /* UTIL_FORMAT_UTILS_H */
208