1 /**************************************************************************
3 * Copyright 2009 VMware, Inc.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 **************************************************************************/
31 * Helper functions for type conversions.
33 * We want to use the fastest type for a given computation whenever feasible.
34 * The other side of this is that we need to be able convert between several
35 * types accurately and efficiently.
37 * Conversion between types of different bit width is quite complex since a
39 * To remember there are a few invariants in type conversions:
41 * - register width must remain constant:
43 * src_type.width * src_type.length == dst_type.width * dst_type.length
45 * - total number of elements must remain constant:
47 * src_type.length * num_srcs == dst_type.length * num_dsts
49 * It is not always possible to do the conversion both accurately and
50 * efficiently, usually due to lack of adequate machine instructions. In these
51 * cases it is important not to cut shortcuts here and sacrifice accuracy, as
52 * there this functions can be used anywhere. In the future we might have a
53 * precision parameter which can gauge the accuracy vs efficiency compromise,
54 * but for now if the data conversion between two stages happens to be the
55 * bottleneck, then most likely should just avoid converting at all and run
56 * both stages with the same type.
58 * Make sure to run lp_test_conv unit test after any change to this file.
60 * @author Jose Fonseca <jfonseca@vmware.com>
64 #include "util/u_debug.h"
65 #include "util/u_math.h"
67 #include "lp_bld_type.h"
68 #include "lp_bld_const.h"
69 #include "lp_bld_arit.h"
70 #include "lp_bld_pack.h"
71 #include "lp_bld_conv.h"
75 * Special case for converting clamped IEEE-754 floats to unsigned norms.
77 * The mathematical voodoo below may seem excessive but it is actually
78 * paramount we do it this way for several reasons. First, there is no single
79 * precision FP to unsigned integer conversion Intel SSE instruction. Second,
80 * secondly, even if there was, since the FP's mantissa takes only a fraction
81 * of register bits the typically scale and cast approach would require double
82 * precision for accurate results, and therefore half the throughput
84 * Although the result values can be scaled to an arbitrary bit width specified
85 * by dst_width, the actual result type will have the same width.
87 * Ex: src = { float, float, float, float }
88 * return { i32, i32, i32, i32 } where each value is in [0, 2^dst_width-1].
91 lp_build_clamped_float_to_unsigned_norm(LLVMBuilderRef builder
,
92 struct lp_type src_type
,
96 LLVMTypeRef int_vec_type
= lp_build_int_vec_type(src_type
);
100 unsigned long long ubound
;
101 unsigned long long mask
;
105 assert(src_type
.floating
);
107 mantissa
= lp_mantissa(src_type
);
109 /* We cannot carry more bits than the mantissa */
110 n
= MIN2(mantissa
, dst_width
);
112 /* This magic coefficients will make the desired result to appear in the
113 * lowest significant bits of the mantissa.
115 ubound
= ((unsigned long long)1 << n
);
117 scale
= (double)mask
/ubound
;
118 bias
= (double)((unsigned long long)1 << (mantissa
- n
));
120 res
= LLVMBuildMul(builder
, src
, lp_build_const_vec(src_type
, scale
), "");
121 res
= LLVMBuildAdd(builder
, res
, lp_build_const_vec(src_type
, bias
), "");
122 res
= LLVMBuildBitCast(builder
, res
, int_vec_type
, "");
125 int shift
= dst_width
- n
;
126 res
= LLVMBuildShl(builder
, res
, lp_build_const_int_vec(src_type
, shift
), "");
128 /* TODO: Fill in the empty lower bits for additional precision? */
129 /* YES: this fixes progs/trivial/tri-z-eq.c.
130 * Otherwise vertex Z=1.0 values get converted to something like
131 * 0xfffffb00 and the test for equality with 0xffffffff fails.
136 msb
= LLVMBuildLShr(builder
, res
, lp_build_const_int_vec(src_type
, dst_width
- 1), "");
137 msb
= LLVMBuildShl(builder
, msb
, lp_build_const_int_vec(src_type
, shift
), "");
138 msb
= LLVMBuildSub(builder
, msb
, lp_build_const_int_vec(src_type
, 1), "");
139 res
= LLVMBuildOr(builder
, res
, msb
, "");
143 res
= LLVMBuildOr(builder
, res
, LLVMBuildLShr(builder
, res
, lp_build_const_int_vec(src_type
, n
), ""), "");
150 res
= LLVMBuildAnd(builder
, res
, lp_build_const_int_vec(src_type
, mask
), "");
157 * Inverse of lp_build_clamped_float_to_unsigned_norm above.
158 * Ex: src = { i32, i32, i32, i32 } with values in range [0, 2^src_width-1]
159 * return {float, float, float, float} with values in range [0, 1].
162 lp_build_unsigned_norm_to_float(LLVMBuilderRef builder
,
164 struct lp_type dst_type
,
167 LLVMTypeRef vec_type
= lp_build_vec_type(dst_type
);
168 LLVMTypeRef int_vec_type
= lp_build_int_vec_type(dst_type
);
173 unsigned long long ubound
;
174 unsigned long long mask
;
178 mantissa
= lp_mantissa(dst_type
);
180 n
= MIN2(mantissa
, src_width
);
182 ubound
= ((unsigned long long)1 << n
);
184 scale
= (double)ubound
/mask
;
185 bias
= (double)((unsigned long long)1 << (mantissa
- n
));
189 if(src_width
> mantissa
) {
190 int shift
= src_width
- mantissa
;
191 res
= LLVMBuildLShr(builder
, res
, lp_build_const_int_vec(dst_type
, shift
), "");
194 bias_
= lp_build_const_vec(dst_type
, bias
);
196 res
= LLVMBuildOr(builder
,
198 LLVMBuildBitCast(builder
, bias_
, int_vec_type
, ""), "");
200 res
= LLVMBuildBitCast(builder
, res
, vec_type
, "");
202 res
= LLVMBuildSub(builder
, res
, bias_
, "");
203 res
= LLVMBuildMul(builder
, res
, lp_build_const_vec(dst_type
, scale
), "");
210 * Generic type conversion.
212 * TODO: Take a precision argument, or even better, add a new precision member
213 * to the lp_type union.
216 lp_build_conv(LLVMBuilderRef builder
,
217 struct lp_type src_type
,
218 struct lp_type dst_type
,
219 const LLVMValueRef
*src
, unsigned num_srcs
,
220 LLVMValueRef
*dst
, unsigned num_dsts
)
222 struct lp_type tmp_type
;
223 LLVMValueRef tmp
[LP_MAX_VECTOR_LENGTH
];
227 /* We must not loose or gain channels. Only precision */
228 assert(src_type
.length
* num_srcs
== dst_type
.length
* num_dsts
);
230 assert(src_type
.length
<= LP_MAX_VECTOR_LENGTH
);
231 assert(dst_type
.length
<= LP_MAX_VECTOR_LENGTH
);
232 assert(num_srcs
<= LP_MAX_VECTOR_LENGTH
);
233 assert(num_dsts
<= LP_MAX_VECTOR_LENGTH
);
236 for(i
= 0; i
< num_srcs
; ++i
)
244 if(memcmp(&src_type
, &dst_type
, sizeof src_type
) != 0) {
245 struct lp_build_context bld
;
246 double src_min
= lp_const_min(src_type
);
247 double dst_min
= lp_const_min(dst_type
);
248 double src_max
= lp_const_max(src_type
);
249 double dst_max
= lp_const_max(dst_type
);
252 lp_build_context_init(&bld
, builder
, tmp_type
);
254 if(src_min
< dst_min
) {
258 thres
= lp_build_const_vec(src_type
, dst_min
);
259 for(i
= 0; i
< num_tmps
; ++i
)
260 tmp
[i
] = lp_build_max(&bld
, tmp
[i
], thres
);
263 if(src_max
> dst_max
) {
267 thres
= lp_build_const_vec(src_type
, dst_max
);
268 for(i
= 0; i
< num_tmps
; ++i
)
269 tmp
[i
] = lp_build_min(&bld
, tmp
[i
], thres
);
274 * Scale to the narrowest range
277 if(dst_type
.floating
) {
280 else if(tmp_type
.floating
) {
281 if(!dst_type
.fixed
&& !dst_type
.sign
&& dst_type
.norm
) {
282 for(i
= 0; i
< num_tmps
; ++i
) {
283 tmp
[i
] = lp_build_clamped_float_to_unsigned_norm(builder
,
288 tmp_type
.floating
= FALSE
;
291 double dst_scale
= lp_const_scale(dst_type
);
292 LLVMTypeRef tmp_vec_type
;
294 if (dst_scale
!= 1.0) {
295 LLVMValueRef scale
= lp_build_const_vec(tmp_type
, dst_scale
);
296 for(i
= 0; i
< num_tmps
; ++i
)
297 tmp
[i
] = LLVMBuildMul(builder
, tmp
[i
], scale
, "");
300 /* Use an equally sized integer for intermediate computations */
301 tmp_type
.floating
= FALSE
;
302 tmp_vec_type
= lp_build_vec_type(tmp_type
);
303 for(i
= 0; i
< num_tmps
; ++i
) {
306 tmp
[i
] = LLVMBuildFPToSI(builder
, tmp
[i
], tmp_vec_type
, "");
308 tmp
[i
] = LLVMBuildFPToUI(builder
, tmp
[i
], tmp_vec_type
, "");
310 /* FIXME: there is no SSE counterpart for LLVMBuildFPToUI */
311 tmp
[i
] = LLVMBuildFPToSI(builder
, tmp
[i
], tmp_vec_type
, "");
317 unsigned src_shift
= lp_const_shift(src_type
);
318 unsigned dst_shift
= lp_const_shift(dst_type
);
320 /* FIXME: compensate different offsets too */
321 if(src_shift
> dst_shift
) {
322 LLVMValueRef shift
= lp_build_const_int_vec(tmp_type
, src_shift
- dst_shift
);
323 for(i
= 0; i
< num_tmps
; ++i
)
325 tmp
[i
] = LLVMBuildAShr(builder
, tmp
[i
], shift
, "");
327 tmp
[i
] = LLVMBuildLShr(builder
, tmp
[i
], shift
, "");
332 * Truncate or expand bit width
334 * No data conversion should happen here, although the sign bits are
335 * crucial to avoid bad clamping.
339 struct lp_type new_type
;
342 new_type
.sign
= dst_type
.sign
;
343 new_type
.width
= dst_type
.width
;
344 new_type
.length
= dst_type
.length
;
346 lp_build_resize(builder
, tmp_type
, new_type
, tmp
, num_srcs
, tmp
, num_dsts
);
353 * Scale to the widest range
356 if(src_type
.floating
) {
359 else if(!src_type
.floating
&& dst_type
.floating
) {
360 if(!src_type
.fixed
&& !src_type
.sign
&& src_type
.norm
) {
361 for(i
= 0; i
< num_tmps
; ++i
) {
362 tmp
[i
] = lp_build_unsigned_norm_to_float(builder
,
367 tmp_type
.floating
= TRUE
;
370 double src_scale
= lp_const_scale(src_type
);
371 LLVMTypeRef tmp_vec_type
;
373 /* Use an equally sized integer for intermediate computations */
374 tmp_type
.floating
= TRUE
;
375 tmp_type
.sign
= TRUE
;
376 tmp_vec_type
= lp_build_vec_type(tmp_type
);
377 for(i
= 0; i
< num_tmps
; ++i
) {
380 tmp
[i
] = LLVMBuildSIToFP(builder
, tmp
[i
], tmp_vec_type
, "");
382 tmp
[i
] = LLVMBuildUIToFP(builder
, tmp
[i
], tmp_vec_type
, "");
384 /* FIXME: there is no SSE counterpart for LLVMBuildUIToFP */
385 tmp
[i
] = LLVMBuildSIToFP(builder
, tmp
[i
], tmp_vec_type
, "");
389 if (src_scale
!= 1.0) {
390 LLVMValueRef scale
= lp_build_const_vec(tmp_type
, 1.0/src_scale
);
391 for(i
= 0; i
< num_tmps
; ++i
)
392 tmp
[i
] = LLVMBuildMul(builder
, tmp
[i
], scale
, "");
397 unsigned src_shift
= lp_const_shift(src_type
);
398 unsigned dst_shift
= lp_const_shift(dst_type
);
400 /* FIXME: compensate different offsets too */
401 if(src_shift
< dst_shift
) {
402 LLVMValueRef shift
= lp_build_const_int_vec(tmp_type
, dst_shift
- src_shift
);
403 for(i
= 0; i
< num_tmps
; ++i
)
404 tmp
[i
] = LLVMBuildShl(builder
, tmp
[i
], shift
, "");
408 for(i
= 0; i
< num_dsts
; ++i
)
414 * Bit mask conversion.
416 * This will convert the integer masks that match the given types.
418 * The mask values should 0 or -1, i.e., all bits either set to zero or one.
419 * Any other value will likely cause in unpredictable results.
421 * This is basically a very trimmed down version of lp_build_conv.
424 lp_build_conv_mask(LLVMBuilderRef builder
,
425 struct lp_type src_type
,
426 struct lp_type dst_type
,
427 const LLVMValueRef
*src
, unsigned num_srcs
,
428 LLVMValueRef
*dst
, unsigned num_dsts
)
430 /* Register width must remain constant */
431 assert(src_type
.width
* src_type
.length
== dst_type
.width
* dst_type
.length
);
433 /* We must not loose or gain channels. Only precision */
434 assert(src_type
.length
* num_srcs
== dst_type
.length
* num_dsts
);
439 * We assume all values are 0 or -1
442 src_type
.floating
= FALSE
;
443 src_type
.fixed
= FALSE
;
444 src_type
.sign
= TRUE
;
445 src_type
.norm
= FALSE
;
447 dst_type
.floating
= FALSE
;
448 dst_type
.fixed
= FALSE
;
449 dst_type
.sign
= TRUE
;
450 dst_type
.norm
= FALSE
;
453 * Truncate or expand bit width
456 if(src_type
.width
> dst_type
.width
) {
457 assert(num_dsts
== 1);
458 dst
[0] = lp_build_pack(builder
, src_type
, dst_type
, TRUE
, src
, num_srcs
);
460 else if(src_type
.width
< dst_type
.width
) {
461 assert(num_srcs
== 1);
462 lp_build_unpack(builder
, src_type
, dst_type
, src
[0], dst
, num_dsts
);
465 assert(num_srcs
== num_dsts
);
466 memcpy(dst
, src
, num_dsts
* sizeof *dst
);