1 /**************************************************************************
3 * Copyright 2009 VMware, Inc.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 **************************************************************************/
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_debug.h"
60 #include "lp_bld_arit.h"
65 * No checks for special case values of a or b = 1 or 0 are done.
68 lp_build_min_simple(struct lp_build_context
*bld
,
72 const struct lp_type type
= bld
->type
;
73 const char *intrinsic
= NULL
;
76 /* TODO: optimize the constant case */
78 if(type
.width
* type
.length
== 128) {
80 if(type
.width
== 32 && util_cpu_caps
.has_sse
)
81 intrinsic
= "llvm.x86.sse.min.ps";
82 if(type
.width
== 64 && util_cpu_caps
.has_sse2
)
83 intrinsic
= "llvm.x86.sse2.min.pd";
86 if(type
.width
== 8 && !type
.sign
&& util_cpu_caps
.has_sse2
)
87 intrinsic
= "llvm.x86.sse2.pminu.b";
88 if(type
.width
== 8 && type
.sign
&& util_cpu_caps
.has_sse4_1
)
89 intrinsic
= "llvm.x86.sse41.pminsb";
90 if(type
.width
== 16 && !type
.sign
&& util_cpu_caps
.has_sse4_1
)
91 intrinsic
= "llvm.x86.sse41.pminuw";
92 if(type
.width
== 16 && type
.sign
&& util_cpu_caps
.has_sse2
)
93 intrinsic
= "llvm.x86.sse2.pmins.w";
94 if(type
.width
== 32 && !type
.sign
&& util_cpu_caps
.has_sse4_1
)
95 intrinsic
= "llvm.x86.sse41.pminud";
96 if(type
.width
== 32 && type
.sign
&& util_cpu_caps
.has_sse4_1
)
97 intrinsic
= "llvm.x86.sse41.pminsd";
102 return lp_build_intrinsic_binary(bld
->builder
, intrinsic
, lp_build_vec_type(bld
->type
), a
, b
);
104 cond
= lp_build_cmp(bld
, PIPE_FUNC_LESS
, a
, b
);
105 return lp_build_select(bld
, cond
, a
, b
);
111 * No checks for special case values of a or b = 1 or 0 are done.
114 lp_build_max_simple(struct lp_build_context
*bld
,
118 const struct lp_type type
= bld
->type
;
119 const char *intrinsic
= NULL
;
122 /* TODO: optimize the constant case */
124 if(type
.width
* type
.length
== 128) {
126 if(type
.width
== 32 && util_cpu_caps
.has_sse
)
127 intrinsic
= "llvm.x86.sse.max.ps";
128 if(type
.width
== 64 && util_cpu_caps
.has_sse2
)
129 intrinsic
= "llvm.x86.sse2.max.pd";
132 if(type
.width
== 8 && !type
.sign
&& util_cpu_caps
.has_sse2
)
133 intrinsic
= "llvm.x86.sse2.pmaxu.b";
134 if(type
.width
== 8 && type
.sign
&& util_cpu_caps
.has_sse4_1
)
135 intrinsic
= "llvm.x86.sse41.pmaxsb";
136 if(type
.width
== 16 && !type
.sign
&& util_cpu_caps
.has_sse4_1
)
137 intrinsic
= "llvm.x86.sse41.pmaxuw";
138 if(type
.width
== 16 && type
.sign
&& util_cpu_caps
.has_sse2
)
139 intrinsic
= "llvm.x86.sse2.pmaxs.w";
140 if(type
.width
== 32 && !type
.sign
&& util_cpu_caps
.has_sse4_1
)
141 intrinsic
= "llvm.x86.sse41.pmaxud";
142 if(type
.width
== 32 && type
.sign
&& util_cpu_caps
.has_sse4_1
)
143 intrinsic
= "llvm.x86.sse41.pmaxsd";
148 return lp_build_intrinsic_binary(bld
->builder
, intrinsic
, lp_build_vec_type(bld
->type
), a
, b
);
150 cond
= lp_build_cmp(bld
, PIPE_FUNC_GREATER
, a
, b
);
151 return lp_build_select(bld
, cond
, a
, b
);
156 * Generate 1 - a, or ~a depending on bld->type.
159 lp_build_comp(struct lp_build_context
*bld
,
162 const struct lp_type type
= bld
->type
;
169 if(type
.norm
&& !type
.floating
&& !type
.fixed
&& !type
.sign
) {
170 if(LLVMIsConstant(a
))
171 return LLVMConstNot(a
);
173 return LLVMBuildNot(bld
->builder
, a
, "");
176 if(LLVMIsConstant(a
))
177 return LLVMConstSub(bld
->one
, a
);
179 return LLVMBuildSub(bld
->builder
, bld
->one
, a
, "");
187 lp_build_add(struct lp_build_context
*bld
,
191 const struct lp_type type
= bld
->type
;
198 if(a
== bld
->undef
|| b
== bld
->undef
)
202 const char *intrinsic
= NULL
;
204 if(a
== bld
->one
|| b
== bld
->one
)
207 if(util_cpu_caps
.has_sse2
&&
208 type
.width
* type
.length
== 128 &&
209 !type
.floating
&& !type
.fixed
) {
211 intrinsic
= type
.sign
? "llvm.x86.sse2.padds.b" : "llvm.x86.sse2.paddus.b";
213 intrinsic
= type
.sign
? "llvm.x86.sse2.padds.w" : "llvm.x86.sse2.paddus.w";
217 return lp_build_intrinsic_binary(bld
->builder
, intrinsic
, lp_build_vec_type(bld
->type
), a
, b
);
220 if(LLVMIsConstant(a
) && LLVMIsConstant(b
))
221 res
= LLVMConstAdd(a
, b
);
223 res
= LLVMBuildAdd(bld
->builder
, a
, b
, "");
225 /* clamp to ceiling of 1.0 */
226 if(bld
->type
.norm
&& (bld
->type
.floating
|| bld
->type
.fixed
))
227 res
= lp_build_min_simple(bld
, res
, bld
->one
);
229 /* XXX clamp to floor of -1 or 0??? */
235 /** Return the sum of the elements of a */
237 lp_build_sum_vector(struct lp_build_context
*bld
,
240 const struct lp_type type
= bld
->type
;
241 LLVMValueRef index
, res
;
248 assert(type
.length
> 1);
250 assert(!bld
->type
.norm
);
252 index
= LLVMConstInt(LLVMInt32Type(), 0, 0);
253 res
= LLVMBuildExtractElement(bld
->builder
, a
, index
, "");
255 for (i
= 1; i
< type
.length
; i
++) {
256 index
= LLVMConstInt(LLVMInt32Type(), i
, 0);
257 res
= LLVMBuildAdd(bld
->builder
, res
,
258 LLVMBuildExtractElement(bld
->builder
, a
, index
, ""),
270 lp_build_sub(struct lp_build_context
*bld
,
274 const struct lp_type type
= bld
->type
;
279 if(a
== bld
->undef
|| b
== bld
->undef
)
285 const char *intrinsic
= NULL
;
290 if(util_cpu_caps
.has_sse2
&&
291 type
.width
* type
.length
== 128 &&
292 !type
.floating
&& !type
.fixed
) {
294 intrinsic
= type
.sign
? "llvm.x86.sse2.psubs.b" : "llvm.x86.sse2.psubus.b";
296 intrinsic
= type
.sign
? "llvm.x86.sse2.psubs.w" : "llvm.x86.sse2.psubus.w";
300 return lp_build_intrinsic_binary(bld
->builder
, intrinsic
, lp_build_vec_type(bld
->type
), a
, b
);
303 if(LLVMIsConstant(a
) && LLVMIsConstant(b
))
304 res
= LLVMConstSub(a
, b
);
306 res
= LLVMBuildSub(bld
->builder
, a
, b
, "");
308 if(bld
->type
.norm
&& (bld
->type
.floating
|| bld
->type
.fixed
))
309 res
= lp_build_max_simple(bld
, res
, bld
->zero
);
316 * Normalized 8bit multiplication.
320 * makes the following approximation to the division (Sree)
322 * a*b/255 ~= (a*(b + 1)) >> 256
324 * which is the fastest method that satisfies the following OpenGL criteria
326 * 0*0 = 0 and 255*255 = 255
330 * takes the geometric series approximation to the division
332 * t/255 = (t >> 8) + (t >> 16) + (t >> 24) ..
334 * in this case just the first two terms to fit in 16bit arithmetic
336 * t/255 ~= (t + (t >> 8)) >> 8
338 * note that just by itself it doesn't satisfies the OpenGL criteria, as
339 * 255*255 = 254, so the special case b = 255 must be accounted or roundoff
342 * - geometric series plus rounding
344 * when using a geometric series division instead of truncating the result
345 * use roundoff in the approximation (Jim Blinn)
347 * t/255 ~= (t + (t >> 8) + 0x80) >> 8
349 * achieving the exact results
351 * @sa Alvy Ray Smith, Image Compositing Fundamentals, Tech Memo 4, Aug 15, 1995,
352 * ftp://ftp.alvyray.com/Acrobat/4_Comp.pdf
353 * @sa Michael Herf, The "double blend trick", May 2000,
354 * http://www.stereopsis.com/doubleblend.html
357 lp_build_mul_u8n(LLVMBuilderRef builder
,
358 struct lp_type i16_type
,
359 LLVMValueRef a
, LLVMValueRef b
)
364 c8
= lp_build_const_int_vec(i16_type
, 8);
368 /* a*b/255 ~= (a*(b + 1)) >> 256 */
369 b
= LLVMBuildAdd(builder
, b
, lp_build_const_int_vec(i16_type
, 1), "");
370 ab
= LLVMBuildMul(builder
, a
, b
, "");
374 /* ab/255 ~= (ab + (ab >> 8) + 0x80) >> 8 */
375 ab
= LLVMBuildMul(builder
, a
, b
, "");
376 ab
= LLVMBuildAdd(builder
, ab
, LLVMBuildLShr(builder
, ab
, c8
, ""), "");
377 ab
= LLVMBuildAdd(builder
, ab
, lp_build_const_int_vec(i16_type
, 0x80), "");
381 ab
= LLVMBuildLShr(builder
, ab
, c8
, "");
391 lp_build_mul(struct lp_build_context
*bld
,
395 const struct lp_type type
= bld
->type
;
407 if(a
== bld
->undef
|| b
== bld
->undef
)
410 if(!type
.floating
&& !type
.fixed
&& type
.norm
) {
411 if(type
.width
== 8) {
412 struct lp_type i16_type
= lp_wider_type(type
);
413 LLVMValueRef al
, ah
, bl
, bh
, abl
, abh
, ab
;
415 lp_build_unpack2(bld
->builder
, type
, i16_type
, a
, &al
, &ah
);
416 lp_build_unpack2(bld
->builder
, type
, i16_type
, b
, &bl
, &bh
);
418 /* PMULLW, PSRLW, PADDW */
419 abl
= lp_build_mul_u8n(bld
->builder
, i16_type
, al
, bl
);
420 abh
= lp_build_mul_u8n(bld
->builder
, i16_type
, ah
, bh
);
422 ab
= lp_build_pack2(bld
->builder
, i16_type
, type
, abl
, abh
);
432 shift
= lp_build_const_int_vec(type
, type
.width
/2);
436 if(LLVMIsConstant(a
) && LLVMIsConstant(b
)) {
437 res
= LLVMConstMul(a
, b
);
440 res
= LLVMConstAShr(res
, shift
);
442 res
= LLVMConstLShr(res
, shift
);
446 res
= LLVMBuildMul(bld
->builder
, a
, b
, "");
449 res
= LLVMBuildAShr(bld
->builder
, res
, shift
, "");
451 res
= LLVMBuildLShr(bld
->builder
, res
, shift
, "");
460 * Small vector x scale multiplication optimization.
463 lp_build_mul_imm(struct lp_build_context
*bld
,
476 return LLVMBuildNeg(bld
->builder
, a
, "");
478 if(b
== 2 && bld
->type
.floating
)
479 return lp_build_add(bld
, a
, a
);
482 unsigned shift
= ffs(b
) - 1;
484 if(bld
->type
.floating
) {
487 * Power of two multiplication by directly manipulating the mantissa.
489 * XXX: This might not be always faster, it will introduce a small error
490 * for multiplication by zero, and it will produce wrong results
493 unsigned mantissa
= lp_mantissa(bld
->type
);
494 factor
= lp_build_const_int_vec(bld
->type
, (unsigned long long)shift
<< mantissa
);
495 a
= LLVMBuildBitCast(bld
->builder
, a
, lp_build_int_vec_type(bld
->type
), "");
496 a
= LLVMBuildAdd(bld
->builder
, a
, factor
, "");
497 a
= LLVMBuildBitCast(bld
->builder
, a
, lp_build_vec_type(bld
->type
), "");
502 factor
= lp_build_const_vec(bld
->type
, shift
);
503 return LLVMBuildShl(bld
->builder
, a
, factor
, "");
507 factor
= lp_build_const_vec(bld
->type
, (double)b
);
508 return lp_build_mul(bld
, a
, factor
);
516 lp_build_div(struct lp_build_context
*bld
,
520 const struct lp_type type
= bld
->type
;
525 return lp_build_rcp(bld
, b
);
530 if(a
== bld
->undef
|| b
== bld
->undef
)
533 if(LLVMIsConstant(a
) && LLVMIsConstant(b
))
534 return LLVMConstFDiv(a
, b
);
536 if(util_cpu_caps
.has_sse
&& type
.width
== 32 && type
.length
== 4)
537 return lp_build_mul(bld
, a
, lp_build_rcp(bld
, b
));
539 return LLVMBuildFDiv(bld
->builder
, a
, b
, "");
544 * Linear interpolation.
546 * This also works for integer values with a few caveats.
548 * @sa http://www.stereopsis.com/doubleblend.html
551 lp_build_lerp(struct lp_build_context
*bld
,
559 delta
= lp_build_sub(bld
, v1
, v0
);
561 res
= lp_build_mul(bld
, x
, delta
);
563 res
= lp_build_add(bld
, v0
, res
);
566 /* XXX: This step is necessary for lerping 8bit colors stored on 16bits,
567 * but it will be wrong for other uses. Basically we need a more
568 * powerful lp_type, capable of further distinguishing the values
569 * interpretation from the value storage. */
570 res
= LLVMBuildAnd(bld
->builder
, res
, lp_build_const_int_vec(bld
->type
, (1 << bld
->type
.width
/2) - 1), "");
577 lp_build_lerp_2d(struct lp_build_context
*bld
,
585 LLVMValueRef v0
= lp_build_lerp(bld
, x
, v00
, v01
);
586 LLVMValueRef v1
= lp_build_lerp(bld
, x
, v10
, v11
);
587 return lp_build_lerp(bld
, y
, v0
, v1
);
593 * Do checks for special cases.
596 lp_build_min(struct lp_build_context
*bld
,
600 if(a
== bld
->undef
|| b
== bld
->undef
)
607 if(a
== bld
->zero
|| b
== bld
->zero
)
615 return lp_build_min_simple(bld
, a
, b
);
621 * Do checks for special cases.
624 lp_build_max(struct lp_build_context
*bld
,
628 if(a
== bld
->undef
|| b
== bld
->undef
)
635 if(a
== bld
->one
|| b
== bld
->one
)
643 return lp_build_max_simple(bld
, a
, b
);
648 * Generate clamp(a, min, max)
649 * Do checks for special cases.
652 lp_build_clamp(struct lp_build_context
*bld
,
657 a
= lp_build_min(bld
, a
, max
);
658 a
= lp_build_max(bld
, a
, min
);
667 lp_build_abs(struct lp_build_context
*bld
,
670 const struct lp_type type
= bld
->type
;
671 LLVMTypeRef vec_type
= lp_build_vec_type(type
);
677 /* Mask out the sign bit */
678 LLVMTypeRef int_vec_type
= lp_build_int_vec_type(type
);
679 unsigned long long absMask
= ~(1ULL << (type
.width
- 1));
680 LLVMValueRef mask
= lp_build_const_int_vec(type
, ((unsigned long long) absMask
));
681 a
= LLVMBuildBitCast(bld
->builder
, a
, int_vec_type
, "");
682 a
= LLVMBuildAnd(bld
->builder
, a
, mask
, "");
683 a
= LLVMBuildBitCast(bld
->builder
, a
, vec_type
, "");
687 if(type
.width
*type
.length
== 128 && util_cpu_caps
.has_ssse3
) {
690 return lp_build_intrinsic_unary(bld
->builder
, "llvm.x86.ssse3.pabs.b.128", vec_type
, a
);
692 return lp_build_intrinsic_unary(bld
->builder
, "llvm.x86.ssse3.pabs.w.128", vec_type
, a
);
694 return lp_build_intrinsic_unary(bld
->builder
, "llvm.x86.ssse3.pabs.d.128", vec_type
, a
);
698 return lp_build_max(bld
, a
, LLVMBuildNeg(bld
->builder
, a
, ""));
703 lp_build_negate(struct lp_build_context
*bld
,
706 return LLVMBuildNeg(bld
->builder
, a
, "");
710 /** Return -1, 0 or +1 depending on the sign of a */
712 lp_build_sgn(struct lp_build_context
*bld
,
715 const struct lp_type type
= bld
->type
;
719 /* Handle non-zero case */
721 /* if not zero then sign must be positive */
724 else if(type
.floating
) {
725 LLVMTypeRef vec_type
;
726 LLVMTypeRef int_type
;
730 unsigned long long maskBit
= (unsigned long long)1 << (type
.width
- 1);
732 int_type
= lp_build_int_vec_type(type
);
733 vec_type
= lp_build_vec_type(type
);
734 mask
= lp_build_const_int_vec(type
, maskBit
);
736 /* Take the sign bit and add it to 1 constant */
737 sign
= LLVMBuildBitCast(bld
->builder
, a
, int_type
, "");
738 sign
= LLVMBuildAnd(bld
->builder
, sign
, mask
, "");
739 one
= LLVMConstBitCast(bld
->one
, int_type
);
740 res
= LLVMBuildOr(bld
->builder
, sign
, one
, "");
741 res
= LLVMBuildBitCast(bld
->builder
, res
, vec_type
, "");
745 LLVMValueRef minus_one
= lp_build_const_vec(type
, -1.0);
746 cond
= lp_build_cmp(bld
, PIPE_FUNC_GREATER
, a
, bld
->zero
);
747 res
= lp_build_select(bld
, cond
, bld
->one
, minus_one
);
751 cond
= lp_build_cmp(bld
, PIPE_FUNC_EQUAL
, a
, bld
->zero
);
752 res
= lp_build_select(bld
, cond
, bld
->zero
, res
);
759 * Set the sign of float vector 'a' according to 'sign'.
760 * If sign==0, return abs(a).
761 * If sign==1, return -abs(a);
762 * Other values for sign produce undefined results.
765 lp_build_set_sign(struct lp_build_context
*bld
,
766 LLVMValueRef a
, LLVMValueRef sign
)
768 const struct lp_type type
= bld
->type
;
769 LLVMTypeRef int_vec_type
= lp_build_int_vec_type(type
);
770 LLVMTypeRef vec_type
= lp_build_vec_type(type
);
771 LLVMValueRef shift
= lp_build_const_int_vec(type
, type
.width
- 1);
772 LLVMValueRef mask
= lp_build_const_int_vec(type
,
773 ~((unsigned long long) 1 << (type
.width
- 1)));
774 LLVMValueRef val
, res
;
776 assert(type
.floating
);
778 /* val = reinterpret_cast<int>(a) */
779 val
= LLVMBuildBitCast(bld
->builder
, a
, int_vec_type
, "");
780 /* val = val & mask */
781 val
= LLVMBuildAnd(bld
->builder
, val
, mask
, "");
782 /* sign = sign << shift */
783 sign
= LLVMBuildShl(bld
->builder
, sign
, shift
, "");
784 /* res = val | sign */
785 res
= LLVMBuildOr(bld
->builder
, val
, sign
, "");
786 /* res = reinterpret_cast<float>(res) */
787 res
= LLVMBuildBitCast(bld
->builder
, res
, vec_type
, "");
794 * Convert vector of (or scalar) int to vector of (or scalar) float.
797 lp_build_int_to_float(struct lp_build_context
*bld
,
800 const struct lp_type type
= bld
->type
;
801 LLVMTypeRef vec_type
= lp_build_vec_type(type
);
803 assert(type
.floating
);
805 return LLVMBuildSIToFP(bld
->builder
, a
, vec_type
, "");
810 enum lp_build_round_sse41_mode
812 LP_BUILD_ROUND_SSE41_NEAREST
= 0,
813 LP_BUILD_ROUND_SSE41_FLOOR
= 1,
814 LP_BUILD_ROUND_SSE41_CEIL
= 2,
815 LP_BUILD_ROUND_SSE41_TRUNCATE
= 3
819 static INLINE LLVMValueRef
820 lp_build_round_sse41(struct lp_build_context
*bld
,
822 enum lp_build_round_sse41_mode mode
)
824 const struct lp_type type
= bld
->type
;
825 LLVMTypeRef vec_type
= lp_build_vec_type(type
);
826 const char *intrinsic
;
828 assert(type
.floating
);
829 assert(type
.width
*type
.length
== 128);
830 assert(lp_check_value(type
, a
));
831 assert(util_cpu_caps
.has_sse4_1
);
835 intrinsic
= "llvm.x86.sse41.round.ps";
838 intrinsic
= "llvm.x86.sse41.round.pd";
845 return lp_build_intrinsic_binary(bld
->builder
, intrinsic
, vec_type
, a
,
846 LLVMConstInt(LLVMInt32Type(), mode
, 0));
851 lp_build_trunc(struct lp_build_context
*bld
,
854 const struct lp_type type
= bld
->type
;
856 assert(type
.floating
);
857 assert(lp_check_value(type
, a
));
859 if (util_cpu_caps
.has_sse4_1
&& type
.width
*type
.length
== 128)
860 return lp_build_round_sse41(bld
, a
, LP_BUILD_ROUND_SSE41_TRUNCATE
);
862 LLVMTypeRef vec_type
= lp_build_vec_type(type
);
863 LLVMTypeRef int_vec_type
= lp_build_int_vec_type(type
);
865 res
= LLVMBuildFPToSI(bld
->builder
, a
, int_vec_type
, "");
866 res
= LLVMBuildSIToFP(bld
->builder
, res
, vec_type
, "");
873 lp_build_round(struct lp_build_context
*bld
,
876 const struct lp_type type
= bld
->type
;
878 assert(type
.floating
);
879 assert(lp_check_value(type
, a
));
881 if (util_cpu_caps
.has_sse4_1
&& type
.width
*type
.length
== 128)
882 return lp_build_round_sse41(bld
, a
, LP_BUILD_ROUND_SSE41_NEAREST
);
884 LLVMTypeRef vec_type
= lp_build_vec_type(type
);
886 res
= lp_build_iround(bld
, a
);
887 res
= LLVMBuildSIToFP(bld
->builder
, res
, vec_type
, "");
894 lp_build_floor(struct lp_build_context
*bld
,
897 const struct lp_type type
= bld
->type
;
899 assert(type
.floating
);
900 assert(lp_check_value(type
, a
));
902 if (util_cpu_caps
.has_sse4_1
&& type
.width
*type
.length
== 128)
903 return lp_build_round_sse41(bld
, a
, LP_BUILD_ROUND_SSE41_FLOOR
);
905 LLVMTypeRef vec_type
= lp_build_vec_type(type
);
907 res
= lp_build_ifloor(bld
, a
);
908 res
= LLVMBuildSIToFP(bld
->builder
, res
, vec_type
, "");
915 lp_build_ceil(struct lp_build_context
*bld
,
918 const struct lp_type type
= bld
->type
;
920 assert(type
.floating
);
921 assert(lp_check_value(type
, a
));
923 if (util_cpu_caps
.has_sse4_1
&& type
.width
*type
.length
== 128)
924 return lp_build_round_sse41(bld
, a
, LP_BUILD_ROUND_SSE41_CEIL
);
926 LLVMTypeRef vec_type
= lp_build_vec_type(type
);
928 res
= lp_build_iceil(bld
, a
);
929 res
= LLVMBuildSIToFP(bld
->builder
, res
, vec_type
, "");
936 * Return fractional part of 'a' computed as a - floor(f)
937 * Typically used in texture coord arithmetic.
940 lp_build_fract(struct lp_build_context
*bld
,
943 assert(bld
->type
.floating
);
944 return lp_build_sub(bld
, a
, lp_build_floor(bld
, a
));
949 * Convert to integer, through whichever rounding method that's fastest,
950 * typically truncating toward zero.
953 lp_build_itrunc(struct lp_build_context
*bld
,
956 const struct lp_type type
= bld
->type
;
957 LLVMTypeRef int_vec_type
= lp_build_int_vec_type(type
);
959 assert(type
.floating
);
960 assert(lp_check_value(type
, a
));
962 return LLVMBuildFPToSI(bld
->builder
, a
, int_vec_type
, "");
967 * Convert float[] to int[] with round().
970 lp_build_iround(struct lp_build_context
*bld
,
973 const struct lp_type type
= bld
->type
;
974 LLVMTypeRef int_vec_type
= lp_build_int_vec_type(type
);
977 assert(type
.floating
);
979 assert(lp_check_value(type
, a
));
981 if (util_cpu_caps
.has_sse4_1
&& type
.width
*type
.length
== 128) {
982 res
= lp_build_round_sse41(bld
, a
, LP_BUILD_ROUND_SSE41_NEAREST
);
985 LLVMTypeRef vec_type
= lp_build_vec_type(type
);
986 LLVMValueRef mask
= lp_build_const_int_vec(type
, (unsigned long long)1 << (type
.width
- 1));
991 sign
= LLVMBuildBitCast(bld
->builder
, a
, int_vec_type
, "");
992 sign
= LLVMBuildAnd(bld
->builder
, sign
, mask
, "");
995 half
= lp_build_const_vec(type
, 0.5);
996 half
= LLVMBuildBitCast(bld
->builder
, half
, int_vec_type
, "");
997 half
= LLVMBuildOr(bld
->builder
, sign
, half
, "");
998 half
= LLVMBuildBitCast(bld
->builder
, half
, vec_type
, "");
1000 res
= LLVMBuildAdd(bld
->builder
, a
, half
, "");
1003 res
= LLVMBuildFPToSI(bld
->builder
, res
, int_vec_type
, "");
1010 * Convert float[] to int[] with floor().
1013 lp_build_ifloor(struct lp_build_context
*bld
,
1016 const struct lp_type type
= bld
->type
;
1017 LLVMTypeRef int_vec_type
= lp_build_int_vec_type(type
);
1020 assert(type
.floating
);
1021 assert(lp_check_value(type
, a
));
1023 if (util_cpu_caps
.has_sse4_1
&& type
.width
*type
.length
== 128) {
1024 res
= lp_build_round_sse41(bld
, a
, LP_BUILD_ROUND_SSE41_FLOOR
);
1027 /* Take the sign bit and add it to 1 constant */
1028 LLVMTypeRef vec_type
= lp_build_vec_type(type
);
1029 unsigned mantissa
= lp_mantissa(type
);
1030 LLVMValueRef mask
= lp_build_const_int_vec(type
, (unsigned long long)1 << (type
.width
- 1));
1032 LLVMValueRef offset
;
1034 /* sign = a < 0 ? ~0 : 0 */
1035 sign
= LLVMBuildBitCast(bld
->builder
, a
, int_vec_type
, "");
1036 sign
= LLVMBuildAnd(bld
->builder
, sign
, mask
, "");
1037 sign
= LLVMBuildAShr(bld
->builder
, sign
, lp_build_const_int_vec(type
, type
.width
- 1), "");
1038 lp_build_name(sign
, "floor.sign");
1040 /* offset = -0.99999(9)f */
1041 offset
= lp_build_const_vec(type
, -(double)(((unsigned long long)1 << mantissa
) - 1)/((unsigned long long)1 << mantissa
));
1042 offset
= LLVMConstBitCast(offset
, int_vec_type
);
1044 /* offset = a < 0 ? -0.99999(9)f : 0.0f */
1045 offset
= LLVMBuildAnd(bld
->builder
, offset
, sign
, "");
1046 offset
= LLVMBuildBitCast(bld
->builder
, offset
, vec_type
, "");
1047 lp_build_name(offset
, "floor.offset");
1049 res
= LLVMBuildAdd(bld
->builder
, a
, offset
, "");
1050 lp_build_name(res
, "floor.res");
1053 res
= LLVMBuildFPToSI(bld
->builder
, res
, int_vec_type
, "");
1054 lp_build_name(res
, "floor");
1061 lp_build_iceil(struct lp_build_context
*bld
,
1064 const struct lp_type type
= bld
->type
;
1065 LLVMTypeRef int_vec_type
= lp_build_int_vec_type(type
);
1068 assert(type
.floating
);
1069 assert(lp_check_value(type
, a
));
1071 if (util_cpu_caps
.has_sse4_1
&& type
.width
*type
.length
== 128) {
1072 res
= lp_build_round_sse41(bld
, a
, LP_BUILD_ROUND_SSE41_CEIL
);
1075 /* TODO: mimic lp_build_ifloor() here */
1080 res
= LLVMBuildFPToSI(bld
->builder
, res
, int_vec_type
, "");
1087 lp_build_sqrt(struct lp_build_context
*bld
,
1090 const struct lp_type type
= bld
->type
;
1091 LLVMTypeRef vec_type
= lp_build_vec_type(type
);
1094 /* TODO: optimize the constant case */
1095 /* TODO: optimize the constant case */
1097 assert(type
.floating
);
1098 util_snprintf(intrinsic
, sizeof intrinsic
, "llvm.sqrt.v%uf%u", type
.length
, type
.width
);
1100 return lp_build_intrinsic_unary(bld
->builder
, intrinsic
, vec_type
, a
);
1105 lp_build_rcp(struct lp_build_context
*bld
,
1108 const struct lp_type type
= bld
->type
;
1117 assert(type
.floating
);
1119 if(LLVMIsConstant(a
))
1120 return LLVMConstFDiv(bld
->one
, a
);
1122 if(util_cpu_caps
.has_sse
&& type
.width
== 32 && type
.length
== 4) {
1124 * XXX: Added precision is not always necessary, so only enable this
1125 * when we have a better system in place to track minimum precision.
1130 * Do one Newton-Raphson step to improve precision:
1132 * x1 = (2 - a * rcp(a)) * rcp(a)
1135 LLVMValueRef two
= lp_build_const_vec(bld
->type
, 2.0);
1139 rcp_a
= lp_build_intrinsic_unary(bld
->builder
, "llvm.x86.sse.rcp.ps", lp_build_vec_type(type
), a
);
1141 res
= LLVMBuildMul(bld
->builder
, a
, rcp_a
, "");
1142 res
= LLVMBuildSub(bld
->builder
, two
, res
, "");
1143 res
= LLVMBuildMul(bld
->builder
, res
, rcp_a
, "");
1147 return lp_build_intrinsic_unary(bld
->builder
, "llvm.x86.sse.rcp.ps", lp_build_vec_type(type
), a
);
1151 return LLVMBuildFDiv(bld
->builder
, bld
->one
, a
, "");
1156 * Generate 1/sqrt(a)
1159 lp_build_rsqrt(struct lp_build_context
*bld
,
1162 const struct lp_type type
= bld
->type
;
1164 assert(type
.floating
);
1166 if(util_cpu_caps
.has_sse
&& type
.width
== 32 && type
.length
== 4)
1167 return lp_build_intrinsic_unary(bld
->builder
, "llvm.x86.sse.rsqrt.ps", lp_build_vec_type(type
), a
);
1169 return lp_build_rcp(bld
, lp_build_sqrt(bld
, a
));
1173 static inline LLVMValueRef
1174 lp_build_const_v4si(unsigned long value
)
1176 LLVMValueRef element
= LLVMConstInt(LLVMInt32Type(), value
, 0);
1177 LLVMValueRef elements
[4] = { element
, element
, element
, element
};
1178 return LLVMConstVector(elements
, 4);
1181 static inline LLVMValueRef
1182 lp_build_const_v4sf(float value
)
1184 LLVMValueRef element
= LLVMConstReal(LLVMFloatType(), value
);
1185 LLVMValueRef elements
[4] = { element
, element
, element
, element
};
1186 return LLVMConstVector(elements
, 4);
1191 * Generate sin(a) using SSE2
1194 lp_build_sin(struct lp_build_context
*bld
,
1197 struct lp_type int_type
= lp_int_type(bld
->type
);
1198 LLVMBuilderRef b
= bld
->builder
;
1199 LLVMTypeRef v4sf
= LLVMVectorType(LLVMFloatType(), 4);
1200 LLVMTypeRef v4si
= LLVMVectorType(LLVMInt32Type(), 4);
1203 * take the absolute value,
1204 * x = _mm_and_ps(x, *(v4sf*)_ps_inv_sign_mask);
1207 LLVMValueRef inv_sig_mask
= lp_build_const_v4si(~0x80000000);
1208 LLVMValueRef a_v4si
= LLVMBuildBitCast(b
, a
, v4si
, "a_v4si");
1210 LLVMValueRef absi
= LLVMBuildAnd(b
, a_v4si
, inv_sig_mask
, "absi");
1211 LLVMValueRef x_abs
= LLVMBuildBitCast(b
, absi
, v4sf
, "x_abs");
1214 * extract the sign bit (upper one)
1215 * sign_bit = _mm_and_ps(sign_bit, *(v4sf*)_ps_sign_mask);
1217 LLVMValueRef sig_mask
= lp_build_const_v4si(0x80000000);
1218 LLVMValueRef sign_bit_i
= LLVMBuildAnd(b
, a_v4si
, sig_mask
, "sign_bit_i");
1222 * y = _mm_mul_ps(x, *(v4sf*)_ps_cephes_FOPI);
1225 LLVMValueRef FOPi
= lp_build_const_v4sf(1.27323954473516);
1226 LLVMValueRef scale_y
= LLVMBuildMul(b
, x_abs
, FOPi
, "scale_y");
1229 * store the integer part of y in mm0
1230 * emm2 = _mm_cvttps_epi32(y);
1233 LLVMValueRef emm2_i
= LLVMBuildFPToSI(b
, scale_y
, v4si
, "emm2_i");
1236 * j=(j+1) & (~1) (see the cephes sources)
1237 * emm2 = _mm_add_epi32(emm2, *(v4si*)_pi32_1);
1240 LLVMValueRef all_one
= lp_build_const_v4si(1);
1241 LLVMValueRef emm2_add
= LLVMBuildAdd(b
, emm2_i
, all_one
, "emm2_add");
1243 * emm2 = _mm_and_si128(emm2, *(v4si*)_pi32_inv1);
1245 LLVMValueRef inv_one
= lp_build_const_v4si(~1);
1246 LLVMValueRef emm2_and
= LLVMBuildAnd(b
, emm2_add
, inv_one
, "emm2_and");
1249 * y = _mm_cvtepi32_ps(emm2);
1251 LLVMValueRef y_2
= LLVMBuildSIToFP(b
, emm2_and
, v4sf
, "y_2");
1253 /* get the swap sign flag
1254 * emm0 = _mm_and_si128(emm2, *(v4si*)_pi32_4);
1256 LLVMValueRef pi32_4
= lp_build_const_v4si(4);
1257 LLVMValueRef emm0_and
= LLVMBuildAnd(b
, emm2_add
, pi32_4
, "emm0_and");
1260 * emm2 = _mm_slli_epi32(emm0, 29);
1262 LLVMValueRef const_29
= lp_build_const_v4si(29);
1263 LLVMValueRef swap_sign_bit
= LLVMBuildShl(b
, emm0_and
, const_29
, "swap_sign_bit");
1266 * get the polynom selection mask
1267 * there is one polynom for 0 <= x <= Pi/4
1268 * and another one for Pi/4<x<=Pi/2
1269 * Both branches will be computed.
1271 * emm2 = _mm_and_si128(emm2, *(v4si*)_pi32_2);
1272 * emm2 = _mm_cmpeq_epi32(emm2, _mm_setzero_si128());
1275 LLVMValueRef pi32_2
= lp_build_const_v4si(2);
1276 LLVMValueRef emm2_3
= LLVMBuildAnd(b
, emm2_and
, pi32_2
, "emm2_3");
1277 LLVMValueRef poly_mask
= lp_build_compare(b
, int_type
, PIPE_FUNC_EQUAL
,
1278 emm2_3
, lp_build_const_v4si(0));
1280 * sign_bit = _mm_xor_ps(sign_bit, swap_sign_bit);
1282 LLVMValueRef sign_bit_1
= LLVMBuildXor(b
, sign_bit_i
, swap_sign_bit
, "sign_bit");
1285 * _PS_CONST(minus_cephes_DP1, -0.78515625);
1286 * _PS_CONST(minus_cephes_DP2, -2.4187564849853515625e-4);
1287 * _PS_CONST(minus_cephes_DP3, -3.77489497744594108e-8);
1289 LLVMValueRef DP1
= lp_build_const_v4sf(-0.78515625);
1290 LLVMValueRef DP2
= lp_build_const_v4sf(-2.4187564849853515625e-4);
1291 LLVMValueRef DP3
= lp_build_const_v4sf(-3.77489497744594108e-8);
1294 * The magic pass: "Extended precision modular arithmetic"
1295 * x = ((x - y * DP1) - y * DP2) - y * DP3;
1296 * xmm1 = _mm_mul_ps(y, xmm1);
1297 * xmm2 = _mm_mul_ps(y, xmm2);
1298 * xmm3 = _mm_mul_ps(y, xmm3);
1300 LLVMValueRef xmm1
= LLVMBuildMul(b
, y_2
, DP1
, "xmm1");
1301 LLVMValueRef xmm2
= LLVMBuildMul(b
, y_2
, DP2
, "xmm2");
1302 LLVMValueRef xmm3
= LLVMBuildMul(b
, y_2
, DP3
, "xmm3");
1305 * x = _mm_add_ps(x, xmm1);
1306 * x = _mm_add_ps(x, xmm2);
1307 * x = _mm_add_ps(x, xmm3);
1310 LLVMValueRef x_1
= LLVMBuildAdd(b
, x_abs
, xmm1
, "x_1");
1311 LLVMValueRef x_2
= LLVMBuildAdd(b
, x_1
, xmm2
, "x_2");
1312 LLVMValueRef x_3
= LLVMBuildAdd(b
, x_2
, xmm3
, "x_3");
1315 * Evaluate the first polynom (0 <= x <= Pi/4)
1317 * z = _mm_mul_ps(x,x);
1319 LLVMValueRef z
= LLVMBuildMul(b
, x_3
, x_3
, "z");
1322 * _PS_CONST(coscof_p0, 2.443315711809948E-005);
1323 * _PS_CONST(coscof_p1, -1.388731625493765E-003);
1324 * _PS_CONST(coscof_p2, 4.166664568298827E-002);
1326 LLVMValueRef coscof_p0
= lp_build_const_v4sf(2.443315711809948E-005);
1327 LLVMValueRef coscof_p1
= lp_build_const_v4sf(-1.388731625493765E-003);
1328 LLVMValueRef coscof_p2
= lp_build_const_v4sf(4.166664568298827E-002);
1331 * y = *(v4sf*)_ps_coscof_p0;
1332 * y = _mm_mul_ps(y, z);
1334 LLVMValueRef y_3
= LLVMBuildMul(b
, z
, coscof_p0
, "y_3");
1335 LLVMValueRef y_4
= LLVMBuildAdd(b
, y_3
, coscof_p1
, "y_4");
1336 LLVMValueRef y_5
= LLVMBuildMul(b
, y_4
, z
, "y_5");
1337 LLVMValueRef y_6
= LLVMBuildAdd(b
, y_5
, coscof_p2
, "y_6");
1338 LLVMValueRef y_7
= LLVMBuildMul(b
, y_6
, z
, "y_7");
1339 LLVMValueRef y_8
= LLVMBuildMul(b
, y_7
, z
, "y_8");
1343 * tmp = _mm_mul_ps(z, *(v4sf*)_ps_0p5);
1344 * y = _mm_sub_ps(y, tmp);
1345 * y = _mm_add_ps(y, *(v4sf*)_ps_1);
1347 LLVMValueRef half
= lp_build_const_v4sf(0.5);
1348 LLVMValueRef tmp
= LLVMBuildMul(b
, z
, half
, "tmp");
1349 LLVMValueRef y_9
= LLVMBuildSub(b
, y_8
, tmp
, "y_8");
1350 LLVMValueRef one
= lp_build_const_v4sf(1.0);
1351 LLVMValueRef y_10
= LLVMBuildAdd(b
, y_9
, one
, "y_9");
1354 * _PS_CONST(sincof_p0, -1.9515295891E-4);
1355 * _PS_CONST(sincof_p1, 8.3321608736E-3);
1356 * _PS_CONST(sincof_p2, -1.6666654611E-1);
1358 LLVMValueRef sincof_p0
= lp_build_const_v4sf(-1.9515295891E-4);
1359 LLVMValueRef sincof_p1
= lp_build_const_v4sf(8.3321608736E-3);
1360 LLVMValueRef sincof_p2
= lp_build_const_v4sf(-1.6666654611E-1);
1363 * Evaluate the second polynom (Pi/4 <= x <= 0)
1365 * y2 = *(v4sf*)_ps_sincof_p0;
1366 * y2 = _mm_mul_ps(y2, z);
1367 * y2 = _mm_add_ps(y2, *(v4sf*)_ps_sincof_p1);
1368 * y2 = _mm_mul_ps(y2, z);
1369 * y2 = _mm_add_ps(y2, *(v4sf*)_ps_sincof_p2);
1370 * y2 = _mm_mul_ps(y2, z);
1371 * y2 = _mm_mul_ps(y2, x);
1372 * y2 = _mm_add_ps(y2, x);
1375 LLVMValueRef y2_3
= LLVMBuildMul(b
, z
, sincof_p0
, "y2_3");
1376 LLVMValueRef y2_4
= LLVMBuildAdd(b
, y2_3
, sincof_p1
, "y2_4");
1377 LLVMValueRef y2_5
= LLVMBuildMul(b
, y2_4
, z
, "y2_5");
1378 LLVMValueRef y2_6
= LLVMBuildAdd(b
, y2_5
, sincof_p2
, "y2_6");
1379 LLVMValueRef y2_7
= LLVMBuildMul(b
, y2_6
, z
, "y2_7");
1380 LLVMValueRef y2_8
= LLVMBuildMul(b
, y2_7
, x_3
, "y2_8");
1381 LLVMValueRef y2_9
= LLVMBuildAdd(b
, y2_8
, x_3
, "y2_9");
1384 * select the correct result from the two polynoms
1386 * y2 = _mm_and_ps(xmm3, y2); //, xmm3);
1387 * y = _mm_andnot_ps(xmm3, y);
1388 * y = _mm_add_ps(y,y2);
1390 LLVMValueRef y2_i
= LLVMBuildBitCast(b
, y2_9
, v4si
, "y2_i");
1391 LLVMValueRef y_i
= LLVMBuildBitCast(b
, y_10
, v4si
, "y_i");
1392 LLVMValueRef y2_and
= LLVMBuildAnd(b
, y2_i
, poly_mask
, "y2_and");
1393 LLVMValueRef inv
= lp_build_const_v4si(~0);
1394 LLVMValueRef poly_mask_inv
= LLVMBuildXor(b
, poly_mask
, inv
, "poly_mask_inv");
1395 LLVMValueRef y_and
= LLVMBuildAnd(b
, y_i
, poly_mask_inv
, "y_and");
1396 LLVMValueRef y_combine
= LLVMBuildAdd(b
, y_and
, y2_and
, "y_combine");
1400 * y = _mm_xor_ps(y, sign_bit);
1402 LLVMValueRef y_sign
= LLVMBuildXor(b
, y_combine
, sign_bit_1
, "y_sin");
1403 LLVMValueRef y_result
= LLVMBuildBitCast(b
, y_sign
, v4sf
, "y_result");
1409 * Generate cos(a) using SSE2
1412 lp_build_cos(struct lp_build_context
*bld
,
1415 struct lp_type int_type
= lp_int_type(bld
->type
);
1416 LLVMBuilderRef b
= bld
->builder
;
1417 LLVMTypeRef v4sf
= LLVMVectorType(LLVMFloatType(), 4);
1418 LLVMTypeRef v4si
= LLVMVectorType(LLVMInt32Type(), 4);
1421 * take the absolute value,
1422 * x = _mm_and_ps(x, *(v4sf*)_ps_inv_sign_mask);
1425 LLVMValueRef inv_sig_mask
= lp_build_const_v4si(~0x80000000);
1426 LLVMValueRef a_v4si
= LLVMBuildBitCast(b
, a
, v4si
, "a_v4si");
1428 LLVMValueRef absi
= LLVMBuildAnd(b
, a_v4si
, inv_sig_mask
, "absi");
1429 LLVMValueRef x_abs
= LLVMBuildBitCast(b
, absi
, v4sf
, "x_abs");
1433 * y = _mm_mul_ps(x, *(v4sf*)_ps_cephes_FOPI);
1436 LLVMValueRef FOPi
= lp_build_const_v4sf(1.27323954473516);
1437 LLVMValueRef scale_y
= LLVMBuildMul(b
, x_abs
, FOPi
, "scale_y");
1440 * store the integer part of y in mm0
1441 * emm2 = _mm_cvttps_epi32(y);
1444 LLVMValueRef emm2_i
= LLVMBuildFPToSI(b
, scale_y
, v4si
, "emm2_i");
1447 * j=(j+1) & (~1) (see the cephes sources)
1448 * emm2 = _mm_add_epi32(emm2, *(v4si*)_pi32_1);
1451 LLVMValueRef all_one
= lp_build_const_v4si(1);
1452 LLVMValueRef emm2_add
= LLVMBuildAdd(b
, emm2_i
, all_one
, "emm2_add");
1454 * emm2 = _mm_and_si128(emm2, *(v4si*)_pi32_inv1);
1456 LLVMValueRef inv_one
= lp_build_const_v4si(~1);
1457 LLVMValueRef emm2_and
= LLVMBuildAnd(b
, emm2_add
, inv_one
, "emm2_and");
1460 * y = _mm_cvtepi32_ps(emm2);
1462 LLVMValueRef y_2
= LLVMBuildSIToFP(b
, emm2_and
, v4sf
, "y_2");
1466 * emm2 = _mm_sub_epi32(emm2, *(v4si*)_pi32_2);
1468 LLVMValueRef const_2
= lp_build_const_v4si(2);
1469 LLVMValueRef emm2_2
= LLVMBuildSub(b
, emm2_and
, const_2
, "emm2_2");
1472 /* get the swap sign flag
1473 * emm0 = _mm_andnot_si128(emm2, *(v4si*)_pi32_4);
1475 LLVMValueRef inv
= lp_build_const_v4si(~0);
1476 LLVMValueRef emm0_not
= LLVMBuildXor(b
, emm2_2
, inv
, "emm0_not");
1477 LLVMValueRef pi32_4
= lp_build_const_v4si(4);
1478 LLVMValueRef emm0_and
= LLVMBuildAnd(b
, emm0_not
, pi32_4
, "emm0_and");
1481 * emm2 = _mm_slli_epi32(emm0, 29);
1483 LLVMValueRef const_29
= lp_build_const_v4si(29);
1484 LLVMValueRef sign_bit
= LLVMBuildShl(b
, emm0_and
, const_29
, "sign_bit");
1487 * get the polynom selection mask
1488 * there is one polynom for 0 <= x <= Pi/4
1489 * and another one for Pi/4<x<=Pi/2
1490 * Both branches will be computed.
1492 * emm2 = _mm_and_si128(emm2, *(v4si*)_pi32_2);
1493 * emm2 = _mm_cmpeq_epi32(emm2, _mm_setzero_si128());
1496 LLVMValueRef pi32_2
= lp_build_const_v4si(2);
1497 LLVMValueRef emm2_3
= LLVMBuildAnd(b
, emm2_2
, pi32_2
, "emm2_3");
1498 LLVMValueRef poly_mask
= lp_build_compare(b
, int_type
, PIPE_FUNC_EQUAL
,
1499 emm2_3
, lp_build_const_v4si(0));
1502 * _PS_CONST(minus_cephes_DP1, -0.78515625);
1503 * _PS_CONST(minus_cephes_DP2, -2.4187564849853515625e-4);
1504 * _PS_CONST(minus_cephes_DP3, -3.77489497744594108e-8);
1506 LLVMValueRef DP1
= lp_build_const_v4sf(-0.78515625);
1507 LLVMValueRef DP2
= lp_build_const_v4sf(-2.4187564849853515625e-4);
1508 LLVMValueRef DP3
= lp_build_const_v4sf(-3.77489497744594108e-8);
1511 * The magic pass: "Extended precision modular arithmetic"
1512 * x = ((x - y * DP1) - y * DP2) - y * DP3;
1513 * xmm1 = _mm_mul_ps(y, xmm1);
1514 * xmm2 = _mm_mul_ps(y, xmm2);
1515 * xmm3 = _mm_mul_ps(y, xmm3);
1517 LLVMValueRef xmm1
= LLVMBuildMul(b
, y_2
, DP1
, "xmm1");
1518 LLVMValueRef xmm2
= LLVMBuildMul(b
, y_2
, DP2
, "xmm2");
1519 LLVMValueRef xmm3
= LLVMBuildMul(b
, y_2
, DP3
, "xmm3");
1522 * x = _mm_add_ps(x, xmm1);
1523 * x = _mm_add_ps(x, xmm2);
1524 * x = _mm_add_ps(x, xmm3);
1527 LLVMValueRef x_1
= LLVMBuildAdd(b
, x_abs
, xmm1
, "x_1");
1528 LLVMValueRef x_2
= LLVMBuildAdd(b
, x_1
, xmm2
, "x_2");
1529 LLVMValueRef x_3
= LLVMBuildAdd(b
, x_2
, xmm3
, "x_3");
1532 * Evaluate the first polynom (0 <= x <= Pi/4)
1534 * z = _mm_mul_ps(x,x);
1536 LLVMValueRef z
= LLVMBuildMul(b
, x_3
, x_3
, "z");
1539 * _PS_CONST(coscof_p0, 2.443315711809948E-005);
1540 * _PS_CONST(coscof_p1, -1.388731625493765E-003);
1541 * _PS_CONST(coscof_p2, 4.166664568298827E-002);
1543 LLVMValueRef coscof_p0
= lp_build_const_v4sf(2.443315711809948E-005);
1544 LLVMValueRef coscof_p1
= lp_build_const_v4sf(-1.388731625493765E-003);
1545 LLVMValueRef coscof_p2
= lp_build_const_v4sf(4.166664568298827E-002);
1548 * y = *(v4sf*)_ps_coscof_p0;
1549 * y = _mm_mul_ps(y, z);
1551 LLVMValueRef y_3
= LLVMBuildMul(b
, z
, coscof_p0
, "y_3");
1552 LLVMValueRef y_4
= LLVMBuildAdd(b
, y_3
, coscof_p1
, "y_4");
1553 LLVMValueRef y_5
= LLVMBuildMul(b
, y_4
, z
, "y_5");
1554 LLVMValueRef y_6
= LLVMBuildAdd(b
, y_5
, coscof_p2
, "y_6");
1555 LLVMValueRef y_7
= LLVMBuildMul(b
, y_6
, z
, "y_7");
1556 LLVMValueRef y_8
= LLVMBuildMul(b
, y_7
, z
, "y_8");
1560 * tmp = _mm_mul_ps(z, *(v4sf*)_ps_0p5);
1561 * y = _mm_sub_ps(y, tmp);
1562 * y = _mm_add_ps(y, *(v4sf*)_ps_1);
1564 LLVMValueRef half
= lp_build_const_v4sf(0.5);
1565 LLVMValueRef tmp
= LLVMBuildMul(b
, z
, half
, "tmp");
1566 LLVMValueRef y_9
= LLVMBuildSub(b
, y_8
, tmp
, "y_8");
1567 LLVMValueRef one
= lp_build_const_v4sf(1.0);
1568 LLVMValueRef y_10
= LLVMBuildAdd(b
, y_9
, one
, "y_9");
1571 * _PS_CONST(sincof_p0, -1.9515295891E-4);
1572 * _PS_CONST(sincof_p1, 8.3321608736E-3);
1573 * _PS_CONST(sincof_p2, -1.6666654611E-1);
1575 LLVMValueRef sincof_p0
= lp_build_const_v4sf(-1.9515295891E-4);
1576 LLVMValueRef sincof_p1
= lp_build_const_v4sf(8.3321608736E-3);
1577 LLVMValueRef sincof_p2
= lp_build_const_v4sf(-1.6666654611E-1);
1580 * Evaluate the second polynom (Pi/4 <= x <= 0)
1582 * y2 = *(v4sf*)_ps_sincof_p0;
1583 * y2 = _mm_mul_ps(y2, z);
1584 * y2 = _mm_add_ps(y2, *(v4sf*)_ps_sincof_p1);
1585 * y2 = _mm_mul_ps(y2, z);
1586 * y2 = _mm_add_ps(y2, *(v4sf*)_ps_sincof_p2);
1587 * y2 = _mm_mul_ps(y2, z);
1588 * y2 = _mm_mul_ps(y2, x);
1589 * y2 = _mm_add_ps(y2, x);
1592 LLVMValueRef y2_3
= LLVMBuildMul(b
, z
, sincof_p0
, "y2_3");
1593 LLVMValueRef y2_4
= LLVMBuildAdd(b
, y2_3
, sincof_p1
, "y2_4");
1594 LLVMValueRef y2_5
= LLVMBuildMul(b
, y2_4
, z
, "y2_5");
1595 LLVMValueRef y2_6
= LLVMBuildAdd(b
, y2_5
, sincof_p2
, "y2_6");
1596 LLVMValueRef y2_7
= LLVMBuildMul(b
, y2_6
, z
, "y2_7");
1597 LLVMValueRef y2_8
= LLVMBuildMul(b
, y2_7
, x_3
, "y2_8");
1598 LLVMValueRef y2_9
= LLVMBuildAdd(b
, y2_8
, x_3
, "y2_9");
1601 * select the correct result from the two polynoms
1603 * y2 = _mm_and_ps(xmm3, y2); //, xmm3);
1604 * y = _mm_andnot_ps(xmm3, y);
1605 * y = _mm_add_ps(y,y2);
1607 LLVMValueRef y2_i
= LLVMBuildBitCast(b
, y2_9
, v4si
, "y2_i");
1608 LLVMValueRef y_i
= LLVMBuildBitCast(b
, y_10
, v4si
, "y_i");
1609 LLVMValueRef y2_and
= LLVMBuildAnd(b
, y2_i
, poly_mask
, "y2_and");
1610 LLVMValueRef poly_mask_inv
= LLVMBuildXor(b
, poly_mask
, inv
, "poly_mask_inv");
1611 LLVMValueRef y_and
= LLVMBuildAnd(b
, y_i
, poly_mask_inv
, "y_and");
1612 LLVMValueRef y_combine
= LLVMBuildAdd(b
, y_and
, y2_and
, "y_combine");
1616 * y = _mm_xor_ps(y, sign_bit);
1618 LLVMValueRef y_sign
= LLVMBuildXor(b
, y_combine
, sign_bit
, "y_sin");
1619 LLVMValueRef y_result
= LLVMBuildBitCast(b
, y_sign
, v4sf
, "y_result");
1625 * Generate pow(x, y)
1628 lp_build_pow(struct lp_build_context
*bld
,
1632 /* TODO: optimize the constant case */
1633 if(LLVMIsConstant(x
) && LLVMIsConstant(y
))
1634 debug_printf("%s: inefficient/imprecise constant arithmetic\n",
1637 return lp_build_exp2(bld
, lp_build_mul(bld
, lp_build_log2(bld
, x
), y
));
1645 lp_build_exp(struct lp_build_context
*bld
,
1648 /* log2(e) = 1/log(2) */
1649 LLVMValueRef log2e
= lp_build_const_vec(bld
->type
, 1.4426950408889634);
1651 return lp_build_mul(bld
, log2e
, lp_build_exp2(bld
, x
));
1659 lp_build_log(struct lp_build_context
*bld
,
1663 LLVMValueRef log2
= lp_build_const_vec(bld
->type
, 0.69314718055994529);
1665 return lp_build_mul(bld
, log2
, lp_build_exp2(bld
, x
));
1669 #define EXP_POLY_DEGREE 3
1670 #define LOG_POLY_DEGREE 5
1674 * Generate polynomial.
1675 * Ex: coeffs[0] + x * coeffs[1] + x^2 * coeffs[2].
1678 lp_build_polynomial(struct lp_build_context
*bld
,
1680 const double *coeffs
,
1681 unsigned num_coeffs
)
1683 const struct lp_type type
= bld
->type
;
1684 LLVMValueRef res
= NULL
;
1687 /* TODO: optimize the constant case */
1688 if(LLVMIsConstant(x
))
1689 debug_printf("%s: inefficient/imprecise constant arithmetic\n",
1692 for (i
= num_coeffs
; i
--; ) {
1695 coeff
= lp_build_const_vec(type
, coeffs
[i
]);
1698 res
= lp_build_add(bld
, coeff
, lp_build_mul(bld
, x
, res
));
1711 * Minimax polynomial fit of 2**x, in range [0, 1[
1713 const double lp_build_exp2_polynomial
[] = {
1714 #if EXP_POLY_DEGREE == 5
1715 0.999999999690134838155,
1716 0.583974334321735217258,
1717 0.164553105719676828492,
1718 0.0292811063701710962255,
1719 0.00354944426657875141846,
1720 0.000296253726543423377365
1721 #elif EXP_POLY_DEGREE == 4
1722 1.00000001502262084505,
1723 0.563586057338685991394,
1724 0.150436017652442413623,
1725 0.0243220604213317927308,
1726 0.0025359088446580436489
1727 #elif EXP_POLY_DEGREE == 3
1728 0.999925218562710312959,
1729 0.695833540494823811697,
1730 0.226067155427249155588,
1731 0.0780245226406372992967
1732 #elif EXP_POLY_DEGREE == 2
1733 1.00172476321474503578,
1734 0.657636275736077639316,
1735 0.33718943461968720704
1743 lp_build_exp2_approx(struct lp_build_context
*bld
,
1745 LLVMValueRef
*p_exp2_int_part
,
1746 LLVMValueRef
*p_frac_part
,
1747 LLVMValueRef
*p_exp2
)
1749 const struct lp_type type
= bld
->type
;
1750 LLVMTypeRef vec_type
= lp_build_vec_type(type
);
1751 LLVMTypeRef int_vec_type
= lp_build_int_vec_type(type
);
1752 LLVMValueRef ipart
= NULL
;
1753 LLVMValueRef fpart
= NULL
;
1754 LLVMValueRef expipart
= NULL
;
1755 LLVMValueRef expfpart
= NULL
;
1756 LLVMValueRef res
= NULL
;
1758 if(p_exp2_int_part
|| p_frac_part
|| p_exp2
) {
1759 /* TODO: optimize the constant case */
1760 if(LLVMIsConstant(x
))
1761 debug_printf("%s: inefficient/imprecise constant arithmetic\n",
1764 assert(type
.floating
&& type
.width
== 32);
1766 x
= lp_build_min(bld
, x
, lp_build_const_vec(type
, 129.0));
1767 x
= lp_build_max(bld
, x
, lp_build_const_vec(type
, -126.99999));
1769 /* ipart = floor(x) */
1770 ipart
= lp_build_floor(bld
, x
);
1772 /* fpart = x - ipart */
1773 fpart
= LLVMBuildSub(bld
->builder
, x
, ipart
, "");
1776 if(p_exp2_int_part
|| p_exp2
) {
1777 /* expipart = (float) (1 << ipart) */
1778 ipart
= LLVMBuildFPToSI(bld
->builder
, ipart
, int_vec_type
, "");
1779 expipart
= LLVMBuildAdd(bld
->builder
, ipart
, lp_build_const_int_vec(type
, 127), "");
1780 expipart
= LLVMBuildShl(bld
->builder
, expipart
, lp_build_const_int_vec(type
, 23), "");
1781 expipart
= LLVMBuildBitCast(bld
->builder
, expipart
, vec_type
, "");
1785 expfpart
= lp_build_polynomial(bld
, fpart
, lp_build_exp2_polynomial
,
1786 Elements(lp_build_exp2_polynomial
));
1788 res
= LLVMBuildMul(bld
->builder
, expipart
, expfpart
, "");
1792 *p_exp2_int_part
= expipart
;
1795 *p_frac_part
= fpart
;
1803 lp_build_exp2(struct lp_build_context
*bld
,
1807 lp_build_exp2_approx(bld
, x
, NULL
, NULL
, &res
);
1813 * Minimax polynomial fit of log2(x)/(x - 1), for x in range [1, 2[
1814 * These coefficients can be generate with
1815 * http://www.boost.org/doc/libs/1_36_0/libs/math/doc/sf_and_dist/html/math_toolkit/toolkit/internals2/minimax.html
1817 const double lp_build_log2_polynomial
[] = {
1818 #if LOG_POLY_DEGREE == 6
1819 3.11578814719469302614,
1820 -3.32419399085241980044,
1821 2.59883907202499966007,
1822 -1.23152682416275988241,
1823 0.318212422185251071475,
1824 -0.0344359067839062357313
1825 #elif LOG_POLY_DEGREE == 5
1826 2.8882704548164776201,
1827 -2.52074962577807006663,
1828 1.48116647521213171641,
1829 -0.465725644288844778798,
1830 0.0596515482674574969533
1831 #elif LOG_POLY_DEGREE == 4
1832 2.61761038894603480148,
1833 -1.75647175389045657003,
1834 0.688243882994381274313,
1835 -0.107254423828329604454
1836 #elif LOG_POLY_DEGREE == 3
1837 2.28330284476918490682,
1838 -1.04913055217340124191,
1839 0.204446009836232697516
1847 * See http://www.devmaster.net/forums/showthread.php?p=43580
1850 lp_build_log2_approx(struct lp_build_context
*bld
,
1852 LLVMValueRef
*p_exp
,
1853 LLVMValueRef
*p_floor_log2
,
1854 LLVMValueRef
*p_log2
)
1856 const struct lp_type type
= bld
->type
;
1857 LLVMTypeRef vec_type
= lp_build_vec_type(type
);
1858 LLVMTypeRef int_vec_type
= lp_build_int_vec_type(type
);
1860 LLVMValueRef expmask
= lp_build_const_int_vec(type
, 0x7f800000);
1861 LLVMValueRef mantmask
= lp_build_const_int_vec(type
, 0x007fffff);
1862 LLVMValueRef one
= LLVMConstBitCast(bld
->one
, int_vec_type
);
1864 LLVMValueRef i
= NULL
;
1865 LLVMValueRef exp
= NULL
;
1866 LLVMValueRef mant
= NULL
;
1867 LLVMValueRef logexp
= NULL
;
1868 LLVMValueRef logmant
= NULL
;
1869 LLVMValueRef res
= NULL
;
1871 if(p_exp
|| p_floor_log2
|| p_log2
) {
1872 /* TODO: optimize the constant case */
1873 if(LLVMIsConstant(x
))
1874 debug_printf("%s: inefficient/imprecise constant arithmetic\n",
1877 assert(type
.floating
&& type
.width
== 32);
1879 i
= LLVMBuildBitCast(bld
->builder
, x
, int_vec_type
, "");
1881 /* exp = (float) exponent(x) */
1882 exp
= LLVMBuildAnd(bld
->builder
, i
, expmask
, "");
1885 if(p_floor_log2
|| p_log2
) {
1886 logexp
= LLVMBuildLShr(bld
->builder
, exp
, lp_build_const_int_vec(type
, 23), "");
1887 logexp
= LLVMBuildSub(bld
->builder
, logexp
, lp_build_const_int_vec(type
, 127), "");
1888 logexp
= LLVMBuildSIToFP(bld
->builder
, logexp
, vec_type
, "");
1892 /* mant = (float) mantissa(x) */
1893 mant
= LLVMBuildAnd(bld
->builder
, i
, mantmask
, "");
1894 mant
= LLVMBuildOr(bld
->builder
, mant
, one
, "");
1895 mant
= LLVMBuildBitCast(bld
->builder
, mant
, vec_type
, "");
1897 logmant
= lp_build_polynomial(bld
, mant
, lp_build_log2_polynomial
,
1898 Elements(lp_build_log2_polynomial
));
1900 /* This effectively increases the polynomial degree by one, but ensures that log2(1) == 0*/
1901 logmant
= LLVMBuildMul(bld
->builder
, logmant
, LLVMBuildSub(bld
->builder
, mant
, bld
->one
, ""), "");
1903 res
= LLVMBuildAdd(bld
->builder
, logmant
, logexp
, "");
1907 exp
= LLVMBuildBitCast(bld
->builder
, exp
, vec_type
, "");
1912 *p_floor_log2
= logexp
;
1920 lp_build_log2(struct lp_build_context
*bld
,
1924 lp_build_log2_approx(bld
, x
, NULL
, NULL
, &res
);