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 <llvm/Config/llvm-config.h>
52 #include "util/u_memory.h"
53 #include "util/u_debug.h"
54 #include "util/u_math.h"
55 #include "util/u_cpu_detect.h"
57 #include "lp_bld_type.h"
58 #include "lp_bld_const.h"
59 #include "lp_bld_init.h"
60 #include "lp_bld_intr.h"
61 #include "lp_bld_logic.h"
62 #include "lp_bld_pack.h"
63 #include "lp_bld_debug.h"
64 #include "lp_bld_bitarit.h"
65 #include "lp_bld_arit.h"
66 #include "lp_bld_flow.h"
68 #if defined(PIPE_ARCH_SSE)
69 #include <xmmintrin.h>
72 #ifndef _MM_DENORMALS_ZERO_MASK
73 #define _MM_DENORMALS_ZERO_MASK 0x0040
76 #ifndef _MM_FLUSH_ZERO_MASK
77 #define _MM_FLUSH_ZERO_MASK 0x8000
80 #define EXP_POLY_DEGREE 5
82 #define LOG_POLY_DEGREE 4
87 * No checks for special case values of a or b = 1 or 0 are done.
88 * NaN's are handled according to the behavior specified by the
89 * nan_behavior argument.
92 lp_build_min_simple(struct lp_build_context
*bld
,
95 enum gallivm_nan_behavior nan_behavior
)
97 const struct lp_type type
= bld
->type
;
98 const char *intrinsic
= NULL
;
99 unsigned intr_size
= 0;
102 assert(lp_check_value(type
, a
));
103 assert(lp_check_value(type
, b
));
105 /* TODO: optimize the constant case */
107 if (type
.floating
&& util_cpu_caps
.has_sse
) {
108 if (type
.width
== 32) {
109 if (type
.length
== 1) {
110 intrinsic
= "llvm.x86.sse.min.ss";
113 else if (type
.length
<= 4 || !util_cpu_caps
.has_avx
) {
114 intrinsic
= "llvm.x86.sse.min.ps";
118 intrinsic
= "llvm.x86.avx.min.ps.256";
122 if (type
.width
== 64 && util_cpu_caps
.has_sse2
) {
123 if (type
.length
== 1) {
124 intrinsic
= "llvm.x86.sse2.min.sd";
127 else if (type
.length
== 2 || !util_cpu_caps
.has_avx
) {
128 intrinsic
= "llvm.x86.sse2.min.pd";
132 intrinsic
= "llvm.x86.avx.min.pd.256";
137 else if (type
.floating
&& util_cpu_caps
.has_altivec
) {
138 if (nan_behavior
== GALLIVM_NAN_RETURN_NAN
||
139 nan_behavior
== GALLIVM_NAN_RETURN_NAN_FIRST_NONNAN
) {
140 debug_printf("%s: altivec doesn't support nan return nan behavior\n",
143 if (type
.width
== 32 && type
.length
== 4) {
144 intrinsic
= "llvm.ppc.altivec.vminfp";
147 } else if (LLVM_VERSION_MAJOR
== 3 && LLVM_VERSION_MINOR
< 9 &&
148 util_cpu_caps
.has_avx2
&& type
.length
> 4) {
150 switch (type
.width
) {
152 intrinsic
= type
.sign
? "llvm.x86.avx2.pmins.b" : "llvm.x86.avx2.pminu.b";
155 intrinsic
= type
.sign
? "llvm.x86.avx2.pmins.w" : "llvm.x86.avx2.pminu.w";
158 intrinsic
= type
.sign
? "llvm.x86.avx2.pmins.d" : "llvm.x86.avx2.pminu.d";
161 } else if (LLVM_VERSION_MAJOR
== 3 && LLVM_VERSION_MINOR
< 9 &&
162 util_cpu_caps
.has_sse2
&& type
.length
>= 2) {
164 if ((type
.width
== 8 || type
.width
== 16) &&
165 (type
.width
* type
.length
<= 64) &&
166 (gallivm_debug
& GALLIVM_DEBUG_PERF
)) {
167 debug_printf("%s: inefficient code, bogus shuffle due to packing\n",
170 if (type
.width
== 8 && !type
.sign
) {
171 intrinsic
= "llvm.x86.sse2.pminu.b";
173 else if (type
.width
== 16 && type
.sign
) {
174 intrinsic
= "llvm.x86.sse2.pmins.w";
176 if (util_cpu_caps
.has_sse4_1
) {
177 if (type
.width
== 8 && type
.sign
) {
178 intrinsic
= "llvm.x86.sse41.pminsb";
180 if (type
.width
== 16 && !type
.sign
) {
181 intrinsic
= "llvm.x86.sse41.pminuw";
183 if (type
.width
== 32 && !type
.sign
) {
184 intrinsic
= "llvm.x86.sse41.pminud";
186 if (type
.width
== 32 && type
.sign
) {
187 intrinsic
= "llvm.x86.sse41.pminsd";
190 } else if (util_cpu_caps
.has_altivec
) {
192 if (type
.width
== 8) {
194 intrinsic
= "llvm.ppc.altivec.vminub";
196 intrinsic
= "llvm.ppc.altivec.vminsb";
198 } else if (type
.width
== 16) {
200 intrinsic
= "llvm.ppc.altivec.vminuh";
202 intrinsic
= "llvm.ppc.altivec.vminsh";
204 } else if (type
.width
== 32) {
206 intrinsic
= "llvm.ppc.altivec.vminuw";
208 intrinsic
= "llvm.ppc.altivec.vminsw";
214 /* We need to handle nan's for floating point numbers. If one of the
215 * inputs is nan the other should be returned (required by both D3D10+
217 * The sse intrinsics return the second operator in case of nan by
218 * default so we need to special code to handle those.
220 if (util_cpu_caps
.has_sse
&& type
.floating
&&
221 nan_behavior
!= GALLIVM_NAN_BEHAVIOR_UNDEFINED
&&
222 nan_behavior
!= GALLIVM_NAN_RETURN_OTHER_SECOND_NONNAN
&&
223 nan_behavior
!= GALLIVM_NAN_RETURN_NAN_FIRST_NONNAN
) {
224 LLVMValueRef isnan
, min
;
225 min
= lp_build_intrinsic_binary_anylength(bld
->gallivm
, intrinsic
,
228 if (nan_behavior
== GALLIVM_NAN_RETURN_OTHER
) {
229 isnan
= lp_build_isnan(bld
, b
);
230 return lp_build_select(bld
, isnan
, a
, min
);
232 assert(nan_behavior
== GALLIVM_NAN_RETURN_NAN
);
233 isnan
= lp_build_isnan(bld
, a
);
234 return lp_build_select(bld
, isnan
, a
, min
);
237 return lp_build_intrinsic_binary_anylength(bld
->gallivm
, intrinsic
,
244 switch (nan_behavior
) {
245 case GALLIVM_NAN_RETURN_NAN
: {
246 LLVMValueRef isnan
= lp_build_isnan(bld
, b
);
247 cond
= lp_build_cmp(bld
, PIPE_FUNC_LESS
, a
, b
);
248 cond
= LLVMBuildXor(bld
->gallivm
->builder
, cond
, isnan
, "");
249 return lp_build_select(bld
, cond
, a
, b
);
252 case GALLIVM_NAN_RETURN_OTHER
: {
253 LLVMValueRef isnan
= lp_build_isnan(bld
, a
);
254 cond
= lp_build_cmp(bld
, PIPE_FUNC_LESS
, a
, b
);
255 cond
= LLVMBuildXor(bld
->gallivm
->builder
, cond
, isnan
, "");
256 return lp_build_select(bld
, cond
, a
, b
);
259 case GALLIVM_NAN_RETURN_OTHER_SECOND_NONNAN
:
260 cond
= lp_build_cmp_ordered(bld
, PIPE_FUNC_LESS
, a
, b
);
261 return lp_build_select(bld
, cond
, a
, b
);
262 case GALLIVM_NAN_RETURN_NAN_FIRST_NONNAN
:
263 cond
= lp_build_cmp(bld
, PIPE_FUNC_LESS
, b
, a
);
264 return lp_build_select(bld
, cond
, b
, a
);
265 case GALLIVM_NAN_BEHAVIOR_UNDEFINED
:
266 cond
= lp_build_cmp(bld
, PIPE_FUNC_LESS
, a
, b
);
267 return lp_build_select(bld
, cond
, a
, b
);
271 cond
= lp_build_cmp(bld
, PIPE_FUNC_LESS
, a
, b
);
272 return lp_build_select(bld
, cond
, a
, b
);
275 cond
= lp_build_cmp(bld
, PIPE_FUNC_LESS
, a
, b
);
276 return lp_build_select(bld
, cond
, a
, b
);
282 lp_build_fmuladd(LLVMBuilderRef builder
,
287 LLVMTypeRef type
= LLVMTypeOf(a
);
288 assert(type
== LLVMTypeOf(b
));
289 assert(type
== LLVMTypeOf(c
));
292 lp_format_intrinsic(intrinsic
, sizeof intrinsic
, "llvm.fmuladd", type
);
293 LLVMValueRef args
[] = { a
, b
, c
};
294 return lp_build_intrinsic(builder
, intrinsic
, type
, args
, 3, 0);
300 * No checks for special case values of a or b = 1 or 0 are done.
301 * NaN's are handled according to the behavior specified by the
302 * nan_behavior argument.
305 lp_build_max_simple(struct lp_build_context
*bld
,
308 enum gallivm_nan_behavior nan_behavior
)
310 const struct lp_type type
= bld
->type
;
311 const char *intrinsic
= NULL
;
312 unsigned intr_size
= 0;
315 assert(lp_check_value(type
, a
));
316 assert(lp_check_value(type
, b
));
318 /* TODO: optimize the constant case */
320 if (type
.floating
&& util_cpu_caps
.has_sse
) {
321 if (type
.width
== 32) {
322 if (type
.length
== 1) {
323 intrinsic
= "llvm.x86.sse.max.ss";
326 else if (type
.length
<= 4 || !util_cpu_caps
.has_avx
) {
327 intrinsic
= "llvm.x86.sse.max.ps";
331 intrinsic
= "llvm.x86.avx.max.ps.256";
335 if (type
.width
== 64 && util_cpu_caps
.has_sse2
) {
336 if (type
.length
== 1) {
337 intrinsic
= "llvm.x86.sse2.max.sd";
340 else if (type
.length
== 2 || !util_cpu_caps
.has_avx
) {
341 intrinsic
= "llvm.x86.sse2.max.pd";
345 intrinsic
= "llvm.x86.avx.max.pd.256";
350 else if (type
.floating
&& util_cpu_caps
.has_altivec
) {
351 if (nan_behavior
== GALLIVM_NAN_RETURN_NAN
||
352 nan_behavior
== GALLIVM_NAN_RETURN_NAN_FIRST_NONNAN
) {
353 debug_printf("%s: altivec doesn't support nan return nan behavior\n",
356 if (type
.width
== 32 || type
.length
== 4) {
357 intrinsic
= "llvm.ppc.altivec.vmaxfp";
360 } else if (LLVM_VERSION_MAJOR
== 3 && LLVM_VERSION_MINOR
< 9 &&
361 util_cpu_caps
.has_avx2
&& type
.length
> 4) {
363 switch (type
.width
) {
365 intrinsic
= type
.sign
? "llvm.x86.avx2.pmaxs.b" : "llvm.x86.avx2.pmaxu.b";
368 intrinsic
= type
.sign
? "llvm.x86.avx2.pmaxs.w" : "llvm.x86.avx2.pmaxu.w";
371 intrinsic
= type
.sign
? "llvm.x86.avx2.pmaxs.d" : "llvm.x86.avx2.pmaxu.d";
374 } else if (LLVM_VERSION_MAJOR
== 3 && LLVM_VERSION_MINOR
< 9 &&
375 util_cpu_caps
.has_sse2
&& type
.length
>= 2) {
377 if ((type
.width
== 8 || type
.width
== 16) &&
378 (type
.width
* type
.length
<= 64) &&
379 (gallivm_debug
& GALLIVM_DEBUG_PERF
)) {
380 debug_printf("%s: inefficient code, bogus shuffle due to packing\n",
383 if (type
.width
== 8 && !type
.sign
) {
384 intrinsic
= "llvm.x86.sse2.pmaxu.b";
387 else if (type
.width
== 16 && type
.sign
) {
388 intrinsic
= "llvm.x86.sse2.pmaxs.w";
390 if (util_cpu_caps
.has_sse4_1
) {
391 if (type
.width
== 8 && type
.sign
) {
392 intrinsic
= "llvm.x86.sse41.pmaxsb";
394 if (type
.width
== 16 && !type
.sign
) {
395 intrinsic
= "llvm.x86.sse41.pmaxuw";
397 if (type
.width
== 32 && !type
.sign
) {
398 intrinsic
= "llvm.x86.sse41.pmaxud";
400 if (type
.width
== 32 && type
.sign
) {
401 intrinsic
= "llvm.x86.sse41.pmaxsd";
404 } else if (util_cpu_caps
.has_altivec
) {
406 if (type
.width
== 8) {
408 intrinsic
= "llvm.ppc.altivec.vmaxub";
410 intrinsic
= "llvm.ppc.altivec.vmaxsb";
412 } else if (type
.width
== 16) {
414 intrinsic
= "llvm.ppc.altivec.vmaxuh";
416 intrinsic
= "llvm.ppc.altivec.vmaxsh";
418 } else if (type
.width
== 32) {
420 intrinsic
= "llvm.ppc.altivec.vmaxuw";
422 intrinsic
= "llvm.ppc.altivec.vmaxsw";
428 if (util_cpu_caps
.has_sse
&& type
.floating
&&
429 nan_behavior
!= GALLIVM_NAN_BEHAVIOR_UNDEFINED
&&
430 nan_behavior
!= GALLIVM_NAN_RETURN_OTHER_SECOND_NONNAN
&&
431 nan_behavior
!= GALLIVM_NAN_RETURN_NAN_FIRST_NONNAN
) {
432 LLVMValueRef isnan
, max
;
433 max
= lp_build_intrinsic_binary_anylength(bld
->gallivm
, intrinsic
,
436 if (nan_behavior
== GALLIVM_NAN_RETURN_OTHER
) {
437 isnan
= lp_build_isnan(bld
, b
);
438 return lp_build_select(bld
, isnan
, a
, max
);
440 assert(nan_behavior
== GALLIVM_NAN_RETURN_NAN
);
441 isnan
= lp_build_isnan(bld
, a
);
442 return lp_build_select(bld
, isnan
, a
, max
);
445 return lp_build_intrinsic_binary_anylength(bld
->gallivm
, intrinsic
,
452 switch (nan_behavior
) {
453 case GALLIVM_NAN_RETURN_NAN
: {
454 LLVMValueRef isnan
= lp_build_isnan(bld
, b
);
455 cond
= lp_build_cmp(bld
, PIPE_FUNC_GREATER
, a
, b
);
456 cond
= LLVMBuildXor(bld
->gallivm
->builder
, cond
, isnan
, "");
457 return lp_build_select(bld
, cond
, a
, b
);
460 case GALLIVM_NAN_RETURN_OTHER
: {
461 LLVMValueRef isnan
= lp_build_isnan(bld
, a
);
462 cond
= lp_build_cmp(bld
, PIPE_FUNC_GREATER
, a
, b
);
463 cond
= LLVMBuildXor(bld
->gallivm
->builder
, cond
, isnan
, "");
464 return lp_build_select(bld
, cond
, a
, b
);
467 case GALLIVM_NAN_RETURN_OTHER_SECOND_NONNAN
:
468 cond
= lp_build_cmp_ordered(bld
, PIPE_FUNC_GREATER
, a
, b
);
469 return lp_build_select(bld
, cond
, a
, b
);
470 case GALLIVM_NAN_RETURN_NAN_FIRST_NONNAN
:
471 cond
= lp_build_cmp(bld
, PIPE_FUNC_GREATER
, b
, a
);
472 return lp_build_select(bld
, cond
, b
, a
);
473 case GALLIVM_NAN_BEHAVIOR_UNDEFINED
:
474 cond
= lp_build_cmp(bld
, PIPE_FUNC_GREATER
, a
, b
);
475 return lp_build_select(bld
, cond
, a
, b
);
479 cond
= lp_build_cmp(bld
, PIPE_FUNC_GREATER
, a
, b
);
480 return lp_build_select(bld
, cond
, a
, b
);
483 cond
= lp_build_cmp(bld
, PIPE_FUNC_GREATER
, a
, b
);
484 return lp_build_select(bld
, cond
, a
, b
);
490 * Generate 1 - a, or ~a depending on bld->type.
493 lp_build_comp(struct lp_build_context
*bld
,
496 LLVMBuilderRef builder
= bld
->gallivm
->builder
;
497 const struct lp_type type
= bld
->type
;
499 assert(lp_check_value(type
, a
));
506 if(type
.norm
&& !type
.floating
&& !type
.fixed
&& !type
.sign
) {
507 if(LLVMIsConstant(a
))
508 return LLVMConstNot(a
);
510 return LLVMBuildNot(builder
, a
, "");
513 if(LLVMIsConstant(a
))
515 return LLVMConstFSub(bld
->one
, a
);
517 return LLVMConstSub(bld
->one
, a
);
520 return LLVMBuildFSub(builder
, bld
->one
, a
, "");
522 return LLVMBuildSub(builder
, bld
->one
, a
, "");
530 lp_build_add(struct lp_build_context
*bld
,
534 LLVMBuilderRef builder
= bld
->gallivm
->builder
;
535 const struct lp_type type
= bld
->type
;
538 assert(lp_check_value(type
, a
));
539 assert(lp_check_value(type
, b
));
545 if (a
== bld
->undef
|| b
== bld
->undef
)
549 const char *intrinsic
= NULL
;
551 if (!type
.sign
&& (a
== bld
->one
|| b
== bld
->one
))
554 if (!type
.floating
&& !type
.fixed
) {
555 if (LLVM_VERSION_MAJOR
>= 9) {
557 intrinsic
= type
.sign
? "llvm.sadd.sat" : "llvm.uadd.sat";
558 lp_format_intrinsic(intrin
, sizeof intrin
, intrinsic
, bld
->vec_type
);
559 return lp_build_intrinsic_binary(builder
, intrin
, bld
->vec_type
, a
, b
);
561 if (type
.width
* type
.length
== 128) {
562 if (util_cpu_caps
.has_sse2
) {
564 intrinsic
= type
.sign
? "llvm.x86.sse2.padds.b" :
565 LLVM_VERSION_MAJOR
< 8 ? "llvm.x86.sse2.paddus.b" : NULL
;
566 if (type
.width
== 16)
567 intrinsic
= type
.sign
? "llvm.x86.sse2.padds.w" :
568 LLVM_VERSION_MAJOR
< 8 ? "llvm.x86.sse2.paddus.w" : NULL
;
569 } else if (util_cpu_caps
.has_altivec
) {
571 intrinsic
= type
.sign
? "llvm.ppc.altivec.vaddsbs" : "llvm.ppc.altivec.vaddubs";
572 if (type
.width
== 16)
573 intrinsic
= type
.sign
? "llvm.ppc.altivec.vaddshs" : "llvm.ppc.altivec.vadduhs";
576 if (type
.width
* type
.length
== 256) {
577 if (util_cpu_caps
.has_avx2
) {
579 intrinsic
= type
.sign
? "llvm.x86.avx2.padds.b" :
580 LLVM_VERSION_MAJOR
< 8 ? "llvm.x86.avx2.paddus.b" : NULL
;
581 if (type
.width
== 16)
582 intrinsic
= type
.sign
? "llvm.x86.avx2.padds.w" :
583 LLVM_VERSION_MAJOR
< 8 ? "llvm.x86.avx2.paddus.w" : NULL
;
589 return lp_build_intrinsic_binary(builder
, intrinsic
, lp_build_vec_type(bld
->gallivm
, bld
->type
), a
, b
);
592 if(type
.norm
&& !type
.floating
&& !type
.fixed
) {
594 uint64_t sign
= (uint64_t)1 << (type
.width
- 1);
595 LLVMValueRef max_val
= lp_build_const_int_vec(bld
->gallivm
, type
, sign
- 1);
596 LLVMValueRef min_val
= lp_build_const_int_vec(bld
->gallivm
, type
, sign
);
597 /* a_clamp_max is the maximum a for positive b,
598 a_clamp_min is the minimum a for negative b. */
599 LLVMValueRef a_clamp_max
= lp_build_min_simple(bld
, a
, LLVMBuildSub(builder
, max_val
, b
, ""), GALLIVM_NAN_BEHAVIOR_UNDEFINED
);
600 LLVMValueRef a_clamp_min
= lp_build_max_simple(bld
, a
, LLVMBuildSub(builder
, min_val
, b
, ""), GALLIVM_NAN_BEHAVIOR_UNDEFINED
);
601 a
= lp_build_select(bld
, lp_build_cmp(bld
, PIPE_FUNC_GREATER
, b
, bld
->zero
), a_clamp_max
, a_clamp_min
);
605 if(LLVMIsConstant(a
) && LLVMIsConstant(b
))
607 res
= LLVMConstFAdd(a
, b
);
609 res
= LLVMConstAdd(a
, b
);
612 res
= LLVMBuildFAdd(builder
, a
, b
, "");
614 res
= LLVMBuildAdd(builder
, a
, b
, "");
616 /* clamp to ceiling of 1.0 */
617 if(bld
->type
.norm
&& (bld
->type
.floating
|| bld
->type
.fixed
))
618 res
= lp_build_min_simple(bld
, res
, bld
->one
, GALLIVM_NAN_BEHAVIOR_UNDEFINED
);
620 if (type
.norm
&& !type
.floating
&& !type
.fixed
) {
623 * newer llvm versions no longer support the intrinsics, but recognize
624 * the pattern. Since auto-upgrade of intrinsics doesn't work for jit
625 * code, it is important we match the pattern llvm uses (and pray llvm
626 * doesn't change it - and hope they decide on the same pattern for
627 * all backends supporting it...).
628 * NOTE: cmp/select does sext/trunc of the mask. Does not seem to
629 * interfere with llvm's ability to recognize the pattern but seems
631 * NOTE: llvm 9+ always uses (non arch specific) intrinsic.
633 LLVMValueRef overflowed
= lp_build_cmp(bld
, PIPE_FUNC_GREATER
, a
, res
);
634 res
= lp_build_select(bld
, overflowed
,
635 LLVMConstAllOnes(bld
->int_vec_type
), res
);
639 /* XXX clamp to floor of -1 or 0??? */
645 /** Return the scalar sum of the elements of a.
646 * Should avoid this operation whenever possible.
649 lp_build_horizontal_add(struct lp_build_context
*bld
,
652 LLVMBuilderRef builder
= bld
->gallivm
->builder
;
653 const struct lp_type type
= bld
->type
;
654 LLVMValueRef index
, res
;
656 LLVMValueRef shuffles1
[LP_MAX_VECTOR_LENGTH
/ 2];
657 LLVMValueRef shuffles2
[LP_MAX_VECTOR_LENGTH
/ 2];
658 LLVMValueRef vecres
, elem2
;
660 assert(lp_check_value(type
, a
));
662 if (type
.length
== 1) {
666 assert(!bld
->type
.norm
);
669 * for byte vectors can do much better with psadbw.
670 * Using repeated shuffle/adds here. Note with multiple vectors
671 * this can be done more efficiently as outlined in the intel
672 * optimization manual.
673 * Note: could cause data rearrangement if used with smaller element
678 length
= type
.length
/ 2;
680 LLVMValueRef vec1
, vec2
;
681 for (i
= 0; i
< length
; i
++) {
682 shuffles1
[i
] = lp_build_const_int32(bld
->gallivm
, i
);
683 shuffles2
[i
] = lp_build_const_int32(bld
->gallivm
, i
+ length
);
685 vec1
= LLVMBuildShuffleVector(builder
, vecres
, vecres
,
686 LLVMConstVector(shuffles1
, length
), "");
687 vec2
= LLVMBuildShuffleVector(builder
, vecres
, vecres
,
688 LLVMConstVector(shuffles2
, length
), "");
690 vecres
= LLVMBuildFAdd(builder
, vec1
, vec2
, "");
693 vecres
= LLVMBuildAdd(builder
, vec1
, vec2
, "");
695 length
= length
>> 1;
698 /* always have vector of size 2 here */
701 index
= lp_build_const_int32(bld
->gallivm
, 0);
702 res
= LLVMBuildExtractElement(builder
, vecres
, index
, "");
703 index
= lp_build_const_int32(bld
->gallivm
, 1);
704 elem2
= LLVMBuildExtractElement(builder
, vecres
, index
, "");
707 res
= LLVMBuildFAdd(builder
, res
, elem2
, "");
709 res
= LLVMBuildAdd(builder
, res
, elem2
, "");
715 * Return the horizontal sums of 4 float vectors as a float4 vector.
716 * This uses the technique as outlined in Intel Optimization Manual.
719 lp_build_horizontal_add4x4f(struct lp_build_context
*bld
,
722 struct gallivm_state
*gallivm
= bld
->gallivm
;
723 LLVMBuilderRef builder
= gallivm
->builder
;
724 LLVMValueRef shuffles
[4];
726 LLVMValueRef sumtmp
[2], shuftmp
[2];
728 /* lower half of regs */
729 shuffles
[0] = lp_build_const_int32(gallivm
, 0);
730 shuffles
[1] = lp_build_const_int32(gallivm
, 1);
731 shuffles
[2] = lp_build_const_int32(gallivm
, 4);
732 shuffles
[3] = lp_build_const_int32(gallivm
, 5);
733 tmp
[0] = LLVMBuildShuffleVector(builder
, src
[0], src
[1],
734 LLVMConstVector(shuffles
, 4), "");
735 tmp
[2] = LLVMBuildShuffleVector(builder
, src
[2], src
[3],
736 LLVMConstVector(shuffles
, 4), "");
738 /* upper half of regs */
739 shuffles
[0] = lp_build_const_int32(gallivm
, 2);
740 shuffles
[1] = lp_build_const_int32(gallivm
, 3);
741 shuffles
[2] = lp_build_const_int32(gallivm
, 6);
742 shuffles
[3] = lp_build_const_int32(gallivm
, 7);
743 tmp
[1] = LLVMBuildShuffleVector(builder
, src
[0], src
[1],
744 LLVMConstVector(shuffles
, 4), "");
745 tmp
[3] = LLVMBuildShuffleVector(builder
, src
[2], src
[3],
746 LLVMConstVector(shuffles
, 4), "");
748 sumtmp
[0] = LLVMBuildFAdd(builder
, tmp
[0], tmp
[1], "");
749 sumtmp
[1] = LLVMBuildFAdd(builder
, tmp
[2], tmp
[3], "");
751 shuffles
[0] = lp_build_const_int32(gallivm
, 0);
752 shuffles
[1] = lp_build_const_int32(gallivm
, 2);
753 shuffles
[2] = lp_build_const_int32(gallivm
, 4);
754 shuffles
[3] = lp_build_const_int32(gallivm
, 6);
755 shuftmp
[0] = LLVMBuildShuffleVector(builder
, sumtmp
[0], sumtmp
[1],
756 LLVMConstVector(shuffles
, 4), "");
758 shuffles
[0] = lp_build_const_int32(gallivm
, 1);
759 shuffles
[1] = lp_build_const_int32(gallivm
, 3);
760 shuffles
[2] = lp_build_const_int32(gallivm
, 5);
761 shuffles
[3] = lp_build_const_int32(gallivm
, 7);
762 shuftmp
[1] = LLVMBuildShuffleVector(builder
, sumtmp
[0], sumtmp
[1],
763 LLVMConstVector(shuffles
, 4), "");
765 return LLVMBuildFAdd(builder
, shuftmp
[0], shuftmp
[1], "");
770 * partially horizontally add 2-4 float vectors with length nx4,
771 * i.e. only four adjacent values in each vector will be added,
772 * assuming values are really grouped in 4 which also determines
775 * Return a vector of the same length as the initial vectors,
776 * with the excess elements (if any) being undefined.
777 * The element order is independent of number of input vectors.
778 * For 3 vectors x0x1x2x3x4x5x6x7, y0y1y2y3y4y5y6y7, z0z1z2z3z4z5z6z7
779 * the output order thus will be
780 * sumx0-x3,sumy0-y3,sumz0-z3,undef,sumx4-x7,sumy4-y7,sumz4z7,undef
783 lp_build_hadd_partial4(struct lp_build_context
*bld
,
784 LLVMValueRef vectors
[],
787 struct gallivm_state
*gallivm
= bld
->gallivm
;
788 LLVMBuilderRef builder
= gallivm
->builder
;
789 LLVMValueRef ret_vec
;
791 const char *intrinsic
= NULL
;
793 assert(num_vecs
>= 2 && num_vecs
<= 4);
794 assert(bld
->type
.floating
);
796 /* only use this with at least 2 vectors, as it is sort of expensive
797 * (depending on cpu) and we always need two horizontal adds anyway,
798 * so a shuffle/add approach might be better.
804 tmp
[2] = num_vecs
> 2 ? vectors
[2] : vectors
[0];
805 tmp
[3] = num_vecs
> 3 ? vectors
[3] : vectors
[0];
807 if (util_cpu_caps
.has_sse3
&& bld
->type
.width
== 32 &&
808 bld
->type
.length
== 4) {
809 intrinsic
= "llvm.x86.sse3.hadd.ps";
811 else if (util_cpu_caps
.has_avx
&& bld
->type
.width
== 32 &&
812 bld
->type
.length
== 8) {
813 intrinsic
= "llvm.x86.avx.hadd.ps.256";
816 tmp
[0] = lp_build_intrinsic_binary(builder
, intrinsic
,
817 lp_build_vec_type(gallivm
, bld
->type
),
820 tmp
[1] = lp_build_intrinsic_binary(builder
, intrinsic
,
821 lp_build_vec_type(gallivm
, bld
->type
),
827 return lp_build_intrinsic_binary(builder
, intrinsic
,
828 lp_build_vec_type(gallivm
, bld
->type
),
832 if (bld
->type
.length
== 4) {
833 ret_vec
= lp_build_horizontal_add4x4f(bld
, tmp
);
836 LLVMValueRef partres
[LP_MAX_VECTOR_LENGTH
/4];
838 unsigned num_iter
= bld
->type
.length
/ 4;
839 struct lp_type parttype
= bld
->type
;
841 for (j
= 0; j
< num_iter
; j
++) {
842 LLVMValueRef partsrc
[4];
844 for (i
= 0; i
< 4; i
++) {
845 partsrc
[i
] = lp_build_extract_range(gallivm
, tmp
[i
], j
*4, 4);
847 partres
[j
] = lp_build_horizontal_add4x4f(bld
, partsrc
);
849 ret_vec
= lp_build_concat(gallivm
, partres
, parttype
, num_iter
);
858 lp_build_sub(struct lp_build_context
*bld
,
862 LLVMBuilderRef builder
= bld
->gallivm
->builder
;
863 const struct lp_type type
= bld
->type
;
866 assert(lp_check_value(type
, a
));
867 assert(lp_check_value(type
, b
));
871 if (a
== bld
->undef
|| b
== bld
->undef
)
877 const char *intrinsic
= NULL
;
879 if (!type
.sign
&& b
== bld
->one
)
882 if (!type
.floating
&& !type
.fixed
) {
883 if (LLVM_VERSION_MAJOR
>= 9) {
885 intrinsic
= type
.sign
? "llvm.ssub.sat" : "llvm.usub.sat";
886 lp_format_intrinsic(intrin
, sizeof intrin
, intrinsic
, bld
->vec_type
);
887 return lp_build_intrinsic_binary(builder
, intrin
, bld
->vec_type
, a
, b
);
889 if (type
.width
* type
.length
== 128) {
890 if (util_cpu_caps
.has_sse2
) {
892 intrinsic
= type
.sign
? "llvm.x86.sse2.psubs.b" :
893 LLVM_VERSION_MAJOR
< 8 ? "llvm.x86.sse2.psubus.b" : NULL
;
894 if (type
.width
== 16)
895 intrinsic
= type
.sign
? "llvm.x86.sse2.psubs.w" :
896 LLVM_VERSION_MAJOR
< 8 ? "llvm.x86.sse2.psubus.w" : NULL
;
897 } else if (util_cpu_caps
.has_altivec
) {
899 intrinsic
= type
.sign
? "llvm.ppc.altivec.vsubsbs" : "llvm.ppc.altivec.vsububs";
900 if (type
.width
== 16)
901 intrinsic
= type
.sign
? "llvm.ppc.altivec.vsubshs" : "llvm.ppc.altivec.vsubuhs";
904 if (type
.width
* type
.length
== 256) {
905 if (util_cpu_caps
.has_avx2
) {
907 intrinsic
= type
.sign
? "llvm.x86.avx2.psubs.b" :
908 LLVM_VERSION_MAJOR
< 8 ? "llvm.x86.avx2.psubus.b" : NULL
;
909 if (type
.width
== 16)
910 intrinsic
= type
.sign
? "llvm.x86.avx2.psubs.w" :
911 LLVM_VERSION_MAJOR
< 8 ? "llvm.x86.avx2.psubus.w" : NULL
;
917 return lp_build_intrinsic_binary(builder
, intrinsic
, lp_build_vec_type(bld
->gallivm
, bld
->type
), a
, b
);
920 if(type
.norm
&& !type
.floating
&& !type
.fixed
) {
922 uint64_t sign
= (uint64_t)1 << (type
.width
- 1);
923 LLVMValueRef max_val
= lp_build_const_int_vec(bld
->gallivm
, type
, sign
- 1);
924 LLVMValueRef min_val
= lp_build_const_int_vec(bld
->gallivm
, type
, sign
);
925 /* a_clamp_max is the maximum a for negative b,
926 a_clamp_min is the minimum a for positive b. */
927 LLVMValueRef a_clamp_max
= lp_build_min_simple(bld
, a
, LLVMBuildAdd(builder
, max_val
, b
, ""), GALLIVM_NAN_BEHAVIOR_UNDEFINED
);
928 LLVMValueRef a_clamp_min
= lp_build_max_simple(bld
, a
, LLVMBuildAdd(builder
, min_val
, b
, ""), GALLIVM_NAN_BEHAVIOR_UNDEFINED
);
929 a
= lp_build_select(bld
, lp_build_cmp(bld
, PIPE_FUNC_GREATER
, b
, bld
->zero
), a_clamp_min
, a_clamp_max
);
932 * This must match llvm pattern for saturated unsigned sub.
933 * (lp_build_max_simple actually does the job with its current
934 * definition but do it explicitly here.)
935 * NOTE: cmp/select does sext/trunc of the mask. Does not seem to
936 * interfere with llvm's ability to recognize the pattern but seems
938 * NOTE: llvm 9+ always uses (non arch specific) intrinsic.
940 LLVMValueRef no_ov
= lp_build_cmp(bld
, PIPE_FUNC_GREATER
, a
, b
);
941 a
= lp_build_select(bld
, no_ov
, a
, b
);
945 if(LLVMIsConstant(a
) && LLVMIsConstant(b
))
947 res
= LLVMConstFSub(a
, b
);
949 res
= LLVMConstSub(a
, b
);
952 res
= LLVMBuildFSub(builder
, a
, b
, "");
954 res
= LLVMBuildSub(builder
, a
, b
, "");
956 if(bld
->type
.norm
&& (bld
->type
.floating
|| bld
->type
.fixed
))
957 res
= lp_build_max_simple(bld
, res
, bld
->zero
, GALLIVM_NAN_BEHAVIOR_UNDEFINED
);
965 * Normalized multiplication.
967 * There are several approaches for (using 8-bit normalized multiplication as
972 * makes the following approximation to the division (Sree)
974 * a*b/255 ~= (a*(b + 1)) >> 256
976 * which is the fastest method that satisfies the following OpenGL criteria of
978 * 0*0 = 0 and 255*255 = 255
982 * takes the geometric series approximation to the division
984 * t/255 = (t >> 8) + (t >> 16) + (t >> 24) ..
986 * in this case just the first two terms to fit in 16bit arithmetic
988 * t/255 ~= (t + (t >> 8)) >> 8
990 * note that just by itself it doesn't satisfies the OpenGL criteria, as
991 * 255*255 = 254, so the special case b = 255 must be accounted or roundoff
994 * - geometric series plus rounding
996 * when using a geometric series division instead of truncating the result
997 * use roundoff in the approximation (Jim Blinn)
999 * t/255 ~= (t + (t >> 8) + 0x80) >> 8
1001 * achieving the exact results.
1005 * @sa Alvy Ray Smith, Image Compositing Fundamentals, Tech Memo 4, Aug 15, 1995,
1006 * ftp://ftp.alvyray.com/Acrobat/4_Comp.pdf
1007 * @sa Michael Herf, The "double blend trick", May 2000,
1008 * http://www.stereopsis.com/doubleblend.html
1011 lp_build_mul_norm(struct gallivm_state
*gallivm
,
1012 struct lp_type wide_type
,
1013 LLVMValueRef a
, LLVMValueRef b
)
1015 LLVMBuilderRef builder
= gallivm
->builder
;
1016 struct lp_build_context bld
;
1021 assert(!wide_type
.floating
);
1022 assert(lp_check_value(wide_type
, a
));
1023 assert(lp_check_value(wide_type
, b
));
1025 lp_build_context_init(&bld
, gallivm
, wide_type
);
1027 n
= wide_type
.width
/ 2;
1028 if (wide_type
.sign
) {
1033 * TODO: for 16bits normalized SSE2 vectors we could consider using PMULHUW
1034 * http://ssp.impulsetrain.com/2011/07/03/multiplying-normalized-16-bit-numbers-with-sse2/
1038 * a*b / (2**n - 1) ~= (a*b + (a*b >> n) + half) >> n
1041 ab
= LLVMBuildMul(builder
, a
, b
, "");
1042 ab
= LLVMBuildAdd(builder
, ab
, lp_build_shr_imm(&bld
, ab
, n
), "");
1045 * half = sgn(ab) * 0.5 * (2 ** n) = sgn(ab) * (1 << (n - 1))
1048 half
= lp_build_const_int_vec(gallivm
, wide_type
, 1LL << (n
- 1));
1049 if (wide_type
.sign
) {
1050 LLVMValueRef minus_half
= LLVMBuildNeg(builder
, half
, "");
1051 LLVMValueRef sign
= lp_build_shr_imm(&bld
, ab
, wide_type
.width
- 1);
1052 half
= lp_build_select(&bld
, sign
, minus_half
, half
);
1054 ab
= LLVMBuildAdd(builder
, ab
, half
, "");
1056 /* Final division */
1057 ab
= lp_build_shr_imm(&bld
, ab
, n
);
1066 lp_build_mul(struct lp_build_context
*bld
,
1070 LLVMBuilderRef builder
= bld
->gallivm
->builder
;
1071 const struct lp_type type
= bld
->type
;
1075 assert(lp_check_value(type
, a
));
1076 assert(lp_check_value(type
, b
));
1086 if(a
== bld
->undef
|| b
== bld
->undef
)
1089 if (!type
.floating
&& !type
.fixed
&& type
.norm
) {
1090 struct lp_type wide_type
= lp_wider_type(type
);
1091 LLVMValueRef al
, ah
, bl
, bh
, abl
, abh
, ab
;
1093 lp_build_unpack2_native(bld
->gallivm
, type
, wide_type
, a
, &al
, &ah
);
1094 lp_build_unpack2_native(bld
->gallivm
, type
, wide_type
, b
, &bl
, &bh
);
1096 /* PMULLW, PSRLW, PADDW */
1097 abl
= lp_build_mul_norm(bld
->gallivm
, wide_type
, al
, bl
);
1098 abh
= lp_build_mul_norm(bld
->gallivm
, wide_type
, ah
, bh
);
1100 ab
= lp_build_pack2_native(bld
->gallivm
, wide_type
, type
, abl
, abh
);
1106 shift
= lp_build_const_int_vec(bld
->gallivm
, type
, type
.width
/2);
1110 if(LLVMIsConstant(a
) && LLVMIsConstant(b
)) {
1112 res
= LLVMConstFMul(a
, b
);
1114 res
= LLVMConstMul(a
, b
);
1117 res
= LLVMConstAShr(res
, shift
);
1119 res
= LLVMConstLShr(res
, shift
);
1124 res
= LLVMBuildFMul(builder
, a
, b
, "");
1126 res
= LLVMBuildMul(builder
, a
, b
, "");
1129 res
= LLVMBuildAShr(builder
, res
, shift
, "");
1131 res
= LLVMBuildLShr(builder
, res
, shift
, "");
1139 * Widening mul, valid for 32x32 bit -> 64bit only.
1140 * Result is low 32bits, high bits returned in res_hi.
1142 * Emits code that is meant to be compiled for the host CPU.
1145 lp_build_mul_32_lohi_cpu(struct lp_build_context
*bld
,
1148 LLVMValueRef
*res_hi
)
1150 struct gallivm_state
*gallivm
= bld
->gallivm
;
1151 LLVMBuilderRef builder
= gallivm
->builder
;
1153 assert(bld
->type
.width
== 32);
1154 assert(bld
->type
.floating
== 0);
1155 assert(bld
->type
.fixed
== 0);
1156 assert(bld
->type
.norm
== 0);
1159 * XXX: for some reason, with zext/zext/mul/trunc the code llvm produces
1160 * for x86 simd is atrocious (even if the high bits weren't required),
1161 * trying to handle real 64bit inputs (which of course can't happen due
1162 * to using 64bit umul with 32bit numbers zero-extended to 64bit, but
1163 * apparently llvm does not recognize this widening mul). This includes 6
1164 * (instead of 2) pmuludq plus extra adds and shifts
1165 * The same story applies to signed mul, albeit fixing this requires sse41.
1166 * https://llvm.org/bugs/show_bug.cgi?id=30845
1167 * So, whip up our own code, albeit only for length 4 and 8 (which
1168 * should be good enough)...
1169 * FIXME: For llvm >= 7.0 we should match the autoupgrade pattern
1170 * (bitcast/and/mul/shuffle for unsigned, bitcast/shl/ashr/mul/shuffle
1171 * for signed), which the fallback code does not, without this llvm
1172 * will likely still produce atrocious code.
1174 if (LLVM_VERSION_MAJOR
< 7 &&
1175 (bld
->type
.length
== 4 || bld
->type
.length
== 8) &&
1176 ((util_cpu_caps
.has_sse2
&& (bld
->type
.sign
== 0)) ||
1177 util_cpu_caps
.has_sse4_1
)) {
1178 const char *intrinsic
= NULL
;
1179 LLVMValueRef aeven
, aodd
, beven
, bodd
, muleven
, mulodd
;
1180 LLVMValueRef shuf
[LP_MAX_VECTOR_WIDTH
/ 32], shuf_vec
;
1181 struct lp_type type_wide
= lp_wider_type(bld
->type
);
1182 LLVMTypeRef wider_type
= lp_build_vec_type(gallivm
, type_wide
);
1184 for (i
= 0; i
< bld
->type
.length
; i
+= 2) {
1185 shuf
[i
] = lp_build_const_int32(gallivm
, i
+1);
1186 shuf
[i
+1] = LLVMGetUndef(LLVMInt32TypeInContext(gallivm
->context
));
1188 shuf_vec
= LLVMConstVector(shuf
, bld
->type
.length
);
1191 aodd
= LLVMBuildShuffleVector(builder
, aeven
, bld
->undef
, shuf_vec
, "");
1192 bodd
= LLVMBuildShuffleVector(builder
, beven
, bld
->undef
, shuf_vec
, "");
1194 if (util_cpu_caps
.has_avx2
&& bld
->type
.length
== 8) {
1195 if (bld
->type
.sign
) {
1196 intrinsic
= "llvm.x86.avx2.pmul.dq";
1198 intrinsic
= "llvm.x86.avx2.pmulu.dq";
1200 muleven
= lp_build_intrinsic_binary(builder
, intrinsic
,
1201 wider_type
, aeven
, beven
);
1202 mulodd
= lp_build_intrinsic_binary(builder
, intrinsic
,
1203 wider_type
, aodd
, bodd
);
1206 /* for consistent naming look elsewhere... */
1207 if (bld
->type
.sign
) {
1208 intrinsic
= "llvm.x86.sse41.pmuldq";
1210 intrinsic
= "llvm.x86.sse2.pmulu.dq";
1213 * XXX If we only have AVX but not AVX2 this is a pain.
1214 * lp_build_intrinsic_binary_anylength() can't handle it
1215 * (due to src and dst type not being identical).
1217 if (bld
->type
.length
== 8) {
1218 LLVMValueRef aevenlo
, aevenhi
, bevenlo
, bevenhi
;
1219 LLVMValueRef aoddlo
, aoddhi
, boddlo
, boddhi
;
1220 LLVMValueRef muleven2
[2], mulodd2
[2];
1221 struct lp_type type_wide_half
= type_wide
;
1222 LLVMTypeRef wtype_half
;
1223 type_wide_half
.length
= 2;
1224 wtype_half
= lp_build_vec_type(gallivm
, type_wide_half
);
1225 aevenlo
= lp_build_extract_range(gallivm
, aeven
, 0, 4);
1226 aevenhi
= lp_build_extract_range(gallivm
, aeven
, 4, 4);
1227 bevenlo
= lp_build_extract_range(gallivm
, beven
, 0, 4);
1228 bevenhi
= lp_build_extract_range(gallivm
, beven
, 4, 4);
1229 aoddlo
= lp_build_extract_range(gallivm
, aodd
, 0, 4);
1230 aoddhi
= lp_build_extract_range(gallivm
, aodd
, 4, 4);
1231 boddlo
= lp_build_extract_range(gallivm
, bodd
, 0, 4);
1232 boddhi
= lp_build_extract_range(gallivm
, bodd
, 4, 4);
1233 muleven2
[0] = lp_build_intrinsic_binary(builder
, intrinsic
,
1234 wtype_half
, aevenlo
, bevenlo
);
1235 mulodd2
[0] = lp_build_intrinsic_binary(builder
, intrinsic
,
1236 wtype_half
, aoddlo
, boddlo
);
1237 muleven2
[1] = lp_build_intrinsic_binary(builder
, intrinsic
,
1238 wtype_half
, aevenhi
, bevenhi
);
1239 mulodd2
[1] = lp_build_intrinsic_binary(builder
, intrinsic
,
1240 wtype_half
, aoddhi
, boddhi
);
1241 muleven
= lp_build_concat(gallivm
, muleven2
, type_wide_half
, 2);
1242 mulodd
= lp_build_concat(gallivm
, mulodd2
, type_wide_half
, 2);
1246 muleven
= lp_build_intrinsic_binary(builder
, intrinsic
,
1247 wider_type
, aeven
, beven
);
1248 mulodd
= lp_build_intrinsic_binary(builder
, intrinsic
,
1249 wider_type
, aodd
, bodd
);
1252 muleven
= LLVMBuildBitCast(builder
, muleven
, bld
->vec_type
, "");
1253 mulodd
= LLVMBuildBitCast(builder
, mulodd
, bld
->vec_type
, "");
1255 for (i
= 0; i
< bld
->type
.length
; i
+= 2) {
1256 shuf
[i
] = lp_build_const_int32(gallivm
, i
+ 1);
1257 shuf
[i
+1] = lp_build_const_int32(gallivm
, i
+ 1 + bld
->type
.length
);
1259 shuf_vec
= LLVMConstVector(shuf
, bld
->type
.length
);
1260 *res_hi
= LLVMBuildShuffleVector(builder
, muleven
, mulodd
, shuf_vec
, "");
1262 for (i
= 0; i
< bld
->type
.length
; i
+= 2) {
1263 shuf
[i
] = lp_build_const_int32(gallivm
, i
);
1264 shuf
[i
+1] = lp_build_const_int32(gallivm
, i
+ bld
->type
.length
);
1266 shuf_vec
= LLVMConstVector(shuf
, bld
->type
.length
);
1267 return LLVMBuildShuffleVector(builder
, muleven
, mulodd
, shuf_vec
, "");
1270 return lp_build_mul_32_lohi(bld
, a
, b
, res_hi
);
1276 * Widening mul, valid for 32x32 bit -> 64bit only.
1277 * Result is low 32bits, high bits returned in res_hi.
1279 * Emits generic code.
1282 lp_build_mul_32_lohi(struct lp_build_context
*bld
,
1285 LLVMValueRef
*res_hi
)
1287 struct gallivm_state
*gallivm
= bld
->gallivm
;
1288 LLVMBuilderRef builder
= gallivm
->builder
;
1289 LLVMValueRef tmp
, shift
, res_lo
;
1290 struct lp_type type_tmp
;
1291 LLVMTypeRef wide_type
, narrow_type
;
1293 type_tmp
= bld
->type
;
1294 narrow_type
= lp_build_vec_type(gallivm
, type_tmp
);
1295 type_tmp
.width
*= 2;
1296 wide_type
= lp_build_vec_type(gallivm
, type_tmp
);
1297 shift
= lp_build_const_vec(gallivm
, type_tmp
, 32);
1299 if (bld
->type
.sign
) {
1300 a
= LLVMBuildSExt(builder
, a
, wide_type
, "");
1301 b
= LLVMBuildSExt(builder
, b
, wide_type
, "");
1303 a
= LLVMBuildZExt(builder
, a
, wide_type
, "");
1304 b
= LLVMBuildZExt(builder
, b
, wide_type
, "");
1306 tmp
= LLVMBuildMul(builder
, a
, b
, "");
1308 res_lo
= LLVMBuildTrunc(builder
, tmp
, narrow_type
, "");
1310 /* Since we truncate anyway, LShr and AShr are equivalent. */
1311 tmp
= LLVMBuildLShr(builder
, tmp
, shift
, "");
1312 *res_hi
= LLVMBuildTrunc(builder
, tmp
, narrow_type
, "");
1320 lp_build_mad(struct lp_build_context
*bld
,
1325 const struct lp_type type
= bld
->type
;
1326 if (type
.floating
) {
1327 return lp_build_fmuladd(bld
->gallivm
->builder
, a
, b
, c
);
1329 return lp_build_add(bld
, lp_build_mul(bld
, a
, b
), c
);
1335 * Small vector x scale multiplication optimization.
1338 lp_build_mul_imm(struct lp_build_context
*bld
,
1342 LLVMBuilderRef builder
= bld
->gallivm
->builder
;
1343 LLVMValueRef factor
;
1345 assert(lp_check_value(bld
->type
, a
));
1354 return lp_build_negate(bld
, a
);
1356 if(b
== 2 && bld
->type
.floating
)
1357 return lp_build_add(bld
, a
, a
);
1359 if(util_is_power_of_two_or_zero(b
)) {
1360 unsigned shift
= ffs(b
) - 1;
1362 if(bld
->type
.floating
) {
1365 * Power of two multiplication by directly manipulating the exponent.
1367 * XXX: This might not be always faster, it will introduce a small error
1368 * for multiplication by zero, and it will produce wrong results
1371 unsigned mantissa
= lp_mantissa(bld
->type
);
1372 factor
= lp_build_const_int_vec(bld
->gallivm
, bld
->type
, (unsigned long long)shift
<< mantissa
);
1373 a
= LLVMBuildBitCast(builder
, a
, lp_build_int_vec_type(bld
->type
), "");
1374 a
= LLVMBuildAdd(builder
, a
, factor
, "");
1375 a
= LLVMBuildBitCast(builder
, a
, lp_build_vec_type(bld
->gallivm
, bld
->type
), "");
1380 factor
= lp_build_const_vec(bld
->gallivm
, bld
->type
, shift
);
1381 return LLVMBuildShl(builder
, a
, factor
, "");
1385 factor
= lp_build_const_vec(bld
->gallivm
, bld
->type
, (double)b
);
1386 return lp_build_mul(bld
, a
, factor
);
1394 lp_build_div(struct lp_build_context
*bld
,
1398 LLVMBuilderRef builder
= bld
->gallivm
->builder
;
1399 const struct lp_type type
= bld
->type
;
1401 assert(lp_check_value(type
, a
));
1402 assert(lp_check_value(type
, b
));
1406 if(a
== bld
->one
&& type
.floating
)
1407 return lp_build_rcp(bld
, b
);
1412 if(a
== bld
->undef
|| b
== bld
->undef
)
1415 if(LLVMIsConstant(a
) && LLVMIsConstant(b
)) {
1417 return LLVMConstFDiv(a
, b
);
1419 return LLVMConstSDiv(a
, b
);
1421 return LLVMConstUDiv(a
, b
);
1424 /* fast rcp is disabled (just uses div), so makes no sense to try that */
1426 ((util_cpu_caps
.has_sse
&& type
.width
== 32 && type
.length
== 4) ||
1427 (util_cpu_caps
.has_avx
&& type
.width
== 32 && type
.length
== 8)) &&
1429 return lp_build_mul(bld
, a
, lp_build_rcp(bld
, b
));
1432 return LLVMBuildFDiv(builder
, a
, b
, "");
1434 return LLVMBuildSDiv(builder
, a
, b
, "");
1436 return LLVMBuildUDiv(builder
, a
, b
, "");
1441 * Linear interpolation helper.
1443 * @param normalized whether we are interpolating normalized values,
1444 * encoded in normalized integers, twice as wide.
1446 * @sa http://www.stereopsis.com/doubleblend.html
1448 static inline LLVMValueRef
1449 lp_build_lerp_simple(struct lp_build_context
*bld
,
1455 unsigned half_width
= bld
->type
.width
/2;
1456 LLVMBuilderRef builder
= bld
->gallivm
->builder
;
1460 assert(lp_check_value(bld
->type
, x
));
1461 assert(lp_check_value(bld
->type
, v0
));
1462 assert(lp_check_value(bld
->type
, v1
));
1464 delta
= lp_build_sub(bld
, v1
, v0
);
1466 if (bld
->type
.floating
) {
1468 return lp_build_mad(bld
, x
, delta
, v0
);
1471 if (flags
& LP_BLD_LERP_WIDE_NORMALIZED
) {
1472 if (!bld
->type
.sign
) {
1473 if (!(flags
& LP_BLD_LERP_PRESCALED_WEIGHTS
)) {
1475 * Scale x from [0, 2**n - 1] to [0, 2**n] by adding the
1476 * most-significant-bit to the lowest-significant-bit, so that
1477 * later we can just divide by 2**n instead of 2**n - 1.
1480 x
= lp_build_add(bld
, x
, lp_build_shr_imm(bld
, x
, half_width
- 1));
1483 /* (x * delta) >> n */
1484 res
= lp_build_mul(bld
, x
, delta
);
1485 res
= lp_build_shr_imm(bld
, res
, half_width
);
1488 * The rescaling trick above doesn't work for signed numbers, so
1489 * use the 2**n - 1 divison approximation in lp_build_mul_norm
1492 assert(!(flags
& LP_BLD_LERP_PRESCALED_WEIGHTS
));
1493 res
= lp_build_mul_norm(bld
->gallivm
, bld
->type
, x
, delta
);
1496 assert(!(flags
& LP_BLD_LERP_PRESCALED_WEIGHTS
));
1497 res
= lp_build_mul(bld
, x
, delta
);
1500 if ((flags
& LP_BLD_LERP_WIDE_NORMALIZED
) && !bld
->type
.sign
) {
1502 * At this point both res and v0 only use the lower half of the bits,
1503 * the rest is zero. Instead of add / mask, do add with half wide type.
1505 struct lp_type narrow_type
;
1506 struct lp_build_context narrow_bld
;
1508 memset(&narrow_type
, 0, sizeof narrow_type
);
1509 narrow_type
.sign
= bld
->type
.sign
;
1510 narrow_type
.width
= bld
->type
.width
/2;
1511 narrow_type
.length
= bld
->type
.length
*2;
1513 lp_build_context_init(&narrow_bld
, bld
->gallivm
, narrow_type
);
1514 res
= LLVMBuildBitCast(builder
, res
, narrow_bld
.vec_type
, "");
1515 v0
= LLVMBuildBitCast(builder
, v0
, narrow_bld
.vec_type
, "");
1516 res
= lp_build_add(&narrow_bld
, v0
, res
);
1517 res
= LLVMBuildBitCast(builder
, res
, bld
->vec_type
, "");
1519 res
= lp_build_add(bld
, v0
, res
);
1521 if (bld
->type
.fixed
) {
1523 * We need to mask out the high order bits when lerping 8bit
1524 * normalized colors stored on 16bits
1526 /* XXX: This step is necessary for lerping 8bit colors stored on
1527 * 16bits, but it will be wrong for true fixed point use cases.
1528 * Basically we need a more powerful lp_type, capable of further
1529 * distinguishing the values interpretation from the value storage.
1531 LLVMValueRef low_bits
;
1532 low_bits
= lp_build_const_int_vec(bld
->gallivm
, bld
->type
, (1 << half_width
) - 1);
1533 res
= LLVMBuildAnd(builder
, res
, low_bits
, "");
1542 * Linear interpolation.
1545 lp_build_lerp(struct lp_build_context
*bld
,
1551 const struct lp_type type
= bld
->type
;
1554 assert(lp_check_value(type
, x
));
1555 assert(lp_check_value(type
, v0
));
1556 assert(lp_check_value(type
, v1
));
1558 assert(!(flags
& LP_BLD_LERP_WIDE_NORMALIZED
));
1561 struct lp_type wide_type
;
1562 struct lp_build_context wide_bld
;
1563 LLVMValueRef xl
, xh
, v0l
, v0h
, v1l
, v1h
, resl
, resh
;
1565 assert(type
.length
>= 2);
1568 * Create a wider integer type, enough to hold the
1569 * intermediate result of the multiplication.
1571 memset(&wide_type
, 0, sizeof wide_type
);
1572 wide_type
.sign
= type
.sign
;
1573 wide_type
.width
= type
.width
*2;
1574 wide_type
.length
= type
.length
/2;
1576 lp_build_context_init(&wide_bld
, bld
->gallivm
, wide_type
);
1578 lp_build_unpack2_native(bld
->gallivm
, type
, wide_type
, x
, &xl
, &xh
);
1579 lp_build_unpack2_native(bld
->gallivm
, type
, wide_type
, v0
, &v0l
, &v0h
);
1580 lp_build_unpack2_native(bld
->gallivm
, type
, wide_type
, v1
, &v1l
, &v1h
);
1586 flags
|= LP_BLD_LERP_WIDE_NORMALIZED
;
1588 resl
= lp_build_lerp_simple(&wide_bld
, xl
, v0l
, v1l
, flags
);
1589 resh
= lp_build_lerp_simple(&wide_bld
, xh
, v0h
, v1h
, flags
);
1591 res
= lp_build_pack2_native(bld
->gallivm
, wide_type
, type
, resl
, resh
);
1593 res
= lp_build_lerp_simple(bld
, x
, v0
, v1
, flags
);
1601 * Bilinear interpolation.
1603 * Values indices are in v_{yx}.
1606 lp_build_lerp_2d(struct lp_build_context
*bld
,
1615 LLVMValueRef v0
= lp_build_lerp(bld
, x
, v00
, v01
, flags
);
1616 LLVMValueRef v1
= lp_build_lerp(bld
, x
, v10
, v11
, flags
);
1617 return lp_build_lerp(bld
, y
, v0
, v1
, flags
);
1622 lp_build_lerp_3d(struct lp_build_context
*bld
,
1636 LLVMValueRef v0
= lp_build_lerp_2d(bld
, x
, y
, v000
, v001
, v010
, v011
, flags
);
1637 LLVMValueRef v1
= lp_build_lerp_2d(bld
, x
, y
, v100
, v101
, v110
, v111
, flags
);
1638 return lp_build_lerp(bld
, z
, v0
, v1
, flags
);
1643 * Generate min(a, b)
1644 * Do checks for special cases but not for nans.
1647 lp_build_min(struct lp_build_context
*bld
,
1651 assert(lp_check_value(bld
->type
, a
));
1652 assert(lp_check_value(bld
->type
, b
));
1654 if(a
== bld
->undef
|| b
== bld
->undef
)
1660 if (bld
->type
.norm
) {
1661 if (!bld
->type
.sign
) {
1662 if (a
== bld
->zero
|| b
== bld
->zero
) {
1672 return lp_build_min_simple(bld
, a
, b
, GALLIVM_NAN_BEHAVIOR_UNDEFINED
);
1677 * Generate min(a, b)
1678 * NaN's are handled according to the behavior specified by the
1679 * nan_behavior argument.
1682 lp_build_min_ext(struct lp_build_context
*bld
,
1685 enum gallivm_nan_behavior nan_behavior
)
1687 assert(lp_check_value(bld
->type
, a
));
1688 assert(lp_check_value(bld
->type
, b
));
1690 if(a
== bld
->undef
|| b
== bld
->undef
)
1696 if (bld
->type
.norm
) {
1697 if (!bld
->type
.sign
) {
1698 if (a
== bld
->zero
|| b
== bld
->zero
) {
1708 return lp_build_min_simple(bld
, a
, b
, nan_behavior
);
1712 * Generate max(a, b)
1713 * Do checks for special cases, but NaN behavior is undefined.
1716 lp_build_max(struct lp_build_context
*bld
,
1720 assert(lp_check_value(bld
->type
, a
));
1721 assert(lp_check_value(bld
->type
, b
));
1723 if(a
== bld
->undef
|| b
== bld
->undef
)
1729 if(bld
->type
.norm
) {
1730 if(a
== bld
->one
|| b
== bld
->one
)
1732 if (!bld
->type
.sign
) {
1733 if (a
== bld
->zero
) {
1736 if (b
== bld
->zero
) {
1742 return lp_build_max_simple(bld
, a
, b
, GALLIVM_NAN_BEHAVIOR_UNDEFINED
);
1747 * Generate max(a, b)
1748 * Checks for special cases.
1749 * NaN's are handled according to the behavior specified by the
1750 * nan_behavior argument.
1753 lp_build_max_ext(struct lp_build_context
*bld
,
1756 enum gallivm_nan_behavior nan_behavior
)
1758 assert(lp_check_value(bld
->type
, a
));
1759 assert(lp_check_value(bld
->type
, b
));
1761 if(a
== bld
->undef
|| b
== bld
->undef
)
1767 if(bld
->type
.norm
) {
1768 if(a
== bld
->one
|| b
== bld
->one
)
1770 if (!bld
->type
.sign
) {
1771 if (a
== bld
->zero
) {
1774 if (b
== bld
->zero
) {
1780 return lp_build_max_simple(bld
, a
, b
, nan_behavior
);
1784 * Generate clamp(a, min, max)
1785 * NaN behavior (for any of a, min, max) is undefined.
1786 * Do checks for special cases.
1789 lp_build_clamp(struct lp_build_context
*bld
,
1794 assert(lp_check_value(bld
->type
, a
));
1795 assert(lp_check_value(bld
->type
, min
));
1796 assert(lp_check_value(bld
->type
, max
));
1798 a
= lp_build_min(bld
, a
, max
);
1799 a
= lp_build_max(bld
, a
, min
);
1805 * Generate clamp(a, 0, 1)
1806 * A NaN will get converted to zero.
1809 lp_build_clamp_zero_one_nanzero(struct lp_build_context
*bld
,
1812 a
= lp_build_max_ext(bld
, a
, bld
->zero
, GALLIVM_NAN_RETURN_OTHER_SECOND_NONNAN
);
1813 a
= lp_build_min(bld
, a
, bld
->one
);
1822 lp_build_abs(struct lp_build_context
*bld
,
1825 LLVMBuilderRef builder
= bld
->gallivm
->builder
;
1826 const struct lp_type type
= bld
->type
;
1827 LLVMTypeRef vec_type
= lp_build_vec_type(bld
->gallivm
, type
);
1829 assert(lp_check_value(type
, a
));
1835 if (LLVM_VERSION_MAJOR
== 3 && LLVM_VERSION_MINOR
< 9) {
1836 /* Workaround llvm.org/PR27332 */
1837 LLVMTypeRef int_vec_type
= lp_build_int_vec_type(bld
->gallivm
, type
);
1838 unsigned long long absMask
= ~(1ULL << (type
.width
- 1));
1839 LLVMValueRef mask
= lp_build_const_int_vec(bld
->gallivm
, type
, ((unsigned long long) absMask
));
1840 a
= LLVMBuildBitCast(builder
, a
, int_vec_type
, "");
1841 a
= LLVMBuildAnd(builder
, a
, mask
, "");
1842 a
= LLVMBuildBitCast(builder
, a
, vec_type
, "");
1846 lp_format_intrinsic(intrinsic
, sizeof intrinsic
, "llvm.fabs", vec_type
);
1847 return lp_build_intrinsic_unary(builder
, intrinsic
, vec_type
, a
);
1851 if(type
.width
*type
.length
== 128 && util_cpu_caps
.has_ssse3
&& LLVM_VERSION_MAJOR
< 6) {
1852 switch(type
.width
) {
1854 return lp_build_intrinsic_unary(builder
, "llvm.x86.ssse3.pabs.b.128", vec_type
, a
);
1856 return lp_build_intrinsic_unary(builder
, "llvm.x86.ssse3.pabs.w.128", vec_type
, a
);
1858 return lp_build_intrinsic_unary(builder
, "llvm.x86.ssse3.pabs.d.128", vec_type
, a
);
1861 else if (type
.width
*type
.length
== 256 && util_cpu_caps
.has_avx2
&& LLVM_VERSION_MAJOR
< 6) {
1862 switch(type
.width
) {
1864 return lp_build_intrinsic_unary(builder
, "llvm.x86.avx2.pabs.b", vec_type
, a
);
1866 return lp_build_intrinsic_unary(builder
, "llvm.x86.avx2.pabs.w", vec_type
, a
);
1868 return lp_build_intrinsic_unary(builder
, "llvm.x86.avx2.pabs.d", vec_type
, a
);
1872 return lp_build_select(bld
, lp_build_cmp(bld
, PIPE_FUNC_GREATER
, a
, bld
->zero
),
1873 a
, LLVMBuildNeg(builder
, a
, ""));
1878 lp_build_negate(struct lp_build_context
*bld
,
1881 LLVMBuilderRef builder
= bld
->gallivm
->builder
;
1883 assert(lp_check_value(bld
->type
, a
));
1885 if (bld
->type
.floating
)
1886 a
= LLVMBuildFNeg(builder
, a
, "");
1888 a
= LLVMBuildNeg(builder
, a
, "");
1894 /** Return -1, 0 or +1 depending on the sign of a */
1896 lp_build_sgn(struct lp_build_context
*bld
,
1899 LLVMBuilderRef builder
= bld
->gallivm
->builder
;
1900 const struct lp_type type
= bld
->type
;
1904 assert(lp_check_value(type
, a
));
1906 /* Handle non-zero case */
1908 /* if not zero then sign must be positive */
1911 else if(type
.floating
) {
1912 LLVMTypeRef vec_type
;
1913 LLVMTypeRef int_type
;
1917 unsigned long long maskBit
= (unsigned long long)1 << (type
.width
- 1);
1919 int_type
= lp_build_int_vec_type(bld
->gallivm
, type
);
1920 vec_type
= lp_build_vec_type(bld
->gallivm
, type
);
1921 mask
= lp_build_const_int_vec(bld
->gallivm
, type
, maskBit
);
1923 /* Take the sign bit and add it to 1 constant */
1924 sign
= LLVMBuildBitCast(builder
, a
, int_type
, "");
1925 sign
= LLVMBuildAnd(builder
, sign
, mask
, "");
1926 one
= LLVMConstBitCast(bld
->one
, int_type
);
1927 res
= LLVMBuildOr(builder
, sign
, one
, "");
1928 res
= LLVMBuildBitCast(builder
, res
, vec_type
, "");
1932 /* signed int/norm/fixed point */
1933 /* could use psign with sse3 and appropriate vectors here */
1934 LLVMValueRef minus_one
= lp_build_const_vec(bld
->gallivm
, type
, -1.0);
1935 cond
= lp_build_cmp(bld
, PIPE_FUNC_GREATER
, a
, bld
->zero
);
1936 res
= lp_build_select(bld
, cond
, bld
->one
, minus_one
);
1940 cond
= lp_build_cmp(bld
, PIPE_FUNC_EQUAL
, a
, bld
->zero
);
1941 res
= lp_build_select(bld
, cond
, bld
->zero
, res
);
1948 * Set the sign of float vector 'a' according to 'sign'.
1949 * If sign==0, return abs(a).
1950 * If sign==1, return -abs(a);
1951 * Other values for sign produce undefined results.
1954 lp_build_set_sign(struct lp_build_context
*bld
,
1955 LLVMValueRef a
, LLVMValueRef sign
)
1957 LLVMBuilderRef builder
= bld
->gallivm
->builder
;
1958 const struct lp_type type
= bld
->type
;
1959 LLVMTypeRef int_vec_type
= lp_build_int_vec_type(bld
->gallivm
, type
);
1960 LLVMTypeRef vec_type
= lp_build_vec_type(bld
->gallivm
, type
);
1961 LLVMValueRef shift
= lp_build_const_int_vec(bld
->gallivm
, type
, type
.width
- 1);
1962 LLVMValueRef mask
= lp_build_const_int_vec(bld
->gallivm
, type
,
1963 ~((unsigned long long) 1 << (type
.width
- 1)));
1964 LLVMValueRef val
, res
;
1966 assert(type
.floating
);
1967 assert(lp_check_value(type
, a
));
1969 /* val = reinterpret_cast<int>(a) */
1970 val
= LLVMBuildBitCast(builder
, a
, int_vec_type
, "");
1971 /* val = val & mask */
1972 val
= LLVMBuildAnd(builder
, val
, mask
, "");
1973 /* sign = sign << shift */
1974 sign
= LLVMBuildShl(builder
, sign
, shift
, "");
1975 /* res = val | sign */
1976 res
= LLVMBuildOr(builder
, val
, sign
, "");
1977 /* res = reinterpret_cast<float>(res) */
1978 res
= LLVMBuildBitCast(builder
, res
, vec_type
, "");
1985 * Convert vector of (or scalar) int to vector of (or scalar) float.
1988 lp_build_int_to_float(struct lp_build_context
*bld
,
1991 LLVMBuilderRef builder
= bld
->gallivm
->builder
;
1992 const struct lp_type type
= bld
->type
;
1993 LLVMTypeRef vec_type
= lp_build_vec_type(bld
->gallivm
, type
);
1995 assert(type
.floating
);
1997 return LLVMBuildSIToFP(builder
, a
, vec_type
, "");
2001 arch_rounding_available(const struct lp_type type
)
2003 if ((util_cpu_caps
.has_sse4_1
&&
2004 (type
.length
== 1 || type
.width
*type
.length
== 128)) ||
2005 (util_cpu_caps
.has_avx
&& type
.width
*type
.length
== 256) ||
2006 (util_cpu_caps
.has_avx512f
&& type
.width
*type
.length
== 512))
2008 else if ((util_cpu_caps
.has_altivec
&&
2009 (type
.width
== 32 && type
.length
== 4)))
2011 else if (util_cpu_caps
.has_neon
)
2017 enum lp_build_round_mode
2019 LP_BUILD_ROUND_NEAREST
= 0,
2020 LP_BUILD_ROUND_FLOOR
= 1,
2021 LP_BUILD_ROUND_CEIL
= 2,
2022 LP_BUILD_ROUND_TRUNCATE
= 3
2025 static inline LLVMValueRef
2026 lp_build_iround_nearest_sse2(struct lp_build_context
*bld
,
2029 LLVMBuilderRef builder
= bld
->gallivm
->builder
;
2030 const struct lp_type type
= bld
->type
;
2031 LLVMTypeRef i32t
= LLVMInt32TypeInContext(bld
->gallivm
->context
);
2032 LLVMTypeRef ret_type
= lp_build_int_vec_type(bld
->gallivm
, type
);
2033 const char *intrinsic
;
2036 assert(type
.floating
);
2037 /* using the double precision conversions is a bit more complicated */
2038 assert(type
.width
== 32);
2040 assert(lp_check_value(type
, a
));
2041 assert(util_cpu_caps
.has_sse2
);
2043 /* This is relying on MXCSR rounding mode, which should always be nearest. */
2044 if (type
.length
== 1) {
2045 LLVMTypeRef vec_type
;
2048 LLVMValueRef index0
= LLVMConstInt(i32t
, 0, 0);
2050 vec_type
= LLVMVectorType(bld
->elem_type
, 4);
2052 intrinsic
= "llvm.x86.sse.cvtss2si";
2054 undef
= LLVMGetUndef(vec_type
);
2056 arg
= LLVMBuildInsertElement(builder
, undef
, a
, index0
, "");
2058 res
= lp_build_intrinsic_unary(builder
, intrinsic
,
2062 if (type
.width
* type
.length
== 128) {
2063 intrinsic
= "llvm.x86.sse2.cvtps2dq";
2066 assert(type
.width
*type
.length
== 256);
2067 assert(util_cpu_caps
.has_avx
);
2069 intrinsic
= "llvm.x86.avx.cvt.ps2dq.256";
2071 res
= lp_build_intrinsic_unary(builder
, intrinsic
,
2081 static inline LLVMValueRef
2082 lp_build_round_altivec(struct lp_build_context
*bld
,
2084 enum lp_build_round_mode mode
)
2086 LLVMBuilderRef builder
= bld
->gallivm
->builder
;
2087 const struct lp_type type
= bld
->type
;
2088 const char *intrinsic
= NULL
;
2090 assert(type
.floating
);
2092 assert(lp_check_value(type
, a
));
2093 assert(util_cpu_caps
.has_altivec
);
2098 case LP_BUILD_ROUND_NEAREST
:
2099 intrinsic
= "llvm.ppc.altivec.vrfin";
2101 case LP_BUILD_ROUND_FLOOR
:
2102 intrinsic
= "llvm.ppc.altivec.vrfim";
2104 case LP_BUILD_ROUND_CEIL
:
2105 intrinsic
= "llvm.ppc.altivec.vrfip";
2107 case LP_BUILD_ROUND_TRUNCATE
:
2108 intrinsic
= "llvm.ppc.altivec.vrfiz";
2112 return lp_build_intrinsic_unary(builder
, intrinsic
, bld
->vec_type
, a
);
2115 static inline LLVMValueRef
2116 lp_build_round_arch(struct lp_build_context
*bld
,
2118 enum lp_build_round_mode mode
)
2120 if (util_cpu_caps
.has_sse4_1
|| util_cpu_caps
.has_neon
) {
2121 LLVMBuilderRef builder
= bld
->gallivm
->builder
;
2122 const struct lp_type type
= bld
->type
;
2123 const char *intrinsic_root
;
2126 assert(type
.floating
);
2127 assert(lp_check_value(type
, a
));
2131 case LP_BUILD_ROUND_NEAREST
:
2132 intrinsic_root
= "llvm.nearbyint";
2134 case LP_BUILD_ROUND_FLOOR
:
2135 intrinsic_root
= "llvm.floor";
2137 case LP_BUILD_ROUND_CEIL
:
2138 intrinsic_root
= "llvm.ceil";
2140 case LP_BUILD_ROUND_TRUNCATE
:
2141 intrinsic_root
= "llvm.trunc";
2145 lp_format_intrinsic(intrinsic
, sizeof intrinsic
, intrinsic_root
, bld
->vec_type
);
2146 return lp_build_intrinsic_unary(builder
, intrinsic
, bld
->vec_type
, a
);
2148 else /* (util_cpu_caps.has_altivec) */
2149 return lp_build_round_altivec(bld
, a
, mode
);
2153 * Return the integer part of a float (vector) value (== round toward zero).
2154 * The returned value is a float (vector).
2155 * Ex: trunc(-1.5) = -1.0
2158 lp_build_trunc(struct lp_build_context
*bld
,
2161 LLVMBuilderRef builder
= bld
->gallivm
->builder
;
2162 const struct lp_type type
= bld
->type
;
2164 assert(type
.floating
);
2165 assert(lp_check_value(type
, a
));
2167 if (arch_rounding_available(type
)) {
2168 return lp_build_round_arch(bld
, a
, LP_BUILD_ROUND_TRUNCATE
);
2171 const struct lp_type type
= bld
->type
;
2172 struct lp_type inttype
;
2173 struct lp_build_context intbld
;
2174 LLVMValueRef cmpval
= lp_build_const_vec(bld
->gallivm
, type
, 1<<24);
2175 LLVMValueRef trunc
, res
, anosign
, mask
;
2176 LLVMTypeRef int_vec_type
= bld
->int_vec_type
;
2177 LLVMTypeRef vec_type
= bld
->vec_type
;
2179 assert(type
.width
== 32); /* might want to handle doubles at some point */
2182 inttype
.floating
= 0;
2183 lp_build_context_init(&intbld
, bld
->gallivm
, inttype
);
2185 /* round by truncation */
2186 trunc
= LLVMBuildFPToSI(builder
, a
, int_vec_type
, "");
2187 res
= LLVMBuildSIToFP(builder
, trunc
, vec_type
, "floor.trunc");
2189 /* mask out sign bit */
2190 anosign
= lp_build_abs(bld
, a
);
2192 * mask out all values if anosign > 2^24
2193 * This should work both for large ints (all rounding is no-op for them
2194 * because such floats are always exact) as well as special cases like
2195 * NaNs, Infs (taking advantage of the fact they use max exponent).
2196 * (2^24 is arbitrary anything between 2^24 and 2^31 should work.)
2198 anosign
= LLVMBuildBitCast(builder
, anosign
, int_vec_type
, "");
2199 cmpval
= LLVMBuildBitCast(builder
, cmpval
, int_vec_type
, "");
2200 mask
= lp_build_cmp(&intbld
, PIPE_FUNC_GREATER
, anosign
, cmpval
);
2201 return lp_build_select(bld
, mask
, a
, res
);
2207 * Return float (vector) rounded to nearest integer (vector). The returned
2208 * value is a float (vector).
2209 * Ex: round(0.9) = 1.0
2210 * Ex: round(-1.5) = -2.0
2213 lp_build_round(struct lp_build_context
*bld
,
2216 LLVMBuilderRef builder
= bld
->gallivm
->builder
;
2217 const struct lp_type type
= bld
->type
;
2219 assert(type
.floating
);
2220 assert(lp_check_value(type
, a
));
2222 if (arch_rounding_available(type
)) {
2223 return lp_build_round_arch(bld
, a
, LP_BUILD_ROUND_NEAREST
);
2226 const struct lp_type type
= bld
->type
;
2227 struct lp_type inttype
;
2228 struct lp_build_context intbld
;
2229 LLVMValueRef cmpval
= lp_build_const_vec(bld
->gallivm
, type
, 1<<24);
2230 LLVMValueRef res
, anosign
, mask
;
2231 LLVMTypeRef int_vec_type
= bld
->int_vec_type
;
2232 LLVMTypeRef vec_type
= bld
->vec_type
;
2234 assert(type
.width
== 32); /* might want to handle doubles at some point */
2237 inttype
.floating
= 0;
2238 lp_build_context_init(&intbld
, bld
->gallivm
, inttype
);
2240 res
= lp_build_iround(bld
, a
);
2241 res
= LLVMBuildSIToFP(builder
, res
, vec_type
, "");
2243 /* mask out sign bit */
2244 anosign
= lp_build_abs(bld
, a
);
2246 * mask out all values if anosign > 2^24
2247 * This should work both for large ints (all rounding is no-op for them
2248 * because such floats are always exact) as well as special cases like
2249 * NaNs, Infs (taking advantage of the fact they use max exponent).
2250 * (2^24 is arbitrary anything between 2^24 and 2^31 should work.)
2252 anosign
= LLVMBuildBitCast(builder
, anosign
, int_vec_type
, "");
2253 cmpval
= LLVMBuildBitCast(builder
, cmpval
, int_vec_type
, "");
2254 mask
= lp_build_cmp(&intbld
, PIPE_FUNC_GREATER
, anosign
, cmpval
);
2255 return lp_build_select(bld
, mask
, a
, res
);
2261 * Return floor of float (vector), result is a float (vector)
2262 * Ex: floor(1.1) = 1.0
2263 * Ex: floor(-1.1) = -2.0
2266 lp_build_floor(struct lp_build_context
*bld
,
2269 LLVMBuilderRef builder
= bld
->gallivm
->builder
;
2270 const struct lp_type type
= bld
->type
;
2272 assert(type
.floating
);
2273 assert(lp_check_value(type
, a
));
2275 if (arch_rounding_available(type
)) {
2276 return lp_build_round_arch(bld
, a
, LP_BUILD_ROUND_FLOOR
);
2279 const struct lp_type type
= bld
->type
;
2280 struct lp_type inttype
;
2281 struct lp_build_context intbld
;
2282 LLVMValueRef cmpval
= lp_build_const_vec(bld
->gallivm
, type
, 1<<24);
2283 LLVMValueRef trunc
, res
, anosign
, mask
;
2284 LLVMTypeRef int_vec_type
= bld
->int_vec_type
;
2285 LLVMTypeRef vec_type
= bld
->vec_type
;
2287 if (type
.width
!= 32) {
2289 lp_format_intrinsic(intrinsic
, sizeof intrinsic
, "llvm.floor", vec_type
);
2290 return lp_build_intrinsic_unary(builder
, intrinsic
, vec_type
, a
);
2293 assert(type
.width
== 32); /* might want to handle doubles at some point */
2296 inttype
.floating
= 0;
2297 lp_build_context_init(&intbld
, bld
->gallivm
, inttype
);
2299 /* round by truncation */
2300 trunc
= LLVMBuildFPToSI(builder
, a
, int_vec_type
, "");
2301 res
= LLVMBuildSIToFP(builder
, trunc
, vec_type
, "floor.trunc");
2307 * fix values if rounding is wrong (for non-special cases)
2308 * - this is the case if trunc > a
2310 mask
= lp_build_cmp(bld
, PIPE_FUNC_GREATER
, res
, a
);
2311 /* tmp = trunc > a ? 1.0 : 0.0 */
2312 tmp
= LLVMBuildBitCast(builder
, bld
->one
, int_vec_type
, "");
2313 tmp
= lp_build_and(&intbld
, mask
, tmp
);
2314 tmp
= LLVMBuildBitCast(builder
, tmp
, vec_type
, "");
2315 res
= lp_build_sub(bld
, res
, tmp
);
2318 /* mask out sign bit */
2319 anosign
= lp_build_abs(bld
, a
);
2321 * mask out all values if anosign > 2^24
2322 * This should work both for large ints (all rounding is no-op for them
2323 * because such floats are always exact) as well as special cases like
2324 * NaNs, Infs (taking advantage of the fact they use max exponent).
2325 * (2^24 is arbitrary anything between 2^24 and 2^31 should work.)
2327 anosign
= LLVMBuildBitCast(builder
, anosign
, int_vec_type
, "");
2328 cmpval
= LLVMBuildBitCast(builder
, cmpval
, int_vec_type
, "");
2329 mask
= lp_build_cmp(&intbld
, PIPE_FUNC_GREATER
, anosign
, cmpval
);
2330 return lp_build_select(bld
, mask
, a
, res
);
2336 * Return ceiling of float (vector), returning float (vector).
2337 * Ex: ceil( 1.1) = 2.0
2338 * Ex: ceil(-1.1) = -1.0
2341 lp_build_ceil(struct lp_build_context
*bld
,
2344 LLVMBuilderRef builder
= bld
->gallivm
->builder
;
2345 const struct lp_type type
= bld
->type
;
2347 assert(type
.floating
);
2348 assert(lp_check_value(type
, a
));
2350 if (arch_rounding_available(type
)) {
2351 return lp_build_round_arch(bld
, a
, LP_BUILD_ROUND_CEIL
);
2354 const struct lp_type type
= bld
->type
;
2355 struct lp_type inttype
;
2356 struct lp_build_context intbld
;
2357 LLVMValueRef cmpval
= lp_build_const_vec(bld
->gallivm
, type
, 1<<24);
2358 LLVMValueRef trunc
, res
, anosign
, mask
, tmp
;
2359 LLVMTypeRef int_vec_type
= bld
->int_vec_type
;
2360 LLVMTypeRef vec_type
= bld
->vec_type
;
2362 if (type
.width
!= 32) {
2364 lp_format_intrinsic(intrinsic
, sizeof intrinsic
, "llvm.ceil", vec_type
);
2365 return lp_build_intrinsic_unary(builder
, intrinsic
, vec_type
, a
);
2368 assert(type
.width
== 32); /* might want to handle doubles at some point */
2371 inttype
.floating
= 0;
2372 lp_build_context_init(&intbld
, bld
->gallivm
, inttype
);
2374 /* round by truncation */
2375 trunc
= LLVMBuildFPToSI(builder
, a
, int_vec_type
, "");
2376 trunc
= LLVMBuildSIToFP(builder
, trunc
, vec_type
, "ceil.trunc");
2379 * fix values if rounding is wrong (for non-special cases)
2380 * - this is the case if trunc < a
2382 mask
= lp_build_cmp(bld
, PIPE_FUNC_LESS
, trunc
, a
);
2383 /* tmp = trunc < a ? 1.0 : 0.0 */
2384 tmp
= LLVMBuildBitCast(builder
, bld
->one
, int_vec_type
, "");
2385 tmp
= lp_build_and(&intbld
, mask
, tmp
);
2386 tmp
= LLVMBuildBitCast(builder
, tmp
, vec_type
, "");
2387 res
= lp_build_add(bld
, trunc
, tmp
);
2389 /* mask out sign bit */
2390 anosign
= lp_build_abs(bld
, a
);
2392 * mask out all values if anosign > 2^24
2393 * This should work both for large ints (all rounding is no-op for them
2394 * because such floats are always exact) as well as special cases like
2395 * NaNs, Infs (taking advantage of the fact they use max exponent).
2396 * (2^24 is arbitrary anything between 2^24 and 2^31 should work.)
2398 anosign
= LLVMBuildBitCast(builder
, anosign
, int_vec_type
, "");
2399 cmpval
= LLVMBuildBitCast(builder
, cmpval
, int_vec_type
, "");
2400 mask
= lp_build_cmp(&intbld
, PIPE_FUNC_GREATER
, anosign
, cmpval
);
2401 return lp_build_select(bld
, mask
, a
, res
);
2407 * Return fractional part of 'a' computed as a - floor(a)
2408 * Typically used in texture coord arithmetic.
2411 lp_build_fract(struct lp_build_context
*bld
,
2414 assert(bld
->type
.floating
);
2415 return lp_build_sub(bld
, a
, lp_build_floor(bld
, a
));
2420 * Prevent returning 1.0 for very small negative values of 'a' by clamping
2421 * against 0.99999(9). (Will also return that value for NaNs.)
2423 static inline LLVMValueRef
2424 clamp_fract(struct lp_build_context
*bld
, LLVMValueRef fract
)
2428 /* this is the largest number smaller than 1.0 representable as float */
2429 max
= lp_build_const_vec(bld
->gallivm
, bld
->type
,
2430 1.0 - 1.0/(1LL << (lp_mantissa(bld
->type
) + 1)));
2431 return lp_build_min_ext(bld
, fract
, max
,
2432 GALLIVM_NAN_RETURN_OTHER_SECOND_NONNAN
);
2437 * Same as lp_build_fract, but guarantees that the result is always smaller
2438 * than one. Will also return the smaller-than-one value for infs, NaNs.
2441 lp_build_fract_safe(struct lp_build_context
*bld
,
2444 return clamp_fract(bld
, lp_build_fract(bld
, a
));
2449 * Return the integer part of a float (vector) value (== round toward zero).
2450 * The returned value is an integer (vector).
2451 * Ex: itrunc(-1.5) = -1
2454 lp_build_itrunc(struct lp_build_context
*bld
,
2457 LLVMBuilderRef builder
= bld
->gallivm
->builder
;
2458 const struct lp_type type
= bld
->type
;
2459 LLVMTypeRef int_vec_type
= lp_build_int_vec_type(bld
->gallivm
, type
);
2461 assert(type
.floating
);
2462 assert(lp_check_value(type
, a
));
2464 return LLVMBuildFPToSI(builder
, a
, int_vec_type
, "");
2469 * Return float (vector) rounded to nearest integer (vector). The returned
2470 * value is an integer (vector).
2471 * Ex: iround(0.9) = 1
2472 * Ex: iround(-1.5) = -2
2475 lp_build_iround(struct lp_build_context
*bld
,
2478 LLVMBuilderRef builder
= bld
->gallivm
->builder
;
2479 const struct lp_type type
= bld
->type
;
2480 LLVMTypeRef int_vec_type
= bld
->int_vec_type
;
2483 assert(type
.floating
);
2485 assert(lp_check_value(type
, a
));
2487 if ((util_cpu_caps
.has_sse2
&&
2488 ((type
.width
== 32) && (type
.length
== 1 || type
.length
== 4))) ||
2489 (util_cpu_caps
.has_avx
&& type
.width
== 32 && type
.length
== 8)) {
2490 return lp_build_iround_nearest_sse2(bld
, a
);
2492 if (arch_rounding_available(type
)) {
2493 res
= lp_build_round_arch(bld
, a
, LP_BUILD_ROUND_NEAREST
);
2498 half
= lp_build_const_vec(bld
->gallivm
, type
, nextafterf(0.5, 0.0));
2501 LLVMTypeRef vec_type
= bld
->vec_type
;
2502 LLVMValueRef mask
= lp_build_const_int_vec(bld
->gallivm
, type
,
2503 (unsigned long long)1 << (type
.width
- 1));
2507 sign
= LLVMBuildBitCast(builder
, a
, int_vec_type
, "");
2508 sign
= LLVMBuildAnd(builder
, sign
, mask
, "");
2511 half
= LLVMBuildBitCast(builder
, half
, int_vec_type
, "");
2512 half
= LLVMBuildOr(builder
, sign
, half
, "");
2513 half
= LLVMBuildBitCast(builder
, half
, vec_type
, "");
2516 res
= LLVMBuildFAdd(builder
, a
, half
, "");
2519 res
= LLVMBuildFPToSI(builder
, res
, int_vec_type
, "");
2526 * Return floor of float (vector), result is an int (vector)
2527 * Ex: ifloor(1.1) = 1.0
2528 * Ex: ifloor(-1.1) = -2.0
2531 lp_build_ifloor(struct lp_build_context
*bld
,
2534 LLVMBuilderRef builder
= bld
->gallivm
->builder
;
2535 const struct lp_type type
= bld
->type
;
2536 LLVMTypeRef int_vec_type
= bld
->int_vec_type
;
2539 assert(type
.floating
);
2540 assert(lp_check_value(type
, a
));
2544 if (arch_rounding_available(type
)) {
2545 res
= lp_build_round_arch(bld
, a
, LP_BUILD_ROUND_FLOOR
);
2548 struct lp_type inttype
;
2549 struct lp_build_context intbld
;
2550 LLVMValueRef trunc
, itrunc
, mask
;
2552 assert(type
.floating
);
2553 assert(lp_check_value(type
, a
));
2556 inttype
.floating
= 0;
2557 lp_build_context_init(&intbld
, bld
->gallivm
, inttype
);
2559 /* round by truncation */
2560 itrunc
= LLVMBuildFPToSI(builder
, a
, int_vec_type
, "");
2561 trunc
= LLVMBuildSIToFP(builder
, itrunc
, bld
->vec_type
, "ifloor.trunc");
2564 * fix values if rounding is wrong (for non-special cases)
2565 * - this is the case if trunc > a
2566 * The results of doing this with NaNs, very large values etc.
2567 * are undefined but this seems to be the case anyway.
2569 mask
= lp_build_cmp(bld
, PIPE_FUNC_GREATER
, trunc
, a
);
2570 /* cheapie minus one with mask since the mask is minus one / zero */
2571 return lp_build_add(&intbld
, itrunc
, mask
);
2575 /* round to nearest (toward zero) */
2576 res
= LLVMBuildFPToSI(builder
, res
, int_vec_type
, "ifloor.res");
2583 * Return ceiling of float (vector), returning int (vector).
2584 * Ex: iceil( 1.1) = 2
2585 * Ex: iceil(-1.1) = -1
2588 lp_build_iceil(struct lp_build_context
*bld
,
2591 LLVMBuilderRef builder
= bld
->gallivm
->builder
;
2592 const struct lp_type type
= bld
->type
;
2593 LLVMTypeRef int_vec_type
= bld
->int_vec_type
;
2596 assert(type
.floating
);
2597 assert(lp_check_value(type
, a
));
2599 if (arch_rounding_available(type
)) {
2600 res
= lp_build_round_arch(bld
, a
, LP_BUILD_ROUND_CEIL
);
2603 struct lp_type inttype
;
2604 struct lp_build_context intbld
;
2605 LLVMValueRef trunc
, itrunc
, mask
;
2607 assert(type
.floating
);
2608 assert(lp_check_value(type
, a
));
2611 inttype
.floating
= 0;
2612 lp_build_context_init(&intbld
, bld
->gallivm
, inttype
);
2614 /* round by truncation */
2615 itrunc
= LLVMBuildFPToSI(builder
, a
, int_vec_type
, "");
2616 trunc
= LLVMBuildSIToFP(builder
, itrunc
, bld
->vec_type
, "iceil.trunc");
2619 * fix values if rounding is wrong (for non-special cases)
2620 * - this is the case if trunc < a
2621 * The results of doing this with NaNs, very large values etc.
2622 * are undefined but this seems to be the case anyway.
2624 mask
= lp_build_cmp(bld
, PIPE_FUNC_LESS
, trunc
, a
);
2625 /* cheapie plus one with mask since the mask is minus one / zero */
2626 return lp_build_sub(&intbld
, itrunc
, mask
);
2629 /* round to nearest (toward zero) */
2630 res
= LLVMBuildFPToSI(builder
, res
, int_vec_type
, "iceil.res");
2637 * Combined ifloor() & fract().
2639 * Preferred to calling the functions separately, as it will ensure that the
2640 * strategy (floor() vs ifloor()) that results in less redundant work is used.
2643 lp_build_ifloor_fract(struct lp_build_context
*bld
,
2645 LLVMValueRef
*out_ipart
,
2646 LLVMValueRef
*out_fpart
)
2648 LLVMBuilderRef builder
= bld
->gallivm
->builder
;
2649 const struct lp_type type
= bld
->type
;
2652 assert(type
.floating
);
2653 assert(lp_check_value(type
, a
));
2655 if (arch_rounding_available(type
)) {
2657 * floor() is easier.
2660 ipart
= lp_build_floor(bld
, a
);
2661 *out_fpart
= LLVMBuildFSub(builder
, a
, ipart
, "fpart");
2662 *out_ipart
= LLVMBuildFPToSI(builder
, ipart
, bld
->int_vec_type
, "ipart");
2666 * ifloor() is easier.
2669 *out_ipart
= lp_build_ifloor(bld
, a
);
2670 ipart
= LLVMBuildSIToFP(builder
, *out_ipart
, bld
->vec_type
, "ipart");
2671 *out_fpart
= LLVMBuildFSub(builder
, a
, ipart
, "fpart");
2677 * Same as lp_build_ifloor_fract, but guarantees that the fractional part is
2678 * always smaller than one.
2681 lp_build_ifloor_fract_safe(struct lp_build_context
*bld
,
2683 LLVMValueRef
*out_ipart
,
2684 LLVMValueRef
*out_fpart
)
2686 lp_build_ifloor_fract(bld
, a
, out_ipart
, out_fpart
);
2687 *out_fpart
= clamp_fract(bld
, *out_fpart
);
2692 lp_build_sqrt(struct lp_build_context
*bld
,
2695 LLVMBuilderRef builder
= bld
->gallivm
->builder
;
2696 const struct lp_type type
= bld
->type
;
2697 LLVMTypeRef vec_type
= lp_build_vec_type(bld
->gallivm
, type
);
2700 assert(lp_check_value(type
, a
));
2702 assert(type
.floating
);
2703 lp_format_intrinsic(intrinsic
, sizeof intrinsic
, "llvm.sqrt", vec_type
);
2705 return lp_build_intrinsic_unary(builder
, intrinsic
, vec_type
, a
);
2710 * Do one Newton-Raphson step to improve reciprocate precision:
2712 * x_{i+1} = x_i + x_i * (1 - a * x_i)
2714 * XXX: Unfortunately this won't give IEEE-754 conformant results for 0 or
2715 * +/-Inf, giving NaN instead. Certain applications rely on this behavior,
2716 * such as Google Earth, which does RCP(RSQRT(0.0)) when drawing the Earth's
2717 * halo. It would be necessary to clamp the argument to prevent this.
2720 * - http://en.wikipedia.org/wiki/Division_(digital)#Newton.E2.80.93Raphson_division
2721 * - http://softwarecommunity.intel.com/articles/eng/1818.htm
2723 static inline LLVMValueRef
2724 lp_build_rcp_refine(struct lp_build_context
*bld
,
2728 LLVMBuilderRef builder
= bld
->gallivm
->builder
;
2732 neg_a
= LLVMBuildFNeg(builder
, a
, "");
2733 res
= lp_build_fmuladd(builder
, neg_a
, rcp_a
, bld
->one
);
2734 res
= lp_build_fmuladd(builder
, res
, rcp_a
, rcp_a
);
2741 lp_build_rcp(struct lp_build_context
*bld
,
2744 LLVMBuilderRef builder
= bld
->gallivm
->builder
;
2745 const struct lp_type type
= bld
->type
;
2747 assert(lp_check_value(type
, a
));
2756 assert(type
.floating
);
2758 if(LLVMIsConstant(a
))
2759 return LLVMConstFDiv(bld
->one
, a
);
2762 * We don't use RCPPS because:
2763 * - it only has 10bits of precision
2764 * - it doesn't even get the reciprocate of 1.0 exactly
2765 * - doing Newton-Rapshon steps yields wrong (NaN) values for 0.0 or Inf
2766 * - for recent processors the benefit over DIVPS is marginal, a case
2769 * We could still use it on certain processors if benchmarks show that the
2770 * RCPPS plus necessary workarounds are still preferrable to DIVPS; or for
2771 * particular uses that require less workarounds.
2774 if (FALSE
&& ((util_cpu_caps
.has_sse
&& type
.width
== 32 && type
.length
== 4) ||
2775 (util_cpu_caps
.has_avx
&& type
.width
== 32 && type
.length
== 8))){
2776 const unsigned num_iterations
= 0;
2779 const char *intrinsic
= NULL
;
2781 if (type
.length
== 4) {
2782 intrinsic
= "llvm.x86.sse.rcp.ps";
2785 intrinsic
= "llvm.x86.avx.rcp.ps.256";
2788 res
= lp_build_intrinsic_unary(builder
, intrinsic
, bld
->vec_type
, a
);
2790 for (i
= 0; i
< num_iterations
; ++i
) {
2791 res
= lp_build_rcp_refine(bld
, a
, res
);
2797 return LLVMBuildFDiv(builder
, bld
->one
, a
, "");
2802 * Do one Newton-Raphson step to improve rsqrt precision:
2804 * x_{i+1} = 0.5 * x_i * (3.0 - a * x_i * x_i)
2806 * See also Intel 64 and IA-32 Architectures Optimization Manual.
2808 static inline LLVMValueRef
2809 lp_build_rsqrt_refine(struct lp_build_context
*bld
,
2811 LLVMValueRef rsqrt_a
)
2813 LLVMBuilderRef builder
= bld
->gallivm
->builder
;
2814 LLVMValueRef half
= lp_build_const_vec(bld
->gallivm
, bld
->type
, 0.5);
2815 LLVMValueRef three
= lp_build_const_vec(bld
->gallivm
, bld
->type
, 3.0);
2818 res
= LLVMBuildFMul(builder
, rsqrt_a
, rsqrt_a
, "");
2819 res
= LLVMBuildFMul(builder
, a
, res
, "");
2820 res
= LLVMBuildFSub(builder
, three
, res
, "");
2821 res
= LLVMBuildFMul(builder
, rsqrt_a
, res
, "");
2822 res
= LLVMBuildFMul(builder
, half
, res
, "");
2829 * Generate 1/sqrt(a).
2830 * Result is undefined for values < 0, infinity for +0.
2833 lp_build_rsqrt(struct lp_build_context
*bld
,
2836 const struct lp_type type
= bld
->type
;
2838 assert(lp_check_value(type
, a
));
2840 assert(type
.floating
);
2843 * This should be faster but all denormals will end up as infinity.
2845 if (0 && lp_build_fast_rsqrt_available(type
)) {
2846 const unsigned num_iterations
= 1;
2850 /* rsqrt(1.0) != 1.0 here */
2851 res
= lp_build_fast_rsqrt(bld
, a
);
2853 if (num_iterations
) {
2855 * Newton-Raphson will result in NaN instead of infinity for zero,
2856 * and NaN instead of zero for infinity.
2857 * Also, need to ensure rsqrt(1.0) == 1.0.
2858 * All numbers smaller than FLT_MIN will result in +infinity
2859 * (rsqrtps treats all denormals as zero).
2862 LLVMValueRef flt_min
= lp_build_const_vec(bld
->gallivm
, type
, FLT_MIN
);
2863 LLVMValueRef inf
= lp_build_const_vec(bld
->gallivm
, type
, INFINITY
);
2865 for (i
= 0; i
< num_iterations
; ++i
) {
2866 res
= lp_build_rsqrt_refine(bld
, a
, res
);
2868 cmp
= lp_build_compare(bld
->gallivm
, type
, PIPE_FUNC_LESS
, a
, flt_min
);
2869 res
= lp_build_select(bld
, cmp
, inf
, res
);
2870 cmp
= lp_build_compare(bld
->gallivm
, type
, PIPE_FUNC_EQUAL
, a
, inf
);
2871 res
= lp_build_select(bld
, cmp
, bld
->zero
, res
);
2872 cmp
= lp_build_compare(bld
->gallivm
, type
, PIPE_FUNC_EQUAL
, a
, bld
->one
);
2873 res
= lp_build_select(bld
, cmp
, bld
->one
, res
);
2879 return lp_build_rcp(bld
, lp_build_sqrt(bld
, a
));
2883 * If there's a fast (inaccurate) rsqrt instruction available
2884 * (caller may want to avoid to call rsqrt_fast if it's not available,
2885 * i.e. for calculating x^0.5 it may do rsqrt_fast(x) * x but if
2886 * unavailable it would result in sqrt/div/mul so obviously
2887 * much better to just call sqrt, skipping both div and mul).
2890 lp_build_fast_rsqrt_available(struct lp_type type
)
2892 assert(type
.floating
);
2894 if ((util_cpu_caps
.has_sse
&& type
.width
== 32 && type
.length
== 4) ||
2895 (util_cpu_caps
.has_avx
&& type
.width
== 32 && type
.length
== 8)) {
2903 * Generate 1/sqrt(a).
2904 * Result is undefined for values < 0, infinity for +0.
2905 * Precision is limited, only ~10 bits guaranteed
2906 * (rsqrt 1.0 may not be 1.0, denorms may be flushed to 0).
2909 lp_build_fast_rsqrt(struct lp_build_context
*bld
,
2912 LLVMBuilderRef builder
= bld
->gallivm
->builder
;
2913 const struct lp_type type
= bld
->type
;
2915 assert(lp_check_value(type
, a
));
2917 if (lp_build_fast_rsqrt_available(type
)) {
2918 const char *intrinsic
= NULL
;
2920 if (type
.length
== 4) {
2921 intrinsic
= "llvm.x86.sse.rsqrt.ps";
2924 intrinsic
= "llvm.x86.avx.rsqrt.ps.256";
2926 return lp_build_intrinsic_unary(builder
, intrinsic
, bld
->vec_type
, a
);
2929 debug_printf("%s: emulating fast rsqrt with rcp/sqrt\n", __FUNCTION__
);
2931 return lp_build_rcp(bld
, lp_build_sqrt(bld
, a
));
2936 * Generate sin(a) or cos(a) using polynomial approximation.
2937 * TODO: it might be worth recognizing sin and cos using same source
2938 * (i.e. d3d10 sincos opcode). Obviously doing both at the same time
2939 * would be way cheaper than calculating (nearly) everything twice...
2940 * Not sure it's common enough to be worth bothering however, scs
2941 * opcode could also benefit from calculating both though.
2944 lp_build_sin_or_cos(struct lp_build_context
*bld
,
2948 struct gallivm_state
*gallivm
= bld
->gallivm
;
2949 LLVMBuilderRef b
= gallivm
->builder
;
2950 struct lp_type int_type
= lp_int_type(bld
->type
);
2953 * take the absolute value,
2954 * x = _mm_and_ps(x, *(v4sf*)_ps_inv_sign_mask);
2957 LLVMValueRef inv_sig_mask
= lp_build_const_int_vec(gallivm
, bld
->type
, ~0x80000000);
2958 LLVMValueRef a_v4si
= LLVMBuildBitCast(b
, a
, bld
->int_vec_type
, "a_v4si");
2960 LLVMValueRef absi
= LLVMBuildAnd(b
, a_v4si
, inv_sig_mask
, "absi");
2961 LLVMValueRef x_abs
= LLVMBuildBitCast(b
, absi
, bld
->vec_type
, "x_abs");
2965 * y = _mm_mul_ps(x, *(v4sf*)_ps_cephes_FOPI);
2968 LLVMValueRef FOPi
= lp_build_const_vec(gallivm
, bld
->type
, 1.27323954473516);
2969 LLVMValueRef scale_y
= LLVMBuildFMul(b
, x_abs
, FOPi
, "scale_y");
2972 * store the integer part of y in mm0
2973 * emm2 = _mm_cvttps_epi32(y);
2976 LLVMValueRef emm2_i
= LLVMBuildFPToSI(b
, scale_y
, bld
->int_vec_type
, "emm2_i");
2979 * j=(j+1) & (~1) (see the cephes sources)
2980 * emm2 = _mm_add_epi32(emm2, *(v4si*)_pi32_1);
2983 LLVMValueRef all_one
= lp_build_const_int_vec(gallivm
, bld
->type
, 1);
2984 LLVMValueRef emm2_add
= LLVMBuildAdd(b
, emm2_i
, all_one
, "emm2_add");
2986 * emm2 = _mm_and_si128(emm2, *(v4si*)_pi32_inv1);
2988 LLVMValueRef inv_one
= lp_build_const_int_vec(gallivm
, bld
->type
, ~1);
2989 LLVMValueRef emm2_and
= LLVMBuildAnd(b
, emm2_add
, inv_one
, "emm2_and");
2992 * y = _mm_cvtepi32_ps(emm2);
2994 LLVMValueRef y_2
= LLVMBuildSIToFP(b
, emm2_and
, bld
->vec_type
, "y_2");
2996 LLVMValueRef const_2
= lp_build_const_int_vec(gallivm
, bld
->type
, 2);
2997 LLVMValueRef const_4
= lp_build_const_int_vec(gallivm
, bld
->type
, 4);
2998 LLVMValueRef const_29
= lp_build_const_int_vec(gallivm
, bld
->type
, 29);
2999 LLVMValueRef sign_mask
= lp_build_const_int_vec(gallivm
, bld
->type
, 0x80000000);
3002 * Argument used for poly selection and sign bit determination
3003 * is different for sin vs. cos.
3005 LLVMValueRef emm2_2
= cos
? LLVMBuildSub(b
, emm2_and
, const_2
, "emm2_2") :
3008 LLVMValueRef sign_bit
= cos
? LLVMBuildShl(b
, LLVMBuildAnd(b
, const_4
,
3009 LLVMBuildNot(b
, emm2_2
, ""), ""),
3010 const_29
, "sign_bit") :
3011 LLVMBuildAnd(b
, LLVMBuildXor(b
, a_v4si
,
3012 LLVMBuildShl(b
, emm2_add
,
3014 sign_mask
, "sign_bit");
3017 * get the polynom selection mask
3018 * there is one polynom for 0 <= x <= Pi/4
3019 * and another one for Pi/4<x<=Pi/2
3020 * Both branches will be computed.
3022 * emm2 = _mm_and_si128(emm2, *(v4si*)_pi32_2);
3023 * emm2 = _mm_cmpeq_epi32(emm2, _mm_setzero_si128());
3026 LLVMValueRef emm2_3
= LLVMBuildAnd(b
, emm2_2
, const_2
, "emm2_3");
3027 LLVMValueRef poly_mask
= lp_build_compare(gallivm
,
3028 int_type
, PIPE_FUNC_EQUAL
,
3029 emm2_3
, lp_build_const_int_vec(gallivm
, bld
->type
, 0));
3032 * _PS_CONST(minus_cephes_DP1, -0.78515625);
3033 * _PS_CONST(minus_cephes_DP2, -2.4187564849853515625e-4);
3034 * _PS_CONST(minus_cephes_DP3, -3.77489497744594108e-8);
3036 LLVMValueRef DP1
= lp_build_const_vec(gallivm
, bld
->type
, -0.78515625);
3037 LLVMValueRef DP2
= lp_build_const_vec(gallivm
, bld
->type
, -2.4187564849853515625e-4);
3038 LLVMValueRef DP3
= lp_build_const_vec(gallivm
, bld
->type
, -3.77489497744594108e-8);
3041 * The magic pass: "Extended precision modular arithmetic"
3042 * x = ((x - y * DP1) - y * DP2) - y * DP3;
3044 LLVMValueRef x_1
= lp_build_fmuladd(b
, y_2
, DP1
, x_abs
);
3045 LLVMValueRef x_2
= lp_build_fmuladd(b
, y_2
, DP2
, x_1
);
3046 LLVMValueRef x_3
= lp_build_fmuladd(b
, y_2
, DP3
, x_2
);
3049 * Evaluate the first polynom (0 <= x <= Pi/4)
3051 * z = _mm_mul_ps(x,x);
3053 LLVMValueRef z
= LLVMBuildFMul(b
, x_3
, x_3
, "z");
3056 * _PS_CONST(coscof_p0, 2.443315711809948E-005);
3057 * _PS_CONST(coscof_p1, -1.388731625493765E-003);
3058 * _PS_CONST(coscof_p2, 4.166664568298827E-002);
3060 LLVMValueRef coscof_p0
= lp_build_const_vec(gallivm
, bld
->type
, 2.443315711809948E-005);
3061 LLVMValueRef coscof_p1
= lp_build_const_vec(gallivm
, bld
->type
, -1.388731625493765E-003);
3062 LLVMValueRef coscof_p2
= lp_build_const_vec(gallivm
, bld
->type
, 4.166664568298827E-002);
3065 * y = *(v4sf*)_ps_coscof_p0;
3066 * y = _mm_mul_ps(y, z);
3068 LLVMValueRef y_4
= lp_build_fmuladd(b
, z
, coscof_p0
, coscof_p1
);
3069 LLVMValueRef y_6
= lp_build_fmuladd(b
, y_4
, z
, coscof_p2
);
3070 LLVMValueRef y_7
= LLVMBuildFMul(b
, y_6
, z
, "y_7");
3071 LLVMValueRef y_8
= LLVMBuildFMul(b
, y_7
, z
, "y_8");
3075 * tmp = _mm_mul_ps(z, *(v4sf*)_ps_0p5);
3076 * y = _mm_sub_ps(y, tmp);
3077 * y = _mm_add_ps(y, *(v4sf*)_ps_1);
3079 LLVMValueRef half
= lp_build_const_vec(gallivm
, bld
->type
, 0.5);
3080 LLVMValueRef tmp
= LLVMBuildFMul(b
, z
, half
, "tmp");
3081 LLVMValueRef y_9
= LLVMBuildFSub(b
, y_8
, tmp
, "y_8");
3082 LLVMValueRef one
= lp_build_const_vec(gallivm
, bld
->type
, 1.0);
3083 LLVMValueRef y_10
= LLVMBuildFAdd(b
, y_9
, one
, "y_9");
3086 * _PS_CONST(sincof_p0, -1.9515295891E-4);
3087 * _PS_CONST(sincof_p1, 8.3321608736E-3);
3088 * _PS_CONST(sincof_p2, -1.6666654611E-1);
3090 LLVMValueRef sincof_p0
= lp_build_const_vec(gallivm
, bld
->type
, -1.9515295891E-4);
3091 LLVMValueRef sincof_p1
= lp_build_const_vec(gallivm
, bld
->type
, 8.3321608736E-3);
3092 LLVMValueRef sincof_p2
= lp_build_const_vec(gallivm
, bld
->type
, -1.6666654611E-1);
3095 * Evaluate the second polynom (Pi/4 <= x <= 0)
3097 * y2 = *(v4sf*)_ps_sincof_p0;
3098 * y2 = _mm_mul_ps(y2, z);
3099 * y2 = _mm_add_ps(y2, *(v4sf*)_ps_sincof_p1);
3100 * y2 = _mm_mul_ps(y2, z);
3101 * y2 = _mm_add_ps(y2, *(v4sf*)_ps_sincof_p2);
3102 * y2 = _mm_mul_ps(y2, z);
3103 * y2 = _mm_mul_ps(y2, x);
3104 * y2 = _mm_add_ps(y2, x);
3107 LLVMValueRef y2_4
= lp_build_fmuladd(b
, z
, sincof_p0
, sincof_p1
);
3108 LLVMValueRef y2_6
= lp_build_fmuladd(b
, y2_4
, z
, sincof_p2
);
3109 LLVMValueRef y2_7
= LLVMBuildFMul(b
, y2_6
, z
, "y2_7");
3110 LLVMValueRef y2_9
= lp_build_fmuladd(b
, y2_7
, x_3
, x_3
);
3113 * select the correct result from the two polynoms
3115 * y2 = _mm_and_ps(xmm3, y2); //, xmm3);
3116 * y = _mm_andnot_ps(xmm3, y);
3117 * y = _mm_or_ps(y,y2);
3119 LLVMValueRef y2_i
= LLVMBuildBitCast(b
, y2_9
, bld
->int_vec_type
, "y2_i");
3120 LLVMValueRef y_i
= LLVMBuildBitCast(b
, y_10
, bld
->int_vec_type
, "y_i");
3121 LLVMValueRef y2_and
= LLVMBuildAnd(b
, y2_i
, poly_mask
, "y2_and");
3122 LLVMValueRef poly_mask_inv
= LLVMBuildNot(b
, poly_mask
, "poly_mask_inv");
3123 LLVMValueRef y_and
= LLVMBuildAnd(b
, y_i
, poly_mask_inv
, "y_and");
3124 LLVMValueRef y_combine
= LLVMBuildOr(b
, y_and
, y2_and
, "y_combine");
3128 * y = _mm_xor_ps(y, sign_bit);
3130 LLVMValueRef y_sign
= LLVMBuildXor(b
, y_combine
, sign_bit
, "y_sign");
3131 LLVMValueRef y_result
= LLVMBuildBitCast(b
, y_sign
, bld
->vec_type
, "y_result");
3133 LLVMValueRef isfinite
= lp_build_isfinite(bld
, a
);
3135 /* clamp output to be within [-1, 1] */
3136 y_result
= lp_build_clamp(bld
, y_result
,
3137 lp_build_const_vec(bld
->gallivm
, bld
->type
, -1.f
),
3138 lp_build_const_vec(bld
->gallivm
, bld
->type
, 1.f
));
3139 /* If a is -inf, inf or NaN then return NaN */
3140 y_result
= lp_build_select(bld
, isfinite
, y_result
,
3141 lp_build_const_vec(bld
->gallivm
, bld
->type
, NAN
));
3150 lp_build_sin(struct lp_build_context
*bld
,
3153 return lp_build_sin_or_cos(bld
, a
, FALSE
);
3161 lp_build_cos(struct lp_build_context
*bld
,
3164 return lp_build_sin_or_cos(bld
, a
, TRUE
);
3169 * Generate pow(x, y)
3172 lp_build_pow(struct lp_build_context
*bld
,
3176 /* TODO: optimize the constant case */
3177 if (gallivm_debug
& GALLIVM_DEBUG_PERF
&&
3178 LLVMIsConstant(x
) && LLVMIsConstant(y
)) {
3179 debug_printf("%s: inefficient/imprecise constant arithmetic\n",
3183 return lp_build_exp2(bld
, lp_build_mul(bld
, lp_build_log2(bld
, x
), y
));
3191 lp_build_exp(struct lp_build_context
*bld
,
3194 /* log2(e) = 1/log(2) */
3195 LLVMValueRef log2e
= lp_build_const_vec(bld
->gallivm
, bld
->type
,
3196 1.4426950408889634);
3198 assert(lp_check_value(bld
->type
, x
));
3200 return lp_build_exp2(bld
, lp_build_mul(bld
, log2e
, x
));
3206 * Behavior is undefined with infs, 0s and nans
3209 lp_build_log(struct lp_build_context
*bld
,
3213 LLVMValueRef log2
= lp_build_const_vec(bld
->gallivm
, bld
->type
,
3214 0.69314718055994529);
3216 assert(lp_check_value(bld
->type
, x
));
3218 return lp_build_mul(bld
, log2
, lp_build_log2(bld
, x
));
3222 * Generate log(x) that handles edge cases (infs, 0s and nans)
3225 lp_build_log_safe(struct lp_build_context
*bld
,
3229 LLVMValueRef log2
= lp_build_const_vec(bld
->gallivm
, bld
->type
,
3230 0.69314718055994529);
3232 assert(lp_check_value(bld
->type
, x
));
3234 return lp_build_mul(bld
, log2
, lp_build_log2_safe(bld
, x
));
3239 * Generate polynomial.
3240 * Ex: coeffs[0] + x * coeffs[1] + x^2 * coeffs[2].
3243 lp_build_polynomial(struct lp_build_context
*bld
,
3245 const double *coeffs
,
3246 unsigned num_coeffs
)
3248 const struct lp_type type
= bld
->type
;
3249 LLVMValueRef even
= NULL
, odd
= NULL
;
3253 assert(lp_check_value(bld
->type
, x
));
3255 /* TODO: optimize the constant case */
3256 if (gallivm_debug
& GALLIVM_DEBUG_PERF
&&
3257 LLVMIsConstant(x
)) {
3258 debug_printf("%s: inefficient/imprecise constant arithmetic\n",
3263 * Calculate odd and even terms seperately to decrease data dependency
3265 * c[0] + x^2 * c[2] + x^4 * c[4] ...
3266 * + x * (c[1] + x^2 * c[3] + x^4 * c[5]) ...
3268 x2
= lp_build_mul(bld
, x
, x
);
3270 for (i
= num_coeffs
; i
--; ) {
3273 coeff
= lp_build_const_vec(bld
->gallivm
, type
, coeffs
[i
]);
3277 even
= lp_build_mad(bld
, x2
, even
, coeff
);
3282 odd
= lp_build_mad(bld
, x2
, odd
, coeff
);
3289 return lp_build_mad(bld
, odd
, x
, even
);
3298 * Minimax polynomial fit of 2**x, in range [0, 1[
3300 const double lp_build_exp2_polynomial
[] = {
3301 #if EXP_POLY_DEGREE == 5
3302 1.000000000000000000000, /*XXX: was 0.999999925063526176901, recompute others */
3303 0.693153073200168932794,
3304 0.240153617044375388211,
3305 0.0558263180532956664775,
3306 0.00898934009049466391101,
3307 0.00187757667519147912699
3308 #elif EXP_POLY_DEGREE == 4
3309 1.00000259337069434683,
3310 0.693003834469974940458,
3311 0.24144275689150793076,
3312 0.0520114606103070150235,
3313 0.0135341679161270268764
3314 #elif EXP_POLY_DEGREE == 3
3315 0.999925218562710312959,
3316 0.695833540494823811697,
3317 0.226067155427249155588,
3318 0.0780245226406372992967
3319 #elif EXP_POLY_DEGREE == 2
3320 1.00172476321474503578,
3321 0.657636275736077639316,
3322 0.33718943461968720704
3330 lp_build_exp2(struct lp_build_context
*bld
,
3333 LLVMBuilderRef builder
= bld
->gallivm
->builder
;
3334 const struct lp_type type
= bld
->type
;
3335 LLVMTypeRef vec_type
= lp_build_vec_type(bld
->gallivm
, type
);
3336 LLVMValueRef ipart
= NULL
;
3337 LLVMValueRef fpart
= NULL
;
3338 LLVMValueRef expipart
= NULL
;
3339 LLVMValueRef expfpart
= NULL
;
3340 LLVMValueRef res
= NULL
;
3342 assert(lp_check_value(bld
->type
, x
));
3344 /* TODO: optimize the constant case */
3345 if (gallivm_debug
& GALLIVM_DEBUG_PERF
&&
3346 LLVMIsConstant(x
)) {
3347 debug_printf("%s: inefficient/imprecise constant arithmetic\n",
3351 assert(type
.floating
&& type
.width
== 32);
3353 /* We want to preserve NaN and make sure than for exp2 if x > 128,
3354 * the result is INF and if it's smaller than -126.9 the result is 0 */
3355 x
= lp_build_min_ext(bld
, lp_build_const_vec(bld
->gallivm
, type
, 128.0), x
,
3356 GALLIVM_NAN_RETURN_NAN_FIRST_NONNAN
);
3357 x
= lp_build_max_ext(bld
, lp_build_const_vec(bld
->gallivm
, type
, -126.99999),
3358 x
, GALLIVM_NAN_RETURN_NAN_FIRST_NONNAN
);
3360 /* ipart = floor(x) */
3361 /* fpart = x - ipart */
3362 lp_build_ifloor_fract(bld
, x
, &ipart
, &fpart
);
3364 /* expipart = (float) (1 << ipart) */
3365 expipart
= LLVMBuildAdd(builder
, ipart
,
3366 lp_build_const_int_vec(bld
->gallivm
, type
, 127), "");
3367 expipart
= LLVMBuildShl(builder
, expipart
,
3368 lp_build_const_int_vec(bld
->gallivm
, type
, 23), "");
3369 expipart
= LLVMBuildBitCast(builder
, expipart
, vec_type
, "");
3371 expfpart
= lp_build_polynomial(bld
, fpart
, lp_build_exp2_polynomial
,
3372 ARRAY_SIZE(lp_build_exp2_polynomial
));
3374 res
= LLVMBuildFMul(builder
, expipart
, expfpart
, "");
3382 * Extract the exponent of a IEEE-754 floating point value.
3384 * Optionally apply an integer bias.
3386 * Result is an integer value with
3388 * ifloor(log2(x)) + bias
3391 lp_build_extract_exponent(struct lp_build_context
*bld
,
3395 LLVMBuilderRef builder
= bld
->gallivm
->builder
;
3396 const struct lp_type type
= bld
->type
;
3397 unsigned mantissa
= lp_mantissa(type
);
3400 assert(type
.floating
);
3402 assert(lp_check_value(bld
->type
, x
));
3404 x
= LLVMBuildBitCast(builder
, x
, bld
->int_vec_type
, "");
3406 res
= LLVMBuildLShr(builder
, x
,
3407 lp_build_const_int_vec(bld
->gallivm
, type
, mantissa
), "");
3408 res
= LLVMBuildAnd(builder
, res
,
3409 lp_build_const_int_vec(bld
->gallivm
, type
, 255), "");
3410 res
= LLVMBuildSub(builder
, res
,
3411 lp_build_const_int_vec(bld
->gallivm
, type
, 127 - bias
), "");
3418 * Extract the mantissa of the a floating.
3420 * Result is a floating point value with
3422 * x / floor(log2(x))
3425 lp_build_extract_mantissa(struct lp_build_context
*bld
,
3428 LLVMBuilderRef builder
= bld
->gallivm
->builder
;
3429 const struct lp_type type
= bld
->type
;
3430 unsigned mantissa
= lp_mantissa(type
);
3431 LLVMValueRef mantmask
= lp_build_const_int_vec(bld
->gallivm
, type
,
3432 (1ULL << mantissa
) - 1);
3433 LLVMValueRef one
= LLVMConstBitCast(bld
->one
, bld
->int_vec_type
);
3436 assert(lp_check_value(bld
->type
, x
));
3438 assert(type
.floating
);
3440 x
= LLVMBuildBitCast(builder
, x
, bld
->int_vec_type
, "");
3442 /* res = x / 2**ipart */
3443 res
= LLVMBuildAnd(builder
, x
, mantmask
, "");
3444 res
= LLVMBuildOr(builder
, res
, one
, "");
3445 res
= LLVMBuildBitCast(builder
, res
, bld
->vec_type
, "");
3453 * Minimax polynomial fit of log2((1.0 + sqrt(x))/(1.0 - sqrt(x)))/sqrt(x) ,for x in range of [0, 1/9[
3454 * These coefficients can be generate with
3455 * http://www.boost.org/doc/libs/1_36_0/libs/math/doc/sf_and_dist/html/math_toolkit/toolkit/internals2/minimax.html
3457 const double lp_build_log2_polynomial
[] = {
3458 #if LOG_POLY_DEGREE == 5
3459 2.88539008148777786488L,
3460 0.961796878841293367824L,
3461 0.577058946784739859012L,
3462 0.412914355135828735411L,
3463 0.308591899232910175289L,
3464 0.352376952300281371868L,
3465 #elif LOG_POLY_DEGREE == 4
3466 2.88539009343309178325L,
3467 0.961791550404184197881L,
3468 0.577440339438736392009L,
3469 0.403343858251329912514L,
3470 0.406718052498846252698L,
3471 #elif LOG_POLY_DEGREE == 3
3472 2.88538959748872753838L,
3473 0.961932915889597772928L,
3474 0.571118517972136195241L,
3475 0.493997535084709500285L,
3482 * See http://www.devmaster.net/forums/showthread.php?p=43580
3483 * http://en.wikipedia.org/wiki/Logarithm#Calculation
3484 * http://www.nezumi.demon.co.uk/consult/logx.htm
3486 * If handle_edge_cases is true the function will perform computations
3487 * to match the required D3D10+ behavior for each of the edge cases.
3488 * That means that if input is:
3489 * - less than zero (to and including -inf) then NaN will be returned
3490 * - equal to zero (-denorm, -0, +0 or +denorm), then -inf will be returned
3491 * - +infinity, then +infinity will be returned
3492 * - NaN, then NaN will be returned
3494 * Those checks are fairly expensive so if you don't need them make sure
3495 * handle_edge_cases is false.
3498 lp_build_log2_approx(struct lp_build_context
*bld
,
3500 LLVMValueRef
*p_exp
,
3501 LLVMValueRef
*p_floor_log2
,
3502 LLVMValueRef
*p_log2
,
3503 boolean handle_edge_cases
)
3505 LLVMBuilderRef builder
= bld
->gallivm
->builder
;
3506 const struct lp_type type
= bld
->type
;
3507 LLVMTypeRef vec_type
= lp_build_vec_type(bld
->gallivm
, type
);
3508 LLVMTypeRef int_vec_type
= lp_build_int_vec_type(bld
->gallivm
, type
);
3510 LLVMValueRef expmask
= lp_build_const_int_vec(bld
->gallivm
, type
, 0x7f800000);
3511 LLVMValueRef mantmask
= lp_build_const_int_vec(bld
->gallivm
, type
, 0x007fffff);
3512 LLVMValueRef one
= LLVMConstBitCast(bld
->one
, int_vec_type
);
3514 LLVMValueRef i
= NULL
;
3515 LLVMValueRef y
= NULL
;
3516 LLVMValueRef z
= NULL
;
3517 LLVMValueRef exp
= NULL
;
3518 LLVMValueRef mant
= NULL
;
3519 LLVMValueRef logexp
= NULL
;
3520 LLVMValueRef p_z
= NULL
;
3521 LLVMValueRef res
= NULL
;
3523 assert(lp_check_value(bld
->type
, x
));
3525 if(p_exp
|| p_floor_log2
|| p_log2
) {
3526 /* TODO: optimize the constant case */
3527 if (gallivm_debug
& GALLIVM_DEBUG_PERF
&&
3528 LLVMIsConstant(x
)) {
3529 debug_printf("%s: inefficient/imprecise constant arithmetic\n",
3533 assert(type
.floating
&& type
.width
== 32);
3536 * We don't explicitly handle denormalized numbers. They will yield a
3537 * result in the neighbourhood of -127, which appears to be adequate
3541 i
= LLVMBuildBitCast(builder
, x
, int_vec_type
, "");
3543 /* exp = (float) exponent(x) */
3544 exp
= LLVMBuildAnd(builder
, i
, expmask
, "");
3547 if(p_floor_log2
|| p_log2
) {
3548 logexp
= LLVMBuildLShr(builder
, exp
, lp_build_const_int_vec(bld
->gallivm
, type
, 23), "");
3549 logexp
= LLVMBuildSub(builder
, logexp
, lp_build_const_int_vec(bld
->gallivm
, type
, 127), "");
3550 logexp
= LLVMBuildSIToFP(builder
, logexp
, vec_type
, "");
3554 /* mant = 1 + (float) mantissa(x) */
3555 mant
= LLVMBuildAnd(builder
, i
, mantmask
, "");
3556 mant
= LLVMBuildOr(builder
, mant
, one
, "");
3557 mant
= LLVMBuildBitCast(builder
, mant
, vec_type
, "");
3559 /* y = (mant - 1) / (mant + 1) */
3560 y
= lp_build_div(bld
,
3561 lp_build_sub(bld
, mant
, bld
->one
),
3562 lp_build_add(bld
, mant
, bld
->one
)
3566 z
= lp_build_mul(bld
, y
, y
);
3569 p_z
= lp_build_polynomial(bld
, z
, lp_build_log2_polynomial
,
3570 ARRAY_SIZE(lp_build_log2_polynomial
));
3572 /* y * P(z) + logexp */
3573 res
= lp_build_mad(bld
, y
, p_z
, logexp
);
3575 if (type
.floating
&& handle_edge_cases
) {
3576 LLVMValueRef negmask
, infmask
, zmask
;
3577 negmask
= lp_build_cmp(bld
, PIPE_FUNC_LESS
, x
,
3578 lp_build_const_vec(bld
->gallivm
, type
, 0.0f
));
3579 zmask
= lp_build_cmp(bld
, PIPE_FUNC_EQUAL
, x
,
3580 lp_build_const_vec(bld
->gallivm
, type
, 0.0f
));
3581 infmask
= lp_build_cmp(bld
, PIPE_FUNC_GEQUAL
, x
,
3582 lp_build_const_vec(bld
->gallivm
, type
, INFINITY
));
3584 /* If x is qual to inf make sure we return inf */
3585 res
= lp_build_select(bld
, infmask
,
3586 lp_build_const_vec(bld
->gallivm
, type
, INFINITY
),
3588 /* If x is qual to 0, return -inf */
3589 res
= lp_build_select(bld
, zmask
,
3590 lp_build_const_vec(bld
->gallivm
, type
, -INFINITY
),
3592 /* If x is nan or less than 0, return nan */
3593 res
= lp_build_select(bld
, negmask
,
3594 lp_build_const_vec(bld
->gallivm
, type
, NAN
),
3600 exp
= LLVMBuildBitCast(builder
, exp
, vec_type
, "");
3605 *p_floor_log2
= logexp
;
3613 * log2 implementation which doesn't have special code to
3614 * handle edge cases (-inf, 0, inf, NaN). It's faster but
3615 * the results for those cases are undefined.
3618 lp_build_log2(struct lp_build_context
*bld
,
3622 lp_build_log2_approx(bld
, x
, NULL
, NULL
, &res
, FALSE
);
3627 * Version of log2 which handles all edge cases.
3628 * Look at documentation of lp_build_log2_approx for
3629 * description of the behavior for each of the edge cases.
3632 lp_build_log2_safe(struct lp_build_context
*bld
,
3636 lp_build_log2_approx(bld
, x
, NULL
, NULL
, &res
, TRUE
);
3642 * Faster (and less accurate) log2.
3644 * log2(x) = floor(log2(x)) - 1 + x / 2**floor(log2(x))
3646 * Piece-wise linear approximation, with exact results when x is a
3649 * See http://www.flipcode.com/archives/Fast_log_Function.shtml
3652 lp_build_fast_log2(struct lp_build_context
*bld
,
3655 LLVMBuilderRef builder
= bld
->gallivm
->builder
;
3659 assert(lp_check_value(bld
->type
, x
));
3661 assert(bld
->type
.floating
);
3663 /* ipart = floor(log2(x)) - 1 */
3664 ipart
= lp_build_extract_exponent(bld
, x
, -1);
3665 ipart
= LLVMBuildSIToFP(builder
, ipart
, bld
->vec_type
, "");
3667 /* fpart = x / 2**ipart */
3668 fpart
= lp_build_extract_mantissa(bld
, x
);
3671 return LLVMBuildFAdd(builder
, ipart
, fpart
, "");
3676 * Fast implementation of iround(log2(x)).
3678 * Not an approximation -- it should give accurate results all the time.
3681 lp_build_ilog2(struct lp_build_context
*bld
,
3684 LLVMBuilderRef builder
= bld
->gallivm
->builder
;
3685 LLVMValueRef sqrt2
= lp_build_const_vec(bld
->gallivm
, bld
->type
, M_SQRT2
);
3688 assert(bld
->type
.floating
);
3690 assert(lp_check_value(bld
->type
, x
));
3692 /* x * 2^(0.5) i.e., add 0.5 to the log2(x) */
3693 x
= LLVMBuildFMul(builder
, x
, sqrt2
, "");
3695 /* ipart = floor(log2(x) + 0.5) */
3696 ipart
= lp_build_extract_exponent(bld
, x
, 0);
3702 lp_build_mod(struct lp_build_context
*bld
,
3706 LLVMBuilderRef builder
= bld
->gallivm
->builder
;
3708 const struct lp_type type
= bld
->type
;
3710 assert(lp_check_value(type
, x
));
3711 assert(lp_check_value(type
, y
));
3714 res
= LLVMBuildFRem(builder
, x
, y
, "");
3716 res
= LLVMBuildSRem(builder
, x
, y
, "");
3718 res
= LLVMBuildURem(builder
, x
, y
, "");
3724 * For floating inputs it creates and returns a mask
3725 * which is all 1's for channels which are NaN.
3726 * Channels inside x which are not NaN will be 0.
3729 lp_build_isnan(struct lp_build_context
*bld
,
3733 LLVMTypeRef int_vec_type
= lp_build_int_vec_type(bld
->gallivm
, bld
->type
);
3735 assert(bld
->type
.floating
);
3736 assert(lp_check_value(bld
->type
, x
));
3738 mask
= LLVMBuildFCmp(bld
->gallivm
->builder
, LLVMRealOEQ
, x
, x
,
3740 mask
= LLVMBuildNot(bld
->gallivm
->builder
, mask
, "");
3741 mask
= LLVMBuildSExt(bld
->gallivm
->builder
, mask
, int_vec_type
, "isnan");
3745 /* Returns all 1's for floating point numbers that are
3746 * finite numbers and returns all zeros for -inf,
3749 lp_build_isfinite(struct lp_build_context
*bld
,
3752 LLVMBuilderRef builder
= bld
->gallivm
->builder
;
3753 LLVMTypeRef int_vec_type
= lp_build_int_vec_type(bld
->gallivm
, bld
->type
);
3754 struct lp_type int_type
= lp_int_type(bld
->type
);
3755 LLVMValueRef intx
= LLVMBuildBitCast(builder
, x
, int_vec_type
, "");
3756 LLVMValueRef infornan32
= lp_build_const_int_vec(bld
->gallivm
, bld
->type
,
3759 if (!bld
->type
.floating
) {
3760 return lp_build_const_int_vec(bld
->gallivm
, bld
->type
, 0);
3762 assert(bld
->type
.floating
);
3763 assert(lp_check_value(bld
->type
, x
));
3764 assert(bld
->type
.width
== 32);
3766 intx
= LLVMBuildAnd(builder
, intx
, infornan32
, "");
3767 return lp_build_compare(bld
->gallivm
, int_type
, PIPE_FUNC_NOTEQUAL
,
3772 * Returns true if the number is nan or inf and false otherwise.
3773 * The input has to be a floating point vector.
3776 lp_build_is_inf_or_nan(struct gallivm_state
*gallivm
,
3777 const struct lp_type type
,
3780 LLVMBuilderRef builder
= gallivm
->builder
;
3781 struct lp_type int_type
= lp_int_type(type
);
3782 LLVMValueRef const0
= lp_build_const_int_vec(gallivm
, int_type
,
3786 assert(type
.floating
);
3788 ret
= LLVMBuildBitCast(builder
, x
, lp_build_vec_type(gallivm
, int_type
), "");
3789 ret
= LLVMBuildAnd(builder
, ret
, const0
, "");
3790 ret
= lp_build_compare(gallivm
, int_type
, PIPE_FUNC_EQUAL
,
3798 lp_build_fpstate_get(struct gallivm_state
*gallivm
)
3800 if (util_cpu_caps
.has_sse
) {
3801 LLVMBuilderRef builder
= gallivm
->builder
;
3802 LLVMValueRef mxcsr_ptr
= lp_build_alloca(
3804 LLVMInt32TypeInContext(gallivm
->context
),
3806 LLVMValueRef mxcsr_ptr8
= LLVMBuildPointerCast(builder
, mxcsr_ptr
,
3807 LLVMPointerType(LLVMInt8TypeInContext(gallivm
->context
), 0), "");
3808 lp_build_intrinsic(builder
,
3809 "llvm.x86.sse.stmxcsr",
3810 LLVMVoidTypeInContext(gallivm
->context
),
3818 lp_build_fpstate_set_denorms_zero(struct gallivm_state
*gallivm
,
3821 if (util_cpu_caps
.has_sse
) {
3822 /* turn on DAZ (64) | FTZ (32768) = 32832 if available */
3823 int daz_ftz
= _MM_FLUSH_ZERO_MASK
;
3825 LLVMBuilderRef builder
= gallivm
->builder
;
3826 LLVMValueRef mxcsr_ptr
= lp_build_fpstate_get(gallivm
);
3827 LLVMValueRef mxcsr
=
3828 LLVMBuildLoad(builder
, mxcsr_ptr
, "mxcsr");
3830 if (util_cpu_caps
.has_daz
) {
3831 /* Enable denormals are zero mode */
3832 daz_ftz
|= _MM_DENORMALS_ZERO_MASK
;
3835 mxcsr
= LLVMBuildOr(builder
, mxcsr
,
3836 LLVMConstInt(LLVMTypeOf(mxcsr
), daz_ftz
, 0), "");
3838 mxcsr
= LLVMBuildAnd(builder
, mxcsr
,
3839 LLVMConstInt(LLVMTypeOf(mxcsr
), ~daz_ftz
, 0), "");
3842 LLVMBuildStore(builder
, mxcsr
, mxcsr_ptr
);
3843 lp_build_fpstate_set(gallivm
, mxcsr_ptr
);
3848 lp_build_fpstate_set(struct gallivm_state
*gallivm
,
3849 LLVMValueRef mxcsr_ptr
)
3851 if (util_cpu_caps
.has_sse
) {
3852 LLVMBuilderRef builder
= gallivm
->builder
;
3853 mxcsr_ptr
= LLVMBuildPointerCast(builder
, mxcsr_ptr
,
3854 LLVMPointerType(LLVMInt8TypeInContext(gallivm
->context
), 0), "");
3855 lp_build_intrinsic(builder
,
3856 "llvm.x86.sse.ldmxcsr",
3857 LLVMVoidTypeInContext(gallivm
->context
),