1 /**************************************************************************
3 * Copyright 2009-2010 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 **************************************************************************/
33 * LLVM IR doesn't support all basic arithmetic operations we care about (most
34 * notably min/max and saturated operations), and it is often necessary to
35 * resort machine-specific intrinsics directly. The functions here hide all
36 * these implementation details from the other modules.
38 * We also do simple expressions simplification here. Reasons are:
39 * - it is very easy given we have all necessary information readily available
40 * - LLVM optimization passes fail to simplify several vector expressions
41 * - We often know value constraints which the optimization passes have no way
42 * of knowing, such as when source arguments are known to be in [0, 1] range.
44 * @author Jose Fonseca <jfonseca@vmware.com>
50 #include "util/u_memory.h"
51 #include "util/u_debug.h"
52 #include "util/u_math.h"
53 #include "util/u_string.h"
54 #include "util/u_cpu_detect.h"
56 #include "lp_bld_type.h"
57 #include "lp_bld_const.h"
58 #include "lp_bld_init.h"
59 #include "lp_bld_intr.h"
60 #include "lp_bld_logic.h"
61 #include "lp_bld_pack.h"
62 #include "lp_bld_debug.h"
63 #include "lp_bld_bitarit.h"
64 #include "lp_bld_arit.h"
65 #include "lp_bld_flow.h"
68 #define EXP_POLY_DEGREE 5
70 #define LOG_POLY_DEGREE 4
75 * No checks for special case values of a or b = 1 or 0 are done.
76 * NaN's are handled according to the behavior specified by the
77 * nan_behavior argument.
80 lp_build_min_simple(struct lp_build_context
*bld
,
83 enum gallivm_nan_behavior nan_behavior
)
85 const struct lp_type type
= bld
->type
;
86 const char *intrinsic
= NULL
;
87 unsigned intr_size
= 0;
90 assert(lp_check_value(type
, a
));
91 assert(lp_check_value(type
, b
));
93 /* TODO: optimize the constant case */
95 if (type
.floating
&& util_cpu_caps
.has_sse
) {
96 if (type
.width
== 32) {
97 if (type
.length
== 1) {
98 intrinsic
= "llvm.x86.sse.min.ss";
101 else if (type
.length
<= 4 || !util_cpu_caps
.has_avx
) {
102 intrinsic
= "llvm.x86.sse.min.ps";
106 intrinsic
= "llvm.x86.avx.min.ps.256";
110 if (type
.width
== 64 && util_cpu_caps
.has_sse2
) {
111 if (type
.length
== 1) {
112 intrinsic
= "llvm.x86.sse2.min.sd";
115 else if (type
.length
== 2 || !util_cpu_caps
.has_avx
) {
116 intrinsic
= "llvm.x86.sse2.min.pd";
120 intrinsic
= "llvm.x86.avx.min.pd.256";
125 else if (type
.floating
&& util_cpu_caps
.has_altivec
) {
126 debug_printf("%s: altivec doesn't support nan behavior modes\n",
128 if (type
.width
== 32 && type
.length
== 4) {
129 intrinsic
= "llvm.ppc.altivec.vminfp";
132 } else if (util_cpu_caps
.has_sse2
&& type
.length
>= 2) {
134 if ((type
.width
== 8 || type
.width
== 16) &&
135 (type
.width
* type
.length
<= 64) &&
136 (gallivm_debug
& GALLIVM_DEBUG_PERF
)) {
137 debug_printf("%s: inefficient code, bogus shuffle due to packing\n",
140 if (type
.width
== 8 && !type
.sign
) {
141 intrinsic
= "llvm.x86.sse2.pminu.b";
143 else if (type
.width
== 16 && type
.sign
) {
144 intrinsic
= "llvm.x86.sse2.pmins.w";
146 if (util_cpu_caps
.has_sse4_1
) {
147 if (type
.width
== 8 && type
.sign
) {
148 intrinsic
= "llvm.x86.sse41.pminsb";
150 if (type
.width
== 16 && !type
.sign
) {
151 intrinsic
= "llvm.x86.sse41.pminuw";
153 if (type
.width
== 32 && !type
.sign
) {
154 intrinsic
= "llvm.x86.sse41.pminud";
156 if (type
.width
== 32 && type
.sign
) {
157 intrinsic
= "llvm.x86.sse41.pminsd";
160 } else if (util_cpu_caps
.has_altivec
) {
162 debug_printf("%s: altivec doesn't support nan behavior modes\n",
164 if (type
.width
== 8) {
166 intrinsic
= "llvm.ppc.altivec.vminub";
168 intrinsic
= "llvm.ppc.altivec.vminsb";
170 } else if (type
.width
== 16) {
172 intrinsic
= "llvm.ppc.altivec.vminuh";
174 intrinsic
= "llvm.ppc.altivec.vminsh";
176 } else if (type
.width
== 32) {
178 intrinsic
= "llvm.ppc.altivec.vminuw";
180 intrinsic
= "llvm.ppc.altivec.vminsw";
186 /* We need to handle nan's for floating point numbers. If one of the
187 * inputs is nan the other should be returned (required by both D3D10+
189 * The sse intrinsics return the second operator in case of nan by
190 * default so we need to special code to handle those.
192 if (util_cpu_caps
.has_sse
&& type
.floating
&&
193 nan_behavior
!= GALLIVM_NAN_BEHAVIOR_UNDEFINED
&&
194 nan_behavior
!= GALLIVM_NAN_RETURN_SECOND
) {
195 LLVMValueRef isnan
, max
;
196 max
= lp_build_intrinsic_binary_anylength(bld
->gallivm
, intrinsic
,
199 if (nan_behavior
== GALLIVM_NAN_RETURN_OTHER
) {
200 isnan
= lp_build_isnan(bld
, b
);
201 return lp_build_select(bld
, isnan
, a
, max
);
203 assert(nan_behavior
== GALLIVM_NAN_RETURN_NAN
);
204 isnan
= lp_build_isnan(bld
, a
);
205 return lp_build_select(bld
, isnan
, a
, max
);
208 return lp_build_intrinsic_binary_anylength(bld
->gallivm
, intrinsic
,
215 switch (nan_behavior
) {
216 case GALLIVM_NAN_RETURN_NAN
: {
217 LLVMValueRef isnan
= lp_build_isnan(bld
, b
);
218 cond
= lp_build_cmp(bld
, PIPE_FUNC_LESS
, a
, b
);
219 cond
= LLVMBuildXor(bld
->gallivm
->builder
, cond
, isnan
, "");
220 return lp_build_select(bld
, cond
, a
, b
);
223 case GALLIVM_NAN_RETURN_OTHER
: {
224 LLVMValueRef isnan
= lp_build_isnan(bld
, a
);
225 cond
= lp_build_cmp(bld
, PIPE_FUNC_LESS
, a
, b
);
226 cond
= LLVMBuildXor(bld
->gallivm
->builder
, cond
, isnan
, "");
227 return lp_build_select(bld
, cond
, a
, b
);
230 case GALLIVM_NAN_RETURN_SECOND
:
231 cond
= lp_build_cmp_ordered(bld
, PIPE_FUNC_LESS
, a
, b
);
232 return lp_build_select(bld
, cond
, a
, b
);
233 case GALLIVM_NAN_BEHAVIOR_UNDEFINED
:
234 cond
= lp_build_cmp(bld
, PIPE_FUNC_LESS
, a
, b
);
235 return lp_build_select(bld
, cond
, a
, b
);
239 cond
= lp_build_cmp(bld
, PIPE_FUNC_LESS
, a
, b
);
240 return lp_build_select(bld
, cond
, a
, b
);
243 cond
= lp_build_cmp(bld
, PIPE_FUNC_LESS
, a
, b
);
244 return lp_build_select(bld
, cond
, a
, b
);
251 * No checks for special case values of a or b = 1 or 0 are done.
252 * NaN's are handled according to the behavior specified by the
253 * nan_behavior argument.
256 lp_build_max_simple(struct lp_build_context
*bld
,
259 enum gallivm_nan_behavior nan_behavior
)
261 const struct lp_type type
= bld
->type
;
262 const char *intrinsic
= NULL
;
263 unsigned intr_size
= 0;
266 assert(lp_check_value(type
, a
));
267 assert(lp_check_value(type
, b
));
269 /* TODO: optimize the constant case */
271 if (type
.floating
&& util_cpu_caps
.has_sse
) {
272 if (type
.width
== 32) {
273 if (type
.length
== 1) {
274 intrinsic
= "llvm.x86.sse.max.ss";
277 else if (type
.length
<= 4 || !util_cpu_caps
.has_avx
) {
278 intrinsic
= "llvm.x86.sse.max.ps";
282 intrinsic
= "llvm.x86.avx.max.ps.256";
286 if (type
.width
== 64 && util_cpu_caps
.has_sse2
) {
287 if (type
.length
== 1) {
288 intrinsic
= "llvm.x86.sse2.max.sd";
291 else if (type
.length
== 2 || !util_cpu_caps
.has_avx
) {
292 intrinsic
= "llvm.x86.sse2.max.pd";
296 intrinsic
= "llvm.x86.avx.max.pd.256";
301 else if (type
.floating
&& util_cpu_caps
.has_altivec
) {
302 debug_printf("%s: altivec doesn't support nan behavior modes\n",
304 if (type
.width
== 32 || type
.length
== 4) {
305 intrinsic
= "llvm.ppc.altivec.vmaxfp";
308 } else if (util_cpu_caps
.has_sse2
&& type
.length
>= 2) {
310 if ((type
.width
== 8 || type
.width
== 16) &&
311 (type
.width
* type
.length
<= 64) &&
312 (gallivm_debug
& GALLIVM_DEBUG_PERF
)) {
313 debug_printf("%s: inefficient code, bogus shuffle due to packing\n",
316 if (type
.width
== 8 && !type
.sign
) {
317 intrinsic
= "llvm.x86.sse2.pmaxu.b";
320 else if (type
.width
== 16 && type
.sign
) {
321 intrinsic
= "llvm.x86.sse2.pmaxs.w";
323 if (util_cpu_caps
.has_sse4_1
) {
324 if (type
.width
== 8 && type
.sign
) {
325 intrinsic
= "llvm.x86.sse41.pmaxsb";
327 if (type
.width
== 16 && !type
.sign
) {
328 intrinsic
= "llvm.x86.sse41.pmaxuw";
330 if (type
.width
== 32 && !type
.sign
) {
331 intrinsic
= "llvm.x86.sse41.pmaxud";
333 if (type
.width
== 32 && type
.sign
) {
334 intrinsic
= "llvm.x86.sse41.pmaxsd";
337 } else if (util_cpu_caps
.has_altivec
) {
339 debug_printf("%s: altivec doesn't support nan behavior modes\n",
341 if (type
.width
== 8) {
343 intrinsic
= "llvm.ppc.altivec.vmaxub";
345 intrinsic
= "llvm.ppc.altivec.vmaxsb";
347 } else if (type
.width
== 16) {
349 intrinsic
= "llvm.ppc.altivec.vmaxuh";
351 intrinsic
= "llvm.ppc.altivec.vmaxsh";
353 } else if (type
.width
== 32) {
355 intrinsic
= "llvm.ppc.altivec.vmaxuw";
357 intrinsic
= "llvm.ppc.altivec.vmaxsw";
363 if (util_cpu_caps
.has_sse
&& type
.floating
&&
364 nan_behavior
!= GALLIVM_NAN_BEHAVIOR_UNDEFINED
&&
365 nan_behavior
!= GALLIVM_NAN_RETURN_SECOND
) {
366 LLVMValueRef isnan
, min
;
367 min
= lp_build_intrinsic_binary_anylength(bld
->gallivm
, intrinsic
,
370 if (nan_behavior
== GALLIVM_NAN_RETURN_OTHER
) {
371 isnan
= lp_build_isnan(bld
, b
);
372 return lp_build_select(bld
, isnan
, a
, min
);
374 assert(nan_behavior
== GALLIVM_NAN_RETURN_NAN
);
375 isnan
= lp_build_isnan(bld
, a
);
376 return lp_build_select(bld
, isnan
, a
, min
);
379 return lp_build_intrinsic_binary_anylength(bld
->gallivm
, intrinsic
,
386 switch (nan_behavior
) {
387 case GALLIVM_NAN_RETURN_NAN
: {
388 LLVMValueRef isnan
= lp_build_isnan(bld
, b
);
389 cond
= lp_build_cmp(bld
, PIPE_FUNC_GREATER
, a
, b
);
390 cond
= LLVMBuildXor(bld
->gallivm
->builder
, cond
, isnan
, "");
391 return lp_build_select(bld
, cond
, a
, b
);
394 case GALLIVM_NAN_RETURN_OTHER
: {
395 LLVMValueRef isnan
= lp_build_isnan(bld
, a
);
396 cond
= lp_build_cmp(bld
, PIPE_FUNC_GREATER
, a
, b
);
397 cond
= LLVMBuildXor(bld
->gallivm
->builder
, cond
, isnan
, "");
398 return lp_build_select(bld
, cond
, a
, b
);
401 case GALLIVM_NAN_RETURN_SECOND
:
402 cond
= lp_build_cmp_ordered(bld
, PIPE_FUNC_GREATER
, a
, b
);
403 return lp_build_select(bld
, cond
, a
, b
);
404 case GALLIVM_NAN_BEHAVIOR_UNDEFINED
:
405 cond
= lp_build_cmp(bld
, PIPE_FUNC_GREATER
, a
, b
);
406 return lp_build_select(bld
, cond
, a
, b
);
410 cond
= lp_build_cmp(bld
, PIPE_FUNC_GREATER
, a
, b
);
411 return lp_build_select(bld
, cond
, a
, b
);
414 cond
= lp_build_cmp(bld
, PIPE_FUNC_GREATER
, a
, b
);
415 return lp_build_select(bld
, cond
, a
, b
);
421 * Generate 1 - a, or ~a depending on bld->type.
424 lp_build_comp(struct lp_build_context
*bld
,
427 LLVMBuilderRef builder
= bld
->gallivm
->builder
;
428 const struct lp_type type
= bld
->type
;
430 assert(lp_check_value(type
, a
));
437 if(type
.norm
&& !type
.floating
&& !type
.fixed
&& !type
.sign
) {
438 if(LLVMIsConstant(a
))
439 return LLVMConstNot(a
);
441 return LLVMBuildNot(builder
, a
, "");
444 if(LLVMIsConstant(a
))
446 return LLVMConstFSub(bld
->one
, a
);
448 return LLVMConstSub(bld
->one
, a
);
451 return LLVMBuildFSub(builder
, bld
->one
, a
, "");
453 return LLVMBuildSub(builder
, bld
->one
, a
, "");
461 lp_build_add(struct lp_build_context
*bld
,
465 LLVMBuilderRef builder
= bld
->gallivm
->builder
;
466 const struct lp_type type
= bld
->type
;
469 assert(lp_check_value(type
, a
));
470 assert(lp_check_value(type
, b
));
476 if(a
== bld
->undef
|| b
== bld
->undef
)
480 const char *intrinsic
= NULL
;
482 if(a
== bld
->one
|| b
== bld
->one
)
485 if (type
.width
* type
.length
== 128 &&
486 !type
.floating
&& !type
.fixed
) {
487 if(util_cpu_caps
.has_sse2
) {
489 intrinsic
= type
.sign
? "llvm.x86.sse2.padds.b" : "llvm.x86.sse2.paddus.b";
491 intrinsic
= type
.sign
? "llvm.x86.sse2.padds.w" : "llvm.x86.sse2.paddus.w";
492 } else if (util_cpu_caps
.has_altivec
) {
494 intrinsic
= type
.sign
? "llvm.ppc.altivec.vaddsbs" : "llvm.ppc.altivec.vaddubs";
496 intrinsic
= type
.sign
? "llvm.ppc.altivec.vaddshs" : "llvm.ppc.altivec.vadduhs";
501 return lp_build_intrinsic_binary(builder
, intrinsic
, lp_build_vec_type(bld
->gallivm
, bld
->type
), a
, b
);
504 /* TODO: handle signed case */
505 if(type
.norm
&& !type
.floating
&& !type
.fixed
&& !type
.sign
)
506 a
= lp_build_min_simple(bld
, a
, lp_build_comp(bld
, b
), GALLIVM_NAN_BEHAVIOR_UNDEFINED
);
508 if(LLVMIsConstant(a
) && LLVMIsConstant(b
))
510 res
= LLVMConstFAdd(a
, b
);
512 res
= LLVMConstAdd(a
, b
);
515 res
= LLVMBuildFAdd(builder
, a
, b
, "");
517 res
= LLVMBuildAdd(builder
, a
, b
, "");
519 /* clamp to ceiling of 1.0 */
520 if(bld
->type
.norm
&& (bld
->type
.floating
|| bld
->type
.fixed
))
521 res
= lp_build_min_simple(bld
, res
, bld
->one
, GALLIVM_NAN_BEHAVIOR_UNDEFINED
);
523 /* XXX clamp to floor of -1 or 0??? */
529 /** Return the scalar sum of the elements of a.
530 * Should avoid this operation whenever possible.
533 lp_build_horizontal_add(struct lp_build_context
*bld
,
536 LLVMBuilderRef builder
= bld
->gallivm
->builder
;
537 const struct lp_type type
= bld
->type
;
538 LLVMValueRef index
, res
;
540 LLVMValueRef shuffles1
[LP_MAX_VECTOR_LENGTH
/ 2];
541 LLVMValueRef shuffles2
[LP_MAX_VECTOR_LENGTH
/ 2];
542 LLVMValueRef vecres
, elem2
;
544 assert(lp_check_value(type
, a
));
546 if (type
.length
== 1) {
550 assert(!bld
->type
.norm
);
553 * for byte vectors can do much better with psadbw.
554 * Using repeated shuffle/adds here. Note with multiple vectors
555 * this can be done more efficiently as outlined in the intel
556 * optimization manual.
557 * Note: could cause data rearrangement if used with smaller element
562 length
= type
.length
/ 2;
564 LLVMValueRef vec1
, vec2
;
565 for (i
= 0; i
< length
; i
++) {
566 shuffles1
[i
] = lp_build_const_int32(bld
->gallivm
, i
);
567 shuffles2
[i
] = lp_build_const_int32(bld
->gallivm
, i
+ length
);
569 vec1
= LLVMBuildShuffleVector(builder
, vecres
, vecres
,
570 LLVMConstVector(shuffles1
, length
), "");
571 vec2
= LLVMBuildShuffleVector(builder
, vecres
, vecres
,
572 LLVMConstVector(shuffles2
, length
), "");
574 vecres
= LLVMBuildFAdd(builder
, vec1
, vec2
, "");
577 vecres
= LLVMBuildAdd(builder
, vec1
, vec2
, "");
579 length
= length
>> 1;
582 /* always have vector of size 2 here */
585 index
= lp_build_const_int32(bld
->gallivm
, 0);
586 res
= LLVMBuildExtractElement(builder
, vecres
, index
, "");
587 index
= lp_build_const_int32(bld
->gallivm
, 1);
588 elem2
= LLVMBuildExtractElement(builder
, vecres
, index
, "");
591 res
= LLVMBuildFAdd(builder
, res
, elem2
, "");
593 res
= LLVMBuildAdd(builder
, res
, elem2
, "");
599 * Return the horizontal sums of 4 float vectors as a float4 vector.
600 * This uses the technique as outlined in Intel Optimization Manual.
603 lp_build_horizontal_add4x4f(struct lp_build_context
*bld
,
606 struct gallivm_state
*gallivm
= bld
->gallivm
;
607 LLVMBuilderRef builder
= gallivm
->builder
;
608 LLVMValueRef shuffles
[4];
610 LLVMValueRef sumtmp
[2], shuftmp
[2];
612 /* lower half of regs */
613 shuffles
[0] = lp_build_const_int32(gallivm
, 0);
614 shuffles
[1] = lp_build_const_int32(gallivm
, 1);
615 shuffles
[2] = lp_build_const_int32(gallivm
, 4);
616 shuffles
[3] = lp_build_const_int32(gallivm
, 5);
617 tmp
[0] = LLVMBuildShuffleVector(builder
, src
[0], src
[1],
618 LLVMConstVector(shuffles
, 4), "");
619 tmp
[2] = LLVMBuildShuffleVector(builder
, src
[2], src
[3],
620 LLVMConstVector(shuffles
, 4), "");
622 /* upper half of regs */
623 shuffles
[0] = lp_build_const_int32(gallivm
, 2);
624 shuffles
[1] = lp_build_const_int32(gallivm
, 3);
625 shuffles
[2] = lp_build_const_int32(gallivm
, 6);
626 shuffles
[3] = lp_build_const_int32(gallivm
, 7);
627 tmp
[1] = LLVMBuildShuffleVector(builder
, src
[0], src
[1],
628 LLVMConstVector(shuffles
, 4), "");
629 tmp
[3] = LLVMBuildShuffleVector(builder
, src
[2], src
[3],
630 LLVMConstVector(shuffles
, 4), "");
632 sumtmp
[0] = LLVMBuildFAdd(builder
, tmp
[0], tmp
[1], "");
633 sumtmp
[1] = LLVMBuildFAdd(builder
, tmp
[2], tmp
[3], "");
635 shuffles
[0] = lp_build_const_int32(gallivm
, 0);
636 shuffles
[1] = lp_build_const_int32(gallivm
, 2);
637 shuffles
[2] = lp_build_const_int32(gallivm
, 4);
638 shuffles
[3] = lp_build_const_int32(gallivm
, 6);
639 shuftmp
[0] = LLVMBuildShuffleVector(builder
, sumtmp
[0], sumtmp
[1],
640 LLVMConstVector(shuffles
, 4), "");
642 shuffles
[0] = lp_build_const_int32(gallivm
, 1);
643 shuffles
[1] = lp_build_const_int32(gallivm
, 3);
644 shuffles
[2] = lp_build_const_int32(gallivm
, 5);
645 shuffles
[3] = lp_build_const_int32(gallivm
, 7);
646 shuftmp
[1] = LLVMBuildShuffleVector(builder
, sumtmp
[0], sumtmp
[1],
647 LLVMConstVector(shuffles
, 4), "");
649 return LLVMBuildFAdd(builder
, shuftmp
[0], shuftmp
[1], "");
654 * partially horizontally add 2-4 float vectors with length nx4,
655 * i.e. only four adjacent values in each vector will be added,
656 * assuming values are really grouped in 4 which also determines
659 * Return a vector of the same length as the initial vectors,
660 * with the excess elements (if any) being undefined.
661 * The element order is independent of number of input vectors.
662 * For 3 vectors x0x1x2x3x4x5x6x7, y0y1y2y3y4y5y6y7, z0z1z2z3z4z5z6z7
663 * the output order thus will be
664 * sumx0-x3,sumy0-y3,sumz0-z3,undef,sumx4-x7,sumy4-y7,sumz4z7,undef
667 lp_build_hadd_partial4(struct lp_build_context
*bld
,
668 LLVMValueRef vectors
[],
671 struct gallivm_state
*gallivm
= bld
->gallivm
;
672 LLVMBuilderRef builder
= gallivm
->builder
;
673 LLVMValueRef ret_vec
;
675 const char *intrinsic
= NULL
;
677 assert(num_vecs
>= 2 && num_vecs
<= 4);
678 assert(bld
->type
.floating
);
680 /* only use this with at least 2 vectors, as it is sort of expensive
681 * (depending on cpu) and we always need two horizontal adds anyway,
682 * so a shuffle/add approach might be better.
688 tmp
[2] = num_vecs
> 2 ? vectors
[2] : vectors
[0];
689 tmp
[3] = num_vecs
> 3 ? vectors
[3] : vectors
[0];
691 if (util_cpu_caps
.has_sse3
&& bld
->type
.width
== 32 &&
692 bld
->type
.length
== 4) {
693 intrinsic
= "llvm.x86.sse3.hadd.ps";
695 else if (util_cpu_caps
.has_avx
&& bld
->type
.width
== 32 &&
696 bld
->type
.length
== 8) {
697 intrinsic
= "llvm.x86.avx.hadd.ps.256";
700 tmp
[0] = lp_build_intrinsic_binary(builder
, intrinsic
,
701 lp_build_vec_type(gallivm
, bld
->type
),
704 tmp
[1] = lp_build_intrinsic_binary(builder
, intrinsic
,
705 lp_build_vec_type(gallivm
, bld
->type
),
711 return lp_build_intrinsic_binary(builder
, intrinsic
,
712 lp_build_vec_type(gallivm
, bld
->type
),
716 if (bld
->type
.length
== 4) {
717 ret_vec
= lp_build_horizontal_add4x4f(bld
, tmp
);
720 LLVMValueRef partres
[LP_MAX_VECTOR_LENGTH
/4];
722 unsigned num_iter
= bld
->type
.length
/ 4;
723 struct lp_type parttype
= bld
->type
;
725 for (j
= 0; j
< num_iter
; j
++) {
726 LLVMValueRef partsrc
[4];
728 for (i
= 0; i
< 4; i
++) {
729 partsrc
[i
] = lp_build_extract_range(gallivm
, tmp
[i
], j
*4, 4);
731 partres
[j
] = lp_build_horizontal_add4x4f(bld
, partsrc
);
733 ret_vec
= lp_build_concat(gallivm
, partres
, parttype
, num_iter
);
742 lp_build_sub(struct lp_build_context
*bld
,
746 LLVMBuilderRef builder
= bld
->gallivm
->builder
;
747 const struct lp_type type
= bld
->type
;
750 assert(lp_check_value(type
, a
));
751 assert(lp_check_value(type
, b
));
755 if(a
== bld
->undef
|| b
== bld
->undef
)
761 const char *intrinsic
= NULL
;
766 if (type
.width
* type
.length
== 128 &&
767 !type
.floating
&& !type
.fixed
) {
768 if (util_cpu_caps
.has_sse2
) {
770 intrinsic
= type
.sign
? "llvm.x86.sse2.psubs.b" : "llvm.x86.sse2.psubus.b";
772 intrinsic
= type
.sign
? "llvm.x86.sse2.psubs.w" : "llvm.x86.sse2.psubus.w";
773 } else if (util_cpu_caps
.has_altivec
) {
775 intrinsic
= type
.sign
? "llvm.ppc.altivec.vsubsbs" : "llvm.ppc.altivec.vsububs";
777 intrinsic
= type
.sign
? "llvm.ppc.altivec.vsubshs" : "llvm.ppc.altivec.vsubuhs";
782 return lp_build_intrinsic_binary(builder
, intrinsic
, lp_build_vec_type(bld
->gallivm
, bld
->type
), a
, b
);
785 /* TODO: handle signed case */
786 if(type
.norm
&& !type
.floating
&& !type
.fixed
&& !type
.sign
)
787 a
= lp_build_max_simple(bld
, a
, b
, GALLIVM_NAN_BEHAVIOR_UNDEFINED
);
789 if(LLVMIsConstant(a
) && LLVMIsConstant(b
))
791 res
= LLVMConstFSub(a
, b
);
793 res
= LLVMConstSub(a
, b
);
796 res
= LLVMBuildFSub(builder
, a
, b
, "");
798 res
= LLVMBuildSub(builder
, a
, b
, "");
800 if(bld
->type
.norm
&& (bld
->type
.floating
|| bld
->type
.fixed
))
801 res
= lp_build_max_simple(bld
, res
, bld
->zero
, GALLIVM_NAN_BEHAVIOR_UNDEFINED
);
809 * Normalized multiplication.
811 * There are several approaches for (using 8-bit normalized multiplication as
816 * makes the following approximation to the division (Sree)
818 * a*b/255 ~= (a*(b + 1)) >> 256
820 * which is the fastest method that satisfies the following OpenGL criteria of
822 * 0*0 = 0 and 255*255 = 255
826 * takes the geometric series approximation to the division
828 * t/255 = (t >> 8) + (t >> 16) + (t >> 24) ..
830 * in this case just the first two terms to fit in 16bit arithmetic
832 * t/255 ~= (t + (t >> 8)) >> 8
834 * note that just by itself it doesn't satisfies the OpenGL criteria, as
835 * 255*255 = 254, so the special case b = 255 must be accounted or roundoff
838 * - geometric series plus rounding
840 * when using a geometric series division instead of truncating the result
841 * use roundoff in the approximation (Jim Blinn)
843 * t/255 ~= (t + (t >> 8) + 0x80) >> 8
845 * achieving the exact results.
849 * @sa Alvy Ray Smith, Image Compositing Fundamentals, Tech Memo 4, Aug 15, 1995,
850 * ftp://ftp.alvyray.com/Acrobat/4_Comp.pdf
851 * @sa Michael Herf, The "double blend trick", May 2000,
852 * http://www.stereopsis.com/doubleblend.html
855 lp_build_mul_norm(struct gallivm_state
*gallivm
,
856 struct lp_type wide_type
,
857 LLVMValueRef a
, LLVMValueRef b
)
859 LLVMBuilderRef builder
= gallivm
->builder
;
860 struct lp_build_context bld
;
865 assert(!wide_type
.floating
);
866 assert(lp_check_value(wide_type
, a
));
867 assert(lp_check_value(wide_type
, b
));
869 lp_build_context_init(&bld
, gallivm
, wide_type
);
871 n
= wide_type
.width
/ 2;
872 if (wide_type
.sign
) {
877 * TODO: for 16bits normalized SSE2 vectors we could consider using PMULHUW
878 * http://ssp.impulsetrain.com/2011/07/03/multiplying-normalized-16-bit-numbers-with-sse2/
882 * a*b / (2**n - 1) ~= (a*b + (a*b >> n) + half) >> n
885 ab
= LLVMBuildMul(builder
, a
, b
, "");
886 ab
= LLVMBuildAdd(builder
, ab
, lp_build_shr_imm(&bld
, ab
, n
), "");
889 * half = sgn(ab) * 0.5 * (2 ** n) = sgn(ab) * (1 << (n - 1))
892 half
= lp_build_const_int_vec(gallivm
, wide_type
, 1 << (n
- 1));
893 if (wide_type
.sign
) {
894 LLVMValueRef minus_half
= LLVMBuildNeg(builder
, half
, "");
895 LLVMValueRef sign
= lp_build_shr_imm(&bld
, ab
, wide_type
.width
- 1);
896 half
= lp_build_select(&bld
, sign
, minus_half
, half
);
898 ab
= LLVMBuildAdd(builder
, ab
, half
, "");
901 ab
= lp_build_shr_imm(&bld
, ab
, n
);
910 lp_build_mul(struct lp_build_context
*bld
,
914 LLVMBuilderRef builder
= bld
->gallivm
->builder
;
915 const struct lp_type type
= bld
->type
;
919 assert(lp_check_value(type
, a
));
920 assert(lp_check_value(type
, b
));
930 if(a
== bld
->undef
|| b
== bld
->undef
)
933 if (!type
.floating
&& !type
.fixed
&& type
.norm
) {
934 struct lp_type wide_type
= lp_wider_type(type
);
935 LLVMValueRef al
, ah
, bl
, bh
, abl
, abh
, ab
;
937 lp_build_unpack2(bld
->gallivm
, type
, wide_type
, a
, &al
, &ah
);
938 lp_build_unpack2(bld
->gallivm
, type
, wide_type
, b
, &bl
, &bh
);
940 /* PMULLW, PSRLW, PADDW */
941 abl
= lp_build_mul_norm(bld
->gallivm
, wide_type
, al
, bl
);
942 abh
= lp_build_mul_norm(bld
->gallivm
, wide_type
, ah
, bh
);
944 ab
= lp_build_pack2(bld
->gallivm
, wide_type
, type
, abl
, abh
);
950 shift
= lp_build_const_int_vec(bld
->gallivm
, type
, type
.width
/2);
954 if(LLVMIsConstant(a
) && LLVMIsConstant(b
)) {
956 res
= LLVMConstFMul(a
, b
);
958 res
= LLVMConstMul(a
, b
);
961 res
= LLVMConstAShr(res
, shift
);
963 res
= LLVMConstLShr(res
, shift
);
968 res
= LLVMBuildFMul(builder
, a
, b
, "");
970 res
= LLVMBuildMul(builder
, a
, b
, "");
973 res
= LLVMBuildAShr(builder
, res
, shift
, "");
975 res
= LLVMBuildLShr(builder
, res
, shift
, "");
984 * Small vector x scale multiplication optimization.
987 lp_build_mul_imm(struct lp_build_context
*bld
,
991 LLVMBuilderRef builder
= bld
->gallivm
->builder
;
994 assert(lp_check_value(bld
->type
, a
));
1003 return lp_build_negate(bld
, a
);
1005 if(b
== 2 && bld
->type
.floating
)
1006 return lp_build_add(bld
, a
, a
);
1008 if(util_is_power_of_two(b
)) {
1009 unsigned shift
= ffs(b
) - 1;
1011 if(bld
->type
.floating
) {
1014 * Power of two multiplication by directly manipulating the exponent.
1016 * XXX: This might not be always faster, it will introduce a small error
1017 * for multiplication by zero, and it will produce wrong results
1020 unsigned mantissa
= lp_mantissa(bld
->type
);
1021 factor
= lp_build_const_int_vec(bld
->gallivm
, bld
->type
, (unsigned long long)shift
<< mantissa
);
1022 a
= LLVMBuildBitCast(builder
, a
, lp_build_int_vec_type(bld
->type
), "");
1023 a
= LLVMBuildAdd(builder
, a
, factor
, "");
1024 a
= LLVMBuildBitCast(builder
, a
, lp_build_vec_type(bld
->gallivm
, bld
->type
), "");
1029 factor
= lp_build_const_vec(bld
->gallivm
, bld
->type
, shift
);
1030 return LLVMBuildShl(builder
, a
, factor
, "");
1034 factor
= lp_build_const_vec(bld
->gallivm
, bld
->type
, (double)b
);
1035 return lp_build_mul(bld
, a
, factor
);
1043 lp_build_div(struct lp_build_context
*bld
,
1047 LLVMBuilderRef builder
= bld
->gallivm
->builder
;
1048 const struct lp_type type
= bld
->type
;
1050 assert(lp_check_value(type
, a
));
1051 assert(lp_check_value(type
, b
));
1056 return lp_build_rcp(bld
, b
);
1061 if(a
== bld
->undef
|| b
== bld
->undef
)
1064 if(LLVMIsConstant(a
) && LLVMIsConstant(b
)) {
1066 return LLVMConstFDiv(a
, b
);
1068 return LLVMConstSDiv(a
, b
);
1070 return LLVMConstUDiv(a
, b
);
1073 if(((util_cpu_caps
.has_sse
&& type
.width
== 32 && type
.length
== 4) ||
1074 (util_cpu_caps
.has_avx
&& type
.width
== 32 && type
.length
== 8)) &&
1076 return lp_build_mul(bld
, a
, lp_build_rcp(bld
, b
));
1079 return LLVMBuildFDiv(builder
, a
, b
, "");
1081 return LLVMBuildSDiv(builder
, a
, b
, "");
1083 return LLVMBuildUDiv(builder
, a
, b
, "");
1088 * Linear interpolation helper.
1090 * @param normalized whether we are interpolating normalized values,
1091 * encoded in normalized integers, twice as wide.
1093 * @sa http://www.stereopsis.com/doubleblend.html
1095 static INLINE LLVMValueRef
1096 lp_build_lerp_simple(struct lp_build_context
*bld
,
1102 unsigned half_width
= bld
->type
.width
/2;
1103 LLVMBuilderRef builder
= bld
->gallivm
->builder
;
1107 assert(lp_check_value(bld
->type
, x
));
1108 assert(lp_check_value(bld
->type
, v0
));
1109 assert(lp_check_value(bld
->type
, v1
));
1111 delta
= lp_build_sub(bld
, v1
, v0
);
1113 if (flags
& LP_BLD_LERP_WIDE_NORMALIZED
) {
1114 if (!bld
->type
.sign
) {
1115 if (!(flags
& LP_BLD_LERP_PRESCALED_WEIGHTS
)) {
1117 * Scale x from [0, 2**n - 1] to [0, 2**n] by adding the
1118 * most-significant-bit to the lowest-significant-bit, so that
1119 * later we can just divide by 2**n instead of 2**n - 1.
1122 x
= lp_build_add(bld
, x
, lp_build_shr_imm(bld
, x
, half_width
- 1));
1125 /* (x * delta) >> n */
1126 res
= lp_build_mul(bld
, x
, delta
);
1127 res
= lp_build_shr_imm(bld
, res
, half_width
);
1130 * The rescaling trick above doesn't work for signed numbers, so
1131 * use the 2**n - 1 divison approximation in lp_build_mul_norm
1134 assert(!(flags
& LP_BLD_LERP_PRESCALED_WEIGHTS
));
1135 res
= lp_build_mul_norm(bld
->gallivm
, bld
->type
, x
, delta
);
1138 assert(!(flags
& LP_BLD_LERP_PRESCALED_WEIGHTS
));
1139 res
= lp_build_mul(bld
, x
, delta
);
1142 res
= lp_build_add(bld
, v0
, res
);
1144 if (((flags
& LP_BLD_LERP_WIDE_NORMALIZED
) && !bld
->type
.sign
) ||
1146 /* We need to mask out the high order bits when lerping 8bit normalized colors stored on 16bits */
1147 /* XXX: This step is necessary for lerping 8bit colors stored on 16bits,
1148 * but it will be wrong for true fixed point use cases. Basically we need
1149 * a more powerful lp_type, capable of further distinguishing the values
1150 * interpretation from the value storage. */
1151 res
= LLVMBuildAnd(builder
, res
, lp_build_const_int_vec(bld
->gallivm
, bld
->type
, (1 << half_width
) - 1), "");
1159 * Linear interpolation.
1162 lp_build_lerp(struct lp_build_context
*bld
,
1168 const struct lp_type type
= bld
->type
;
1171 assert(lp_check_value(type
, x
));
1172 assert(lp_check_value(type
, v0
));
1173 assert(lp_check_value(type
, v1
));
1175 assert(!(flags
& LP_BLD_LERP_WIDE_NORMALIZED
));
1178 struct lp_type wide_type
;
1179 struct lp_build_context wide_bld
;
1180 LLVMValueRef xl
, xh
, v0l
, v0h
, v1l
, v1h
, resl
, resh
;
1182 assert(type
.length
>= 2);
1185 * Create a wider integer type, enough to hold the
1186 * intermediate result of the multiplication.
1188 memset(&wide_type
, 0, sizeof wide_type
);
1189 wide_type
.sign
= type
.sign
;
1190 wide_type
.width
= type
.width
*2;
1191 wide_type
.length
= type
.length
/2;
1193 lp_build_context_init(&wide_bld
, bld
->gallivm
, wide_type
);
1195 lp_build_unpack2(bld
->gallivm
, type
, wide_type
, x
, &xl
, &xh
);
1196 lp_build_unpack2(bld
->gallivm
, type
, wide_type
, v0
, &v0l
, &v0h
);
1197 lp_build_unpack2(bld
->gallivm
, type
, wide_type
, v1
, &v1l
, &v1h
);
1203 flags
|= LP_BLD_LERP_WIDE_NORMALIZED
;
1205 resl
= lp_build_lerp_simple(&wide_bld
, xl
, v0l
, v1l
, flags
);
1206 resh
= lp_build_lerp_simple(&wide_bld
, xh
, v0h
, v1h
, flags
);
1208 res
= lp_build_pack2(bld
->gallivm
, wide_type
, type
, resl
, resh
);
1210 res
= lp_build_lerp_simple(bld
, x
, v0
, v1
, flags
);
1218 * Bilinear interpolation.
1220 * Values indices are in v_{yx}.
1223 lp_build_lerp_2d(struct lp_build_context
*bld
,
1232 LLVMValueRef v0
= lp_build_lerp(bld
, x
, v00
, v01
, flags
);
1233 LLVMValueRef v1
= lp_build_lerp(bld
, x
, v10
, v11
, flags
);
1234 return lp_build_lerp(bld
, y
, v0
, v1
, flags
);
1239 lp_build_lerp_3d(struct lp_build_context
*bld
,
1253 LLVMValueRef v0
= lp_build_lerp_2d(bld
, x
, y
, v000
, v001
, v010
, v011
, flags
);
1254 LLVMValueRef v1
= lp_build_lerp_2d(bld
, x
, y
, v100
, v101
, v110
, v111
, flags
);
1255 return lp_build_lerp(bld
, z
, v0
, v1
, flags
);
1260 * Generate min(a, b)
1261 * Do checks for special cases but not for nans.
1264 lp_build_min(struct lp_build_context
*bld
,
1268 assert(lp_check_value(bld
->type
, a
));
1269 assert(lp_check_value(bld
->type
, b
));
1271 if(a
== bld
->undef
|| b
== bld
->undef
)
1277 if (bld
->type
.norm
) {
1278 if (!bld
->type
.sign
) {
1279 if (a
== bld
->zero
|| b
== bld
->zero
) {
1289 return lp_build_min_simple(bld
, a
, b
, GALLIVM_NAN_BEHAVIOR_UNDEFINED
);
1294 * Generate min(a, b)
1295 * NaN's are handled according to the behavior specified by the
1296 * nan_behavior argument.
1299 lp_build_min_ext(struct lp_build_context
*bld
,
1302 enum gallivm_nan_behavior nan_behavior
)
1304 assert(lp_check_value(bld
->type
, a
));
1305 assert(lp_check_value(bld
->type
, b
));
1307 if(a
== bld
->undef
|| b
== bld
->undef
)
1313 if (bld
->type
.norm
) {
1314 if (!bld
->type
.sign
) {
1315 if (a
== bld
->zero
|| b
== bld
->zero
) {
1325 return lp_build_min_simple(bld
, a
, b
, nan_behavior
);
1329 * Generate max(a, b)
1330 * Do checks for special cases, but NaN behavior is undefined.
1333 lp_build_max(struct lp_build_context
*bld
,
1337 assert(lp_check_value(bld
->type
, a
));
1338 assert(lp_check_value(bld
->type
, b
));
1340 if(a
== bld
->undef
|| b
== bld
->undef
)
1346 if(bld
->type
.norm
) {
1347 if(a
== bld
->one
|| b
== bld
->one
)
1349 if (!bld
->type
.sign
) {
1350 if (a
== bld
->zero
) {
1353 if (b
== bld
->zero
) {
1359 return lp_build_max_simple(bld
, a
, b
, GALLIVM_NAN_BEHAVIOR_UNDEFINED
);
1364 * Generate max(a, b)
1365 * Checks for special cases.
1366 * NaN's are handled according to the behavior specified by the
1367 * nan_behavior argument.
1370 lp_build_max_ext(struct lp_build_context
*bld
,
1373 enum gallivm_nan_behavior nan_behavior
)
1375 assert(lp_check_value(bld
->type
, a
));
1376 assert(lp_check_value(bld
->type
, b
));
1378 if(a
== bld
->undef
|| b
== bld
->undef
)
1384 if(bld
->type
.norm
) {
1385 if(a
== bld
->one
|| b
== bld
->one
)
1387 if (!bld
->type
.sign
) {
1388 if (a
== bld
->zero
) {
1391 if (b
== bld
->zero
) {
1397 return lp_build_max_simple(bld
, a
, b
, nan_behavior
);
1401 * Generate clamp(a, min, max)
1402 * Do checks for special cases.
1405 lp_build_clamp(struct lp_build_context
*bld
,
1410 assert(lp_check_value(bld
->type
, a
));
1411 assert(lp_check_value(bld
->type
, min
));
1412 assert(lp_check_value(bld
->type
, max
));
1414 a
= lp_build_min(bld
, a
, max
);
1415 a
= lp_build_max(bld
, a
, min
);
1424 lp_build_abs(struct lp_build_context
*bld
,
1427 LLVMBuilderRef builder
= bld
->gallivm
->builder
;
1428 const struct lp_type type
= bld
->type
;
1429 LLVMTypeRef vec_type
= lp_build_vec_type(bld
->gallivm
, type
);
1431 assert(lp_check_value(type
, a
));
1437 /* Mask out the sign bit */
1438 LLVMTypeRef int_vec_type
= lp_build_int_vec_type(bld
->gallivm
, type
);
1439 unsigned long long absMask
= ~(1ULL << (type
.width
- 1));
1440 LLVMValueRef mask
= lp_build_const_int_vec(bld
->gallivm
, type
, ((unsigned long long) absMask
));
1441 a
= LLVMBuildBitCast(builder
, a
, int_vec_type
, "");
1442 a
= LLVMBuildAnd(builder
, a
, mask
, "");
1443 a
= LLVMBuildBitCast(builder
, a
, vec_type
, "");
1447 if(type
.width
*type
.length
== 128 && util_cpu_caps
.has_ssse3
) {
1448 switch(type
.width
) {
1450 return lp_build_intrinsic_unary(builder
, "llvm.x86.ssse3.pabs.b.128", vec_type
, a
);
1452 return lp_build_intrinsic_unary(builder
, "llvm.x86.ssse3.pabs.w.128", vec_type
, a
);
1454 return lp_build_intrinsic_unary(builder
, "llvm.x86.ssse3.pabs.d.128", vec_type
, a
);
1457 else if (type
.width
*type
.length
== 256 && util_cpu_caps
.has_ssse3
&&
1458 (gallivm_debug
& GALLIVM_DEBUG_PERF
) &&
1459 (type
.width
== 8 || type
.width
== 16 || type
.width
== 32)) {
1460 debug_printf("%s: inefficient code, should split vectors manually\n",
1464 return lp_build_max(bld
, a
, LLVMBuildNeg(builder
, a
, ""));
1469 lp_build_negate(struct lp_build_context
*bld
,
1472 LLVMBuilderRef builder
= bld
->gallivm
->builder
;
1474 assert(lp_check_value(bld
->type
, a
));
1476 #if HAVE_LLVM >= 0x0207
1477 if (bld
->type
.floating
)
1478 a
= LLVMBuildFNeg(builder
, a
, "");
1481 a
= LLVMBuildNeg(builder
, a
, "");
1487 /** Return -1, 0 or +1 depending on the sign of a */
1489 lp_build_sgn(struct lp_build_context
*bld
,
1492 LLVMBuilderRef builder
= bld
->gallivm
->builder
;
1493 const struct lp_type type
= bld
->type
;
1497 assert(lp_check_value(type
, a
));
1499 /* Handle non-zero case */
1501 /* if not zero then sign must be positive */
1504 else if(type
.floating
) {
1505 LLVMTypeRef vec_type
;
1506 LLVMTypeRef int_type
;
1510 unsigned long long maskBit
= (unsigned long long)1 << (type
.width
- 1);
1512 int_type
= lp_build_int_vec_type(bld
->gallivm
, type
);
1513 vec_type
= lp_build_vec_type(bld
->gallivm
, type
);
1514 mask
= lp_build_const_int_vec(bld
->gallivm
, type
, maskBit
);
1516 /* Take the sign bit and add it to 1 constant */
1517 sign
= LLVMBuildBitCast(builder
, a
, int_type
, "");
1518 sign
= LLVMBuildAnd(builder
, sign
, mask
, "");
1519 one
= LLVMConstBitCast(bld
->one
, int_type
);
1520 res
= LLVMBuildOr(builder
, sign
, one
, "");
1521 res
= LLVMBuildBitCast(builder
, res
, vec_type
, "");
1525 /* signed int/norm/fixed point */
1526 /* could use psign with sse3 and appropriate vectors here */
1527 LLVMValueRef minus_one
= lp_build_const_vec(bld
->gallivm
, type
, -1.0);
1528 cond
= lp_build_cmp(bld
, PIPE_FUNC_GREATER
, a
, bld
->zero
);
1529 res
= lp_build_select(bld
, cond
, bld
->one
, minus_one
);
1533 cond
= lp_build_cmp(bld
, PIPE_FUNC_EQUAL
, a
, bld
->zero
);
1534 res
= lp_build_select(bld
, cond
, bld
->zero
, res
);
1541 * Set the sign of float vector 'a' according to 'sign'.
1542 * If sign==0, return abs(a).
1543 * If sign==1, return -abs(a);
1544 * Other values for sign produce undefined results.
1547 lp_build_set_sign(struct lp_build_context
*bld
,
1548 LLVMValueRef a
, LLVMValueRef sign
)
1550 LLVMBuilderRef builder
= bld
->gallivm
->builder
;
1551 const struct lp_type type
= bld
->type
;
1552 LLVMTypeRef int_vec_type
= lp_build_int_vec_type(bld
->gallivm
, type
);
1553 LLVMTypeRef vec_type
= lp_build_vec_type(bld
->gallivm
, type
);
1554 LLVMValueRef shift
= lp_build_const_int_vec(bld
->gallivm
, type
, type
.width
- 1);
1555 LLVMValueRef mask
= lp_build_const_int_vec(bld
->gallivm
, type
,
1556 ~((unsigned long long) 1 << (type
.width
- 1)));
1557 LLVMValueRef val
, res
;
1559 assert(type
.floating
);
1560 assert(lp_check_value(type
, a
));
1562 /* val = reinterpret_cast<int>(a) */
1563 val
= LLVMBuildBitCast(builder
, a
, int_vec_type
, "");
1564 /* val = val & mask */
1565 val
= LLVMBuildAnd(builder
, val
, mask
, "");
1566 /* sign = sign << shift */
1567 sign
= LLVMBuildShl(builder
, sign
, shift
, "");
1568 /* res = val | sign */
1569 res
= LLVMBuildOr(builder
, val
, sign
, "");
1570 /* res = reinterpret_cast<float>(res) */
1571 res
= LLVMBuildBitCast(builder
, res
, vec_type
, "");
1578 * Convert vector of (or scalar) int to vector of (or scalar) float.
1581 lp_build_int_to_float(struct lp_build_context
*bld
,
1584 LLVMBuilderRef builder
= bld
->gallivm
->builder
;
1585 const struct lp_type type
= bld
->type
;
1586 LLVMTypeRef vec_type
= lp_build_vec_type(bld
->gallivm
, type
);
1588 assert(type
.floating
);
1590 return LLVMBuildSIToFP(builder
, a
, vec_type
, "");
1594 arch_rounding_available(const struct lp_type type
)
1596 if ((util_cpu_caps
.has_sse4_1
&&
1597 (type
.length
== 1 || type
.width
*type
.length
== 128)) ||
1598 (util_cpu_caps
.has_avx
&& type
.width
*type
.length
== 256))
1600 else if ((util_cpu_caps
.has_altivec
&&
1601 (type
.width
== 32 && type
.length
== 4)))
1607 enum lp_build_round_mode
1609 LP_BUILD_ROUND_NEAREST
= 0,
1610 LP_BUILD_ROUND_FLOOR
= 1,
1611 LP_BUILD_ROUND_CEIL
= 2,
1612 LP_BUILD_ROUND_TRUNCATE
= 3
1616 * Helper for SSE4.1's ROUNDxx instructions.
1618 * NOTE: In the SSE4.1's nearest mode, if two values are equally close, the
1619 * result is the even value. That is, rounding 2.5 will be 2.0, and not 3.0.
1621 static INLINE LLVMValueRef
1622 lp_build_round_sse41(struct lp_build_context
*bld
,
1624 enum lp_build_round_mode mode
)
1626 LLVMBuilderRef builder
= bld
->gallivm
->builder
;
1627 const struct lp_type type
= bld
->type
;
1628 LLVMTypeRef i32t
= LLVMInt32TypeInContext(bld
->gallivm
->context
);
1629 const char *intrinsic
;
1632 assert(type
.floating
);
1634 assert(lp_check_value(type
, a
));
1635 assert(util_cpu_caps
.has_sse4_1
);
1637 if (type
.length
== 1) {
1638 LLVMTypeRef vec_type
;
1640 LLVMValueRef args
[3];
1641 LLVMValueRef index0
= LLVMConstInt(i32t
, 0, 0);
1643 switch(type
.width
) {
1645 intrinsic
= "llvm.x86.sse41.round.ss";
1648 intrinsic
= "llvm.x86.sse41.round.sd";
1655 vec_type
= LLVMVectorType(bld
->elem_type
, 4);
1657 undef
= LLVMGetUndef(vec_type
);
1660 args
[1] = LLVMBuildInsertElement(builder
, undef
, a
, index0
, "");
1661 args
[2] = LLVMConstInt(i32t
, mode
, 0);
1663 res
= lp_build_intrinsic(builder
, intrinsic
,
1664 vec_type
, args
, Elements(args
));
1666 res
= LLVMBuildExtractElement(builder
, res
, index0
, "");
1669 if (type
.width
* type
.length
== 128) {
1670 switch(type
.width
) {
1672 intrinsic
= "llvm.x86.sse41.round.ps";
1675 intrinsic
= "llvm.x86.sse41.round.pd";
1683 assert(type
.width
* type
.length
== 256);
1684 assert(util_cpu_caps
.has_avx
);
1686 switch(type
.width
) {
1688 intrinsic
= "llvm.x86.avx.round.ps.256";
1691 intrinsic
= "llvm.x86.avx.round.pd.256";
1699 res
= lp_build_intrinsic_binary(builder
, intrinsic
,
1701 LLVMConstInt(i32t
, mode
, 0));
1708 static INLINE LLVMValueRef
1709 lp_build_iround_nearest_sse2(struct lp_build_context
*bld
,
1712 LLVMBuilderRef builder
= bld
->gallivm
->builder
;
1713 const struct lp_type type
= bld
->type
;
1714 LLVMTypeRef i32t
= LLVMInt32TypeInContext(bld
->gallivm
->context
);
1715 LLVMTypeRef ret_type
= lp_build_int_vec_type(bld
->gallivm
, type
);
1716 const char *intrinsic
;
1719 assert(type
.floating
);
1720 /* using the double precision conversions is a bit more complicated */
1721 assert(type
.width
== 32);
1723 assert(lp_check_value(type
, a
));
1724 assert(util_cpu_caps
.has_sse2
);
1726 /* This is relying on MXCSR rounding mode, which should always be nearest. */
1727 if (type
.length
== 1) {
1728 LLVMTypeRef vec_type
;
1731 LLVMValueRef index0
= LLVMConstInt(i32t
, 0, 0);
1733 vec_type
= LLVMVectorType(bld
->elem_type
, 4);
1735 intrinsic
= "llvm.x86.sse.cvtss2si";
1737 undef
= LLVMGetUndef(vec_type
);
1739 arg
= LLVMBuildInsertElement(builder
, undef
, a
, index0
, "");
1741 res
= lp_build_intrinsic_unary(builder
, intrinsic
,
1745 if (type
.width
* type
.length
== 128) {
1746 intrinsic
= "llvm.x86.sse2.cvtps2dq";
1749 assert(type
.width
*type
.length
== 256);
1750 assert(util_cpu_caps
.has_avx
);
1752 intrinsic
= "llvm.x86.avx.cvt.ps2dq.256";
1754 res
= lp_build_intrinsic_unary(builder
, intrinsic
,
1764 static INLINE LLVMValueRef
1765 lp_build_round_altivec(struct lp_build_context
*bld
,
1767 enum lp_build_round_mode mode
)
1769 LLVMBuilderRef builder
= bld
->gallivm
->builder
;
1770 const struct lp_type type
= bld
->type
;
1771 const char *intrinsic
= NULL
;
1773 assert(type
.floating
);
1775 assert(lp_check_value(type
, a
));
1776 assert(util_cpu_caps
.has_altivec
);
1779 case LP_BUILD_ROUND_NEAREST
:
1780 intrinsic
= "llvm.ppc.altivec.vrfin";
1782 case LP_BUILD_ROUND_FLOOR
:
1783 intrinsic
= "llvm.ppc.altivec.vrfim";
1785 case LP_BUILD_ROUND_CEIL
:
1786 intrinsic
= "llvm.ppc.altivec.vrfip";
1788 case LP_BUILD_ROUND_TRUNCATE
:
1789 intrinsic
= "llvm.ppc.altivec.vrfiz";
1793 return lp_build_intrinsic_unary(builder
, intrinsic
, bld
->vec_type
, a
);
1796 static INLINE LLVMValueRef
1797 lp_build_round_arch(struct lp_build_context
*bld
,
1799 enum lp_build_round_mode mode
)
1801 if (util_cpu_caps
.has_sse4_1
)
1802 return lp_build_round_sse41(bld
, a
, mode
);
1803 else /* (util_cpu_caps.has_altivec) */
1804 return lp_build_round_altivec(bld
, a
, mode
);
1808 * Return the integer part of a float (vector) value (== round toward zero).
1809 * The returned value is a float (vector).
1810 * Ex: trunc(-1.5) = -1.0
1813 lp_build_trunc(struct lp_build_context
*bld
,
1816 LLVMBuilderRef builder
= bld
->gallivm
->builder
;
1817 const struct lp_type type
= bld
->type
;
1819 assert(type
.floating
);
1820 assert(lp_check_value(type
, a
));
1822 if (arch_rounding_available(type
)) {
1823 return lp_build_round_arch(bld
, a
, LP_BUILD_ROUND_TRUNCATE
);
1826 const struct lp_type type
= bld
->type
;
1827 struct lp_type inttype
;
1828 struct lp_build_context intbld
;
1829 LLVMValueRef cmpval
= lp_build_const_vec(bld
->gallivm
, type
, 2^24);
1830 LLVMValueRef trunc
, res
, anosign
, mask
;
1831 LLVMTypeRef int_vec_type
= bld
->int_vec_type
;
1832 LLVMTypeRef vec_type
= bld
->vec_type
;
1834 assert(type
.width
== 32); /* might want to handle doubles at some point */
1837 inttype
.floating
= 0;
1838 lp_build_context_init(&intbld
, bld
->gallivm
, inttype
);
1840 /* round by truncation */
1841 trunc
= LLVMBuildFPToSI(builder
, a
, int_vec_type
, "");
1842 res
= LLVMBuildSIToFP(builder
, trunc
, vec_type
, "floor.trunc");
1844 /* mask out sign bit */
1845 anosign
= lp_build_abs(bld
, a
);
1847 * mask out all values if anosign > 2^24
1848 * This should work both for large ints (all rounding is no-op for them
1849 * because such floats are always exact) as well as special cases like
1850 * NaNs, Infs (taking advantage of the fact they use max exponent).
1851 * (2^24 is arbitrary anything between 2^24 and 2^31 should work.)
1853 anosign
= LLVMBuildBitCast(builder
, anosign
, int_vec_type
, "");
1854 cmpval
= LLVMBuildBitCast(builder
, cmpval
, int_vec_type
, "");
1855 mask
= lp_build_cmp(&intbld
, PIPE_FUNC_GREATER
, anosign
, cmpval
);
1856 return lp_build_select(bld
, mask
, a
, res
);
1862 * Return float (vector) rounded to nearest integer (vector). The returned
1863 * value is a float (vector).
1864 * Ex: round(0.9) = 1.0
1865 * Ex: round(-1.5) = -2.0
1868 lp_build_round(struct lp_build_context
*bld
,
1871 LLVMBuilderRef builder
= bld
->gallivm
->builder
;
1872 const struct lp_type type
= bld
->type
;
1874 assert(type
.floating
);
1875 assert(lp_check_value(type
, a
));
1877 if (arch_rounding_available(type
)) {
1878 return lp_build_round_arch(bld
, a
, LP_BUILD_ROUND_NEAREST
);
1881 const struct lp_type type
= bld
->type
;
1882 struct lp_type inttype
;
1883 struct lp_build_context intbld
;
1884 LLVMValueRef cmpval
= lp_build_const_vec(bld
->gallivm
, type
, 2^24);
1885 LLVMValueRef res
, anosign
, mask
;
1886 LLVMTypeRef int_vec_type
= bld
->int_vec_type
;
1887 LLVMTypeRef vec_type
= bld
->vec_type
;
1889 assert(type
.width
== 32); /* might want to handle doubles at some point */
1892 inttype
.floating
= 0;
1893 lp_build_context_init(&intbld
, bld
->gallivm
, inttype
);
1895 res
= lp_build_iround(bld
, a
);
1896 res
= LLVMBuildSIToFP(builder
, res
, vec_type
, "");
1898 /* mask out sign bit */
1899 anosign
= lp_build_abs(bld
, a
);
1901 * mask out all values if anosign > 2^24
1902 * This should work both for large ints (all rounding is no-op for them
1903 * because such floats are always exact) as well as special cases like
1904 * NaNs, Infs (taking advantage of the fact they use max exponent).
1905 * (2^24 is arbitrary anything between 2^24 and 2^31 should work.)
1907 anosign
= LLVMBuildBitCast(builder
, anosign
, int_vec_type
, "");
1908 cmpval
= LLVMBuildBitCast(builder
, cmpval
, int_vec_type
, "");
1909 mask
= lp_build_cmp(&intbld
, PIPE_FUNC_GREATER
, anosign
, cmpval
);
1910 return lp_build_select(bld
, mask
, a
, res
);
1916 * Return floor of float (vector), result is a float (vector)
1917 * Ex: floor(1.1) = 1.0
1918 * Ex: floor(-1.1) = -2.0
1921 lp_build_floor(struct lp_build_context
*bld
,
1924 LLVMBuilderRef builder
= bld
->gallivm
->builder
;
1925 const struct lp_type type
= bld
->type
;
1927 assert(type
.floating
);
1928 assert(lp_check_value(type
, a
));
1930 if (arch_rounding_available(type
)) {
1931 return lp_build_round_arch(bld
, a
, LP_BUILD_ROUND_FLOOR
);
1934 const struct lp_type type
= bld
->type
;
1935 struct lp_type inttype
;
1936 struct lp_build_context intbld
;
1937 LLVMValueRef cmpval
= lp_build_const_vec(bld
->gallivm
, type
, 2^24);
1938 LLVMValueRef trunc
, res
, anosign
, mask
;
1939 LLVMTypeRef int_vec_type
= bld
->int_vec_type
;
1940 LLVMTypeRef vec_type
= bld
->vec_type
;
1942 assert(type
.width
== 32); /* might want to handle doubles at some point */
1945 inttype
.floating
= 0;
1946 lp_build_context_init(&intbld
, bld
->gallivm
, inttype
);
1948 /* round by truncation */
1949 trunc
= LLVMBuildFPToSI(builder
, a
, int_vec_type
, "");
1950 res
= LLVMBuildSIToFP(builder
, trunc
, vec_type
, "floor.trunc");
1956 * fix values if rounding is wrong (for non-special cases)
1957 * - this is the case if trunc > a
1959 mask
= lp_build_cmp(bld
, PIPE_FUNC_GREATER
, res
, a
);
1960 /* tmp = trunc > a ? 1.0 : 0.0 */
1961 tmp
= LLVMBuildBitCast(builder
, bld
->one
, int_vec_type
, "");
1962 tmp
= lp_build_and(&intbld
, mask
, tmp
);
1963 tmp
= LLVMBuildBitCast(builder
, tmp
, vec_type
, "");
1964 res
= lp_build_sub(bld
, res
, tmp
);
1967 /* mask out sign bit */
1968 anosign
= lp_build_abs(bld
, a
);
1970 * mask out all values if anosign > 2^24
1971 * This should work both for large ints (all rounding is no-op for them
1972 * because such floats are always exact) as well as special cases like
1973 * NaNs, Infs (taking advantage of the fact they use max exponent).
1974 * (2^24 is arbitrary anything between 2^24 and 2^31 should work.)
1976 anosign
= LLVMBuildBitCast(builder
, anosign
, int_vec_type
, "");
1977 cmpval
= LLVMBuildBitCast(builder
, cmpval
, int_vec_type
, "");
1978 mask
= lp_build_cmp(&intbld
, PIPE_FUNC_GREATER
, anosign
, cmpval
);
1979 return lp_build_select(bld
, mask
, a
, res
);
1985 * Return ceiling of float (vector), returning float (vector).
1986 * Ex: ceil( 1.1) = 2.0
1987 * Ex: ceil(-1.1) = -1.0
1990 lp_build_ceil(struct lp_build_context
*bld
,
1993 LLVMBuilderRef builder
= bld
->gallivm
->builder
;
1994 const struct lp_type type
= bld
->type
;
1996 assert(type
.floating
);
1997 assert(lp_check_value(type
, a
));
1999 if (arch_rounding_available(type
)) {
2000 return lp_build_round_arch(bld
, a
, LP_BUILD_ROUND_CEIL
);
2003 const struct lp_type type
= bld
->type
;
2004 struct lp_type inttype
;
2005 struct lp_build_context intbld
;
2006 LLVMValueRef cmpval
= lp_build_const_vec(bld
->gallivm
, type
, 2^24);
2007 LLVMValueRef trunc
, res
, anosign
, mask
, tmp
;
2008 LLVMTypeRef int_vec_type
= bld
->int_vec_type
;
2009 LLVMTypeRef vec_type
= bld
->vec_type
;
2011 assert(type
.width
== 32); /* might want to handle doubles at some point */
2014 inttype
.floating
= 0;
2015 lp_build_context_init(&intbld
, bld
->gallivm
, inttype
);
2017 /* round by truncation */
2018 trunc
= LLVMBuildFPToSI(builder
, a
, int_vec_type
, "");
2019 trunc
= LLVMBuildSIToFP(builder
, trunc
, vec_type
, "ceil.trunc");
2022 * fix values if rounding is wrong (for non-special cases)
2023 * - this is the case if trunc < a
2025 mask
= lp_build_cmp(bld
, PIPE_FUNC_LESS
, trunc
, a
);
2026 /* tmp = trunc < a ? 1.0 : 0.0 */
2027 tmp
= LLVMBuildBitCast(builder
, bld
->one
, int_vec_type
, "");
2028 tmp
= lp_build_and(&intbld
, mask
, tmp
);
2029 tmp
= LLVMBuildBitCast(builder
, tmp
, vec_type
, "");
2030 res
= lp_build_add(bld
, trunc
, tmp
);
2032 /* mask out sign bit */
2033 anosign
= lp_build_abs(bld
, a
);
2035 * mask out all values if anosign > 2^24
2036 * This should work both for large ints (all rounding is no-op for them
2037 * because such floats are always exact) as well as special cases like
2038 * NaNs, Infs (taking advantage of the fact they use max exponent).
2039 * (2^24 is arbitrary anything between 2^24 and 2^31 should work.)
2041 anosign
= LLVMBuildBitCast(builder
, anosign
, int_vec_type
, "");
2042 cmpval
= LLVMBuildBitCast(builder
, cmpval
, int_vec_type
, "");
2043 mask
= lp_build_cmp(&intbld
, PIPE_FUNC_GREATER
, anosign
, cmpval
);
2044 return lp_build_select(bld
, mask
, a
, res
);
2050 * Return fractional part of 'a' computed as a - floor(a)
2051 * Typically used in texture coord arithmetic.
2054 lp_build_fract(struct lp_build_context
*bld
,
2057 assert(bld
->type
.floating
);
2058 return lp_build_sub(bld
, a
, lp_build_floor(bld
, a
));
2063 * Prevent returning a fractional part of 1.0 for very small negative values of
2064 * 'a' by clamping against 0.99999(9).
2066 static inline LLVMValueRef
2067 clamp_fract(struct lp_build_context
*bld
, LLVMValueRef fract
)
2071 /* this is the largest number smaller than 1.0 representable as float */
2072 max
= lp_build_const_vec(bld
->gallivm
, bld
->type
,
2073 1.0 - 1.0/(1LL << (lp_mantissa(bld
->type
) + 1)));
2074 return lp_build_min(bld
, fract
, max
);
2079 * Same as lp_build_fract, but guarantees that the result is always smaller
2083 lp_build_fract_safe(struct lp_build_context
*bld
,
2086 return clamp_fract(bld
, lp_build_fract(bld
, a
));
2091 * Return the integer part of a float (vector) value (== round toward zero).
2092 * The returned value is an integer (vector).
2093 * Ex: itrunc(-1.5) = -1
2096 lp_build_itrunc(struct lp_build_context
*bld
,
2099 LLVMBuilderRef builder
= bld
->gallivm
->builder
;
2100 const struct lp_type type
= bld
->type
;
2101 LLVMTypeRef int_vec_type
= lp_build_int_vec_type(bld
->gallivm
, type
);
2103 assert(type
.floating
);
2104 assert(lp_check_value(type
, a
));
2106 return LLVMBuildFPToSI(builder
, a
, int_vec_type
, "");
2111 * Return float (vector) rounded to nearest integer (vector). The returned
2112 * value is an integer (vector).
2113 * Ex: iround(0.9) = 1
2114 * Ex: iround(-1.5) = -2
2117 lp_build_iround(struct lp_build_context
*bld
,
2120 LLVMBuilderRef builder
= bld
->gallivm
->builder
;
2121 const struct lp_type type
= bld
->type
;
2122 LLVMTypeRef int_vec_type
= bld
->int_vec_type
;
2125 assert(type
.floating
);
2127 assert(lp_check_value(type
, a
));
2129 if ((util_cpu_caps
.has_sse2
&&
2130 ((type
.width
== 32) && (type
.length
== 1 || type
.length
== 4))) ||
2131 (util_cpu_caps
.has_avx
&& type
.width
== 32 && type
.length
== 8)) {
2132 return lp_build_iround_nearest_sse2(bld
, a
);
2134 if (arch_rounding_available(type
)) {
2135 res
= lp_build_round_arch(bld
, a
, LP_BUILD_ROUND_NEAREST
);
2140 half
= lp_build_const_vec(bld
->gallivm
, type
, 0.5);
2143 LLVMTypeRef vec_type
= bld
->vec_type
;
2144 LLVMValueRef mask
= lp_build_const_int_vec(bld
->gallivm
, type
,
2145 (unsigned long long)1 << (type
.width
- 1));
2149 sign
= LLVMBuildBitCast(builder
, a
, int_vec_type
, "");
2150 sign
= LLVMBuildAnd(builder
, sign
, mask
, "");
2153 half
= LLVMBuildBitCast(builder
, half
, int_vec_type
, "");
2154 half
= LLVMBuildOr(builder
, sign
, half
, "");
2155 half
= LLVMBuildBitCast(builder
, half
, vec_type
, "");
2158 res
= LLVMBuildFAdd(builder
, a
, half
, "");
2161 res
= LLVMBuildFPToSI(builder
, res
, int_vec_type
, "");
2168 * Return floor of float (vector), result is an int (vector)
2169 * Ex: ifloor(1.1) = 1.0
2170 * Ex: ifloor(-1.1) = -2.0
2173 lp_build_ifloor(struct lp_build_context
*bld
,
2176 LLVMBuilderRef builder
= bld
->gallivm
->builder
;
2177 const struct lp_type type
= bld
->type
;
2178 LLVMTypeRef int_vec_type
= bld
->int_vec_type
;
2181 assert(type
.floating
);
2182 assert(lp_check_value(type
, a
));
2186 if (arch_rounding_available(type
)) {
2187 res
= lp_build_round_arch(bld
, a
, LP_BUILD_ROUND_FLOOR
);
2190 struct lp_type inttype
;
2191 struct lp_build_context intbld
;
2192 LLVMValueRef trunc
, itrunc
, mask
;
2194 assert(type
.floating
);
2195 assert(lp_check_value(type
, a
));
2198 inttype
.floating
= 0;
2199 lp_build_context_init(&intbld
, bld
->gallivm
, inttype
);
2201 /* round by truncation */
2202 itrunc
= LLVMBuildFPToSI(builder
, a
, int_vec_type
, "");
2203 trunc
= LLVMBuildSIToFP(builder
, itrunc
, bld
->vec_type
, "ifloor.trunc");
2206 * fix values if rounding is wrong (for non-special cases)
2207 * - this is the case if trunc > a
2208 * The results of doing this with NaNs, very large values etc.
2209 * are undefined but this seems to be the case anyway.
2211 mask
= lp_build_cmp(bld
, PIPE_FUNC_GREATER
, trunc
, a
);
2212 /* cheapie minus one with mask since the mask is minus one / zero */
2213 return lp_build_add(&intbld
, itrunc
, mask
);
2217 /* round to nearest (toward zero) */
2218 res
= LLVMBuildFPToSI(builder
, res
, int_vec_type
, "ifloor.res");
2225 * Return ceiling of float (vector), returning int (vector).
2226 * Ex: iceil( 1.1) = 2
2227 * Ex: iceil(-1.1) = -1
2230 lp_build_iceil(struct lp_build_context
*bld
,
2233 LLVMBuilderRef builder
= bld
->gallivm
->builder
;
2234 const struct lp_type type
= bld
->type
;
2235 LLVMTypeRef int_vec_type
= bld
->int_vec_type
;
2238 assert(type
.floating
);
2239 assert(lp_check_value(type
, a
));
2241 if (arch_rounding_available(type
)) {
2242 res
= lp_build_round_arch(bld
, a
, LP_BUILD_ROUND_CEIL
);
2245 struct lp_type inttype
;
2246 struct lp_build_context intbld
;
2247 LLVMValueRef trunc
, itrunc
, mask
;
2249 assert(type
.floating
);
2250 assert(lp_check_value(type
, a
));
2253 inttype
.floating
= 0;
2254 lp_build_context_init(&intbld
, bld
->gallivm
, inttype
);
2256 /* round by truncation */
2257 itrunc
= LLVMBuildFPToSI(builder
, a
, int_vec_type
, "");
2258 trunc
= LLVMBuildSIToFP(builder
, itrunc
, bld
->vec_type
, "iceil.trunc");
2261 * fix values if rounding is wrong (for non-special cases)
2262 * - this is the case if trunc < a
2263 * The results of doing this with NaNs, very large values etc.
2264 * are undefined but this seems to be the case anyway.
2266 mask
= lp_build_cmp(bld
, PIPE_FUNC_LESS
, trunc
, a
);
2267 /* cheapie plus one with mask since the mask is minus one / zero */
2268 return lp_build_sub(&intbld
, itrunc
, mask
);
2271 /* round to nearest (toward zero) */
2272 res
= LLVMBuildFPToSI(builder
, res
, int_vec_type
, "iceil.res");
2279 * Combined ifloor() & fract().
2281 * Preferred to calling the functions separately, as it will ensure that the
2282 * strategy (floor() vs ifloor()) that results in less redundant work is used.
2285 lp_build_ifloor_fract(struct lp_build_context
*bld
,
2287 LLVMValueRef
*out_ipart
,
2288 LLVMValueRef
*out_fpart
)
2290 LLVMBuilderRef builder
= bld
->gallivm
->builder
;
2291 const struct lp_type type
= bld
->type
;
2294 assert(type
.floating
);
2295 assert(lp_check_value(type
, a
));
2297 if (arch_rounding_available(type
)) {
2299 * floor() is easier.
2302 ipart
= lp_build_floor(bld
, a
);
2303 *out_fpart
= LLVMBuildFSub(builder
, a
, ipart
, "fpart");
2304 *out_ipart
= LLVMBuildFPToSI(builder
, ipart
, bld
->int_vec_type
, "ipart");
2308 * ifloor() is easier.
2311 *out_ipart
= lp_build_ifloor(bld
, a
);
2312 ipart
= LLVMBuildSIToFP(builder
, *out_ipart
, bld
->vec_type
, "ipart");
2313 *out_fpart
= LLVMBuildFSub(builder
, a
, ipart
, "fpart");
2319 * Same as lp_build_ifloor_fract, but guarantees that the fractional part is
2320 * always smaller than one.
2323 lp_build_ifloor_fract_safe(struct lp_build_context
*bld
,
2325 LLVMValueRef
*out_ipart
,
2326 LLVMValueRef
*out_fpart
)
2328 lp_build_ifloor_fract(bld
, a
, out_ipart
, out_fpart
);
2329 *out_fpart
= clamp_fract(bld
, *out_fpart
);
2334 lp_build_sqrt(struct lp_build_context
*bld
,
2337 LLVMBuilderRef builder
= bld
->gallivm
->builder
;
2338 const struct lp_type type
= bld
->type
;
2339 LLVMTypeRef vec_type
= lp_build_vec_type(bld
->gallivm
, type
);
2342 assert(lp_check_value(type
, a
));
2344 /* TODO: optimize the constant case */
2346 assert(type
.floating
);
2347 if (type
.length
== 1) {
2348 util_snprintf(intrinsic
, sizeof intrinsic
, "llvm.sqrt.f%u", type
.width
);
2351 util_snprintf(intrinsic
, sizeof intrinsic
, "llvm.sqrt.v%uf%u", type
.length
, type
.width
);
2354 return lp_build_intrinsic_unary(builder
, intrinsic
, vec_type
, a
);
2359 * Do one Newton-Raphson step to improve reciprocate precision:
2361 * x_{i+1} = x_i * (2 - a * x_i)
2363 * XXX: Unfortunately this won't give IEEE-754 conformant results for 0 or
2364 * +/-Inf, giving NaN instead. Certain applications rely on this behavior,
2365 * such as Google Earth, which does RCP(RSQRT(0.0) when drawing the Earth's
2366 * halo. It would be necessary to clamp the argument to prevent this.
2369 * - http://en.wikipedia.org/wiki/Division_(digital)#Newton.E2.80.93Raphson_division
2370 * - http://softwarecommunity.intel.com/articles/eng/1818.htm
2372 static INLINE LLVMValueRef
2373 lp_build_rcp_refine(struct lp_build_context
*bld
,
2377 LLVMBuilderRef builder
= bld
->gallivm
->builder
;
2378 LLVMValueRef two
= lp_build_const_vec(bld
->gallivm
, bld
->type
, 2.0);
2381 res
= LLVMBuildFMul(builder
, a
, rcp_a
, "");
2382 res
= LLVMBuildFSub(builder
, two
, res
, "");
2383 res
= LLVMBuildFMul(builder
, rcp_a
, res
, "");
2390 lp_build_rcp(struct lp_build_context
*bld
,
2393 LLVMBuilderRef builder
= bld
->gallivm
->builder
;
2394 const struct lp_type type
= bld
->type
;
2396 assert(lp_check_value(type
, a
));
2405 assert(type
.floating
);
2407 if(LLVMIsConstant(a
))
2408 return LLVMConstFDiv(bld
->one
, a
);
2411 * We don't use RCPPS because:
2412 * - it only has 10bits of precision
2413 * - it doesn't even get the reciprocate of 1.0 exactly
2414 * - doing Newton-Rapshon steps yields wrong (NaN) values for 0.0 or Inf
2415 * - for recent processors the benefit over DIVPS is marginal, a case
2418 * We could still use it on certain processors if benchmarks show that the
2419 * RCPPS plus necessary workarounds are still preferrable to DIVPS; or for
2420 * particular uses that require less workarounds.
2423 if (FALSE
&& ((util_cpu_caps
.has_sse
&& type
.width
== 32 && type
.length
== 4) ||
2424 (util_cpu_caps
.has_avx
&& type
.width
== 32 && type
.length
== 8))){
2425 const unsigned num_iterations
= 0;
2428 const char *intrinsic
= NULL
;
2430 if (type
.length
== 4) {
2431 intrinsic
= "llvm.x86.sse.rcp.ps";
2434 intrinsic
= "llvm.x86.avx.rcp.ps.256";
2437 res
= lp_build_intrinsic_unary(builder
, intrinsic
, bld
->vec_type
, a
);
2439 for (i
= 0; i
< num_iterations
; ++i
) {
2440 res
= lp_build_rcp_refine(bld
, a
, res
);
2446 return LLVMBuildFDiv(builder
, bld
->one
, a
, "");
2451 * Do one Newton-Raphson step to improve rsqrt precision:
2453 * x_{i+1} = 0.5 * x_i * (3.0 - a * x_i * x_i)
2455 * See also Intel 64 and IA-32 Architectures Optimization Manual.
2457 static INLINE LLVMValueRef
2458 lp_build_rsqrt_refine(struct lp_build_context
*bld
,
2460 LLVMValueRef rsqrt_a
)
2462 LLVMBuilderRef builder
= bld
->gallivm
->builder
;
2463 LLVMValueRef half
= lp_build_const_vec(bld
->gallivm
, bld
->type
, 0.5);
2464 LLVMValueRef three
= lp_build_const_vec(bld
->gallivm
, bld
->type
, 3.0);
2467 res
= LLVMBuildFMul(builder
, rsqrt_a
, rsqrt_a
, "");
2468 res
= LLVMBuildFMul(builder
, a
, res
, "");
2469 res
= LLVMBuildFSub(builder
, three
, res
, "");
2470 res
= LLVMBuildFMul(builder
, rsqrt_a
, res
, "");
2471 res
= LLVMBuildFMul(builder
, half
, res
, "");
2478 * Generate 1/sqrt(a).
2479 * Result is undefined for values < 0, infinity for +0.
2482 lp_build_rsqrt(struct lp_build_context
*bld
,
2485 LLVMBuilderRef builder
= bld
->gallivm
->builder
;
2486 const struct lp_type type
= bld
->type
;
2488 assert(lp_check_value(type
, a
));
2490 assert(type
.floating
);
2493 * This should be faster but all denormals will end up as infinity.
2495 if (0 && lp_build_fast_rsqrt_available(type
)) {
2496 const unsigned num_iterations
= 1;
2500 /* rsqrt(1.0) != 1.0 here */
2501 res
= lp_build_fast_rsqrt(bld
, a
);
2503 if (num_iterations
) {
2505 * Newton-Raphson will result in NaN instead of infinity for zero,
2506 * and NaN instead of zero for infinity.
2507 * Also, need to ensure rsqrt(1.0) == 1.0.
2508 * All numbers smaller than FLT_MIN will result in +infinity
2509 * (rsqrtps treats all denormals as zero).
2512 * Certain non-c99 compilers don't know INFINITY and might not support
2513 * hacks to evaluate it at compile time neither.
2515 const unsigned posinf_int
= 0x7F800000;
2517 LLVMValueRef flt_min
= lp_build_const_vec(bld
->gallivm
, type
, FLT_MIN
);
2518 LLVMValueRef inf
= lp_build_const_int_vec(bld
->gallivm
, type
, posinf_int
);
2520 inf
= LLVMBuildBitCast(builder
, inf
, lp_build_vec_type(bld
->gallivm
, type
), "");
2522 for (i
= 0; i
< num_iterations
; ++i
) {
2523 res
= lp_build_rsqrt_refine(bld
, a
, res
);
2525 cmp
= lp_build_compare(bld
->gallivm
, type
, PIPE_FUNC_LESS
, a
, flt_min
);
2526 res
= lp_build_select(bld
, cmp
, inf
, res
);
2527 cmp
= lp_build_compare(bld
->gallivm
, type
, PIPE_FUNC_EQUAL
, a
, inf
);
2528 res
= lp_build_select(bld
, cmp
, bld
->zero
, res
);
2529 cmp
= lp_build_compare(bld
->gallivm
, type
, PIPE_FUNC_EQUAL
, a
, bld
->one
);
2530 res
= lp_build_select(bld
, cmp
, bld
->one
, res
);
2536 return lp_build_rcp(bld
, lp_build_sqrt(bld
, a
));
2540 * If there's a fast (inaccurate) rsqrt instruction available
2541 * (caller may want to avoid to call rsqrt_fast if it's not available,
2542 * i.e. for calculating x^0.5 it may do rsqrt_fast(x) * x but if
2543 * unavailable it would result in sqrt/div/mul so obviously
2544 * much better to just call sqrt, skipping both div and mul).
2547 lp_build_fast_rsqrt_available(struct lp_type type
)
2549 assert(type
.floating
);
2551 if ((util_cpu_caps
.has_sse
&& type
.width
== 32 && type
.length
== 4) ||
2552 (util_cpu_caps
.has_avx
&& type
.width
== 32 && type
.length
== 8)) {
2560 * Generate 1/sqrt(a).
2561 * Result is undefined for values < 0, infinity for +0.
2562 * Precision is limited, only ~10 bits guaranteed
2563 * (rsqrt 1.0 may not be 1.0, denorms may be flushed to 0).
2566 lp_build_fast_rsqrt(struct lp_build_context
*bld
,
2569 LLVMBuilderRef builder
= bld
->gallivm
->builder
;
2570 const struct lp_type type
= bld
->type
;
2572 assert(lp_check_value(type
, a
));
2574 if (lp_build_fast_rsqrt_available(type
)) {
2575 const char *intrinsic
= NULL
;
2577 if (type
.length
== 4) {
2578 intrinsic
= "llvm.x86.sse.rsqrt.ps";
2581 intrinsic
= "llvm.x86.avx.rsqrt.ps.256";
2583 return lp_build_intrinsic_unary(builder
, intrinsic
, bld
->vec_type
, a
);
2586 debug_printf("%s: emulating fast rsqrt with rcp/sqrt\n", __FUNCTION__
);
2588 return lp_build_rcp(bld
, lp_build_sqrt(bld
, a
));
2593 * Generate sin(a) using SSE2
2596 lp_build_sin(struct lp_build_context
*bld
,
2599 struct gallivm_state
*gallivm
= bld
->gallivm
;
2600 LLVMBuilderRef builder
= gallivm
->builder
;
2601 struct lp_type int_type
= lp_int_type(bld
->type
);
2602 LLVMBuilderRef b
= builder
;
2605 * take the absolute value,
2606 * x = _mm_and_ps(x, *(v4sf*)_ps_inv_sign_mask);
2609 LLVMValueRef inv_sig_mask
= lp_build_const_int_vec(gallivm
, bld
->type
, ~0x80000000);
2610 LLVMValueRef a_v4si
= LLVMBuildBitCast(b
, a
, bld
->int_vec_type
, "a_v4si");
2612 LLVMValueRef absi
= LLVMBuildAnd(b
, a_v4si
, inv_sig_mask
, "absi");
2613 LLVMValueRef x_abs
= LLVMBuildBitCast(b
, absi
, bld
->vec_type
, "x_abs");
2616 * extract the sign bit (upper one)
2617 * sign_bit = _mm_and_ps(sign_bit, *(v4sf*)_ps_sign_mask);
2619 LLVMValueRef sig_mask
= lp_build_const_int_vec(gallivm
, bld
->type
, 0x80000000);
2620 LLVMValueRef sign_bit_i
= LLVMBuildAnd(b
, a_v4si
, sig_mask
, "sign_bit_i");
2624 * y = _mm_mul_ps(x, *(v4sf*)_ps_cephes_FOPI);
2627 LLVMValueRef FOPi
= lp_build_const_vec(gallivm
, bld
->type
, 1.27323954473516);
2628 LLVMValueRef scale_y
= LLVMBuildFMul(b
, x_abs
, FOPi
, "scale_y");
2631 * store the integer part of y in mm0
2632 * emm2 = _mm_cvttps_epi32(y);
2635 LLVMValueRef emm2_i
= LLVMBuildFPToSI(b
, scale_y
, bld
->int_vec_type
, "emm2_i");
2638 * j=(j+1) & (~1) (see the cephes sources)
2639 * emm2 = _mm_add_epi32(emm2, *(v4si*)_pi32_1);
2642 LLVMValueRef all_one
= lp_build_const_int_vec(gallivm
, bld
->type
, 1);
2643 LLVMValueRef emm2_add
= LLVMBuildAdd(b
, emm2_i
, all_one
, "emm2_add");
2645 * emm2 = _mm_and_si128(emm2, *(v4si*)_pi32_inv1);
2647 LLVMValueRef inv_one
= lp_build_const_int_vec(gallivm
, bld
->type
, ~1);
2648 LLVMValueRef emm2_and
= LLVMBuildAnd(b
, emm2_add
, inv_one
, "emm2_and");
2651 * y = _mm_cvtepi32_ps(emm2);
2653 LLVMValueRef y_2
= LLVMBuildSIToFP(b
, emm2_and
, bld
->vec_type
, "y_2");
2655 /* get the swap sign flag
2656 * emm0 = _mm_and_si128(emm2, *(v4si*)_pi32_4);
2658 LLVMValueRef pi32_4
= lp_build_const_int_vec(gallivm
, bld
->type
, 4);
2659 LLVMValueRef emm0_and
= LLVMBuildAnd(b
, emm2_add
, pi32_4
, "emm0_and");
2662 * emm2 = _mm_slli_epi32(emm0, 29);
2664 LLVMValueRef const_29
= lp_build_const_int_vec(gallivm
, bld
->type
, 29);
2665 LLVMValueRef swap_sign_bit
= LLVMBuildShl(b
, emm0_and
, const_29
, "swap_sign_bit");
2668 * get the polynom selection mask
2669 * there is one polynom for 0 <= x <= Pi/4
2670 * and another one for Pi/4<x<=Pi/2
2671 * Both branches will be computed.
2673 * emm2 = _mm_and_si128(emm2, *(v4si*)_pi32_2);
2674 * emm2 = _mm_cmpeq_epi32(emm2, _mm_setzero_si128());
2677 LLVMValueRef pi32_2
= lp_build_const_int_vec(gallivm
, bld
->type
, 2);
2678 LLVMValueRef emm2_3
= LLVMBuildAnd(b
, emm2_and
, pi32_2
, "emm2_3");
2679 LLVMValueRef poly_mask
= lp_build_compare(gallivm
,
2680 int_type
, PIPE_FUNC_EQUAL
,
2681 emm2_3
, lp_build_const_int_vec(gallivm
, bld
->type
, 0));
2683 * sign_bit = _mm_xor_ps(sign_bit, swap_sign_bit);
2685 LLVMValueRef sign_bit_1
= LLVMBuildXor(b
, sign_bit_i
, swap_sign_bit
, "sign_bit");
2688 * _PS_CONST(minus_cephes_DP1, -0.78515625);
2689 * _PS_CONST(minus_cephes_DP2, -2.4187564849853515625e-4);
2690 * _PS_CONST(minus_cephes_DP3, -3.77489497744594108e-8);
2692 LLVMValueRef DP1
= lp_build_const_vec(gallivm
, bld
->type
, -0.78515625);
2693 LLVMValueRef DP2
= lp_build_const_vec(gallivm
, bld
->type
, -2.4187564849853515625e-4);
2694 LLVMValueRef DP3
= lp_build_const_vec(gallivm
, bld
->type
, -3.77489497744594108e-8);
2697 * The magic pass: "Extended precision modular arithmetic"
2698 * x = ((x - y * DP1) - y * DP2) - y * DP3;
2699 * xmm1 = _mm_mul_ps(y, xmm1);
2700 * xmm2 = _mm_mul_ps(y, xmm2);
2701 * xmm3 = _mm_mul_ps(y, xmm3);
2703 LLVMValueRef xmm1
= LLVMBuildFMul(b
, y_2
, DP1
, "xmm1");
2704 LLVMValueRef xmm2
= LLVMBuildFMul(b
, y_2
, DP2
, "xmm2");
2705 LLVMValueRef xmm3
= LLVMBuildFMul(b
, y_2
, DP3
, "xmm3");
2708 * x = _mm_add_ps(x, xmm1);
2709 * x = _mm_add_ps(x, xmm2);
2710 * x = _mm_add_ps(x, xmm3);
2713 LLVMValueRef x_1
= LLVMBuildFAdd(b
, x_abs
, xmm1
, "x_1");
2714 LLVMValueRef x_2
= LLVMBuildFAdd(b
, x_1
, xmm2
, "x_2");
2715 LLVMValueRef x_3
= LLVMBuildFAdd(b
, x_2
, xmm3
, "x_3");
2718 * Evaluate the first polynom (0 <= x <= Pi/4)
2720 * z = _mm_mul_ps(x,x);
2722 LLVMValueRef z
= LLVMBuildFMul(b
, x_3
, x_3
, "z");
2725 * _PS_CONST(coscof_p0, 2.443315711809948E-005);
2726 * _PS_CONST(coscof_p1, -1.388731625493765E-003);
2727 * _PS_CONST(coscof_p2, 4.166664568298827E-002);
2729 LLVMValueRef coscof_p0
= lp_build_const_vec(gallivm
, bld
->type
, 2.443315711809948E-005);
2730 LLVMValueRef coscof_p1
= lp_build_const_vec(gallivm
, bld
->type
, -1.388731625493765E-003);
2731 LLVMValueRef coscof_p2
= lp_build_const_vec(gallivm
, bld
->type
, 4.166664568298827E-002);
2734 * y = *(v4sf*)_ps_coscof_p0;
2735 * y = _mm_mul_ps(y, z);
2737 LLVMValueRef y_3
= LLVMBuildFMul(b
, z
, coscof_p0
, "y_3");
2738 LLVMValueRef y_4
= LLVMBuildFAdd(b
, y_3
, coscof_p1
, "y_4");
2739 LLVMValueRef y_5
= LLVMBuildFMul(b
, y_4
, z
, "y_5");
2740 LLVMValueRef y_6
= LLVMBuildFAdd(b
, y_5
, coscof_p2
, "y_6");
2741 LLVMValueRef y_7
= LLVMBuildFMul(b
, y_6
, z
, "y_7");
2742 LLVMValueRef y_8
= LLVMBuildFMul(b
, y_7
, z
, "y_8");
2746 * tmp = _mm_mul_ps(z, *(v4sf*)_ps_0p5);
2747 * y = _mm_sub_ps(y, tmp);
2748 * y = _mm_add_ps(y, *(v4sf*)_ps_1);
2750 LLVMValueRef half
= lp_build_const_vec(gallivm
, bld
->type
, 0.5);
2751 LLVMValueRef tmp
= LLVMBuildFMul(b
, z
, half
, "tmp");
2752 LLVMValueRef y_9
= LLVMBuildFSub(b
, y_8
, tmp
, "y_8");
2753 LLVMValueRef one
= lp_build_const_vec(gallivm
, bld
->type
, 1.0);
2754 LLVMValueRef y_10
= LLVMBuildFAdd(b
, y_9
, one
, "y_9");
2757 * _PS_CONST(sincof_p0, -1.9515295891E-4);
2758 * _PS_CONST(sincof_p1, 8.3321608736E-3);
2759 * _PS_CONST(sincof_p2, -1.6666654611E-1);
2761 LLVMValueRef sincof_p0
= lp_build_const_vec(gallivm
, bld
->type
, -1.9515295891E-4);
2762 LLVMValueRef sincof_p1
= lp_build_const_vec(gallivm
, bld
->type
, 8.3321608736E-3);
2763 LLVMValueRef sincof_p2
= lp_build_const_vec(gallivm
, bld
->type
, -1.6666654611E-1);
2766 * Evaluate the second polynom (Pi/4 <= x <= 0)
2768 * y2 = *(v4sf*)_ps_sincof_p0;
2769 * y2 = _mm_mul_ps(y2, z);
2770 * y2 = _mm_add_ps(y2, *(v4sf*)_ps_sincof_p1);
2771 * y2 = _mm_mul_ps(y2, z);
2772 * y2 = _mm_add_ps(y2, *(v4sf*)_ps_sincof_p2);
2773 * y2 = _mm_mul_ps(y2, z);
2774 * y2 = _mm_mul_ps(y2, x);
2775 * y2 = _mm_add_ps(y2, x);
2778 LLVMValueRef y2_3
= LLVMBuildFMul(b
, z
, sincof_p0
, "y2_3");
2779 LLVMValueRef y2_4
= LLVMBuildFAdd(b
, y2_3
, sincof_p1
, "y2_4");
2780 LLVMValueRef y2_5
= LLVMBuildFMul(b
, y2_4
, z
, "y2_5");
2781 LLVMValueRef y2_6
= LLVMBuildFAdd(b
, y2_5
, sincof_p2
, "y2_6");
2782 LLVMValueRef y2_7
= LLVMBuildFMul(b
, y2_6
, z
, "y2_7");
2783 LLVMValueRef y2_8
= LLVMBuildFMul(b
, y2_7
, x_3
, "y2_8");
2784 LLVMValueRef y2_9
= LLVMBuildFAdd(b
, y2_8
, x_3
, "y2_9");
2787 * select the correct result from the two polynoms
2789 * y2 = _mm_and_ps(xmm3, y2); //, xmm3);
2790 * y = _mm_andnot_ps(xmm3, y);
2791 * y = _mm_or_ps(y,y2);
2793 LLVMValueRef y2_i
= LLVMBuildBitCast(b
, y2_9
, bld
->int_vec_type
, "y2_i");
2794 LLVMValueRef y_i
= LLVMBuildBitCast(b
, y_10
, bld
->int_vec_type
, "y_i");
2795 LLVMValueRef y2_and
= LLVMBuildAnd(b
, y2_i
, poly_mask
, "y2_and");
2796 LLVMValueRef poly_mask_inv
= LLVMBuildNot(b
, poly_mask
, "poly_mask_inv");
2797 LLVMValueRef y_and
= LLVMBuildAnd(b
, y_i
, poly_mask_inv
, "y_and");
2798 LLVMValueRef y_combine
= LLVMBuildOr(b
, y_and
, y2_and
, "y_combine");
2802 * y = _mm_xor_ps(y, sign_bit);
2804 LLVMValueRef y_sign
= LLVMBuildXor(b
, y_combine
, sign_bit_1
, "y_sin");
2805 LLVMValueRef y_result
= LLVMBuildBitCast(b
, y_sign
, bld
->vec_type
, "y_result");
2806 LLVMValueRef isfinite
= lp_build_isfinite(bld
, a
);
2808 /* clamp output to be within [-1, 1] */
2809 y_result
= lp_build_clamp(bld
, y_result
,
2810 lp_build_const_vec(bld
->gallivm
, bld
->type
, -1.f
),
2811 lp_build_const_vec(bld
->gallivm
, bld
->type
, 1.f
));
2812 /* If a is -inf, inf or NaN then return NaN */
2813 y_result
= lp_build_select(bld
, isfinite
, y_result
,
2814 lp_build_const_vec(bld
->gallivm
, bld
->type
, NAN
));
2820 * Generate cos(a) using SSE2
2823 lp_build_cos(struct lp_build_context
*bld
,
2826 struct gallivm_state
*gallivm
= bld
->gallivm
;
2827 LLVMBuilderRef builder
= gallivm
->builder
;
2828 struct lp_type int_type
= lp_int_type(bld
->type
);
2829 LLVMBuilderRef b
= builder
;
2832 * take the absolute value,
2833 * x = _mm_and_ps(x, *(v4sf*)_ps_inv_sign_mask);
2836 LLVMValueRef inv_sig_mask
= lp_build_const_int_vec(gallivm
, bld
->type
, ~0x80000000);
2837 LLVMValueRef a_v4si
= LLVMBuildBitCast(b
, a
, bld
->int_vec_type
, "a_v4si");
2839 LLVMValueRef absi
= LLVMBuildAnd(b
, a_v4si
, inv_sig_mask
, "absi");
2840 LLVMValueRef x_abs
= LLVMBuildBitCast(b
, absi
, bld
->vec_type
, "x_abs");
2844 * y = _mm_mul_ps(x, *(v4sf*)_ps_cephes_FOPI);
2847 LLVMValueRef FOPi
= lp_build_const_vec(gallivm
, bld
->type
, 1.27323954473516);
2848 LLVMValueRef scale_y
= LLVMBuildFMul(b
, x_abs
, FOPi
, "scale_y");
2851 * store the integer part of y in mm0
2852 * emm2 = _mm_cvttps_epi32(y);
2855 LLVMValueRef emm2_i
= LLVMBuildFPToSI(b
, scale_y
, bld
->int_vec_type
, "emm2_i");
2858 * j=(j+1) & (~1) (see the cephes sources)
2859 * emm2 = _mm_add_epi32(emm2, *(v4si*)_pi32_1);
2862 LLVMValueRef all_one
= lp_build_const_int_vec(gallivm
, bld
->type
, 1);
2863 LLVMValueRef emm2_add
= LLVMBuildAdd(b
, emm2_i
, all_one
, "emm2_add");
2865 * emm2 = _mm_and_si128(emm2, *(v4si*)_pi32_inv1);
2867 LLVMValueRef inv_one
= lp_build_const_int_vec(gallivm
, bld
->type
, ~1);
2868 LLVMValueRef emm2_and
= LLVMBuildAnd(b
, emm2_add
, inv_one
, "emm2_and");
2871 * y = _mm_cvtepi32_ps(emm2);
2873 LLVMValueRef y_2
= LLVMBuildSIToFP(b
, emm2_and
, bld
->vec_type
, "y_2");
2877 * emm2 = _mm_sub_epi32(emm2, *(v4si*)_pi32_2);
2879 LLVMValueRef const_2
= lp_build_const_int_vec(gallivm
, bld
->type
, 2);
2880 LLVMValueRef emm2_2
= LLVMBuildSub(b
, emm2_and
, const_2
, "emm2_2");
2883 /* get the swap sign flag
2884 * emm0 = _mm_andnot_si128(emm2, *(v4si*)_pi32_4);
2886 LLVMValueRef inv
= lp_build_const_int_vec(gallivm
, bld
->type
, ~0);
2887 LLVMValueRef emm0_not
= LLVMBuildXor(b
, emm2_2
, inv
, "emm0_not");
2888 LLVMValueRef pi32_4
= lp_build_const_int_vec(gallivm
, bld
->type
, 4);
2889 LLVMValueRef emm0_and
= LLVMBuildAnd(b
, emm0_not
, pi32_4
, "emm0_and");
2892 * emm2 = _mm_slli_epi32(emm0, 29);
2894 LLVMValueRef const_29
= lp_build_const_int_vec(gallivm
, bld
->type
, 29);
2895 LLVMValueRef sign_bit
= LLVMBuildShl(b
, emm0_and
, const_29
, "sign_bit");
2898 * get the polynom selection mask
2899 * there is one polynom for 0 <= x <= Pi/4
2900 * and another one for Pi/4<x<=Pi/2
2901 * Both branches will be computed.
2903 * emm2 = _mm_and_si128(emm2, *(v4si*)_pi32_2);
2904 * emm2 = _mm_cmpeq_epi32(emm2, _mm_setzero_si128());
2907 LLVMValueRef pi32_2
= lp_build_const_int_vec(gallivm
, bld
->type
, 2);
2908 LLVMValueRef emm2_3
= LLVMBuildAnd(b
, emm2_2
, pi32_2
, "emm2_3");
2909 LLVMValueRef poly_mask
= lp_build_compare(gallivm
,
2910 int_type
, PIPE_FUNC_EQUAL
,
2911 emm2_3
, lp_build_const_int_vec(gallivm
, bld
->type
, 0));
2914 * _PS_CONST(minus_cephes_DP1, -0.78515625);
2915 * _PS_CONST(minus_cephes_DP2, -2.4187564849853515625e-4);
2916 * _PS_CONST(minus_cephes_DP3, -3.77489497744594108e-8);
2918 LLVMValueRef DP1
= lp_build_const_vec(gallivm
, bld
->type
, -0.78515625);
2919 LLVMValueRef DP2
= lp_build_const_vec(gallivm
, bld
->type
, -2.4187564849853515625e-4);
2920 LLVMValueRef DP3
= lp_build_const_vec(gallivm
, bld
->type
, -3.77489497744594108e-8);
2923 * The magic pass: "Extended precision modular arithmetic"
2924 * x = ((x - y * DP1) - y * DP2) - y * DP3;
2925 * xmm1 = _mm_mul_ps(y, xmm1);
2926 * xmm2 = _mm_mul_ps(y, xmm2);
2927 * xmm3 = _mm_mul_ps(y, xmm3);
2929 LLVMValueRef xmm1
= LLVMBuildFMul(b
, y_2
, DP1
, "xmm1");
2930 LLVMValueRef xmm2
= LLVMBuildFMul(b
, y_2
, DP2
, "xmm2");
2931 LLVMValueRef xmm3
= LLVMBuildFMul(b
, y_2
, DP3
, "xmm3");
2934 * x = _mm_add_ps(x, xmm1);
2935 * x = _mm_add_ps(x, xmm2);
2936 * x = _mm_add_ps(x, xmm3);
2939 LLVMValueRef x_1
= LLVMBuildFAdd(b
, x_abs
, xmm1
, "x_1");
2940 LLVMValueRef x_2
= LLVMBuildFAdd(b
, x_1
, xmm2
, "x_2");
2941 LLVMValueRef x_3
= LLVMBuildFAdd(b
, x_2
, xmm3
, "x_3");
2944 * Evaluate the first polynom (0 <= x <= Pi/4)
2946 * z = _mm_mul_ps(x,x);
2948 LLVMValueRef z
= LLVMBuildFMul(b
, x_3
, x_3
, "z");
2951 * _PS_CONST(coscof_p0, 2.443315711809948E-005);
2952 * _PS_CONST(coscof_p1, -1.388731625493765E-003);
2953 * _PS_CONST(coscof_p2, 4.166664568298827E-002);
2955 LLVMValueRef coscof_p0
= lp_build_const_vec(gallivm
, bld
->type
, 2.443315711809948E-005);
2956 LLVMValueRef coscof_p1
= lp_build_const_vec(gallivm
, bld
->type
, -1.388731625493765E-003);
2957 LLVMValueRef coscof_p2
= lp_build_const_vec(gallivm
, bld
->type
, 4.166664568298827E-002);
2960 * y = *(v4sf*)_ps_coscof_p0;
2961 * y = _mm_mul_ps(y, z);
2963 LLVMValueRef y_3
= LLVMBuildFMul(b
, z
, coscof_p0
, "y_3");
2964 LLVMValueRef y_4
= LLVMBuildFAdd(b
, y_3
, coscof_p1
, "y_4");
2965 LLVMValueRef y_5
= LLVMBuildFMul(b
, y_4
, z
, "y_5");
2966 LLVMValueRef y_6
= LLVMBuildFAdd(b
, y_5
, coscof_p2
, "y_6");
2967 LLVMValueRef y_7
= LLVMBuildFMul(b
, y_6
, z
, "y_7");
2968 LLVMValueRef y_8
= LLVMBuildFMul(b
, y_7
, z
, "y_8");
2972 * tmp = _mm_mul_ps(z, *(v4sf*)_ps_0p5);
2973 * y = _mm_sub_ps(y, tmp);
2974 * y = _mm_add_ps(y, *(v4sf*)_ps_1);
2976 LLVMValueRef half
= lp_build_const_vec(gallivm
, bld
->type
, 0.5);
2977 LLVMValueRef tmp
= LLVMBuildFMul(b
, z
, half
, "tmp");
2978 LLVMValueRef y_9
= LLVMBuildFSub(b
, y_8
, tmp
, "y_8");
2979 LLVMValueRef one
= lp_build_const_vec(gallivm
, bld
->type
, 1.0);
2980 LLVMValueRef y_10
= LLVMBuildFAdd(b
, y_9
, one
, "y_9");
2983 * _PS_CONST(sincof_p0, -1.9515295891E-4);
2984 * _PS_CONST(sincof_p1, 8.3321608736E-3);
2985 * _PS_CONST(sincof_p2, -1.6666654611E-1);
2987 LLVMValueRef sincof_p0
= lp_build_const_vec(gallivm
, bld
->type
, -1.9515295891E-4);
2988 LLVMValueRef sincof_p1
= lp_build_const_vec(gallivm
, bld
->type
, 8.3321608736E-3);
2989 LLVMValueRef sincof_p2
= lp_build_const_vec(gallivm
, bld
->type
, -1.6666654611E-1);
2992 * Evaluate the second polynom (Pi/4 <= x <= 0)
2994 * y2 = *(v4sf*)_ps_sincof_p0;
2995 * y2 = _mm_mul_ps(y2, z);
2996 * y2 = _mm_add_ps(y2, *(v4sf*)_ps_sincof_p1);
2997 * y2 = _mm_mul_ps(y2, z);
2998 * y2 = _mm_add_ps(y2, *(v4sf*)_ps_sincof_p2);
2999 * y2 = _mm_mul_ps(y2, z);
3000 * y2 = _mm_mul_ps(y2, x);
3001 * y2 = _mm_add_ps(y2, x);
3004 LLVMValueRef y2_3
= LLVMBuildFMul(b
, z
, sincof_p0
, "y2_3");
3005 LLVMValueRef y2_4
= LLVMBuildFAdd(b
, y2_3
, sincof_p1
, "y2_4");
3006 LLVMValueRef y2_5
= LLVMBuildFMul(b
, y2_4
, z
, "y2_5");
3007 LLVMValueRef y2_6
= LLVMBuildFAdd(b
, y2_5
, sincof_p2
, "y2_6");
3008 LLVMValueRef y2_7
= LLVMBuildFMul(b
, y2_6
, z
, "y2_7");
3009 LLVMValueRef y2_8
= LLVMBuildFMul(b
, y2_7
, x_3
, "y2_8");
3010 LLVMValueRef y2_9
= LLVMBuildFAdd(b
, y2_8
, x_3
, "y2_9");
3013 * select the correct result from the two polynoms
3015 * y2 = _mm_and_ps(xmm3, y2); //, xmm3);
3016 * y = _mm_andnot_ps(xmm3, y);
3017 * y = _mm_or_ps(y,y2);
3019 LLVMValueRef y2_i
= LLVMBuildBitCast(b
, y2_9
, bld
->int_vec_type
, "y2_i");
3020 LLVMValueRef y_i
= LLVMBuildBitCast(b
, y_10
, bld
->int_vec_type
, "y_i");
3021 LLVMValueRef y2_and
= LLVMBuildAnd(b
, y2_i
, poly_mask
, "y2_and");
3022 LLVMValueRef poly_mask_inv
= LLVMBuildNot(b
, poly_mask
, "poly_mask_inv");
3023 LLVMValueRef y_and
= LLVMBuildAnd(b
, y_i
, poly_mask_inv
, "y_and");
3024 LLVMValueRef y_combine
= LLVMBuildOr(b
, y_and
, y2_and
, "y_combine");
3028 * y = _mm_xor_ps(y, sign_bit);
3030 LLVMValueRef y_sign
= LLVMBuildXor(b
, y_combine
, sign_bit
, "y_sin");
3031 LLVMValueRef y_result
= LLVMBuildBitCast(b
, y_sign
, bld
->vec_type
, "y_result");
3032 LLVMValueRef isfinite
= lp_build_isfinite(bld
, a
);
3034 /* clamp output to be within [-1, 1] */
3035 y_result
= lp_build_clamp(bld
, y_result
,
3036 lp_build_const_vec(bld
->gallivm
, bld
->type
, -1.f
),
3037 lp_build_const_vec(bld
->gallivm
, bld
->type
, 1.f
));
3038 /* If a is -inf, inf or NaN then return NaN */
3039 y_result
= lp_build_select(bld
, isfinite
, y_result
,
3040 lp_build_const_vec(bld
->gallivm
, bld
->type
, NAN
));
3046 * Generate pow(x, y)
3049 lp_build_pow(struct lp_build_context
*bld
,
3053 /* TODO: optimize the constant case */
3054 if (gallivm_debug
& GALLIVM_DEBUG_PERF
&&
3055 LLVMIsConstant(x
) && LLVMIsConstant(y
)) {
3056 debug_printf("%s: inefficient/imprecise constant arithmetic\n",
3060 return lp_build_exp2(bld
, lp_build_mul(bld
, lp_build_log2(bld
, x
), y
));
3068 lp_build_exp(struct lp_build_context
*bld
,
3071 /* log2(e) = 1/log(2) */
3072 LLVMValueRef log2e
= lp_build_const_vec(bld
->gallivm
, bld
->type
,
3073 1.4426950408889634);
3075 assert(lp_check_value(bld
->type
, x
));
3077 return lp_build_exp2(bld
, lp_build_mul(bld
, log2e
, x
));
3083 * Behavior is undefined with infs, 0s and nans
3086 lp_build_log(struct lp_build_context
*bld
,
3090 LLVMValueRef log2
= lp_build_const_vec(bld
->gallivm
, bld
->type
,
3091 0.69314718055994529);
3093 assert(lp_check_value(bld
->type
, x
));
3095 return lp_build_mul(bld
, log2
, lp_build_log2(bld
, x
));
3099 * Generate log(x) that handles edge cases (infs, 0s and nans)
3102 lp_build_log_safe(struct lp_build_context
*bld
,
3106 LLVMValueRef log2
= lp_build_const_vec(bld
->gallivm
, bld
->type
,
3107 0.69314718055994529);
3109 assert(lp_check_value(bld
->type
, x
));
3111 return lp_build_mul(bld
, log2
, lp_build_log2_safe(bld
, x
));
3116 * Generate polynomial.
3117 * Ex: coeffs[0] + x * coeffs[1] + x^2 * coeffs[2].
3120 lp_build_polynomial(struct lp_build_context
*bld
,
3122 const double *coeffs
,
3123 unsigned num_coeffs
)
3125 const struct lp_type type
= bld
->type
;
3126 LLVMValueRef even
= NULL
, odd
= NULL
;
3130 assert(lp_check_value(bld
->type
, x
));
3132 /* TODO: optimize the constant case */
3133 if (gallivm_debug
& GALLIVM_DEBUG_PERF
&&
3134 LLVMIsConstant(x
)) {
3135 debug_printf("%s: inefficient/imprecise constant arithmetic\n",
3140 * Calculate odd and even terms seperately to decrease data dependency
3142 * c[0] + x^2 * c[2] + x^4 * c[4] ...
3143 * + x * (c[1] + x^2 * c[3] + x^4 * c[5]) ...
3145 x2
= lp_build_mul(bld
, x
, x
);
3147 for (i
= num_coeffs
; i
--; ) {
3150 coeff
= lp_build_const_vec(bld
->gallivm
, type
, coeffs
[i
]);
3154 even
= lp_build_add(bld
, coeff
, lp_build_mul(bld
, x2
, even
));
3159 odd
= lp_build_add(bld
, coeff
, lp_build_mul(bld
, x2
, odd
));
3166 return lp_build_add(bld
, lp_build_mul(bld
, odd
, x
), even
);
3175 * Minimax polynomial fit of 2**x, in range [0, 1[
3177 const double lp_build_exp2_polynomial
[] = {
3178 #if EXP_POLY_DEGREE == 5
3179 1.000000000000000000000, /*XXX: was 0.999999925063526176901, recompute others */
3180 0.693153073200168932794,
3181 0.240153617044375388211,
3182 0.0558263180532956664775,
3183 0.00898934009049466391101,
3184 0.00187757667519147912699
3185 #elif EXP_POLY_DEGREE == 4
3186 1.00000259337069434683,
3187 0.693003834469974940458,
3188 0.24144275689150793076,
3189 0.0520114606103070150235,
3190 0.0135341679161270268764
3191 #elif EXP_POLY_DEGREE == 3
3192 0.999925218562710312959,
3193 0.695833540494823811697,
3194 0.226067155427249155588,
3195 0.0780245226406372992967
3196 #elif EXP_POLY_DEGREE == 2
3197 1.00172476321474503578,
3198 0.657636275736077639316,
3199 0.33718943461968720704
3207 lp_build_exp2_approx(struct lp_build_context
*bld
,
3209 LLVMValueRef
*p_exp2_int_part
,
3210 LLVMValueRef
*p_frac_part
,
3211 LLVMValueRef
*p_exp2
)
3213 LLVMBuilderRef builder
= bld
->gallivm
->builder
;
3214 const struct lp_type type
= bld
->type
;
3215 LLVMTypeRef vec_type
= lp_build_vec_type(bld
->gallivm
, type
);
3216 LLVMValueRef ipart
= NULL
;
3217 LLVMValueRef fpart
= NULL
;
3218 LLVMValueRef expipart
= NULL
;
3219 LLVMValueRef expfpart
= NULL
;
3220 LLVMValueRef res
= NULL
;
3222 assert(lp_check_value(bld
->type
, x
));
3224 if(p_exp2_int_part
|| p_frac_part
|| p_exp2
) {
3225 /* TODO: optimize the constant case */
3226 if (gallivm_debug
& GALLIVM_DEBUG_PERF
&&
3227 LLVMIsConstant(x
)) {
3228 debug_printf("%s: inefficient/imprecise constant arithmetic\n",
3232 assert(type
.floating
&& type
.width
== 32);
3234 /* We want to preserve NaN and make sure than for exp2 if x > 128,
3235 * the result is INF and if it's smaller than -126.9 the result is 0 */
3236 x
= lp_build_min_ext(bld
, lp_build_const_vec(bld
->gallivm
, type
, 128.0), x
,
3237 GALLIVM_NAN_RETURN_SECOND
);
3238 x
= lp_build_max_ext(bld
, lp_build_const_vec(bld
->gallivm
, type
, -126.99999), x
,
3239 GALLIVM_NAN_RETURN_SECOND
);
3241 /* ipart = floor(x) */
3242 /* fpart = x - ipart */
3243 lp_build_ifloor_fract(bld
, x
, &ipart
, &fpart
);
3246 if(p_exp2_int_part
|| p_exp2
) {
3247 /* expipart = (float) (1 << ipart) */
3248 expipart
= LLVMBuildAdd(builder
, ipart
,
3249 lp_build_const_int_vec(bld
->gallivm
, type
, 127), "");
3250 expipart
= LLVMBuildShl(builder
, expipart
,
3251 lp_build_const_int_vec(bld
->gallivm
, type
, 23), "");
3252 expipart
= LLVMBuildBitCast(builder
, expipart
, vec_type
, "");
3256 expfpart
= lp_build_polynomial(bld
, fpart
, lp_build_exp2_polynomial
,
3257 Elements(lp_build_exp2_polynomial
));
3259 res
= LLVMBuildFMul(builder
, expipart
, expfpart
, "");
3263 *p_exp2_int_part
= expipart
;
3266 *p_frac_part
= fpart
;
3274 lp_build_exp2(struct lp_build_context
*bld
,
3278 lp_build_exp2_approx(bld
, x
, NULL
, NULL
, &res
);
3284 * Extract the exponent of a IEEE-754 floating point value.
3286 * Optionally apply an integer bias.
3288 * Result is an integer value with
3290 * ifloor(log2(x)) + bias
3293 lp_build_extract_exponent(struct lp_build_context
*bld
,
3297 LLVMBuilderRef builder
= bld
->gallivm
->builder
;
3298 const struct lp_type type
= bld
->type
;
3299 unsigned mantissa
= lp_mantissa(type
);
3302 assert(type
.floating
);
3304 assert(lp_check_value(bld
->type
, x
));
3306 x
= LLVMBuildBitCast(builder
, x
, bld
->int_vec_type
, "");
3308 res
= LLVMBuildLShr(builder
, x
,
3309 lp_build_const_int_vec(bld
->gallivm
, type
, mantissa
), "");
3310 res
= LLVMBuildAnd(builder
, res
,
3311 lp_build_const_int_vec(bld
->gallivm
, type
, 255), "");
3312 res
= LLVMBuildSub(builder
, res
,
3313 lp_build_const_int_vec(bld
->gallivm
, type
, 127 - bias
), "");
3320 * Extract the mantissa of the a floating.
3322 * Result is a floating point value with
3324 * x / floor(log2(x))
3327 lp_build_extract_mantissa(struct lp_build_context
*bld
,
3330 LLVMBuilderRef builder
= bld
->gallivm
->builder
;
3331 const struct lp_type type
= bld
->type
;
3332 unsigned mantissa
= lp_mantissa(type
);
3333 LLVMValueRef mantmask
= lp_build_const_int_vec(bld
->gallivm
, type
,
3334 (1ULL << mantissa
) - 1);
3335 LLVMValueRef one
= LLVMConstBitCast(bld
->one
, bld
->int_vec_type
);
3338 assert(lp_check_value(bld
->type
, x
));
3340 assert(type
.floating
);
3342 x
= LLVMBuildBitCast(builder
, x
, bld
->int_vec_type
, "");
3344 /* res = x / 2**ipart */
3345 res
= LLVMBuildAnd(builder
, x
, mantmask
, "");
3346 res
= LLVMBuildOr(builder
, res
, one
, "");
3347 res
= LLVMBuildBitCast(builder
, res
, bld
->vec_type
, "");
3355 * Minimax polynomial fit of log2((1.0 + sqrt(x))/(1.0 - sqrt(x)))/sqrt(x) ,for x in range of [0, 1/9[
3356 * These coefficients can be generate with
3357 * http://www.boost.org/doc/libs/1_36_0/libs/math/doc/sf_and_dist/html/math_toolkit/toolkit/internals2/minimax.html
3359 const double lp_build_log2_polynomial
[] = {
3360 #if LOG_POLY_DEGREE == 5
3361 2.88539008148777786488L,
3362 0.961796878841293367824L,
3363 0.577058946784739859012L,
3364 0.412914355135828735411L,
3365 0.308591899232910175289L,
3366 0.352376952300281371868L,
3367 #elif LOG_POLY_DEGREE == 4
3368 2.88539009343309178325L,
3369 0.961791550404184197881L,
3370 0.577440339438736392009L,
3371 0.403343858251329912514L,
3372 0.406718052498846252698L,
3373 #elif LOG_POLY_DEGREE == 3
3374 2.88538959748872753838L,
3375 0.961932915889597772928L,
3376 0.571118517972136195241L,
3377 0.493997535084709500285L,
3384 * See http://www.devmaster.net/forums/showthread.php?p=43580
3385 * http://en.wikipedia.org/wiki/Logarithm#Calculation
3386 * http://www.nezumi.demon.co.uk/consult/logx.htm
3388 * If handle_edge_cases is true the function will perform computations
3389 * to match the required D3D10+ behavior for each of the edge cases.
3390 * That means that if input is:
3391 * - less than zero (to and including -inf) then NaN will be returned
3392 * - equal to zero (-denorm, -0, +0 or +denorm), then -inf will be returned
3393 * - +infinity, then +infinity will be returned
3394 * - NaN, then NaN will be returned
3396 * Those checks are fairly expensive so if you don't need them make sure
3397 * handle_edge_cases is false.
3400 lp_build_log2_approx(struct lp_build_context
*bld
,
3402 LLVMValueRef
*p_exp
,
3403 LLVMValueRef
*p_floor_log2
,
3404 LLVMValueRef
*p_log2
,
3405 boolean handle_edge_cases
)
3407 LLVMBuilderRef builder
= bld
->gallivm
->builder
;
3408 const struct lp_type type
= bld
->type
;
3409 LLVMTypeRef vec_type
= lp_build_vec_type(bld
->gallivm
, type
);
3410 LLVMTypeRef int_vec_type
= lp_build_int_vec_type(bld
->gallivm
, type
);
3412 LLVMValueRef expmask
= lp_build_const_int_vec(bld
->gallivm
, type
, 0x7f800000);
3413 LLVMValueRef mantmask
= lp_build_const_int_vec(bld
->gallivm
, type
, 0x007fffff);
3414 LLVMValueRef one
= LLVMConstBitCast(bld
->one
, int_vec_type
);
3416 LLVMValueRef i
= NULL
;
3417 LLVMValueRef y
= NULL
;
3418 LLVMValueRef z
= NULL
;
3419 LLVMValueRef exp
= NULL
;
3420 LLVMValueRef mant
= NULL
;
3421 LLVMValueRef logexp
= NULL
;
3422 LLVMValueRef logmant
= NULL
;
3423 LLVMValueRef res
= NULL
;
3425 assert(lp_check_value(bld
->type
, x
));
3427 if(p_exp
|| p_floor_log2
|| p_log2
) {
3428 /* TODO: optimize the constant case */
3429 if (gallivm_debug
& GALLIVM_DEBUG_PERF
&&
3430 LLVMIsConstant(x
)) {
3431 debug_printf("%s: inefficient/imprecise constant arithmetic\n",
3435 assert(type
.floating
&& type
.width
== 32);
3438 * We don't explicitly handle denormalized numbers. They will yield a
3439 * result in the neighbourhood of -127, which appears to be adequate
3443 i
= LLVMBuildBitCast(builder
, x
, int_vec_type
, "");
3445 /* exp = (float) exponent(x) */
3446 exp
= LLVMBuildAnd(builder
, i
, expmask
, "");
3449 if(p_floor_log2
|| p_log2
) {
3450 logexp
= LLVMBuildLShr(builder
, exp
, lp_build_const_int_vec(bld
->gallivm
, type
, 23), "");
3451 logexp
= LLVMBuildSub(builder
, logexp
, lp_build_const_int_vec(bld
->gallivm
, type
, 127), "");
3452 logexp
= LLVMBuildSIToFP(builder
, logexp
, vec_type
, "");
3456 /* mant = 1 + (float) mantissa(x) */
3457 mant
= LLVMBuildAnd(builder
, i
, mantmask
, "");
3458 mant
= LLVMBuildOr(builder
, mant
, one
, "");
3459 mant
= LLVMBuildBitCast(builder
, mant
, vec_type
, "");
3461 /* y = (mant - 1) / (mant + 1) */
3462 y
= lp_build_div(bld
,
3463 lp_build_sub(bld
, mant
, bld
->one
),
3464 lp_build_add(bld
, mant
, bld
->one
)
3468 z
= lp_build_mul(bld
, y
, y
);
3471 logmant
= lp_build_polynomial(bld
, z
, lp_build_log2_polynomial
,
3472 Elements(lp_build_log2_polynomial
));
3474 /* logmant = y * P(z) */
3475 logmant
= lp_build_mul(bld
, y
, logmant
);
3477 res
= lp_build_add(bld
, logmant
, logexp
);
3479 if (type
.floating
&& handle_edge_cases
) {
3480 LLVMValueRef negmask
, infmask
, zmask
;
3481 negmask
= lp_build_cmp(bld
, PIPE_FUNC_LESS
, x
,
3482 lp_build_const_vec(bld
->gallivm
, type
, 0.0f
));
3483 zmask
= lp_build_cmp(bld
, PIPE_FUNC_EQUAL
, x
,
3484 lp_build_const_vec(bld
->gallivm
, type
, 0.0f
));
3485 infmask
= lp_build_cmp(bld
, PIPE_FUNC_GEQUAL
, x
,
3486 lp_build_const_vec(bld
->gallivm
, type
, INFINITY
));
3488 /* If x is qual to inf make sure we return inf */
3489 res
= lp_build_select(bld
, infmask
,
3490 lp_build_const_vec(bld
->gallivm
, type
, INFINITY
),
3492 /* If x is qual to 0, return -inf */
3493 res
= lp_build_select(bld
, zmask
,
3494 lp_build_const_vec(bld
->gallivm
, type
, -INFINITY
),
3496 /* If x is nan or less than 0, return nan */
3497 res
= lp_build_select(bld
, negmask
,
3498 lp_build_const_vec(bld
->gallivm
, type
, NAN
),
3504 exp
= LLVMBuildBitCast(builder
, exp
, vec_type
, "");
3509 *p_floor_log2
= logexp
;
3517 * log2 implementation which doesn't have special code to
3518 * handle edge cases (-inf, 0, inf, NaN). It's faster but
3519 * the results for those cases are undefined.
3522 lp_build_log2(struct lp_build_context
*bld
,
3526 lp_build_log2_approx(bld
, x
, NULL
, NULL
, &res
, FALSE
);
3531 * Version of log2 which handles all edge cases.
3532 * Look at documentation of lp_build_log2_approx for
3533 * description of the behavior for each of the edge cases.
3536 lp_build_log2_safe(struct lp_build_context
*bld
,
3540 lp_build_log2_approx(bld
, x
, NULL
, NULL
, &res
, TRUE
);
3546 * Faster (and less accurate) log2.
3548 * log2(x) = floor(log2(x)) - 1 + x / 2**floor(log2(x))
3550 * Piece-wise linear approximation, with exact results when x is a
3553 * See http://www.flipcode.com/archives/Fast_log_Function.shtml
3556 lp_build_fast_log2(struct lp_build_context
*bld
,
3559 LLVMBuilderRef builder
= bld
->gallivm
->builder
;
3563 assert(lp_check_value(bld
->type
, x
));
3565 assert(bld
->type
.floating
);
3567 /* ipart = floor(log2(x)) - 1 */
3568 ipart
= lp_build_extract_exponent(bld
, x
, -1);
3569 ipart
= LLVMBuildSIToFP(builder
, ipart
, bld
->vec_type
, "");
3571 /* fpart = x / 2**ipart */
3572 fpart
= lp_build_extract_mantissa(bld
, x
);
3575 return LLVMBuildFAdd(builder
, ipart
, fpart
, "");
3580 * Fast implementation of iround(log2(x)).
3582 * Not an approximation -- it should give accurate results all the time.
3585 lp_build_ilog2(struct lp_build_context
*bld
,
3588 LLVMBuilderRef builder
= bld
->gallivm
->builder
;
3589 LLVMValueRef sqrt2
= lp_build_const_vec(bld
->gallivm
, bld
->type
, M_SQRT2
);
3592 assert(bld
->type
.floating
);
3594 assert(lp_check_value(bld
->type
, x
));
3596 /* x * 2^(0.5) i.e., add 0.5 to the log2(x) */
3597 x
= LLVMBuildFMul(builder
, x
, sqrt2
, "");
3599 /* ipart = floor(log2(x) + 0.5) */
3600 ipart
= lp_build_extract_exponent(bld
, x
, 0);
3606 lp_build_mod(struct lp_build_context
*bld
,
3610 LLVMBuilderRef builder
= bld
->gallivm
->builder
;
3612 const struct lp_type type
= bld
->type
;
3614 assert(lp_check_value(type
, x
));
3615 assert(lp_check_value(type
, y
));
3618 res
= LLVMBuildFRem(builder
, x
, y
, "");
3620 res
= LLVMBuildSRem(builder
, x
, y
, "");
3622 res
= LLVMBuildURem(builder
, x
, y
, "");
3628 * For floating inputs it creates and returns a mask
3629 * which is all 1's for channels which are NaN.
3630 * Channels inside x which are not NaN will be 0.
3633 lp_build_isnan(struct lp_build_context
*bld
,
3637 LLVMTypeRef int_vec_type
= lp_build_int_vec_type(bld
->gallivm
, bld
->type
);
3639 assert(bld
->type
.floating
);
3640 assert(lp_check_value(bld
->type
, x
));
3642 mask
= LLVMBuildFCmp(bld
->gallivm
->builder
, LLVMRealOEQ
, x
, x
,
3644 mask
= LLVMBuildNot(bld
->gallivm
->builder
, mask
, "");
3645 mask
= LLVMBuildSExt(bld
->gallivm
->builder
, mask
, int_vec_type
, "isnan");
3649 /* Returns all 1's for floating point numbers that are
3650 * finite numbers and returns all zeros for -inf,
3653 lp_build_isfinite(struct lp_build_context
*bld
,
3656 LLVMBuilderRef builder
= bld
->gallivm
->builder
;
3657 LLVMTypeRef int_vec_type
= lp_build_int_vec_type(bld
->gallivm
, bld
->type
);
3658 struct lp_type int_type
= lp_int_type(bld
->type
);
3659 LLVMValueRef intx
= LLVMBuildBitCast(builder
, x
, int_vec_type
, "");
3660 LLVMValueRef infornan32
= lp_build_const_int_vec(bld
->gallivm
, bld
->type
,
3663 if (!bld
->type
.floating
) {
3664 return lp_build_const_int_vec(bld
->gallivm
, bld
->type
, 0);
3666 assert(bld
->type
.floating
);
3667 assert(lp_check_value(bld
->type
, x
));
3668 assert(bld
->type
.width
== 32);
3670 intx
= LLVMBuildAnd(builder
, intx
, infornan32
, "");
3671 return lp_build_compare(bld
->gallivm
, int_type
, PIPE_FUNC_NOTEQUAL
,