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_int_const_scalar(i16_type
, 8);
368 /* a*b/255 ~= (a*(b + 1)) >> 256 */
369 b
= LLVMBuildAdd(builder
, b
, lp_build_int_const_scalar(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_int_const_scalar(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_int_const_scalar(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_int_const_scalar(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_scalar(bld
->type
, shift
);
503 return LLVMBuildShl(bld
->builder
, a
, factor
, "");
507 factor
= lp_build_const_scalar(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_int_const_scalar(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_int_const_scalar(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_int_const_scalar(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_scalar(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
, bld
->one
);
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_int_const_scalar(type
, type
.width
- 1);
793 LLVMValueRef mask
= lp_build_int_const_scalar(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) {
933 return LLVMBuildFPTrunc(bld
->builder
, a
, LLVMFloatType(), "");
936 if(util_cpu_caps
.has_sse4_1
)
937 return lp_build_round_sse41(bld
, a
, LP_BUILD_ROUND_SSE41_FLOOR
);
939 LLVMTypeRef vec_type
= lp_build_vec_type(type
);
941 res
= lp_build_ifloor(bld
, a
);
942 res
= LLVMBuildSIToFP(bld
->builder
, res
, vec_type
, "");
949 lp_build_ceil(struct lp_build_context
*bld
,
952 const struct lp_type type
= bld
->type
;
954 assert(type
.floating
);
955 assert(lp_check_value(type
, a
));
957 if(util_cpu_caps
.has_sse4_1
)
958 return lp_build_round_sse41(bld
, a
, LP_BUILD_ROUND_SSE41_CEIL
);
960 LLVMTypeRef vec_type
= lp_build_vec_type(type
);
962 res
= lp_build_iceil(bld
, a
);
963 res
= LLVMBuildSIToFP(bld
->builder
, res
, vec_type
, "");
970 * Return fractional part of 'a' computed as a - floor(f)
971 * Typically used in texture coord arithmetic.
974 lp_build_fract(struct lp_build_context
*bld
,
977 assert(bld
->type
.floating
);
978 return lp_build_sub(bld
, a
, lp_build_floor(bld
, a
));
983 * Convert to integer, through whichever rounding method that's fastest,
984 * typically truncating toward zero.
987 lp_build_itrunc(struct lp_build_context
*bld
,
990 const struct lp_type type
= bld
->type
;
992 assert(type
.floating
);
994 if (type
.length
== 1) {
995 LLVMTypeRef int_type
= LLVMIntType(type
.width
);
996 return LLVMBuildFPTrunc(bld
->builder
, a
, int_type
, "");
999 LLVMTypeRef int_vec_type
= lp_build_int_vec_type(type
);
1000 assert(lp_check_value(type
, a
));
1001 return LLVMBuildFPToSI(bld
->builder
, a
, int_vec_type
, "");
1007 * Convert float[] to int[] with round().
1010 lp_build_iround(struct lp_build_context
*bld
,
1013 const struct lp_type type
= bld
->type
;
1014 LLVMTypeRef int_vec_type
= lp_build_int_vec_type(type
);
1017 assert(type
.floating
);
1019 if (type
.length
== 1) {
1020 /* scalar float to int */
1021 LLVMTypeRef int_type
= LLVMIntType(type
.width
);
1022 /* XXX we want rounding here! */
1023 res
= LLVMBuildFPToSI(bld
->builder
, a
, int_type
, "");
1027 assert(lp_check_value(type
, a
));
1029 if(util_cpu_caps
.has_sse4_1
) {
1030 res
= lp_build_round_sse41(bld
, a
, LP_BUILD_ROUND_SSE41_NEAREST
);
1033 LLVMTypeRef vec_type
= lp_build_vec_type(type
);
1034 LLVMValueRef mask
= lp_build_int_const_scalar(type
, (unsigned long long)1 << (type
.width
- 1));
1039 sign
= LLVMBuildBitCast(bld
->builder
, a
, int_vec_type
, "");
1040 sign
= LLVMBuildAnd(bld
->builder
, sign
, mask
, "");
1043 half
= lp_build_const_scalar(type
, 0.5);
1044 half
= LLVMBuildBitCast(bld
->builder
, half
, int_vec_type
, "");
1045 half
= LLVMBuildOr(bld
->builder
, sign
, half
, "");
1046 half
= LLVMBuildBitCast(bld
->builder
, half
, vec_type
, "");
1048 res
= LLVMBuildAdd(bld
->builder
, a
, half
, "");
1051 res
= LLVMBuildFPToSI(bld
->builder
, res
, int_vec_type
, "");
1058 * Convert float[] to int[] with floor().
1061 lp_build_ifloor(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
);
1070 if (type
.length
== 1) {
1071 /* scalar float to int */
1072 LLVMTypeRef int_type
= LLVMIntType(type
.width
);
1073 res
= LLVMBuildFPToSI(bld
->builder
, a
, int_type
, "");
1077 assert(lp_check_value(type
, a
));
1079 if(util_cpu_caps
.has_sse4_1
) {
1080 res
= lp_build_round_sse41(bld
, a
, LP_BUILD_ROUND_SSE41_FLOOR
);
1083 /* Take the sign bit and add it to 1 constant */
1084 LLVMTypeRef vec_type
= lp_build_vec_type(type
);
1085 unsigned mantissa
= lp_mantissa(type
);
1086 LLVMValueRef mask
= lp_build_int_const_scalar(type
, (unsigned long long)1 << (type
.width
- 1));
1088 LLVMValueRef offset
;
1090 /* sign = a < 0 ? ~0 : 0 */
1091 sign
= LLVMBuildBitCast(bld
->builder
, a
, int_vec_type
, "");
1092 sign
= LLVMBuildAnd(bld
->builder
, sign
, mask
, "");
1093 sign
= LLVMBuildAShr(bld
->builder
, sign
, lp_build_int_const_scalar(type
, type
.width
- 1), "");
1094 lp_build_name(sign
, "floor.sign");
1096 /* offset = -0.99999(9)f */
1097 offset
= lp_build_const_scalar(type
, -(double)(((unsigned long long)1 << mantissa
) - 1)/((unsigned long long)1 << mantissa
));
1098 offset
= LLVMConstBitCast(offset
, int_vec_type
);
1100 /* offset = a < 0 ? -0.99999(9)f : 0.0f */
1101 offset
= LLVMBuildAnd(bld
->builder
, offset
, sign
, "");
1102 offset
= LLVMBuildBitCast(bld
->builder
, offset
, vec_type
, "");
1103 lp_build_name(offset
, "floor.offset");
1105 res
= LLVMBuildAdd(bld
->builder
, a
, offset
, "");
1106 lp_build_name(res
, "floor.res");
1109 res
= LLVMBuildFPToSI(bld
->builder
, res
, int_vec_type
, "");
1110 lp_build_name(res
, "floor");
1117 lp_build_iceil(struct lp_build_context
*bld
,
1120 const struct lp_type type
= bld
->type
;
1121 LLVMTypeRef int_vec_type
= lp_build_int_vec_type(type
);
1124 assert(type
.floating
);
1125 assert(lp_check_value(type
, a
));
1127 if(util_cpu_caps
.has_sse4_1
) {
1128 res
= lp_build_round_sse41(bld
, a
, LP_BUILD_ROUND_SSE41_CEIL
);
1135 res
= LLVMBuildFPToSI(bld
->builder
, res
, int_vec_type
, "");
1142 lp_build_sqrt(struct lp_build_context
*bld
,
1145 const struct lp_type type
= bld
->type
;
1146 LLVMTypeRef vec_type
= lp_build_vec_type(type
);
1149 /* TODO: optimize the constant case */
1150 /* TODO: optimize the constant case */
1152 assert(type
.floating
);
1153 util_snprintf(intrinsic
, sizeof intrinsic
, "llvm.sqrt.v%uf%u", type
.length
, type
.width
);
1155 return lp_build_intrinsic_unary(bld
->builder
, intrinsic
, vec_type
, a
);
1160 lp_build_rcp(struct lp_build_context
*bld
,
1163 const struct lp_type type
= bld
->type
;
1172 assert(type
.floating
);
1174 if(LLVMIsConstant(a
))
1175 return LLVMConstFDiv(bld
->one
, a
);
1177 if(util_cpu_caps
.has_sse
&& type
.width
== 32 && type
.length
== 4)
1178 /* FIXME: improve precision */
1179 return lp_build_intrinsic_unary(bld
->builder
, "llvm.x86.sse.rcp.ps", lp_build_vec_type(type
), a
);
1181 return LLVMBuildFDiv(bld
->builder
, bld
->one
, a
, "");
1186 * Generate 1/sqrt(a)
1189 lp_build_rsqrt(struct lp_build_context
*bld
,
1192 const struct lp_type type
= bld
->type
;
1194 assert(type
.floating
);
1196 if(util_cpu_caps
.has_sse
&& type
.width
== 32 && type
.length
== 4)
1197 return lp_build_intrinsic_unary(bld
->builder
, "llvm.x86.sse.rsqrt.ps", lp_build_vec_type(type
), a
);
1199 return lp_build_rcp(bld
, lp_build_sqrt(bld
, a
));
1207 lp_build_cos(struct lp_build_context
*bld
,
1210 const struct lp_type type
= bld
->type
;
1211 LLVMTypeRef vec_type
= lp_build_vec_type(type
);
1214 /* TODO: optimize the constant case */
1216 assert(type
.floating
);
1217 util_snprintf(intrinsic
, sizeof intrinsic
, "llvm.cos.v%uf%u", type
.length
, type
.width
);
1219 return lp_build_intrinsic_unary(bld
->builder
, intrinsic
, vec_type
, a
);
1227 lp_build_sin(struct lp_build_context
*bld
,
1230 const struct lp_type type
= bld
->type
;
1231 LLVMTypeRef vec_type
= lp_build_vec_type(type
);
1234 /* TODO: optimize the constant case */
1236 assert(type
.floating
);
1237 util_snprintf(intrinsic
, sizeof intrinsic
, "llvm.sin.v%uf%u", type
.length
, type
.width
);
1239 return lp_build_intrinsic_unary(bld
->builder
, intrinsic
, vec_type
, a
);
1244 * Generate pow(x, y)
1247 lp_build_pow(struct lp_build_context
*bld
,
1251 /* TODO: optimize the constant case */
1252 if(LLVMIsConstant(x
) && LLVMIsConstant(y
))
1253 debug_printf("%s: inefficient/imprecise constant arithmetic\n",
1256 return lp_build_exp2(bld
, lp_build_mul(bld
, lp_build_log2(bld
, x
), y
));
1264 lp_build_exp(struct lp_build_context
*bld
,
1267 /* log2(e) = 1/log(2) */
1268 LLVMValueRef log2e
= lp_build_const_scalar(bld
->type
, 1.4426950408889634);
1270 return lp_build_mul(bld
, log2e
, lp_build_exp2(bld
, x
));
1278 lp_build_log(struct lp_build_context
*bld
,
1282 LLVMValueRef log2
= lp_build_const_scalar(bld
->type
, 0.69314718055994529);
1284 return lp_build_mul(bld
, log2
, lp_build_exp2(bld
, x
));
1288 #define EXP_POLY_DEGREE 3
1289 #define LOG_POLY_DEGREE 5
1293 * Generate polynomial.
1294 * Ex: coeffs[0] + x * coeffs[1] + x^2 * coeffs[2].
1297 lp_build_polynomial(struct lp_build_context
*bld
,
1299 const double *coeffs
,
1300 unsigned num_coeffs
)
1302 const struct lp_type type
= bld
->type
;
1303 LLVMTypeRef float_type
= LLVMFloatType();
1304 LLVMValueRef res
= NULL
;
1307 /* TODO: optimize the constant case */
1308 if(LLVMIsConstant(x
))
1309 debug_printf("%s: inefficient/imprecise constant arithmetic\n",
1312 for (i
= num_coeffs
; i
--; ) {
1315 if (type
.length
== 1)
1316 coeff
= LLVMConstReal(float_type
, coeffs
[i
]);
1318 coeff
= lp_build_const_scalar(type
, coeffs
[i
]);
1321 res
= lp_build_add(bld
, coeff
, lp_build_mul(bld
, x
, res
));
1334 * Minimax polynomial fit of 2**x, in range [-0.5, 0.5[
1336 const double lp_build_exp2_polynomial
[] = {
1337 #if EXP_POLY_DEGREE == 5
1338 9.9999994e-1, 6.9315308e-1, 2.4015361e-1, 5.5826318e-2, 8.9893397e-3, 1.8775767e-3
1339 #elif EXP_POLY_DEGREE == 4
1340 1.0000026, 6.9300383e-1, 2.4144275e-1, 5.2011464e-2, 1.3534167e-2
1341 #elif EXP_POLY_DEGREE == 3
1342 9.9992520e-1, 6.9583356e-1, 2.2606716e-1, 7.8024521e-2
1343 #elif EXP_POLY_DEGREE == 2
1344 1.0017247, 6.5763628e-1, 3.3718944e-1
1352 lp_build_exp2_approx(struct lp_build_context
*bld
,
1354 LLVMValueRef
*p_exp2_int_part
,
1355 LLVMValueRef
*p_frac_part
,
1356 LLVMValueRef
*p_exp2
)
1358 const struct lp_type type
= bld
->type
;
1359 LLVMTypeRef vec_type
= lp_build_vec_type(type
);
1360 LLVMTypeRef int_vec_type
= lp_build_int_vec_type(type
);
1361 LLVMValueRef ipart
= NULL
;
1362 LLVMValueRef fpart
= NULL
;
1363 LLVMValueRef expipart
= NULL
;
1364 LLVMValueRef expfpart
= NULL
;
1365 LLVMValueRef res
= NULL
;
1367 if(p_exp2_int_part
|| p_frac_part
|| p_exp2
) {
1368 /* TODO: optimize the constant case */
1369 if(LLVMIsConstant(x
))
1370 debug_printf("%s: inefficient/imprecise constant arithmetic\n",
1373 assert(type
.floating
&& type
.width
== 32);
1375 x
= lp_build_min(bld
, x
, lp_build_const_scalar(type
, 129.0));
1376 x
= lp_build_max(bld
, x
, lp_build_const_scalar(type
, -126.99999));
1378 /* ipart = int(x - 0.5) */
1379 ipart
= LLVMBuildSub(bld
->builder
, x
, lp_build_const_scalar(type
, 0.5f
), "");
1380 ipart
= LLVMBuildFPToSI(bld
->builder
, ipart
, int_vec_type
, "");
1382 /* fpart = x - ipart */
1383 fpart
= LLVMBuildSIToFP(bld
->builder
, ipart
, vec_type
, "");
1384 fpart
= LLVMBuildSub(bld
->builder
, x
, fpart
, "");
1387 if(p_exp2_int_part
|| p_exp2
) {
1388 /* expipart = (float) (1 << ipart) */
1389 expipart
= LLVMBuildAdd(bld
->builder
, ipart
, lp_build_int_const_scalar(type
, 127), "");
1390 expipart
= LLVMBuildShl(bld
->builder
, expipart
, lp_build_int_const_scalar(type
, 23), "");
1391 expipart
= LLVMBuildBitCast(bld
->builder
, expipart
, vec_type
, "");
1395 expfpart
= lp_build_polynomial(bld
, fpart
, lp_build_exp2_polynomial
,
1396 Elements(lp_build_exp2_polynomial
));
1398 res
= LLVMBuildMul(bld
->builder
, expipart
, expfpart
, "");
1402 *p_exp2_int_part
= expipart
;
1405 *p_frac_part
= fpart
;
1413 lp_build_exp2(struct lp_build_context
*bld
,
1417 lp_build_exp2_approx(bld
, x
, NULL
, NULL
, &res
);
1423 * Minimax polynomial fit of log2(x)/(x - 1), for x in range [1, 2[
1424 * These coefficients can be generate with
1425 * http://www.boost.org/doc/libs/1_36_0/libs/math/doc/sf_and_dist/html/math_toolkit/toolkit/internals2/minimax.html
1427 const double lp_build_log2_polynomial
[] = {
1428 #if LOG_POLY_DEGREE == 6
1429 3.11578814719469302614, -3.32419399085241980044, 2.59883907202499966007, -1.23152682416275988241, 0.318212422185251071475, -0.0344359067839062357313
1430 #elif LOG_POLY_DEGREE == 5
1431 2.8882704548164776201, -2.52074962577807006663, 1.48116647521213171641, -0.465725644288844778798, 0.0596515482674574969533
1432 #elif LOG_POLY_DEGREE == 4
1433 2.61761038894603480148, -1.75647175389045657003, 0.688243882994381274313, -0.107254423828329604454
1434 #elif LOG_POLY_DEGREE == 3
1435 2.28330284476918490682, -1.04913055217340124191, 0.204446009836232697516
1443 * See http://www.devmaster.net/forums/showthread.php?p=43580
1446 lp_build_log2_approx(struct lp_build_context
*bld
,
1448 LLVMValueRef
*p_exp
,
1449 LLVMValueRef
*p_floor_log2
,
1450 LLVMValueRef
*p_log2
)
1452 const struct lp_type type
= bld
->type
;
1453 LLVMTypeRef vec_type
= lp_build_vec_type(type
);
1454 LLVMTypeRef int_vec_type
= lp_build_int_vec_type(type
);
1456 LLVMValueRef expmask
= lp_build_int_const_scalar(type
, 0x7f800000);
1457 LLVMValueRef mantmask
= lp_build_int_const_scalar(type
, 0x007fffff);
1458 LLVMValueRef one
= LLVMConstBitCast(bld
->one
, int_vec_type
);
1460 LLVMValueRef i
= NULL
;
1461 LLVMValueRef exp
= NULL
;
1462 LLVMValueRef mant
= NULL
;
1463 LLVMValueRef logexp
= NULL
;
1464 LLVMValueRef logmant
= NULL
;
1465 LLVMValueRef res
= NULL
;
1467 if(p_exp
|| p_floor_log2
|| p_log2
) {
1468 /* TODO: optimize the constant case */
1469 if(LLVMIsConstant(x
))
1470 debug_printf("%s: inefficient/imprecise constant arithmetic\n",
1473 assert(type
.floating
&& type
.width
== 32);
1475 i
= LLVMBuildBitCast(bld
->builder
, x
, int_vec_type
, "");
1477 /* exp = (float) exponent(x) */
1478 exp
= LLVMBuildAnd(bld
->builder
, i
, expmask
, "");
1481 if(p_floor_log2
|| p_log2
) {
1482 logexp
= LLVMBuildLShr(bld
->builder
, exp
, lp_build_int_const_scalar(type
, 23), "");
1483 logexp
= LLVMBuildSub(bld
->builder
, logexp
, lp_build_int_const_scalar(type
, 127), "");
1484 logexp
= LLVMBuildSIToFP(bld
->builder
, logexp
, vec_type
, "");
1488 /* mant = (float) mantissa(x) */
1489 mant
= LLVMBuildAnd(bld
->builder
, i
, mantmask
, "");
1490 mant
= LLVMBuildOr(bld
->builder
, mant
, one
, "");
1491 mant
= LLVMBuildBitCast(bld
->builder
, mant
, vec_type
, "");
1493 logmant
= lp_build_polynomial(bld
, mant
, lp_build_log2_polynomial
,
1494 Elements(lp_build_log2_polynomial
));
1496 /* This effectively increases the polynomial degree by one, but ensures that log2(1) == 0*/
1497 logmant
= LLVMBuildMul(bld
->builder
, logmant
, LLVMBuildSub(bld
->builder
, mant
, bld
->one
, ""), "");
1499 res
= LLVMBuildAdd(bld
->builder
, logmant
, logexp
, "");
1506 *p_floor_log2
= logexp
;
1513 /** scalar version of above function */
1515 lp_build_float_log2_approx(struct lp_build_context
*bld
,
1517 LLVMValueRef
*p_exp
,
1518 LLVMValueRef
*p_floor_log2
,
1519 LLVMValueRef
*p_log2
)
1521 const struct lp_type type
= bld
->type
;
1522 LLVMTypeRef float_type
= LLVMFloatType();
1523 LLVMTypeRef int_type
= LLVMIntType(type
.width
);
1525 LLVMValueRef expmask
= LLVMConstInt(int_type
, 0x7f800000, 0);
1526 LLVMValueRef mantmask
= LLVMConstInt(int_type
, 0x007fffff, 0);
1527 LLVMValueRef one
= LLVMConstBitCast(bld
->one
, int_type
);
1529 LLVMValueRef i
= NULL
;
1530 LLVMValueRef exp
= NULL
;
1531 LLVMValueRef mant
= NULL
;
1532 LLVMValueRef logexp
= NULL
;
1533 LLVMValueRef logmant
= NULL
;
1534 LLVMValueRef res
= NULL
;
1536 if(p_exp
|| p_floor_log2
|| p_log2
) {
1537 /* TODO: optimize the constant case */
1538 if(LLVMIsConstant(x
))
1539 debug_printf("%s: inefficient/imprecise constant arithmetic\n",
1542 assert(type
.floating
&& type
.width
== 32);
1544 i
= LLVMBuildBitCast(bld
->builder
, x
, int_type
, "");
1546 /* exp = (float) exponent(x) */
1547 exp
= LLVMBuildAnd(bld
->builder
, i
, expmask
, "");
1550 if(p_floor_log2
|| p_log2
) {
1551 LLVMValueRef c23
= LLVMConstInt(int_type
, 23, 0);
1552 LLVMValueRef c127
= LLVMConstInt(int_type
, 127, 0);
1553 logexp
= LLVMBuildLShr(bld
->builder
, exp
, c23
, "");
1554 logexp
= LLVMBuildSub(bld
->builder
, logexp
, c127
, "");
1555 logexp
= LLVMBuildSIToFP(bld
->builder
, logexp
, float_type
, "");
1559 /* mant = (float) mantissa(x) */
1560 mant
= LLVMBuildAnd(bld
->builder
, i
, mantmask
, "");
1561 mant
= LLVMBuildOr(bld
->builder
, mant
, one
, "");
1562 mant
= LLVMBuildBitCast(bld
->builder
, mant
, float_type
, "");
1564 logmant
= lp_build_polynomial(bld
, mant
, lp_build_log2_polynomial
,
1565 Elements(lp_build_log2_polynomial
));
1567 /* This effectively increases the polynomial degree by one, but ensures that log2(1) == 0*/
1568 logmant
= LLVMBuildMul(bld
->builder
, logmant
, LLVMBuildSub(bld
->builder
, mant
, bld
->one
, ""), "");
1570 res
= LLVMBuildAdd(bld
->builder
, logmant
, logexp
, "");
1577 *p_floor_log2
= logexp
;
1585 lp_build_log2(struct lp_build_context
*bld
,
1589 if (bld
->type
.length
== 1) {
1590 lp_build_float_log2_approx(bld
, x
, NULL
, NULL
, &res
);
1593 lp_build_log2_approx(bld
, x
, NULL
, NULL
, &res
);