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>
48 #include "util/u_memory.h"
49 #include "util/u_debug.h"
50 #include "util/u_math.h"
51 #include "util/u_string.h"
52 #include "util/u_cpu_detect.h"
54 #include "lp_bld_type.h"
55 #include "lp_bld_const.h"
56 #include "lp_bld_intr.h"
57 #include "lp_bld_logic.h"
58 #include "lp_bld_pack.h"
59 #include "lp_bld_arit.h"
62 #define EXP_POLY_DEGREE 3
64 #define LOG_POLY_DEGREE 5
69 * No checks for special case values of a or b = 1 or 0 are done.
72 lp_build_min_simple(struct lp_build_context
*bld
,
76 const struct lp_type type
= bld
->type
;
77 const char *intrinsic
= NULL
;
80 assert(lp_check_value(type
, a
));
81 assert(lp_check_value(type
, b
));
83 /* TODO: optimize the constant case */
85 if(type
.width
* type
.length
== 128) {
87 if(type
.width
== 32 && util_cpu_caps
.has_sse
)
88 intrinsic
= "llvm.x86.sse.min.ps";
89 if(type
.width
== 64 && util_cpu_caps
.has_sse2
)
90 intrinsic
= "llvm.x86.sse2.min.pd";
93 if(type
.width
== 8 && !type
.sign
&& util_cpu_caps
.has_sse2
)
94 intrinsic
= "llvm.x86.sse2.pminu.b";
95 if(type
.width
== 8 && type
.sign
&& util_cpu_caps
.has_sse4_1
)
96 intrinsic
= "llvm.x86.sse41.pminsb";
97 if(type
.width
== 16 && !type
.sign
&& util_cpu_caps
.has_sse4_1
)
98 intrinsic
= "llvm.x86.sse41.pminuw";
99 if(type
.width
== 16 && type
.sign
&& util_cpu_caps
.has_sse2
)
100 intrinsic
= "llvm.x86.sse2.pmins.w";
101 if(type
.width
== 32 && !type
.sign
&& util_cpu_caps
.has_sse4_1
)
102 intrinsic
= "llvm.x86.sse41.pminud";
103 if(type
.width
== 32 && type
.sign
&& util_cpu_caps
.has_sse4_1
)
104 intrinsic
= "llvm.x86.sse41.pminsd";
109 return lp_build_intrinsic_binary(bld
->builder
, intrinsic
, lp_build_vec_type(bld
->type
), a
, b
);
111 cond
= lp_build_cmp(bld
, PIPE_FUNC_LESS
, a
, b
);
112 return lp_build_select(bld
, cond
, a
, b
);
118 * No checks for special case values of a or b = 1 or 0 are done.
121 lp_build_max_simple(struct lp_build_context
*bld
,
125 const struct lp_type type
= bld
->type
;
126 const char *intrinsic
= NULL
;
129 assert(lp_check_value(type
, a
));
130 assert(lp_check_value(type
, b
));
132 /* TODO: optimize the constant case */
134 if(type
.width
* type
.length
== 128) {
136 if(type
.width
== 32 && util_cpu_caps
.has_sse
)
137 intrinsic
= "llvm.x86.sse.max.ps";
138 if(type
.width
== 64 && util_cpu_caps
.has_sse2
)
139 intrinsic
= "llvm.x86.sse2.max.pd";
142 if(type
.width
== 8 && !type
.sign
&& util_cpu_caps
.has_sse2
)
143 intrinsic
= "llvm.x86.sse2.pmaxu.b";
144 if(type
.width
== 8 && type
.sign
&& util_cpu_caps
.has_sse4_1
)
145 intrinsic
= "llvm.x86.sse41.pmaxsb";
146 if(type
.width
== 16 && !type
.sign
&& util_cpu_caps
.has_sse4_1
)
147 intrinsic
= "llvm.x86.sse41.pmaxuw";
148 if(type
.width
== 16 && type
.sign
&& util_cpu_caps
.has_sse2
)
149 intrinsic
= "llvm.x86.sse2.pmaxs.w";
150 if(type
.width
== 32 && !type
.sign
&& util_cpu_caps
.has_sse4_1
)
151 intrinsic
= "llvm.x86.sse41.pmaxud";
152 if(type
.width
== 32 && type
.sign
&& util_cpu_caps
.has_sse4_1
)
153 intrinsic
= "llvm.x86.sse41.pmaxsd";
158 return lp_build_intrinsic_binary(bld
->builder
, intrinsic
, lp_build_vec_type(bld
->type
), a
, b
);
160 cond
= lp_build_cmp(bld
, PIPE_FUNC_GREATER
, a
, b
);
161 return lp_build_select(bld
, cond
, a
, b
);
166 * Generate 1 - a, or ~a depending on bld->type.
169 lp_build_comp(struct lp_build_context
*bld
,
172 const struct lp_type type
= bld
->type
;
174 assert(lp_check_value(type
, a
));
181 if(type
.norm
&& !type
.floating
&& !type
.fixed
&& !type
.sign
) {
182 if(LLVMIsConstant(a
))
183 return LLVMConstNot(a
);
185 return LLVMBuildNot(bld
->builder
, a
, "");
188 if(LLVMIsConstant(a
))
190 return LLVMConstFSub(bld
->one
, a
);
192 return LLVMConstSub(bld
->one
, a
);
195 return LLVMBuildFSub(bld
->builder
, bld
->one
, a
, "");
197 return LLVMBuildSub(bld
->builder
, bld
->one
, a
, "");
205 lp_build_add(struct lp_build_context
*bld
,
209 const struct lp_type type
= bld
->type
;
212 assert(lp_check_value(type
, a
));
213 assert(lp_check_value(type
, b
));
219 if(a
== bld
->undef
|| b
== bld
->undef
)
223 const char *intrinsic
= NULL
;
225 if(a
== bld
->one
|| b
== bld
->one
)
228 if(util_cpu_caps
.has_sse2
&&
229 type
.width
* type
.length
== 128 &&
230 !type
.floating
&& !type
.fixed
) {
232 intrinsic
= type
.sign
? "llvm.x86.sse2.padds.b" : "llvm.x86.sse2.paddus.b";
234 intrinsic
= type
.sign
? "llvm.x86.sse2.padds.w" : "llvm.x86.sse2.paddus.w";
238 return lp_build_intrinsic_binary(bld
->builder
, intrinsic
, lp_build_vec_type(bld
->type
), a
, b
);
241 if(LLVMIsConstant(a
) && LLVMIsConstant(b
))
243 res
= LLVMConstFAdd(a
, b
);
245 res
= LLVMConstAdd(a
, b
);
248 res
= LLVMBuildFAdd(bld
->builder
, a
, b
, "");
250 res
= LLVMBuildAdd(bld
->builder
, a
, b
, "");
252 /* clamp to ceiling of 1.0 */
253 if(bld
->type
.norm
&& (bld
->type
.floating
|| bld
->type
.fixed
))
254 res
= lp_build_min_simple(bld
, res
, bld
->one
);
256 /* XXX clamp to floor of -1 or 0??? */
262 /** Return the sum of the elements of a */
264 lp_build_sum_vector(struct lp_build_context
*bld
,
267 const struct lp_type type
= bld
->type
;
268 LLVMValueRef index
, res
;
271 assert(lp_check_value(type
, a
));
277 assert(type
.length
> 1);
279 assert(!bld
->type
.norm
);
281 index
= LLVMConstInt(LLVMInt32Type(), 0, 0);
282 res
= LLVMBuildExtractElement(bld
->builder
, a
, index
, "");
284 for (i
= 1; i
< type
.length
; i
++) {
285 index
= LLVMConstInt(LLVMInt32Type(), i
, 0);
287 res
= LLVMBuildFAdd(bld
->builder
, res
,
288 LLVMBuildExtractElement(bld
->builder
,
292 res
= LLVMBuildAdd(bld
->builder
, res
,
293 LLVMBuildExtractElement(bld
->builder
,
306 lp_build_sub(struct lp_build_context
*bld
,
310 const struct lp_type type
= bld
->type
;
313 assert(lp_check_value(type
, a
));
314 assert(lp_check_value(type
, b
));
318 if(a
== bld
->undef
|| b
== bld
->undef
)
324 const char *intrinsic
= NULL
;
329 if(util_cpu_caps
.has_sse2
&&
330 type
.width
* type
.length
== 128 &&
331 !type
.floating
&& !type
.fixed
) {
333 intrinsic
= type
.sign
? "llvm.x86.sse2.psubs.b" : "llvm.x86.sse2.psubus.b";
335 intrinsic
= type
.sign
? "llvm.x86.sse2.psubs.w" : "llvm.x86.sse2.psubus.w";
339 return lp_build_intrinsic_binary(bld
->builder
, intrinsic
, lp_build_vec_type(bld
->type
), a
, b
);
342 if(LLVMIsConstant(a
) && LLVMIsConstant(b
))
344 res
= LLVMConstFSub(a
, b
);
346 res
= LLVMConstSub(a
, b
);
349 res
= LLVMBuildFSub(bld
->builder
, a
, b
, "");
351 res
= LLVMBuildSub(bld
->builder
, a
, b
, "");
353 if(bld
->type
.norm
&& (bld
->type
.floating
|| bld
->type
.fixed
))
354 res
= lp_build_max_simple(bld
, res
, bld
->zero
);
361 * Normalized 8bit multiplication.
365 * makes the following approximation to the division (Sree)
367 * a*b/255 ~= (a*(b + 1)) >> 256
369 * which is the fastest method that satisfies the following OpenGL criteria
371 * 0*0 = 0 and 255*255 = 255
375 * takes the geometric series approximation to the division
377 * t/255 = (t >> 8) + (t >> 16) + (t >> 24) ..
379 * in this case just the first two terms to fit in 16bit arithmetic
381 * t/255 ~= (t + (t >> 8)) >> 8
383 * note that just by itself it doesn't satisfies the OpenGL criteria, as
384 * 255*255 = 254, so the special case b = 255 must be accounted or roundoff
387 * - geometric series plus rounding
389 * when using a geometric series division instead of truncating the result
390 * use roundoff in the approximation (Jim Blinn)
392 * t/255 ~= (t + (t >> 8) + 0x80) >> 8
394 * achieving the exact results
396 * @sa Alvy Ray Smith, Image Compositing Fundamentals, Tech Memo 4, Aug 15, 1995,
397 * ftp://ftp.alvyray.com/Acrobat/4_Comp.pdf
398 * @sa Michael Herf, The "double blend trick", May 2000,
399 * http://www.stereopsis.com/doubleblend.html
402 lp_build_mul_u8n(LLVMBuilderRef builder
,
403 struct lp_type i16_type
,
404 LLVMValueRef a
, LLVMValueRef b
)
409 assert(!i16_type
.floating
);
410 assert(lp_check_value(i16_type
, a
));
411 assert(lp_check_value(i16_type
, b
));
413 c8
= lp_build_const_int_vec(i16_type
, 8);
417 /* a*b/255 ~= (a*(b + 1)) >> 256 */
418 b
= LLVMBuildAdd(builder
, b
, lp_build_const_int_vec(i16_type
, 1), "");
419 ab
= LLVMBuildMul(builder
, a
, b
, "");
423 /* ab/255 ~= (ab + (ab >> 8) + 0x80) >> 8 */
424 ab
= LLVMBuildMul(builder
, a
, b
, "");
425 ab
= LLVMBuildAdd(builder
, ab
, LLVMBuildLShr(builder
, ab
, c8
, ""), "");
426 ab
= LLVMBuildAdd(builder
, ab
, lp_build_const_int_vec(i16_type
, 0x80), "");
430 ab
= LLVMBuildLShr(builder
, ab
, c8
, "");
440 lp_build_mul(struct lp_build_context
*bld
,
444 const struct lp_type type
= bld
->type
;
448 assert(lp_check_value(type
, a
));
449 assert(lp_check_value(type
, b
));
459 if(a
== bld
->undef
|| b
== bld
->undef
)
462 if(!type
.floating
&& !type
.fixed
&& type
.norm
) {
463 if(type
.width
== 8) {
464 struct lp_type i16_type
= lp_wider_type(type
);
465 LLVMValueRef al
, ah
, bl
, bh
, abl
, abh
, ab
;
467 lp_build_unpack2(bld
->builder
, type
, i16_type
, a
, &al
, &ah
);
468 lp_build_unpack2(bld
->builder
, type
, i16_type
, b
, &bl
, &bh
);
470 /* PMULLW, PSRLW, PADDW */
471 abl
= lp_build_mul_u8n(bld
->builder
, i16_type
, al
, bl
);
472 abh
= lp_build_mul_u8n(bld
->builder
, i16_type
, ah
, bh
);
474 ab
= lp_build_pack2(bld
->builder
, i16_type
, type
, abl
, abh
);
484 shift
= lp_build_const_int_vec(type
, type
.width
/2);
488 if(LLVMIsConstant(a
) && LLVMIsConstant(b
)) {
490 res
= LLVMConstFMul(a
, b
);
492 res
= LLVMConstMul(a
, b
);
495 res
= LLVMConstAShr(res
, shift
);
497 res
= LLVMConstLShr(res
, shift
);
502 res
= LLVMBuildFMul(bld
->builder
, a
, b
, "");
504 res
= LLVMBuildMul(bld
->builder
, a
, b
, "");
507 res
= LLVMBuildAShr(bld
->builder
, res
, shift
, "");
509 res
= LLVMBuildLShr(bld
->builder
, res
, shift
, "");
518 * Small vector x scale multiplication optimization.
521 lp_build_mul_imm(struct lp_build_context
*bld
,
527 assert(lp_check_value(bld
->type
, a
));
536 return lp_build_negate(bld
, a
);
538 if(b
== 2 && bld
->type
.floating
)
539 return lp_build_add(bld
, a
, a
);
541 if(util_is_power_of_two(b
)) {
542 unsigned shift
= ffs(b
) - 1;
544 if(bld
->type
.floating
) {
547 * Power of two multiplication by directly manipulating the mantissa.
549 * XXX: This might not be always faster, it will introduce a small error
550 * for multiplication by zero, and it will produce wrong results
553 unsigned mantissa
= lp_mantissa(bld
->type
);
554 factor
= lp_build_const_int_vec(bld
->type
, (unsigned long long)shift
<< mantissa
);
555 a
= LLVMBuildBitCast(bld
->builder
, a
, lp_build_int_vec_type(bld
->type
), "");
556 a
= LLVMBuildAdd(bld
->builder
, a
, factor
, "");
557 a
= LLVMBuildBitCast(bld
->builder
, a
, lp_build_vec_type(bld
->type
), "");
562 factor
= lp_build_const_vec(bld
->type
, shift
);
563 return LLVMBuildShl(bld
->builder
, a
, factor
, "");
567 factor
= lp_build_const_vec(bld
->type
, (double)b
);
568 return lp_build_mul(bld
, a
, factor
);
576 lp_build_div(struct lp_build_context
*bld
,
580 const struct lp_type type
= bld
->type
;
582 assert(lp_check_value(type
, a
));
583 assert(lp_check_value(type
, b
));
588 return lp_build_rcp(bld
, b
);
593 if(a
== bld
->undef
|| b
== bld
->undef
)
596 if(LLVMIsConstant(a
) && LLVMIsConstant(b
)) {
598 return LLVMConstFDiv(a
, b
);
600 return LLVMConstSDiv(a
, b
);
602 return LLVMConstUDiv(a
, b
);
605 if(util_cpu_caps
.has_sse
&& type
.width
== 32 && type
.length
== 4)
606 return lp_build_mul(bld
, a
, lp_build_rcp(bld
, b
));
609 return LLVMBuildFDiv(bld
->builder
, a
, b
, "");
611 return LLVMBuildSDiv(bld
->builder
, a
, b
, "");
613 return LLVMBuildUDiv(bld
->builder
, a
, b
, "");
618 * Linear interpolation.
620 * This also works for integer values with a few caveats.
622 * @sa http://www.stereopsis.com/doubleblend.html
625 lp_build_lerp(struct lp_build_context
*bld
,
633 assert(lp_check_value(bld
->type
, x
));
634 assert(lp_check_value(bld
->type
, v0
));
635 assert(lp_check_value(bld
->type
, v1
));
637 delta
= lp_build_sub(bld
, v1
, v0
);
639 res
= lp_build_mul(bld
, x
, delta
);
641 res
= lp_build_add(bld
, v0
, res
);
644 /* XXX: This step is necessary for lerping 8bit colors stored on 16bits,
645 * but it will be wrong for other uses. Basically we need a more
646 * powerful lp_type, capable of further distinguishing the values
647 * interpretation from the value storage. */
648 res
= LLVMBuildAnd(bld
->builder
, res
, lp_build_const_int_vec(bld
->type
, (1 << bld
->type
.width
/2) - 1), "");
655 lp_build_lerp_2d(struct lp_build_context
*bld
,
663 LLVMValueRef v0
= lp_build_lerp(bld
, x
, v00
, v01
);
664 LLVMValueRef v1
= lp_build_lerp(bld
, x
, v10
, v11
);
665 return lp_build_lerp(bld
, y
, v0
, v1
);
671 * Do checks for special cases.
674 lp_build_min(struct lp_build_context
*bld
,
678 assert(lp_check_value(bld
->type
, a
));
679 assert(lp_check_value(bld
->type
, b
));
681 if(a
== bld
->undef
|| b
== bld
->undef
)
688 if(a
== bld
->zero
|| b
== bld
->zero
)
696 return lp_build_min_simple(bld
, a
, b
);
702 * Do checks for special cases.
705 lp_build_max(struct lp_build_context
*bld
,
709 assert(lp_check_value(bld
->type
, a
));
710 assert(lp_check_value(bld
->type
, b
));
712 if(a
== bld
->undef
|| b
== bld
->undef
)
719 if(a
== bld
->one
|| b
== bld
->one
)
727 return lp_build_max_simple(bld
, a
, b
);
732 * Generate clamp(a, min, max)
733 * Do checks for special cases.
736 lp_build_clamp(struct lp_build_context
*bld
,
741 assert(lp_check_value(bld
->type
, a
));
742 assert(lp_check_value(bld
->type
, min
));
743 assert(lp_check_value(bld
->type
, max
));
745 a
= lp_build_min(bld
, a
, max
);
746 a
= lp_build_max(bld
, a
, min
);
755 lp_build_abs(struct lp_build_context
*bld
,
758 const struct lp_type type
= bld
->type
;
759 LLVMTypeRef vec_type
= lp_build_vec_type(type
);
761 assert(lp_check_value(type
, a
));
767 /* Mask out the sign bit */
768 LLVMTypeRef int_vec_type
= lp_build_int_vec_type(type
);
769 unsigned long long absMask
= ~(1ULL << (type
.width
- 1));
770 LLVMValueRef mask
= lp_build_const_int_vec(type
, ((unsigned long long) absMask
));
771 a
= LLVMBuildBitCast(bld
->builder
, a
, int_vec_type
, "");
772 a
= LLVMBuildAnd(bld
->builder
, a
, mask
, "");
773 a
= LLVMBuildBitCast(bld
->builder
, a
, vec_type
, "");
777 if(type
.width
*type
.length
== 128 && util_cpu_caps
.has_ssse3
) {
780 return lp_build_intrinsic_unary(bld
->builder
, "llvm.x86.ssse3.pabs.b.128", vec_type
, a
);
782 return lp_build_intrinsic_unary(bld
->builder
, "llvm.x86.ssse3.pabs.w.128", vec_type
, a
);
784 return lp_build_intrinsic_unary(bld
->builder
, "llvm.x86.ssse3.pabs.d.128", vec_type
, a
);
788 return lp_build_max(bld
, a
, LLVMBuildNeg(bld
->builder
, a
, ""));
793 lp_build_negate(struct lp_build_context
*bld
,
796 assert(lp_check_value(bld
->type
, a
));
798 #if HAVE_LLVM >= 0x0207
799 if (bld
->type
.floating
)
800 a
= LLVMBuildFNeg(bld
->builder
, a
, "");
803 a
= LLVMBuildNeg(bld
->builder
, a
, "");
809 /** Return -1, 0 or +1 depending on the sign of a */
811 lp_build_sgn(struct lp_build_context
*bld
,
814 const struct lp_type type
= bld
->type
;
818 assert(lp_check_value(type
, a
));
820 /* Handle non-zero case */
822 /* if not zero then sign must be positive */
825 else if(type
.floating
) {
826 LLVMTypeRef vec_type
;
827 LLVMTypeRef int_type
;
831 unsigned long long maskBit
= (unsigned long long)1 << (type
.width
- 1);
833 int_type
= lp_build_int_vec_type(type
);
834 vec_type
= lp_build_vec_type(type
);
835 mask
= lp_build_const_int_vec(type
, maskBit
);
837 /* Take the sign bit and add it to 1 constant */
838 sign
= LLVMBuildBitCast(bld
->builder
, a
, int_type
, "");
839 sign
= LLVMBuildAnd(bld
->builder
, sign
, mask
, "");
840 one
= LLVMConstBitCast(bld
->one
, int_type
);
841 res
= LLVMBuildOr(bld
->builder
, sign
, one
, "");
842 res
= LLVMBuildBitCast(bld
->builder
, res
, vec_type
, "");
846 LLVMValueRef minus_one
= lp_build_const_vec(type
, -1.0);
847 cond
= lp_build_cmp(bld
, PIPE_FUNC_GREATER
, a
, bld
->zero
);
848 res
= lp_build_select(bld
, cond
, bld
->one
, minus_one
);
852 cond
= lp_build_cmp(bld
, PIPE_FUNC_EQUAL
, a
, bld
->zero
);
853 res
= lp_build_select(bld
, cond
, bld
->zero
, res
);
860 * Set the sign of float vector 'a' according to 'sign'.
861 * If sign==0, return abs(a).
862 * If sign==1, return -abs(a);
863 * Other values for sign produce undefined results.
866 lp_build_set_sign(struct lp_build_context
*bld
,
867 LLVMValueRef a
, LLVMValueRef sign
)
869 const struct lp_type type
= bld
->type
;
870 LLVMTypeRef int_vec_type
= lp_build_int_vec_type(type
);
871 LLVMTypeRef vec_type
= lp_build_vec_type(type
);
872 LLVMValueRef shift
= lp_build_const_int_vec(type
, type
.width
- 1);
873 LLVMValueRef mask
= lp_build_const_int_vec(type
,
874 ~((unsigned long long) 1 << (type
.width
- 1)));
875 LLVMValueRef val
, res
;
877 assert(type
.floating
);
878 assert(lp_check_value(type
, a
));
880 /* val = reinterpret_cast<int>(a) */
881 val
= LLVMBuildBitCast(bld
->builder
, a
, int_vec_type
, "");
882 /* val = val & mask */
883 val
= LLVMBuildAnd(bld
->builder
, val
, mask
, "");
884 /* sign = sign << shift */
885 sign
= LLVMBuildShl(bld
->builder
, sign
, shift
, "");
886 /* res = val | sign */
887 res
= LLVMBuildOr(bld
->builder
, val
, sign
, "");
888 /* res = reinterpret_cast<float>(res) */
889 res
= LLVMBuildBitCast(bld
->builder
, res
, vec_type
, "");
896 * Convert vector of (or scalar) int to vector of (or scalar) float.
899 lp_build_int_to_float(struct lp_build_context
*bld
,
902 const struct lp_type type
= bld
->type
;
903 LLVMTypeRef vec_type
= lp_build_vec_type(type
);
905 assert(type
.floating
);
907 return LLVMBuildSIToFP(bld
->builder
, a
, vec_type
, "");
912 enum lp_build_round_sse41_mode
914 LP_BUILD_ROUND_SSE41_NEAREST
= 0,
915 LP_BUILD_ROUND_SSE41_FLOOR
= 1,
916 LP_BUILD_ROUND_SSE41_CEIL
= 2,
917 LP_BUILD_ROUND_SSE41_TRUNCATE
= 3
921 static INLINE LLVMValueRef
922 lp_build_round_sse41(struct lp_build_context
*bld
,
924 enum lp_build_round_sse41_mode mode
)
926 const struct lp_type type
= bld
->type
;
927 LLVMTypeRef vec_type
= lp_build_vec_type(type
);
928 const char *intrinsic
;
930 assert(type
.floating
);
931 assert(type
.width
*type
.length
== 128);
932 assert(lp_check_value(type
, a
));
933 assert(util_cpu_caps
.has_sse4_1
);
937 intrinsic
= "llvm.x86.sse41.round.ps";
940 intrinsic
= "llvm.x86.sse41.round.pd";
947 return lp_build_intrinsic_binary(bld
->builder
, intrinsic
, vec_type
, a
,
948 LLVMConstInt(LLVMInt32Type(), mode
, 0));
953 * Return the integer part of a float (vector) value. The returned value is
955 * Ex: trunc(-1.5) = 1.0
958 lp_build_trunc(struct lp_build_context
*bld
,
961 const struct lp_type type
= bld
->type
;
963 assert(type
.floating
);
964 assert(lp_check_value(type
, a
));
966 if (util_cpu_caps
.has_sse4_1
&& type
.width
*type
.length
== 128)
967 return lp_build_round_sse41(bld
, a
, LP_BUILD_ROUND_SSE41_TRUNCATE
);
969 LLVMTypeRef vec_type
= lp_build_vec_type(type
);
970 LLVMTypeRef int_vec_type
= lp_build_int_vec_type(type
);
972 res
= LLVMBuildFPToSI(bld
->builder
, a
, int_vec_type
, "");
973 res
= LLVMBuildSIToFP(bld
->builder
, res
, vec_type
, "");
980 * Return float (vector) rounded to nearest integer (vector). The returned
981 * value is a float (vector).
982 * Ex: round(0.9) = 1.0
983 * Ex: round(-1.5) = -2.0
986 lp_build_round(struct lp_build_context
*bld
,
989 const struct lp_type type
= bld
->type
;
991 assert(type
.floating
);
992 assert(lp_check_value(type
, a
));
994 if (util_cpu_caps
.has_sse4_1
&& type
.width
*type
.length
== 128)
995 return lp_build_round_sse41(bld
, a
, LP_BUILD_ROUND_SSE41_NEAREST
);
997 LLVMTypeRef vec_type
= lp_build_vec_type(type
);
999 res
= lp_build_iround(bld
, a
);
1000 res
= LLVMBuildSIToFP(bld
->builder
, res
, vec_type
, "");
1007 * Return floor of float (vector), result is a float (vector)
1008 * Ex: floor(1.1) = 1.0
1009 * Ex: floor(-1.1) = -2.0
1012 lp_build_floor(struct lp_build_context
*bld
,
1015 const struct lp_type type
= bld
->type
;
1017 assert(type
.floating
);
1018 assert(lp_check_value(type
, a
));
1020 if (util_cpu_caps
.has_sse4_1
&& type
.width
*type
.length
== 128)
1021 return lp_build_round_sse41(bld
, a
, LP_BUILD_ROUND_SSE41_FLOOR
);
1023 LLVMTypeRef vec_type
= lp_build_vec_type(type
);
1025 res
= lp_build_ifloor(bld
, a
);
1026 res
= LLVMBuildSIToFP(bld
->builder
, res
, vec_type
, "");
1033 * Return ceiling of float (vector), returning float (vector).
1034 * Ex: ceil( 1.1) = 2.0
1035 * Ex: ceil(-1.1) = -1.0
1038 lp_build_ceil(struct lp_build_context
*bld
,
1041 const struct lp_type type
= bld
->type
;
1043 assert(type
.floating
);
1044 assert(lp_check_value(type
, a
));
1046 if (util_cpu_caps
.has_sse4_1
&& type
.width
*type
.length
== 128)
1047 return lp_build_round_sse41(bld
, a
, LP_BUILD_ROUND_SSE41_CEIL
);
1049 LLVMTypeRef vec_type
= lp_build_vec_type(type
);
1051 res
= lp_build_iceil(bld
, a
);
1052 res
= LLVMBuildSIToFP(bld
->builder
, res
, vec_type
, "");
1059 * Return fractional part of 'a' computed as a - floor(a)
1060 * Typically used in texture coord arithmetic.
1063 lp_build_fract(struct lp_build_context
*bld
,
1066 assert(bld
->type
.floating
);
1067 return lp_build_sub(bld
, a
, lp_build_floor(bld
, a
));
1072 * Return the integer part of a float (vector) value. The returned value is
1073 * an integer (vector).
1074 * Ex: itrunc(-1.5) = 1
1077 lp_build_itrunc(struct lp_build_context
*bld
,
1080 const struct lp_type type
= bld
->type
;
1081 LLVMTypeRef int_vec_type
= lp_build_int_vec_type(type
);
1083 assert(type
.floating
);
1084 assert(lp_check_value(type
, a
));
1086 return LLVMBuildFPToSI(bld
->builder
, a
, int_vec_type
, "");
1091 * Return float (vector) rounded to nearest integer (vector). The returned
1092 * value is an integer (vector).
1093 * Ex: iround(0.9) = 1
1094 * Ex: iround(-1.5) = -2
1097 lp_build_iround(struct lp_build_context
*bld
,
1100 const struct lp_type type
= bld
->type
;
1101 LLVMTypeRef int_vec_type
= lp_build_int_vec_type(type
);
1104 assert(type
.floating
);
1106 assert(lp_check_value(type
, a
));
1108 if (util_cpu_caps
.has_sse4_1
&& type
.width
*type
.length
== 128) {
1109 res
= lp_build_round_sse41(bld
, a
, LP_BUILD_ROUND_SSE41_NEAREST
);
1112 LLVMTypeRef vec_type
= lp_build_vec_type(type
);
1113 LLVMValueRef mask
= lp_build_const_int_vec(type
, (unsigned long long)1 << (type
.width
- 1));
1118 sign
= LLVMBuildBitCast(bld
->builder
, a
, int_vec_type
, "");
1119 sign
= LLVMBuildAnd(bld
->builder
, sign
, mask
, "");
1122 half
= lp_build_const_vec(type
, 0.5);
1123 half
= LLVMBuildBitCast(bld
->builder
, half
, int_vec_type
, "");
1124 half
= LLVMBuildOr(bld
->builder
, sign
, half
, "");
1125 half
= LLVMBuildBitCast(bld
->builder
, half
, vec_type
, "");
1127 res
= LLVMBuildFAdd(bld
->builder
, a
, half
, "");
1130 res
= LLVMBuildFPToSI(bld
->builder
, res
, int_vec_type
, "");
1137 * Return floor of float (vector), result is an int (vector)
1138 * Ex: ifloor(1.1) = 1.0
1139 * Ex: ifloor(-1.1) = -2.0
1142 lp_build_ifloor(struct lp_build_context
*bld
,
1145 const struct lp_type type
= bld
->type
;
1146 LLVMTypeRef int_vec_type
= lp_build_int_vec_type(type
);
1149 assert(type
.floating
);
1150 assert(lp_check_value(type
, a
));
1152 if (util_cpu_caps
.has_sse4_1
&& type
.width
*type
.length
== 128) {
1153 res
= lp_build_round_sse41(bld
, a
, LP_BUILD_ROUND_SSE41_FLOOR
);
1156 /* Take the sign bit and add it to 1 constant */
1157 LLVMTypeRef vec_type
= lp_build_vec_type(type
);
1158 unsigned mantissa
= lp_mantissa(type
);
1159 LLVMValueRef mask
= lp_build_const_int_vec(type
, (unsigned long long)1 << (type
.width
- 1));
1161 LLVMValueRef offset
;
1163 /* sign = a < 0 ? ~0 : 0 */
1164 sign
= LLVMBuildBitCast(bld
->builder
, a
, int_vec_type
, "");
1165 sign
= LLVMBuildAnd(bld
->builder
, sign
, mask
, "");
1166 sign
= LLVMBuildAShr(bld
->builder
, sign
, lp_build_const_int_vec(type
, type
.width
- 1), "ifloor.sign");
1168 /* offset = -0.99999(9)f */
1169 offset
= lp_build_const_vec(type
, -(double)(((unsigned long long)1 << mantissa
) - 10)/((unsigned long long)1 << mantissa
));
1170 offset
= LLVMConstBitCast(offset
, int_vec_type
);
1172 /* offset = a < 0 ? offset : 0.0f */
1173 offset
= LLVMBuildAnd(bld
->builder
, offset
, sign
, "");
1174 offset
= LLVMBuildBitCast(bld
->builder
, offset
, vec_type
, "ifloor.offset");
1176 res
= LLVMBuildFAdd(bld
->builder
, a
, offset
, "ifloor.res");
1179 /* round to nearest (toward zero) */
1180 res
= LLVMBuildFPToSI(bld
->builder
, res
, int_vec_type
, "ifloor.res");
1187 * Return ceiling of float (vector), returning int (vector).
1188 * Ex: iceil( 1.1) = 2
1189 * Ex: iceil(-1.1) = -1
1192 lp_build_iceil(struct lp_build_context
*bld
,
1195 const struct lp_type type
= bld
->type
;
1196 LLVMTypeRef int_vec_type
= lp_build_int_vec_type(type
);
1199 assert(type
.floating
);
1200 assert(lp_check_value(type
, a
));
1202 if (util_cpu_caps
.has_sse4_1
&& type
.width
*type
.length
== 128) {
1203 res
= lp_build_round_sse41(bld
, a
, LP_BUILD_ROUND_SSE41_CEIL
);
1206 LLVMTypeRef vec_type
= lp_build_vec_type(type
);
1207 unsigned mantissa
= lp_mantissa(type
);
1208 LLVMValueRef mask
= lp_build_const_int_vec(type
, (unsigned long long)1 << (type
.width
- 1));
1210 LLVMValueRef offset
;
1212 /* sign = a < 0 ? 0 : ~0 */
1213 sign
= LLVMBuildBitCast(bld
->builder
, a
, int_vec_type
, "");
1214 sign
= LLVMBuildAnd(bld
->builder
, sign
, mask
, "");
1215 sign
= LLVMBuildAShr(bld
->builder
, sign
, lp_build_const_int_vec(type
, type
.width
- 1), "iceil.sign");
1216 sign
= LLVMBuildNot(bld
->builder
, sign
, "iceil.not");
1218 /* offset = 0.99999(9)f */
1219 offset
= lp_build_const_vec(type
, (double)(((unsigned long long)1 << mantissa
) - 10)/((unsigned long long)1 << mantissa
));
1220 offset
= LLVMConstBitCast(offset
, int_vec_type
);
1222 /* offset = a < 0 ? 0.0 : offset */
1223 offset
= LLVMBuildAnd(bld
->builder
, offset
, sign
, "");
1224 offset
= LLVMBuildBitCast(bld
->builder
, offset
, vec_type
, "iceil.offset");
1226 res
= LLVMBuildFAdd(bld
->builder
, a
, offset
, "iceil.res");
1229 /* round to nearest (toward zero) */
1230 res
= LLVMBuildFPToSI(bld
->builder
, res
, int_vec_type
, "iceil.res");
1237 lp_build_sqrt(struct lp_build_context
*bld
,
1240 const struct lp_type type
= bld
->type
;
1241 LLVMTypeRef vec_type
= lp_build_vec_type(type
);
1244 assert(lp_check_value(type
, a
));
1246 /* TODO: optimize the constant case */
1247 /* TODO: optimize the constant case */
1249 assert(type
.floating
);
1250 util_snprintf(intrinsic
, sizeof intrinsic
, "llvm.sqrt.v%uf%u", type
.length
, type
.width
);
1252 return lp_build_intrinsic_unary(bld
->builder
, intrinsic
, vec_type
, a
);
1257 * Do one Newton-Raphson step to improve reciprocate precision:
1259 * x_{i+1} = x_i * (2 - a * x_i)
1261 * XXX: Unfortunately this won't give IEEE-754 conformant results for 0 or
1262 * +/-Inf, giving NaN instead. Certain applications rely on this behavior,
1263 * such as Google Earth, which does RCP(RSQRT(0.0) when drawing the Earth's
1264 * halo. It would be necessary to clamp the argument to prevent this.
1267 * - http://en.wikipedia.org/wiki/Division_(digital)#Newton.E2.80.93Raphson_division
1268 * - http://softwarecommunity.intel.com/articles/eng/1818.htm
1270 static INLINE LLVMValueRef
1271 lp_build_rcp_refine(struct lp_build_context
*bld
,
1275 LLVMValueRef two
= lp_build_const_vec(bld
->type
, 2.0);
1278 res
= LLVMBuildFMul(bld
->builder
, a
, rcp_a
, "");
1279 res
= LLVMBuildFSub(bld
->builder
, two
, res
, "");
1280 res
= LLVMBuildFMul(bld
->builder
, rcp_a
, res
, "");
1287 lp_build_rcp(struct lp_build_context
*bld
,
1290 const struct lp_type type
= bld
->type
;
1292 assert(lp_check_value(type
, a
));
1301 assert(type
.floating
);
1303 if(LLVMIsConstant(a
))
1304 return LLVMConstFDiv(bld
->one
, a
);
1307 * We don't use RCPPS because:
1308 * - it only has 10bits of precision
1309 * - it doesn't even get the reciprocate of 1.0 exactly
1310 * - doing Newton-Rapshon steps yields wrong (NaN) values for 0.0 or Inf
1311 * - for recent processors the benefit over DIVPS is marginal, a case
1314 * We could still use it on certain processors if benchmarks show that the
1315 * RCPPS plus necessary workarounds are still preferrable to DIVPS; or for
1316 * particular uses that require less workarounds.
1319 if (FALSE
&& util_cpu_caps
.has_sse
&& type
.width
== 32 && type
.length
== 4) {
1320 const unsigned num_iterations
= 0;
1324 res
= lp_build_intrinsic_unary(bld
->builder
, "llvm.x86.sse.rcp.ps", bld
->vec_type
, a
);
1326 for (i
= 0; i
< num_iterations
; ++i
) {
1327 res
= lp_build_rcp_refine(bld
, a
, res
);
1333 return LLVMBuildFDiv(bld
->builder
, bld
->one
, a
, "");
1338 * Do one Newton-Raphson step to improve rsqrt precision:
1340 * x_{i+1} = 0.5 * x_i * (3.0 - a * x_i * x_i)
1343 * - http://softwarecommunity.intel.com/articles/eng/1818.htm
1345 static INLINE LLVMValueRef
1346 lp_build_rsqrt_refine(struct lp_build_context
*bld
,
1348 LLVMValueRef rsqrt_a
)
1350 LLVMValueRef half
= lp_build_const_vec(bld
->type
, 0.5);
1351 LLVMValueRef three
= lp_build_const_vec(bld
->type
, 3.0);
1354 res
= LLVMBuildFMul(bld
->builder
, rsqrt_a
, rsqrt_a
, "");
1355 res
= LLVMBuildFMul(bld
->builder
, a
, res
, "");
1356 res
= LLVMBuildFSub(bld
->builder
, three
, res
, "");
1357 res
= LLVMBuildFMul(bld
->builder
, rsqrt_a
, res
, "");
1358 res
= LLVMBuildFMul(bld
->builder
, half
, res
, "");
1365 * Generate 1/sqrt(a)
1368 lp_build_rsqrt(struct lp_build_context
*bld
,
1371 const struct lp_type type
= bld
->type
;
1373 assert(lp_check_value(type
, a
));
1375 assert(type
.floating
);
1377 if (util_cpu_caps
.has_sse
&& type
.width
== 32 && type
.length
== 4) {
1378 const unsigned num_iterations
= 0;
1382 res
= lp_build_intrinsic_unary(bld
->builder
, "llvm.x86.sse.rsqrt.ps", bld
->vec_type
, a
);
1384 for (i
= 0; i
< num_iterations
; ++i
) {
1385 res
= lp_build_rsqrt_refine(bld
, a
, res
);
1391 return lp_build_rcp(bld
, lp_build_sqrt(bld
, a
));
1395 static inline LLVMValueRef
1396 lp_build_const_v4si(unsigned long value
)
1398 LLVMValueRef element
= LLVMConstInt(LLVMInt32Type(), value
, 0);
1399 LLVMValueRef elements
[4] = { element
, element
, element
, element
};
1400 return LLVMConstVector(elements
, 4);
1403 static inline LLVMValueRef
1404 lp_build_const_v4sf(float value
)
1406 LLVMValueRef element
= LLVMConstReal(LLVMFloatType(), value
);
1407 LLVMValueRef elements
[4] = { element
, element
, element
, element
};
1408 return LLVMConstVector(elements
, 4);
1413 * Generate sin(a) using SSE2
1416 lp_build_sin(struct lp_build_context
*bld
,
1419 struct lp_type int_type
= lp_int_type(bld
->type
);
1420 LLVMBuilderRef b
= bld
->builder
;
1421 LLVMTypeRef v4sf
= LLVMVectorType(LLVMFloatType(), 4);
1422 LLVMTypeRef v4si
= LLVMVectorType(LLVMInt32Type(), 4);
1425 * take the absolute value,
1426 * x = _mm_and_ps(x, *(v4sf*)_ps_inv_sign_mask);
1429 LLVMValueRef inv_sig_mask
= lp_build_const_v4si(~0x80000000);
1430 LLVMValueRef a_v4si
= LLVMBuildBitCast(b
, a
, v4si
, "a_v4si");
1432 LLVMValueRef absi
= LLVMBuildAnd(b
, a_v4si
, inv_sig_mask
, "absi");
1433 LLVMValueRef x_abs
= LLVMBuildBitCast(b
, absi
, v4sf
, "x_abs");
1436 * extract the sign bit (upper one)
1437 * sign_bit = _mm_and_ps(sign_bit, *(v4sf*)_ps_sign_mask);
1439 LLVMValueRef sig_mask
= lp_build_const_v4si(0x80000000);
1440 LLVMValueRef sign_bit_i
= LLVMBuildAnd(b
, a_v4si
, sig_mask
, "sign_bit_i");
1444 * y = _mm_mul_ps(x, *(v4sf*)_ps_cephes_FOPI);
1447 LLVMValueRef FOPi
= lp_build_const_v4sf(1.27323954473516);
1448 LLVMValueRef scale_y
= LLVMBuildFMul(b
, x_abs
, FOPi
, "scale_y");
1451 * store the integer part of y in mm0
1452 * emm2 = _mm_cvttps_epi32(y);
1455 LLVMValueRef emm2_i
= LLVMBuildFPToSI(b
, scale_y
, v4si
, "emm2_i");
1458 * j=(j+1) & (~1) (see the cephes sources)
1459 * emm2 = _mm_add_epi32(emm2, *(v4si*)_pi32_1);
1462 LLVMValueRef all_one
= lp_build_const_v4si(1);
1463 LLVMValueRef emm2_add
= LLVMBuildAdd(b
, emm2_i
, all_one
, "emm2_add");
1465 * emm2 = _mm_and_si128(emm2, *(v4si*)_pi32_inv1);
1467 LLVMValueRef inv_one
= lp_build_const_v4si(~1);
1468 LLVMValueRef emm2_and
= LLVMBuildAnd(b
, emm2_add
, inv_one
, "emm2_and");
1471 * y = _mm_cvtepi32_ps(emm2);
1473 LLVMValueRef y_2
= LLVMBuildSIToFP(b
, emm2_and
, v4sf
, "y_2");
1475 /* get the swap sign flag
1476 * emm0 = _mm_and_si128(emm2, *(v4si*)_pi32_4);
1478 LLVMValueRef pi32_4
= lp_build_const_v4si(4);
1479 LLVMValueRef emm0_and
= LLVMBuildAnd(b
, emm2_add
, pi32_4
, "emm0_and");
1482 * emm2 = _mm_slli_epi32(emm0, 29);
1484 LLVMValueRef const_29
= lp_build_const_v4si(29);
1485 LLVMValueRef swap_sign_bit
= LLVMBuildShl(b
, emm0_and
, const_29
, "swap_sign_bit");
1488 * get the polynom selection mask
1489 * there is one polynom for 0 <= x <= Pi/4
1490 * and another one for Pi/4<x<=Pi/2
1491 * Both branches will be computed.
1493 * emm2 = _mm_and_si128(emm2, *(v4si*)_pi32_2);
1494 * emm2 = _mm_cmpeq_epi32(emm2, _mm_setzero_si128());
1497 LLVMValueRef pi32_2
= lp_build_const_v4si(2);
1498 LLVMValueRef emm2_3
= LLVMBuildAnd(b
, emm2_and
, pi32_2
, "emm2_3");
1499 LLVMValueRef poly_mask
= lp_build_compare(b
, int_type
, PIPE_FUNC_EQUAL
,
1500 emm2_3
, lp_build_const_v4si(0));
1502 * sign_bit = _mm_xor_ps(sign_bit, swap_sign_bit);
1504 LLVMValueRef sign_bit_1
= LLVMBuildXor(b
, sign_bit_i
, swap_sign_bit
, "sign_bit");
1507 * _PS_CONST(minus_cephes_DP1, -0.78515625);
1508 * _PS_CONST(minus_cephes_DP2, -2.4187564849853515625e-4);
1509 * _PS_CONST(minus_cephes_DP3, -3.77489497744594108e-8);
1511 LLVMValueRef DP1
= lp_build_const_v4sf(-0.78515625);
1512 LLVMValueRef DP2
= lp_build_const_v4sf(-2.4187564849853515625e-4);
1513 LLVMValueRef DP3
= lp_build_const_v4sf(-3.77489497744594108e-8);
1516 * The magic pass: "Extended precision modular arithmetic"
1517 * x = ((x - y * DP1) - y * DP2) - y * DP3;
1518 * xmm1 = _mm_mul_ps(y, xmm1);
1519 * xmm2 = _mm_mul_ps(y, xmm2);
1520 * xmm3 = _mm_mul_ps(y, xmm3);
1522 LLVMValueRef xmm1
= LLVMBuildFMul(b
, y_2
, DP1
, "xmm1");
1523 LLVMValueRef xmm2
= LLVMBuildFMul(b
, y_2
, DP2
, "xmm2");
1524 LLVMValueRef xmm3
= LLVMBuildFMul(b
, y_2
, DP3
, "xmm3");
1527 * x = _mm_add_ps(x, xmm1);
1528 * x = _mm_add_ps(x, xmm2);
1529 * x = _mm_add_ps(x, xmm3);
1532 LLVMValueRef x_1
= LLVMBuildFAdd(b
, x_abs
, xmm1
, "x_1");
1533 LLVMValueRef x_2
= LLVMBuildFAdd(b
, x_1
, xmm2
, "x_2");
1534 LLVMValueRef x_3
= LLVMBuildFAdd(b
, x_2
, xmm3
, "x_3");
1537 * Evaluate the first polynom (0 <= x <= Pi/4)
1539 * z = _mm_mul_ps(x,x);
1541 LLVMValueRef z
= LLVMBuildFMul(b
, x_3
, x_3
, "z");
1544 * _PS_CONST(coscof_p0, 2.443315711809948E-005);
1545 * _PS_CONST(coscof_p1, -1.388731625493765E-003);
1546 * _PS_CONST(coscof_p2, 4.166664568298827E-002);
1548 LLVMValueRef coscof_p0
= lp_build_const_v4sf(2.443315711809948E-005);
1549 LLVMValueRef coscof_p1
= lp_build_const_v4sf(-1.388731625493765E-003);
1550 LLVMValueRef coscof_p2
= lp_build_const_v4sf(4.166664568298827E-002);
1553 * y = *(v4sf*)_ps_coscof_p0;
1554 * y = _mm_mul_ps(y, z);
1556 LLVMValueRef y_3
= LLVMBuildFMul(b
, z
, coscof_p0
, "y_3");
1557 LLVMValueRef y_4
= LLVMBuildFAdd(b
, y_3
, coscof_p1
, "y_4");
1558 LLVMValueRef y_5
= LLVMBuildFMul(b
, y_4
, z
, "y_5");
1559 LLVMValueRef y_6
= LLVMBuildFAdd(b
, y_5
, coscof_p2
, "y_6");
1560 LLVMValueRef y_7
= LLVMBuildFMul(b
, y_6
, z
, "y_7");
1561 LLVMValueRef y_8
= LLVMBuildFMul(b
, y_7
, z
, "y_8");
1565 * tmp = _mm_mul_ps(z, *(v4sf*)_ps_0p5);
1566 * y = _mm_sub_ps(y, tmp);
1567 * y = _mm_add_ps(y, *(v4sf*)_ps_1);
1569 LLVMValueRef half
= lp_build_const_v4sf(0.5);
1570 LLVMValueRef tmp
= LLVMBuildFMul(b
, z
, half
, "tmp");
1571 LLVMValueRef y_9
= LLVMBuildFSub(b
, y_8
, tmp
, "y_8");
1572 LLVMValueRef one
= lp_build_const_v4sf(1.0);
1573 LLVMValueRef y_10
= LLVMBuildFAdd(b
, y_9
, one
, "y_9");
1576 * _PS_CONST(sincof_p0, -1.9515295891E-4);
1577 * _PS_CONST(sincof_p1, 8.3321608736E-3);
1578 * _PS_CONST(sincof_p2, -1.6666654611E-1);
1580 LLVMValueRef sincof_p0
= lp_build_const_v4sf(-1.9515295891E-4);
1581 LLVMValueRef sincof_p1
= lp_build_const_v4sf(8.3321608736E-3);
1582 LLVMValueRef sincof_p2
= lp_build_const_v4sf(-1.6666654611E-1);
1585 * Evaluate the second polynom (Pi/4 <= x <= 0)
1587 * y2 = *(v4sf*)_ps_sincof_p0;
1588 * y2 = _mm_mul_ps(y2, z);
1589 * y2 = _mm_add_ps(y2, *(v4sf*)_ps_sincof_p1);
1590 * y2 = _mm_mul_ps(y2, z);
1591 * y2 = _mm_add_ps(y2, *(v4sf*)_ps_sincof_p2);
1592 * y2 = _mm_mul_ps(y2, z);
1593 * y2 = _mm_mul_ps(y2, x);
1594 * y2 = _mm_add_ps(y2, x);
1597 LLVMValueRef y2_3
= LLVMBuildFMul(b
, z
, sincof_p0
, "y2_3");
1598 LLVMValueRef y2_4
= LLVMBuildFAdd(b
, y2_3
, sincof_p1
, "y2_4");
1599 LLVMValueRef y2_5
= LLVMBuildFMul(b
, y2_4
, z
, "y2_5");
1600 LLVMValueRef y2_6
= LLVMBuildFAdd(b
, y2_5
, sincof_p2
, "y2_6");
1601 LLVMValueRef y2_7
= LLVMBuildFMul(b
, y2_6
, z
, "y2_7");
1602 LLVMValueRef y2_8
= LLVMBuildFMul(b
, y2_7
, x_3
, "y2_8");
1603 LLVMValueRef y2_9
= LLVMBuildFAdd(b
, y2_8
, x_3
, "y2_9");
1606 * select the correct result from the two polynoms
1608 * y2 = _mm_and_ps(xmm3, y2); //, xmm3);
1609 * y = _mm_andnot_ps(xmm3, y);
1610 * y = _mm_add_ps(y,y2);
1612 LLVMValueRef y2_i
= LLVMBuildBitCast(b
, y2_9
, v4si
, "y2_i");
1613 LLVMValueRef y_i
= LLVMBuildBitCast(b
, y_10
, v4si
, "y_i");
1614 LLVMValueRef y2_and
= LLVMBuildAnd(b
, y2_i
, poly_mask
, "y2_and");
1615 LLVMValueRef inv
= lp_build_const_v4si(~0);
1616 LLVMValueRef poly_mask_inv
= LLVMBuildXor(b
, poly_mask
, inv
, "poly_mask_inv");
1617 LLVMValueRef y_and
= LLVMBuildAnd(b
, y_i
, poly_mask_inv
, "y_and");
1618 LLVMValueRef y_combine
= LLVMBuildAdd(b
, y_and
, y2_and
, "y_combine");
1622 * y = _mm_xor_ps(y, sign_bit);
1624 LLVMValueRef y_sign
= LLVMBuildXor(b
, y_combine
, sign_bit_1
, "y_sin");
1625 LLVMValueRef y_result
= LLVMBuildBitCast(b
, y_sign
, v4sf
, "y_result");
1631 * Generate cos(a) using SSE2
1634 lp_build_cos(struct lp_build_context
*bld
,
1637 struct lp_type int_type
= lp_int_type(bld
->type
);
1638 LLVMBuilderRef b
= bld
->builder
;
1639 LLVMTypeRef v4sf
= LLVMVectorType(LLVMFloatType(), 4);
1640 LLVMTypeRef v4si
= LLVMVectorType(LLVMInt32Type(), 4);
1643 * take the absolute value,
1644 * x = _mm_and_ps(x, *(v4sf*)_ps_inv_sign_mask);
1647 LLVMValueRef inv_sig_mask
= lp_build_const_v4si(~0x80000000);
1648 LLVMValueRef a_v4si
= LLVMBuildBitCast(b
, a
, v4si
, "a_v4si");
1650 LLVMValueRef absi
= LLVMBuildAnd(b
, a_v4si
, inv_sig_mask
, "absi");
1651 LLVMValueRef x_abs
= LLVMBuildBitCast(b
, absi
, v4sf
, "x_abs");
1655 * y = _mm_mul_ps(x, *(v4sf*)_ps_cephes_FOPI);
1658 LLVMValueRef FOPi
= lp_build_const_v4sf(1.27323954473516);
1659 LLVMValueRef scale_y
= LLVMBuildFMul(b
, x_abs
, FOPi
, "scale_y");
1662 * store the integer part of y in mm0
1663 * emm2 = _mm_cvttps_epi32(y);
1666 LLVMValueRef emm2_i
= LLVMBuildFPToSI(b
, scale_y
, v4si
, "emm2_i");
1669 * j=(j+1) & (~1) (see the cephes sources)
1670 * emm2 = _mm_add_epi32(emm2, *(v4si*)_pi32_1);
1673 LLVMValueRef all_one
= lp_build_const_v4si(1);
1674 LLVMValueRef emm2_add
= LLVMBuildAdd(b
, emm2_i
, all_one
, "emm2_add");
1676 * emm2 = _mm_and_si128(emm2, *(v4si*)_pi32_inv1);
1678 LLVMValueRef inv_one
= lp_build_const_v4si(~1);
1679 LLVMValueRef emm2_and
= LLVMBuildAnd(b
, emm2_add
, inv_one
, "emm2_and");
1682 * y = _mm_cvtepi32_ps(emm2);
1684 LLVMValueRef y_2
= LLVMBuildSIToFP(b
, emm2_and
, v4sf
, "y_2");
1688 * emm2 = _mm_sub_epi32(emm2, *(v4si*)_pi32_2);
1690 LLVMValueRef const_2
= lp_build_const_v4si(2);
1691 LLVMValueRef emm2_2
= LLVMBuildSub(b
, emm2_and
, const_2
, "emm2_2");
1694 /* get the swap sign flag
1695 * emm0 = _mm_andnot_si128(emm2, *(v4si*)_pi32_4);
1697 LLVMValueRef inv
= lp_build_const_v4si(~0);
1698 LLVMValueRef emm0_not
= LLVMBuildXor(b
, emm2_2
, inv
, "emm0_not");
1699 LLVMValueRef pi32_4
= lp_build_const_v4si(4);
1700 LLVMValueRef emm0_and
= LLVMBuildAnd(b
, emm0_not
, pi32_4
, "emm0_and");
1703 * emm2 = _mm_slli_epi32(emm0, 29);
1705 LLVMValueRef const_29
= lp_build_const_v4si(29);
1706 LLVMValueRef sign_bit
= LLVMBuildShl(b
, emm0_and
, const_29
, "sign_bit");
1709 * get the polynom selection mask
1710 * there is one polynom for 0 <= x <= Pi/4
1711 * and another one for Pi/4<x<=Pi/2
1712 * Both branches will be computed.
1714 * emm2 = _mm_and_si128(emm2, *(v4si*)_pi32_2);
1715 * emm2 = _mm_cmpeq_epi32(emm2, _mm_setzero_si128());
1718 LLVMValueRef pi32_2
= lp_build_const_v4si(2);
1719 LLVMValueRef emm2_3
= LLVMBuildAnd(b
, emm2_2
, pi32_2
, "emm2_3");
1720 LLVMValueRef poly_mask
= lp_build_compare(b
, int_type
, PIPE_FUNC_EQUAL
,
1721 emm2_3
, lp_build_const_v4si(0));
1724 * _PS_CONST(minus_cephes_DP1, -0.78515625);
1725 * _PS_CONST(minus_cephes_DP2, -2.4187564849853515625e-4);
1726 * _PS_CONST(minus_cephes_DP3, -3.77489497744594108e-8);
1728 LLVMValueRef DP1
= lp_build_const_v4sf(-0.78515625);
1729 LLVMValueRef DP2
= lp_build_const_v4sf(-2.4187564849853515625e-4);
1730 LLVMValueRef DP3
= lp_build_const_v4sf(-3.77489497744594108e-8);
1733 * The magic pass: "Extended precision modular arithmetic"
1734 * x = ((x - y * DP1) - y * DP2) - y * DP3;
1735 * xmm1 = _mm_mul_ps(y, xmm1);
1736 * xmm2 = _mm_mul_ps(y, xmm2);
1737 * xmm3 = _mm_mul_ps(y, xmm3);
1739 LLVMValueRef xmm1
= LLVMBuildFMul(b
, y_2
, DP1
, "xmm1");
1740 LLVMValueRef xmm2
= LLVMBuildFMul(b
, y_2
, DP2
, "xmm2");
1741 LLVMValueRef xmm3
= LLVMBuildFMul(b
, y_2
, DP3
, "xmm3");
1744 * x = _mm_add_ps(x, xmm1);
1745 * x = _mm_add_ps(x, xmm2);
1746 * x = _mm_add_ps(x, xmm3);
1749 LLVMValueRef x_1
= LLVMBuildFAdd(b
, x_abs
, xmm1
, "x_1");
1750 LLVMValueRef x_2
= LLVMBuildFAdd(b
, x_1
, xmm2
, "x_2");
1751 LLVMValueRef x_3
= LLVMBuildFAdd(b
, x_2
, xmm3
, "x_3");
1754 * Evaluate the first polynom (0 <= x <= Pi/4)
1756 * z = _mm_mul_ps(x,x);
1758 LLVMValueRef z
= LLVMBuildFMul(b
, x_3
, x_3
, "z");
1761 * _PS_CONST(coscof_p0, 2.443315711809948E-005);
1762 * _PS_CONST(coscof_p1, -1.388731625493765E-003);
1763 * _PS_CONST(coscof_p2, 4.166664568298827E-002);
1765 LLVMValueRef coscof_p0
= lp_build_const_v4sf(2.443315711809948E-005);
1766 LLVMValueRef coscof_p1
= lp_build_const_v4sf(-1.388731625493765E-003);
1767 LLVMValueRef coscof_p2
= lp_build_const_v4sf(4.166664568298827E-002);
1770 * y = *(v4sf*)_ps_coscof_p0;
1771 * y = _mm_mul_ps(y, z);
1773 LLVMValueRef y_3
= LLVMBuildFMul(b
, z
, coscof_p0
, "y_3");
1774 LLVMValueRef y_4
= LLVMBuildFAdd(b
, y_3
, coscof_p1
, "y_4");
1775 LLVMValueRef y_5
= LLVMBuildFMul(b
, y_4
, z
, "y_5");
1776 LLVMValueRef y_6
= LLVMBuildFAdd(b
, y_5
, coscof_p2
, "y_6");
1777 LLVMValueRef y_7
= LLVMBuildFMul(b
, y_6
, z
, "y_7");
1778 LLVMValueRef y_8
= LLVMBuildFMul(b
, y_7
, z
, "y_8");
1782 * tmp = _mm_mul_ps(z, *(v4sf*)_ps_0p5);
1783 * y = _mm_sub_ps(y, tmp);
1784 * y = _mm_add_ps(y, *(v4sf*)_ps_1);
1786 LLVMValueRef half
= lp_build_const_v4sf(0.5);
1787 LLVMValueRef tmp
= LLVMBuildFMul(b
, z
, half
, "tmp");
1788 LLVMValueRef y_9
= LLVMBuildFSub(b
, y_8
, tmp
, "y_8");
1789 LLVMValueRef one
= lp_build_const_v4sf(1.0);
1790 LLVMValueRef y_10
= LLVMBuildFAdd(b
, y_9
, one
, "y_9");
1793 * _PS_CONST(sincof_p0, -1.9515295891E-4);
1794 * _PS_CONST(sincof_p1, 8.3321608736E-3);
1795 * _PS_CONST(sincof_p2, -1.6666654611E-1);
1797 LLVMValueRef sincof_p0
= lp_build_const_v4sf(-1.9515295891E-4);
1798 LLVMValueRef sincof_p1
= lp_build_const_v4sf(8.3321608736E-3);
1799 LLVMValueRef sincof_p2
= lp_build_const_v4sf(-1.6666654611E-1);
1802 * Evaluate the second polynom (Pi/4 <= x <= 0)
1804 * y2 = *(v4sf*)_ps_sincof_p0;
1805 * y2 = _mm_mul_ps(y2, z);
1806 * y2 = _mm_add_ps(y2, *(v4sf*)_ps_sincof_p1);
1807 * y2 = _mm_mul_ps(y2, z);
1808 * y2 = _mm_add_ps(y2, *(v4sf*)_ps_sincof_p2);
1809 * y2 = _mm_mul_ps(y2, z);
1810 * y2 = _mm_mul_ps(y2, x);
1811 * y2 = _mm_add_ps(y2, x);
1814 LLVMValueRef y2_3
= LLVMBuildFMul(b
, z
, sincof_p0
, "y2_3");
1815 LLVMValueRef y2_4
= LLVMBuildFAdd(b
, y2_3
, sincof_p1
, "y2_4");
1816 LLVMValueRef y2_5
= LLVMBuildFMul(b
, y2_4
, z
, "y2_5");
1817 LLVMValueRef y2_6
= LLVMBuildFAdd(b
, y2_5
, sincof_p2
, "y2_6");
1818 LLVMValueRef y2_7
= LLVMBuildFMul(b
, y2_6
, z
, "y2_7");
1819 LLVMValueRef y2_8
= LLVMBuildFMul(b
, y2_7
, x_3
, "y2_8");
1820 LLVMValueRef y2_9
= LLVMBuildFAdd(b
, y2_8
, x_3
, "y2_9");
1823 * select the correct result from the two polynoms
1825 * y2 = _mm_and_ps(xmm3, y2); //, xmm3);
1826 * y = _mm_andnot_ps(xmm3, y);
1827 * y = _mm_add_ps(y,y2);
1829 LLVMValueRef y2_i
= LLVMBuildBitCast(b
, y2_9
, v4si
, "y2_i");
1830 LLVMValueRef y_i
= LLVMBuildBitCast(b
, y_10
, v4si
, "y_i");
1831 LLVMValueRef y2_and
= LLVMBuildAnd(b
, y2_i
, poly_mask
, "y2_and");
1832 LLVMValueRef poly_mask_inv
= LLVMBuildXor(b
, poly_mask
, inv
, "poly_mask_inv");
1833 LLVMValueRef y_and
= LLVMBuildAnd(b
, y_i
, poly_mask_inv
, "y_and");
1834 LLVMValueRef y_combine
= LLVMBuildAdd(b
, y_and
, y2_and
, "y_combine");
1838 * y = _mm_xor_ps(y, sign_bit);
1840 LLVMValueRef y_sign
= LLVMBuildXor(b
, y_combine
, sign_bit
, "y_sin");
1841 LLVMValueRef y_result
= LLVMBuildBitCast(b
, y_sign
, v4sf
, "y_result");
1847 * Generate pow(x, y)
1850 lp_build_pow(struct lp_build_context
*bld
,
1854 /* TODO: optimize the constant case */
1855 if(LLVMIsConstant(x
) && LLVMIsConstant(y
))
1856 debug_printf("%s: inefficient/imprecise constant arithmetic\n",
1859 return lp_build_exp2(bld
, lp_build_mul(bld
, lp_build_log2(bld
, x
), y
));
1867 lp_build_exp(struct lp_build_context
*bld
,
1870 /* log2(e) = 1/log(2) */
1871 LLVMValueRef log2e
= lp_build_const_vec(bld
->type
, 1.4426950408889634);
1873 assert(lp_check_value(bld
->type
, x
));
1875 return lp_build_mul(bld
, log2e
, lp_build_exp2(bld
, x
));
1883 lp_build_log(struct lp_build_context
*bld
,
1887 LLVMValueRef log2
= lp_build_const_vec(bld
->type
, 0.69314718055994529);
1889 assert(lp_check_value(bld
->type
, x
));
1891 return lp_build_mul(bld
, log2
, lp_build_exp2(bld
, x
));
1896 * Generate polynomial.
1897 * Ex: coeffs[0] + x * coeffs[1] + x^2 * coeffs[2].
1900 lp_build_polynomial(struct lp_build_context
*bld
,
1902 const double *coeffs
,
1903 unsigned num_coeffs
)
1905 const struct lp_type type
= bld
->type
;
1906 LLVMValueRef res
= NULL
;
1909 assert(lp_check_value(bld
->type
, x
));
1911 /* TODO: optimize the constant case */
1912 if(LLVMIsConstant(x
))
1913 debug_printf("%s: inefficient/imprecise constant arithmetic\n",
1916 for (i
= num_coeffs
; i
--; ) {
1919 coeff
= lp_build_const_vec(type
, coeffs
[i
]);
1922 res
= lp_build_add(bld
, coeff
, lp_build_mul(bld
, x
, res
));
1935 * Minimax polynomial fit of 2**x, in range [0, 1[
1937 const double lp_build_exp2_polynomial
[] = {
1938 #if EXP_POLY_DEGREE == 5
1939 0.999999999690134838155,
1940 0.583974334321735217258,
1941 0.164553105719676828492,
1942 0.0292811063701710962255,
1943 0.00354944426657875141846,
1944 0.000296253726543423377365
1945 #elif EXP_POLY_DEGREE == 4
1946 1.00000001502262084505,
1947 0.563586057338685991394,
1948 0.150436017652442413623,
1949 0.0243220604213317927308,
1950 0.0025359088446580436489
1951 #elif EXP_POLY_DEGREE == 3
1952 0.999925218562710312959,
1953 0.695833540494823811697,
1954 0.226067155427249155588,
1955 0.0780245226406372992967
1956 #elif EXP_POLY_DEGREE == 2
1957 1.00172476321474503578,
1958 0.657636275736077639316,
1959 0.33718943461968720704
1967 lp_build_exp2_approx(struct lp_build_context
*bld
,
1969 LLVMValueRef
*p_exp2_int_part
,
1970 LLVMValueRef
*p_frac_part
,
1971 LLVMValueRef
*p_exp2
)
1973 const struct lp_type type
= bld
->type
;
1974 LLVMTypeRef vec_type
= lp_build_vec_type(type
);
1975 LLVMTypeRef int_vec_type
= lp_build_int_vec_type(type
);
1976 LLVMValueRef ipart
= NULL
;
1977 LLVMValueRef fpart
= NULL
;
1978 LLVMValueRef expipart
= NULL
;
1979 LLVMValueRef expfpart
= NULL
;
1980 LLVMValueRef res
= NULL
;
1982 assert(lp_check_value(bld
->type
, x
));
1984 if(p_exp2_int_part
|| p_frac_part
|| p_exp2
) {
1985 /* TODO: optimize the constant case */
1986 if(LLVMIsConstant(x
))
1987 debug_printf("%s: inefficient/imprecise constant arithmetic\n",
1990 assert(type
.floating
&& type
.width
== 32);
1992 x
= lp_build_min(bld
, x
, lp_build_const_vec(type
, 129.0));
1993 x
= lp_build_max(bld
, x
, lp_build_const_vec(type
, -126.99999));
1995 /* ipart = floor(x) */
1996 ipart
= lp_build_floor(bld
, x
);
1998 /* fpart = x - ipart */
1999 fpart
= LLVMBuildFSub(bld
->builder
, x
, ipart
, "");
2002 if(p_exp2_int_part
|| p_exp2
) {
2003 /* expipart = (float) (1 << ipart) */
2004 ipart
= LLVMBuildFPToSI(bld
->builder
, ipart
, int_vec_type
, "");
2005 expipart
= LLVMBuildAdd(bld
->builder
, ipart
, lp_build_const_int_vec(type
, 127), "");
2006 expipart
= LLVMBuildShl(bld
->builder
, expipart
, lp_build_const_int_vec(type
, 23), "");
2007 expipart
= LLVMBuildBitCast(bld
->builder
, expipart
, vec_type
, "");
2011 expfpart
= lp_build_polynomial(bld
, fpart
, lp_build_exp2_polynomial
,
2012 Elements(lp_build_exp2_polynomial
));
2014 res
= LLVMBuildFMul(bld
->builder
, expipart
, expfpart
, "");
2018 *p_exp2_int_part
= expipart
;
2021 *p_frac_part
= fpart
;
2029 lp_build_exp2(struct lp_build_context
*bld
,
2033 lp_build_exp2_approx(bld
, x
, NULL
, NULL
, &res
);
2039 * Minimax polynomial fit of log2(x)/(x - 1), for x in range [1, 2[
2040 * These coefficients can be generate with
2041 * http://www.boost.org/doc/libs/1_36_0/libs/math/doc/sf_and_dist/html/math_toolkit/toolkit/internals2/minimax.html
2043 const double lp_build_log2_polynomial
[] = {
2044 #if LOG_POLY_DEGREE == 6
2045 3.11578814719469302614,
2046 -3.32419399085241980044,
2047 2.59883907202499966007,
2048 -1.23152682416275988241,
2049 0.318212422185251071475,
2050 -0.0344359067839062357313
2051 #elif LOG_POLY_DEGREE == 5
2052 2.8882704548164776201,
2053 -2.52074962577807006663,
2054 1.48116647521213171641,
2055 -0.465725644288844778798,
2056 0.0596515482674574969533
2057 #elif LOG_POLY_DEGREE == 4
2058 2.61761038894603480148,
2059 -1.75647175389045657003,
2060 0.688243882994381274313,
2061 -0.107254423828329604454
2062 #elif LOG_POLY_DEGREE == 3
2063 2.28330284476918490682,
2064 -1.04913055217340124191,
2065 0.204446009836232697516
2073 * See http://www.devmaster.net/forums/showthread.php?p=43580
2076 lp_build_log2_approx(struct lp_build_context
*bld
,
2078 LLVMValueRef
*p_exp
,
2079 LLVMValueRef
*p_floor_log2
,
2080 LLVMValueRef
*p_log2
)
2082 const struct lp_type type
= bld
->type
;
2083 LLVMTypeRef vec_type
= lp_build_vec_type(type
);
2084 LLVMTypeRef int_vec_type
= lp_build_int_vec_type(type
);
2086 LLVMValueRef expmask
= lp_build_const_int_vec(type
, 0x7f800000);
2087 LLVMValueRef mantmask
= lp_build_const_int_vec(type
, 0x007fffff);
2088 LLVMValueRef one
= LLVMConstBitCast(bld
->one
, int_vec_type
);
2090 LLVMValueRef i
= NULL
;
2091 LLVMValueRef exp
= NULL
;
2092 LLVMValueRef mant
= NULL
;
2093 LLVMValueRef logexp
= NULL
;
2094 LLVMValueRef logmant
= NULL
;
2095 LLVMValueRef res
= NULL
;
2097 assert(lp_check_value(bld
->type
, x
));
2099 if(p_exp
|| p_floor_log2
|| p_log2
) {
2100 /* TODO: optimize the constant case */
2101 if(LLVMIsConstant(x
))
2102 debug_printf("%s: inefficient/imprecise constant arithmetic\n",
2105 assert(type
.floating
&& type
.width
== 32);
2107 i
= LLVMBuildBitCast(bld
->builder
, x
, int_vec_type
, "");
2109 /* exp = (float) exponent(x) */
2110 exp
= LLVMBuildAnd(bld
->builder
, i
, expmask
, "");
2113 if(p_floor_log2
|| p_log2
) {
2114 logexp
= LLVMBuildLShr(bld
->builder
, exp
, lp_build_const_int_vec(type
, 23), "");
2115 logexp
= LLVMBuildSub(bld
->builder
, logexp
, lp_build_const_int_vec(type
, 127), "");
2116 logexp
= LLVMBuildSIToFP(bld
->builder
, logexp
, vec_type
, "");
2120 /* mant = (float) mantissa(x) */
2121 mant
= LLVMBuildAnd(bld
->builder
, i
, mantmask
, "");
2122 mant
= LLVMBuildOr(bld
->builder
, mant
, one
, "");
2123 mant
= LLVMBuildBitCast(bld
->builder
, mant
, vec_type
, "");
2125 logmant
= lp_build_polynomial(bld
, mant
, lp_build_log2_polynomial
,
2126 Elements(lp_build_log2_polynomial
));
2128 /* This effectively increases the polynomial degree by one, but ensures that log2(1) == 0*/
2129 logmant
= LLVMBuildFMul(bld
->builder
, logmant
, LLVMBuildFSub(bld
->builder
, mant
, bld
->one
, ""), "");
2131 res
= LLVMBuildFAdd(bld
->builder
, logmant
, logexp
, "");
2135 exp
= LLVMBuildBitCast(bld
->builder
, exp
, vec_type
, "");
2140 *p_floor_log2
= logexp
;
2148 lp_build_log2(struct lp_build_context
*bld
,
2152 lp_build_log2_approx(bld
, x
, NULL
, NULL
, &res
);