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 if (type
.length
== 1) {
679 LLVMTypeRef int_type
= LLVMIntType(type
.width
);
680 LLVMTypeRef float_type
= LLVMFloatType();
681 unsigned long long absMask
= ~(1ULL << (type
.width
- 1));
682 LLVMValueRef mask
= LLVMConstInt(int_type
, absMask
, 0);
683 a
= LLVMBuildBitCast(bld
->builder
, a
, int_type
, "");
684 a
= LLVMBuildAnd(bld
->builder
, a
, mask
, "");
685 a
= LLVMBuildBitCast(bld
->builder
, a
, float_type
, "");
689 /* vector of floats */
690 LLVMTypeRef int_vec_type
= lp_build_int_vec_type(type
);
691 unsigned long long absMask
= ~(1ULL << (type
.width
- 1));
692 LLVMValueRef mask
= lp_build_const_int_vec(type
, ((unsigned long long) absMask
));
693 a
= LLVMBuildBitCast(bld
->builder
, a
, int_vec_type
, "");
694 a
= LLVMBuildAnd(bld
->builder
, a
, mask
, "");
695 a
= LLVMBuildBitCast(bld
->builder
, a
, vec_type
, "");
700 if(type
.width
*type
.length
== 128 && util_cpu_caps
.has_ssse3
) {
703 return lp_build_intrinsic_unary(bld
->builder
, "llvm.x86.ssse3.pabs.b.128", vec_type
, a
);
705 return lp_build_intrinsic_unary(bld
->builder
, "llvm.x86.ssse3.pabs.w.128", vec_type
, a
);
707 return lp_build_intrinsic_unary(bld
->builder
, "llvm.x86.ssse3.pabs.d.128", vec_type
, a
);
711 return lp_build_max(bld
, a
, LLVMBuildNeg(bld
->builder
, a
, ""));
716 lp_build_negate(struct lp_build_context
*bld
,
719 return LLVMBuildNeg(bld
->builder
, a
, "");
723 /** Return -1, 0 or +1 depending on the sign of a */
725 lp_build_sgn(struct lp_build_context
*bld
,
728 const struct lp_type type
= bld
->type
;
732 /* Handle non-zero case */
734 /* if not zero then sign must be positive */
737 else if(type
.floating
) {
738 LLVMTypeRef vec_type
;
739 LLVMTypeRef int_type
;
743 unsigned long long maskBit
= (unsigned long long)1 << (type
.width
- 1);
745 if (type
.length
== 1) {
746 int_type
= lp_build_int_elem_type(type
);
747 vec_type
= lp_build_elem_type(type
);
748 mask
= LLVMConstInt(int_type
, maskBit
, 0);
752 int_type
= lp_build_int_vec_type(type
);
753 vec_type
= lp_build_vec_type(type
);
754 mask
= lp_build_const_int_vec(type
, maskBit
);
757 /* Take the sign bit and add it to 1 constant */
758 sign
= LLVMBuildBitCast(bld
->builder
, a
, int_type
, "");
759 sign
= LLVMBuildAnd(bld
->builder
, sign
, mask
, "");
760 one
= LLVMConstBitCast(bld
->one
, int_type
);
761 res
= LLVMBuildOr(bld
->builder
, sign
, one
, "");
762 res
= LLVMBuildBitCast(bld
->builder
, res
, vec_type
, "");
766 LLVMValueRef minus_one
= lp_build_const_vec(type
, -1.0);
767 cond
= lp_build_cmp(bld
, PIPE_FUNC_GREATER
, a
, bld
->zero
);
768 res
= lp_build_select(bld
, cond
, bld
->one
, minus_one
);
772 cond
= lp_build_cmp(bld
, PIPE_FUNC_EQUAL
, a
, bld
->zero
);
773 res
= lp_build_select(bld
, cond
, bld
->zero
, res
);
780 * Set the sign of float vector 'a' according to 'sign'.
781 * If sign==0, return abs(a).
782 * If sign==1, return -abs(a);
783 * Other values for sign produce undefined results.
786 lp_build_set_sign(struct lp_build_context
*bld
,
787 LLVMValueRef a
, LLVMValueRef sign
)
789 const struct lp_type type
= bld
->type
;
790 LLVMTypeRef int_vec_type
= lp_build_int_vec_type(type
);
791 LLVMTypeRef vec_type
= lp_build_vec_type(type
);
792 LLVMValueRef shift
= lp_build_const_int_vec(type
, type
.width
- 1);
793 LLVMValueRef mask
= lp_build_const_int_vec(type
,
794 ~((unsigned long long) 1 << (type
.width
- 1)));
795 LLVMValueRef val
, res
;
797 assert(type
.floating
);
799 /* val = reinterpret_cast<int>(a) */
800 val
= LLVMBuildBitCast(bld
->builder
, a
, int_vec_type
, "");
801 /* val = val & mask */
802 val
= LLVMBuildAnd(bld
->builder
, val
, mask
, "");
803 /* sign = sign << shift */
804 sign
= LLVMBuildShl(bld
->builder
, sign
, shift
, "");
805 /* res = val | sign */
806 res
= LLVMBuildOr(bld
->builder
, val
, sign
, "");
807 /* res = reinterpret_cast<float>(res) */
808 res
= LLVMBuildBitCast(bld
->builder
, res
, vec_type
, "");
815 * Convert vector of (or scalar) int to vector of (or scalar) float.
818 lp_build_int_to_float(struct lp_build_context
*bld
,
821 const struct lp_type type
= bld
->type
;
823 assert(type
.floating
);
824 /*assert(lp_check_value(type, a));*/
826 if (type
.length
== 1) {
827 LLVMTypeRef float_type
= LLVMFloatType();
828 return LLVMBuildSIToFP(bld
->builder
, a
, float_type
, "");
831 LLVMTypeRef vec_type
= lp_build_vec_type(type
);
832 /*LLVMTypeRef int_vec_type = lp_build_int_vec_type(type);*/
834 res
= LLVMBuildSIToFP(bld
->builder
, a
, vec_type
, "");
841 enum lp_build_round_sse41_mode
843 LP_BUILD_ROUND_SSE41_NEAREST
= 0,
844 LP_BUILD_ROUND_SSE41_FLOOR
= 1,
845 LP_BUILD_ROUND_SSE41_CEIL
= 2,
846 LP_BUILD_ROUND_SSE41_TRUNCATE
= 3
850 static INLINE LLVMValueRef
851 lp_build_round_sse41(struct lp_build_context
*bld
,
853 enum lp_build_round_sse41_mode mode
)
855 const struct lp_type type
= bld
->type
;
856 LLVMTypeRef vec_type
= lp_build_vec_type(type
);
857 const char *intrinsic
;
859 assert(type
.floating
);
860 assert(type
.width
*type
.length
== 128);
861 assert(lp_check_value(type
, a
));
862 assert(util_cpu_caps
.has_sse4_1
);
866 intrinsic
= "llvm.x86.sse41.round.ps";
869 intrinsic
= "llvm.x86.sse41.round.pd";
876 return lp_build_intrinsic_binary(bld
->builder
, intrinsic
, vec_type
, a
,
877 LLVMConstInt(LLVMInt32Type(), mode
, 0));
882 lp_build_trunc(struct lp_build_context
*bld
,
885 const struct lp_type type
= bld
->type
;
887 assert(type
.floating
);
888 assert(lp_check_value(type
, a
));
890 if(util_cpu_caps
.has_sse4_1
)
891 return lp_build_round_sse41(bld
, a
, LP_BUILD_ROUND_SSE41_TRUNCATE
);
893 LLVMTypeRef vec_type
= lp_build_vec_type(type
);
894 LLVMTypeRef int_vec_type
= lp_build_int_vec_type(type
);
896 res
= LLVMBuildFPToSI(bld
->builder
, a
, int_vec_type
, "");
897 res
= LLVMBuildSIToFP(bld
->builder
, res
, vec_type
, "");
904 lp_build_round(struct lp_build_context
*bld
,
907 const struct lp_type type
= bld
->type
;
909 assert(type
.floating
);
910 assert(lp_check_value(type
, a
));
912 if(util_cpu_caps
.has_sse4_1
)
913 return lp_build_round_sse41(bld
, a
, LP_BUILD_ROUND_SSE41_NEAREST
);
915 LLVMTypeRef vec_type
= lp_build_vec_type(type
);
917 res
= lp_build_iround(bld
, a
);
918 res
= LLVMBuildSIToFP(bld
->builder
, res
, vec_type
, "");
925 lp_build_floor(struct lp_build_context
*bld
,
928 const struct lp_type type
= bld
->type
;
930 assert(type
.floating
);
932 if (type
.length
== 1) {
934 res
= lp_build_ifloor(bld
, a
);
935 res
= LLVMBuildSIToFP(bld
->builder
, res
, LLVMFloatType(), "");
939 if(util_cpu_caps
.has_sse4_1
)
940 return lp_build_round_sse41(bld
, a
, LP_BUILD_ROUND_SSE41_FLOOR
);
942 LLVMTypeRef vec_type
= lp_build_vec_type(type
);
944 res
= lp_build_ifloor(bld
, a
);
945 res
= LLVMBuildSIToFP(bld
->builder
, res
, vec_type
, "");
952 lp_build_ceil(struct lp_build_context
*bld
,
955 const struct lp_type type
= bld
->type
;
957 assert(type
.floating
);
958 assert(lp_check_value(type
, a
));
960 if(util_cpu_caps
.has_sse4_1
)
961 return lp_build_round_sse41(bld
, a
, LP_BUILD_ROUND_SSE41_CEIL
);
963 LLVMTypeRef vec_type
= lp_build_vec_type(type
);
965 res
= lp_build_iceil(bld
, a
);
966 res
= LLVMBuildSIToFP(bld
->builder
, res
, vec_type
, "");
973 * Return fractional part of 'a' computed as a - floor(f)
974 * Typically used in texture coord arithmetic.
977 lp_build_fract(struct lp_build_context
*bld
,
980 assert(bld
->type
.floating
);
981 return lp_build_sub(bld
, a
, lp_build_floor(bld
, a
));
986 * Convert to integer, through whichever rounding method that's fastest,
987 * typically truncating toward zero.
990 lp_build_itrunc(struct lp_build_context
*bld
,
993 const struct lp_type type
= bld
->type
;
995 assert(type
.floating
);
997 if (type
.length
== 1) {
998 LLVMTypeRef int_type
= LLVMIntType(type
.width
);
999 return LLVMBuildFPToSI(bld
->builder
, a
, int_type
, "");
1002 LLVMTypeRef int_vec_type
= lp_build_int_vec_type(type
);
1003 assert(lp_check_value(type
, a
));
1004 return LLVMBuildFPToSI(bld
->builder
, a
, int_vec_type
, "");
1010 * Convert float[] to int[] with round().
1013 lp_build_iround(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
);
1022 if (type
.length
== 1) {
1023 /* scalar float to int */
1024 LLVMTypeRef int_type
= LLVMIntType(type
.width
);
1025 /* XXX we want rounding here! */
1026 res
= LLVMBuildFPToSI(bld
->builder
, a
, int_type
, "");
1030 assert(lp_check_value(type
, a
));
1032 if(util_cpu_caps
.has_sse4_1
) {
1033 res
= lp_build_round_sse41(bld
, a
, LP_BUILD_ROUND_SSE41_NEAREST
);
1036 LLVMTypeRef vec_type
= lp_build_vec_type(type
);
1037 LLVMValueRef mask
= lp_build_const_int_vec(type
, (unsigned long long)1 << (type
.width
- 1));
1042 sign
= LLVMBuildBitCast(bld
->builder
, a
, int_vec_type
, "");
1043 sign
= LLVMBuildAnd(bld
->builder
, sign
, mask
, "");
1046 half
= lp_build_const_vec(type
, 0.5);
1047 half
= LLVMBuildBitCast(bld
->builder
, half
, int_vec_type
, "");
1048 half
= LLVMBuildOr(bld
->builder
, sign
, half
, "");
1049 half
= LLVMBuildBitCast(bld
->builder
, half
, vec_type
, "");
1051 res
= LLVMBuildAdd(bld
->builder
, a
, half
, "");
1054 res
= LLVMBuildFPToSI(bld
->builder
, res
, int_vec_type
, "");
1061 * Convert float[] to int[] with floor().
1064 lp_build_ifloor(struct lp_build_context
*bld
,
1067 const struct lp_type type
= bld
->type
;
1068 LLVMTypeRef int_vec_type
= lp_build_int_vec_type(type
);
1071 assert(type
.floating
);
1073 if (type
.length
== 1) {
1074 /* scalar float to int */
1075 LLVMTypeRef int_type
= LLVMIntType(type
.width
);
1076 res
= LLVMBuildFPToSI(bld
->builder
, a
, int_type
, "");
1080 assert(lp_check_value(type
, a
));
1082 if(util_cpu_caps
.has_sse4_1
) {
1083 res
= lp_build_round_sse41(bld
, a
, LP_BUILD_ROUND_SSE41_FLOOR
);
1086 /* Take the sign bit and add it to 1 constant */
1087 LLVMTypeRef vec_type
= lp_build_vec_type(type
);
1088 unsigned mantissa
= lp_mantissa(type
);
1089 LLVMValueRef mask
= lp_build_const_int_vec(type
, (unsigned long long)1 << (type
.width
- 1));
1091 LLVMValueRef offset
;
1093 /* sign = a < 0 ? ~0 : 0 */
1094 sign
= LLVMBuildBitCast(bld
->builder
, a
, int_vec_type
, "");
1095 sign
= LLVMBuildAnd(bld
->builder
, sign
, mask
, "");
1096 sign
= LLVMBuildAShr(bld
->builder
, sign
, lp_build_const_int_vec(type
, type
.width
- 1), "");
1097 lp_build_name(sign
, "floor.sign");
1099 /* offset = -0.99999(9)f */
1100 offset
= lp_build_const_vec(type
, -(double)(((unsigned long long)1 << mantissa
) - 1)/((unsigned long long)1 << mantissa
));
1101 offset
= LLVMConstBitCast(offset
, int_vec_type
);
1103 /* offset = a < 0 ? -0.99999(9)f : 0.0f */
1104 offset
= LLVMBuildAnd(bld
->builder
, offset
, sign
, "");
1105 offset
= LLVMBuildBitCast(bld
->builder
, offset
, vec_type
, "");
1106 lp_build_name(offset
, "floor.offset");
1108 res
= LLVMBuildAdd(bld
->builder
, a
, offset
, "");
1109 lp_build_name(res
, "floor.res");
1112 res
= LLVMBuildFPToSI(bld
->builder
, res
, int_vec_type
, "");
1113 lp_build_name(res
, "floor");
1120 lp_build_iceil(struct lp_build_context
*bld
,
1123 const struct lp_type type
= bld
->type
;
1124 LLVMTypeRef int_vec_type
= lp_build_int_vec_type(type
);
1127 assert(type
.floating
);
1128 assert(lp_check_value(type
, a
));
1130 if(util_cpu_caps
.has_sse4_1
) {
1131 res
= lp_build_round_sse41(bld
, a
, LP_BUILD_ROUND_SSE41_CEIL
);
1138 res
= LLVMBuildFPToSI(bld
->builder
, res
, int_vec_type
, "");
1145 lp_build_sqrt(struct lp_build_context
*bld
,
1148 const struct lp_type type
= bld
->type
;
1149 LLVMTypeRef vec_type
= lp_build_vec_type(type
);
1152 /* TODO: optimize the constant case */
1153 /* TODO: optimize the constant case */
1155 assert(type
.floating
);
1156 util_snprintf(intrinsic
, sizeof intrinsic
, "llvm.sqrt.v%uf%u", type
.length
, type
.width
);
1158 return lp_build_intrinsic_unary(bld
->builder
, intrinsic
, vec_type
, a
);
1163 lp_build_rcp(struct lp_build_context
*bld
,
1166 const struct lp_type type
= bld
->type
;
1175 assert(type
.floating
);
1177 if(LLVMIsConstant(a
))
1178 return LLVMConstFDiv(bld
->one
, a
);
1180 if(util_cpu_caps
.has_sse
&& type
.width
== 32 && type
.length
== 4)
1181 /* FIXME: improve precision */
1182 return lp_build_intrinsic_unary(bld
->builder
, "llvm.x86.sse.rcp.ps", lp_build_vec_type(type
), a
);
1184 return LLVMBuildFDiv(bld
->builder
, bld
->one
, a
, "");
1189 * Generate 1/sqrt(a)
1192 lp_build_rsqrt(struct lp_build_context
*bld
,
1195 const struct lp_type type
= bld
->type
;
1197 assert(type
.floating
);
1199 if(util_cpu_caps
.has_sse
&& type
.width
== 32 && type
.length
== 4)
1200 return lp_build_intrinsic_unary(bld
->builder
, "llvm.x86.sse.rsqrt.ps", lp_build_vec_type(type
), a
);
1202 return lp_build_rcp(bld
, lp_build_sqrt(bld
, a
));
1210 lp_build_cos(struct lp_build_context
*bld
,
1213 const struct lp_type type
= bld
->type
;
1214 LLVMTypeRef vec_type
= lp_build_vec_type(type
);
1217 /* TODO: optimize the constant case */
1219 assert(type
.floating
);
1220 util_snprintf(intrinsic
, sizeof intrinsic
, "llvm.cos.v%uf%u", type
.length
, type
.width
);
1222 return lp_build_intrinsic_unary(bld
->builder
, intrinsic
, vec_type
, a
);
1230 lp_build_sin(struct lp_build_context
*bld
,
1233 const struct lp_type type
= bld
->type
;
1234 LLVMTypeRef vec_type
= lp_build_vec_type(type
);
1237 /* TODO: optimize the constant case */
1239 assert(type
.floating
);
1240 util_snprintf(intrinsic
, sizeof intrinsic
, "llvm.sin.v%uf%u", type
.length
, type
.width
);
1242 return lp_build_intrinsic_unary(bld
->builder
, intrinsic
, vec_type
, a
);
1247 * Generate pow(x, y)
1250 lp_build_pow(struct lp_build_context
*bld
,
1254 /* TODO: optimize the constant case */
1255 if(LLVMIsConstant(x
) && LLVMIsConstant(y
))
1256 debug_printf("%s: inefficient/imprecise constant arithmetic\n",
1259 return lp_build_exp2(bld
, lp_build_mul(bld
, lp_build_log2(bld
, x
), y
));
1267 lp_build_exp(struct lp_build_context
*bld
,
1270 /* log2(e) = 1/log(2) */
1271 LLVMValueRef log2e
= lp_build_const_vec(bld
->type
, 1.4426950408889634);
1273 return lp_build_mul(bld
, log2e
, lp_build_exp2(bld
, x
));
1281 lp_build_log(struct lp_build_context
*bld
,
1285 LLVMValueRef log2
= lp_build_const_vec(bld
->type
, 0.69314718055994529);
1287 return lp_build_mul(bld
, log2
, lp_build_exp2(bld
, x
));
1291 #define EXP_POLY_DEGREE 3
1292 #define LOG_POLY_DEGREE 5
1296 * Generate polynomial.
1297 * Ex: coeffs[0] + x * coeffs[1] + x^2 * coeffs[2].
1300 lp_build_polynomial(struct lp_build_context
*bld
,
1302 const double *coeffs
,
1303 unsigned num_coeffs
)
1305 const struct lp_type type
= bld
->type
;
1306 LLVMTypeRef float_type
= LLVMFloatType();
1307 LLVMValueRef res
= NULL
;
1310 /* TODO: optimize the constant case */
1311 if(LLVMIsConstant(x
))
1312 debug_printf("%s: inefficient/imprecise constant arithmetic\n",
1315 for (i
= num_coeffs
; i
--; ) {
1318 if (type
.length
== 1)
1319 coeff
= LLVMConstReal(float_type
, coeffs
[i
]);
1321 coeff
= lp_build_const_vec(type
, coeffs
[i
]);
1324 res
= lp_build_add(bld
, coeff
, lp_build_mul(bld
, x
, res
));
1337 * Minimax polynomial fit of 2**x, in range [-0.5, 0.5[
1339 const double lp_build_exp2_polynomial
[] = {
1340 #if EXP_POLY_DEGREE == 5
1341 9.9999994e-1, 6.9315308e-1, 2.4015361e-1, 5.5826318e-2, 8.9893397e-3, 1.8775767e-3
1342 #elif EXP_POLY_DEGREE == 4
1343 1.0000026, 6.9300383e-1, 2.4144275e-1, 5.2011464e-2, 1.3534167e-2
1344 #elif EXP_POLY_DEGREE == 3
1345 9.9992520e-1, 6.9583356e-1, 2.2606716e-1, 7.8024521e-2
1346 #elif EXP_POLY_DEGREE == 2
1347 1.0017247, 6.5763628e-1, 3.3718944e-1
1355 lp_build_exp2_approx(struct lp_build_context
*bld
,
1357 LLVMValueRef
*p_exp2_int_part
,
1358 LLVMValueRef
*p_frac_part
,
1359 LLVMValueRef
*p_exp2
)
1361 const struct lp_type type
= bld
->type
;
1362 LLVMTypeRef vec_type
= lp_build_vec_type(type
);
1363 LLVMTypeRef int_vec_type
= lp_build_int_vec_type(type
);
1364 LLVMValueRef ipart
= NULL
;
1365 LLVMValueRef fpart
= NULL
;
1366 LLVMValueRef expipart
= NULL
;
1367 LLVMValueRef expfpart
= NULL
;
1368 LLVMValueRef res
= NULL
;
1370 if(p_exp2_int_part
|| p_frac_part
|| p_exp2
) {
1371 /* TODO: optimize the constant case */
1372 if(LLVMIsConstant(x
))
1373 debug_printf("%s: inefficient/imprecise constant arithmetic\n",
1376 assert(type
.floating
&& type
.width
== 32);
1378 x
= lp_build_min(bld
, x
, lp_build_const_vec(type
, 129.0));
1379 x
= lp_build_max(bld
, x
, lp_build_const_vec(type
, -126.99999));
1381 /* ipart = int(x - 0.5) */
1382 ipart
= LLVMBuildSub(bld
->builder
, x
, lp_build_const_vec(type
, 0.5f
), "");
1383 ipart
= LLVMBuildFPToSI(bld
->builder
, ipart
, int_vec_type
, "");
1385 /* fpart = x - ipart */
1386 fpart
= LLVMBuildSIToFP(bld
->builder
, ipart
, vec_type
, "");
1387 fpart
= LLVMBuildSub(bld
->builder
, x
, fpart
, "");
1390 if(p_exp2_int_part
|| p_exp2
) {
1391 /* expipart = (float) (1 << ipart) */
1392 expipart
= LLVMBuildAdd(bld
->builder
, ipart
, lp_build_const_int_vec(type
, 127), "");
1393 expipart
= LLVMBuildShl(bld
->builder
, expipart
, lp_build_const_int_vec(type
, 23), "");
1394 expipart
= LLVMBuildBitCast(bld
->builder
, expipart
, vec_type
, "");
1398 expfpart
= lp_build_polynomial(bld
, fpart
, lp_build_exp2_polynomial
,
1399 Elements(lp_build_exp2_polynomial
));
1401 res
= LLVMBuildMul(bld
->builder
, expipart
, expfpart
, "");
1405 *p_exp2_int_part
= expipart
;
1408 *p_frac_part
= fpart
;
1416 lp_build_exp2(struct lp_build_context
*bld
,
1420 lp_build_exp2_approx(bld
, x
, NULL
, NULL
, &res
);
1426 * Minimax polynomial fit of log2(x)/(x - 1), for x in range [1, 2[
1427 * These coefficients can be generate with
1428 * http://www.boost.org/doc/libs/1_36_0/libs/math/doc/sf_and_dist/html/math_toolkit/toolkit/internals2/minimax.html
1430 const double lp_build_log2_polynomial
[] = {
1431 #if LOG_POLY_DEGREE == 6
1432 3.11578814719469302614, -3.32419399085241980044, 2.59883907202499966007, -1.23152682416275988241, 0.318212422185251071475, -0.0344359067839062357313
1433 #elif LOG_POLY_DEGREE == 5
1434 2.8882704548164776201, -2.52074962577807006663, 1.48116647521213171641, -0.465725644288844778798, 0.0596515482674574969533
1435 #elif LOG_POLY_DEGREE == 4
1436 2.61761038894603480148, -1.75647175389045657003, 0.688243882994381274313, -0.107254423828329604454
1437 #elif LOG_POLY_DEGREE == 3
1438 2.28330284476918490682, -1.04913055217340124191, 0.204446009836232697516
1446 * See http://www.devmaster.net/forums/showthread.php?p=43580
1449 lp_build_log2_approx(struct lp_build_context
*bld
,
1451 LLVMValueRef
*p_exp
,
1452 LLVMValueRef
*p_floor_log2
,
1453 LLVMValueRef
*p_log2
)
1455 const struct lp_type type
= bld
->type
;
1456 LLVMTypeRef vec_type
= lp_build_vec_type(type
);
1457 LLVMTypeRef int_vec_type
= lp_build_int_vec_type(type
);
1459 LLVMValueRef expmask
= lp_build_const_int_vec(type
, 0x7f800000);
1460 LLVMValueRef mantmask
= lp_build_const_int_vec(type
, 0x007fffff);
1461 LLVMValueRef one
= LLVMConstBitCast(bld
->one
, int_vec_type
);
1463 LLVMValueRef i
= NULL
;
1464 LLVMValueRef exp
= NULL
;
1465 LLVMValueRef mant
= NULL
;
1466 LLVMValueRef logexp
= NULL
;
1467 LLVMValueRef logmant
= NULL
;
1468 LLVMValueRef res
= NULL
;
1470 if(p_exp
|| p_floor_log2
|| p_log2
) {
1471 /* TODO: optimize the constant case */
1472 if(LLVMIsConstant(x
))
1473 debug_printf("%s: inefficient/imprecise constant arithmetic\n",
1476 assert(type
.floating
&& type
.width
== 32);
1478 i
= LLVMBuildBitCast(bld
->builder
, x
, int_vec_type
, "");
1480 /* exp = (float) exponent(x) */
1481 exp
= LLVMBuildAnd(bld
->builder
, i
, expmask
, "");
1484 if(p_floor_log2
|| p_log2
) {
1485 logexp
= LLVMBuildLShr(bld
->builder
, exp
, lp_build_const_int_vec(type
, 23), "");
1486 logexp
= LLVMBuildSub(bld
->builder
, logexp
, lp_build_const_int_vec(type
, 127), "");
1487 logexp
= LLVMBuildSIToFP(bld
->builder
, logexp
, vec_type
, "");
1491 /* mant = (float) mantissa(x) */
1492 mant
= LLVMBuildAnd(bld
->builder
, i
, mantmask
, "");
1493 mant
= LLVMBuildOr(bld
->builder
, mant
, one
, "");
1494 mant
= LLVMBuildBitCast(bld
->builder
, mant
, vec_type
, "");
1496 logmant
= lp_build_polynomial(bld
, mant
, lp_build_log2_polynomial
,
1497 Elements(lp_build_log2_polynomial
));
1499 /* This effectively increases the polynomial degree by one, but ensures that log2(1) == 0*/
1500 logmant
= LLVMBuildMul(bld
->builder
, logmant
, LLVMBuildSub(bld
->builder
, mant
, bld
->one
, ""), "");
1502 res
= LLVMBuildAdd(bld
->builder
, logmant
, logexp
, "");
1509 *p_floor_log2
= logexp
;
1516 /** scalar version of above function */
1518 lp_build_float_log2_approx(struct lp_build_context
*bld
,
1520 LLVMValueRef
*p_exp
,
1521 LLVMValueRef
*p_floor_log2
,
1522 LLVMValueRef
*p_log2
)
1524 const struct lp_type type
= bld
->type
;
1525 LLVMTypeRef float_type
= LLVMFloatType();
1526 LLVMTypeRef int_type
= LLVMIntType(type
.width
);
1528 LLVMValueRef expmask
= LLVMConstInt(int_type
, 0x7f800000, 0);
1529 LLVMValueRef mantmask
= LLVMConstInt(int_type
, 0x007fffff, 0);
1530 LLVMValueRef one
= LLVMConstBitCast(bld
->one
, int_type
);
1532 LLVMValueRef i
= NULL
;
1533 LLVMValueRef exp
= NULL
;
1534 LLVMValueRef mant
= NULL
;
1535 LLVMValueRef logexp
= NULL
;
1536 LLVMValueRef logmant
= NULL
;
1537 LLVMValueRef res
= NULL
;
1539 if(p_exp
|| p_floor_log2
|| p_log2
) {
1540 /* TODO: optimize the constant case */
1541 if(LLVMIsConstant(x
))
1542 debug_printf("%s: inefficient/imprecise constant arithmetic\n",
1545 assert(type
.floating
&& type
.width
== 32);
1547 i
= LLVMBuildBitCast(bld
->builder
, x
, int_type
, "");
1549 /* exp = (float) exponent(x) */
1550 exp
= LLVMBuildAnd(bld
->builder
, i
, expmask
, "");
1553 if(p_floor_log2
|| p_log2
) {
1554 LLVMValueRef c23
= LLVMConstInt(int_type
, 23, 0);
1555 LLVMValueRef c127
= LLVMConstInt(int_type
, 127, 0);
1556 logexp
= LLVMBuildLShr(bld
->builder
, exp
, c23
, "");
1557 logexp
= LLVMBuildSub(bld
->builder
, logexp
, c127
, "");
1558 logexp
= LLVMBuildSIToFP(bld
->builder
, logexp
, float_type
, "");
1562 /* mant = (float) mantissa(x) */
1563 mant
= LLVMBuildAnd(bld
->builder
, i
, mantmask
, "");
1564 mant
= LLVMBuildOr(bld
->builder
, mant
, one
, "");
1565 mant
= LLVMBuildBitCast(bld
->builder
, mant
, float_type
, "");
1567 logmant
= lp_build_polynomial(bld
, mant
, lp_build_log2_polynomial
,
1568 Elements(lp_build_log2_polynomial
));
1570 /* This effectively increases the polynomial degree by one, but ensures that log2(1) == 0*/
1571 logmant
= LLVMBuildMul(bld
->builder
, logmant
, LLVMBuildSub(bld
->builder
, mant
, bld
->one
, ""), "");
1573 res
= LLVMBuildAdd(bld
->builder
, logmant
, logexp
, "");
1580 *p_floor_log2
= logexp
;
1588 lp_build_log2(struct lp_build_context
*bld
,
1592 if (bld
->type
.length
== 1) {
1593 lp_build_float_log2_approx(bld
, x
, NULL
, NULL
, &res
);
1596 lp_build_log2_approx(bld
, x
, NULL
, NULL
, &res
);