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??? */
239 lp_build_sub(struct lp_build_context
*bld
,
243 const struct lp_type type
= bld
->type
;
248 if(a
== bld
->undef
|| b
== bld
->undef
)
254 const char *intrinsic
= NULL
;
259 if(util_cpu_caps
.has_sse2
&&
260 type
.width
* type
.length
== 128 &&
261 !type
.floating
&& !type
.fixed
) {
263 intrinsic
= type
.sign
? "llvm.x86.sse2.psubs.b" : "llvm.x86.sse2.psubus.b";
265 intrinsic
= type
.sign
? "llvm.x86.sse2.psubs.w" : "llvm.x86.sse2.psubus.w";
269 return lp_build_intrinsic_binary(bld
->builder
, intrinsic
, lp_build_vec_type(bld
->type
), a
, b
);
272 if(LLVMIsConstant(a
) && LLVMIsConstant(b
))
273 res
= LLVMConstSub(a
, b
);
275 res
= LLVMBuildSub(bld
->builder
, a
, b
, "");
277 if(bld
->type
.norm
&& (bld
->type
.floating
|| bld
->type
.fixed
))
278 res
= lp_build_max_simple(bld
, res
, bld
->zero
);
285 * Normalized 8bit multiplication.
289 * makes the following approximation to the division (Sree)
291 * a*b/255 ~= (a*(b + 1)) >> 256
293 * which is the fastest method that satisfies the following OpenGL criteria
295 * 0*0 = 0 and 255*255 = 255
299 * takes the geometric series approximation to the division
301 * t/255 = (t >> 8) + (t >> 16) + (t >> 24) ..
303 * in this case just the first two terms to fit in 16bit arithmetic
305 * t/255 ~= (t + (t >> 8)) >> 8
307 * note that just by itself it doesn't satisfies the OpenGL criteria, as
308 * 255*255 = 254, so the special case b = 255 must be accounted or roundoff
311 * - geometric series plus rounding
313 * when using a geometric series division instead of truncating the result
314 * use roundoff in the approximation (Jim Blinn)
316 * t/255 ~= (t + (t >> 8) + 0x80) >> 8
318 * achieving the exact results
320 * @sa Alvy Ray Smith, Image Compositing Fundamentals, Tech Memo 4, Aug 15, 1995,
321 * ftp://ftp.alvyray.com/Acrobat/4_Comp.pdf
322 * @sa Michael Herf, The "double blend trick", May 2000,
323 * http://www.stereopsis.com/doubleblend.html
326 lp_build_mul_u8n(LLVMBuilderRef builder
,
327 struct lp_type i16_type
,
328 LLVMValueRef a
, LLVMValueRef b
)
333 c8
= lp_build_int_const_scalar(i16_type
, 8);
337 /* a*b/255 ~= (a*(b + 1)) >> 256 */
338 b
= LLVMBuildAdd(builder
, b
, lp_build_int_const_scalar(i16_type
, 1), "");
339 ab
= LLVMBuildMul(builder
, a
, b
, "");
343 /* ab/255 ~= (ab + (ab >> 8) + 0x80) >> 8 */
344 ab
= LLVMBuildMul(builder
, a
, b
, "");
345 ab
= LLVMBuildAdd(builder
, ab
, LLVMBuildLShr(builder
, ab
, c8
, ""), "");
346 ab
= LLVMBuildAdd(builder
, ab
, lp_build_int_const_scalar(i16_type
, 0x80), "");
350 ab
= LLVMBuildLShr(builder
, ab
, c8
, "");
360 lp_build_mul(struct lp_build_context
*bld
,
364 const struct lp_type type
= bld
->type
;
376 if(a
== bld
->undef
|| b
== bld
->undef
)
379 if(!type
.floating
&& !type
.fixed
&& type
.norm
) {
380 if(type
.width
== 8) {
381 struct lp_type i16_type
= lp_wider_type(type
);
382 LLVMValueRef al
, ah
, bl
, bh
, abl
, abh
, ab
;
384 lp_build_unpack2(bld
->builder
, type
, i16_type
, a
, &al
, &ah
);
385 lp_build_unpack2(bld
->builder
, type
, i16_type
, b
, &bl
, &bh
);
387 /* PMULLW, PSRLW, PADDW */
388 abl
= lp_build_mul_u8n(bld
->builder
, i16_type
, al
, bl
);
389 abh
= lp_build_mul_u8n(bld
->builder
, i16_type
, ah
, bh
);
391 ab
= lp_build_pack2(bld
->builder
, i16_type
, type
, abl
, abh
);
401 shift
= lp_build_int_const_scalar(type
, type
.width
/2);
405 if(LLVMIsConstant(a
) && LLVMIsConstant(b
)) {
406 res
= LLVMConstMul(a
, b
);
409 res
= LLVMConstAShr(res
, shift
);
411 res
= LLVMConstLShr(res
, shift
);
415 res
= LLVMBuildMul(bld
->builder
, a
, b
, "");
418 res
= LLVMBuildAShr(bld
->builder
, res
, shift
, "");
420 res
= LLVMBuildLShr(bld
->builder
, res
, shift
, "");
429 * Small vector x scale multiplication optimization.
432 lp_build_mul_imm(struct lp_build_context
*bld
,
445 return LLVMBuildNeg(bld
->builder
, a
, "");
447 if(b
== 2 && bld
->type
.floating
)
448 return lp_build_add(bld
, a
, a
);
451 unsigned shift
= ffs(b
) - 1;
453 if(bld
->type
.floating
) {
456 * Power of two multiplication by directly manipulating the mantissa.
458 * XXX: This might not be always faster, it will introduce a small error
459 * for multiplication by zero, and it will produce wrong results
462 unsigned mantissa
= lp_mantissa(bld
->type
);
463 factor
= lp_build_int_const_scalar(bld
->type
, (unsigned long long)shift
<< mantissa
);
464 a
= LLVMBuildBitCast(bld
->builder
, a
, lp_build_int_vec_type(bld
->type
), "");
465 a
= LLVMBuildAdd(bld
->builder
, a
, factor
, "");
466 a
= LLVMBuildBitCast(bld
->builder
, a
, lp_build_vec_type(bld
->type
), "");
471 factor
= lp_build_const_scalar(bld
->type
, shift
);
472 return LLVMBuildShl(bld
->builder
, a
, factor
, "");
476 factor
= lp_build_const_scalar(bld
->type
, (double)b
);
477 return lp_build_mul(bld
, a
, factor
);
485 lp_build_div(struct lp_build_context
*bld
,
489 const struct lp_type type
= bld
->type
;
494 return lp_build_rcp(bld
, b
);
499 if(a
== bld
->undef
|| b
== bld
->undef
)
502 if(LLVMIsConstant(a
) && LLVMIsConstant(b
))
503 return LLVMConstFDiv(a
, b
);
505 if(util_cpu_caps
.has_sse
&& type
.width
== 32 && type
.length
== 4)
506 return lp_build_mul(bld
, a
, lp_build_rcp(bld
, b
));
508 return LLVMBuildFDiv(bld
->builder
, a
, b
, "");
513 * Linear interpolation.
515 * This also works for integer values with a few caveats.
517 * @sa http://www.stereopsis.com/doubleblend.html
520 lp_build_lerp(struct lp_build_context
*bld
,
528 delta
= lp_build_sub(bld
, v1
, v0
);
530 res
= lp_build_mul(bld
, x
, delta
);
532 res
= lp_build_add(bld
, v0
, res
);
535 /* XXX: This step is necessary for lerping 8bit colors stored on 16bits,
536 * but it will be wrong for other uses. Basically we need a more
537 * powerful lp_type, capable of further distinguishing the values
538 * interpretation from the value storage. */
539 res
= LLVMBuildAnd(bld
->builder
, res
, lp_build_int_const_scalar(bld
->type
, (1 << bld
->type
.width
/2) - 1), "");
546 lp_build_lerp_2d(struct lp_build_context
*bld
,
554 LLVMValueRef v0
= lp_build_lerp(bld
, x
, v00
, v01
);
555 LLVMValueRef v1
= lp_build_lerp(bld
, x
, v10
, v11
);
556 return lp_build_lerp(bld
, y
, v0
, v1
);
562 * Do checks for special cases.
565 lp_build_min(struct lp_build_context
*bld
,
569 if(a
== bld
->undef
|| b
== bld
->undef
)
576 if(a
== bld
->zero
|| b
== bld
->zero
)
584 return lp_build_min_simple(bld
, a
, b
);
590 * Do checks for special cases.
593 lp_build_max(struct lp_build_context
*bld
,
597 if(a
== bld
->undef
|| b
== bld
->undef
)
604 if(a
== bld
->one
|| b
== bld
->one
)
612 return lp_build_max_simple(bld
, a
, b
);
617 * Generate clamp(a, min, max)
618 * Do checks for special cases.
621 lp_build_clamp(struct lp_build_context
*bld
,
626 a
= lp_build_min(bld
, a
, max
);
627 a
= lp_build_max(bld
, a
, min
);
636 lp_build_abs(struct lp_build_context
*bld
,
639 const struct lp_type type
= bld
->type
;
640 LLVMTypeRef vec_type
= lp_build_vec_type(type
);
646 /* Mask out the sign bit */
647 LLVMTypeRef int_vec_type
= lp_build_int_vec_type(type
);
648 unsigned long long absMask
= ~(1ULL << (type
.width
- 1));
649 LLVMValueRef mask
= lp_build_int_const_scalar(type
, ((unsigned long long) absMask
));
650 a
= LLVMBuildBitCast(bld
->builder
, a
, int_vec_type
, "");
651 a
= LLVMBuildAnd(bld
->builder
, a
, mask
, "");
652 a
= LLVMBuildBitCast(bld
->builder
, a
, vec_type
, "");
656 if(type
.width
*type
.length
== 128 && util_cpu_caps
.has_ssse3
) {
659 return lp_build_intrinsic_unary(bld
->builder
, "llvm.x86.ssse3.pabs.b.128", vec_type
, a
);
661 return lp_build_intrinsic_unary(bld
->builder
, "llvm.x86.ssse3.pabs.w.128", vec_type
, a
);
663 return lp_build_intrinsic_unary(bld
->builder
, "llvm.x86.ssse3.pabs.d.128", vec_type
, a
);
667 return lp_build_max(bld
, a
, LLVMBuildNeg(bld
->builder
, a
, ""));
672 lp_build_sgn(struct lp_build_context
*bld
,
675 const struct lp_type type
= bld
->type
;
676 LLVMTypeRef vec_type
= lp_build_vec_type(type
);
680 /* Handle non-zero case */
682 /* if not zero then sign must be positive */
685 else if(type
.floating
) {
686 /* Take the sign bit and add it to 1 constant */
687 LLVMTypeRef int_vec_type
= lp_build_int_vec_type(type
);
688 LLVMValueRef mask
= lp_build_int_const_scalar(type
, (unsigned long long)1 << (type
.width
- 1));
691 sign
= LLVMBuildBitCast(bld
->builder
, a
, int_vec_type
, "");
692 sign
= LLVMBuildAnd(bld
->builder
, sign
, mask
, "");
693 one
= LLVMConstBitCast(bld
->one
, int_vec_type
);
694 res
= LLVMBuildOr(bld
->builder
, sign
, one
, "");
695 res
= LLVMBuildBitCast(bld
->builder
, res
, vec_type
, "");
699 LLVMValueRef minus_one
= lp_build_const_scalar(type
, -1.0);
700 cond
= lp_build_cmp(bld
, PIPE_FUNC_GREATER
, a
, bld
->zero
);
701 res
= lp_build_select(bld
, cond
, bld
->one
, minus_one
);
705 cond
= lp_build_cmp(bld
, PIPE_FUNC_EQUAL
, a
, bld
->zero
);
706 res
= lp_build_select(bld
, cond
, bld
->zero
, bld
->one
);
713 * Convert vector of int to vector of float.
716 lp_build_int_to_float(struct lp_build_context
*bld
,
719 const struct lp_type type
= bld
->type
;
721 assert(type
.floating
);
722 /*assert(lp_check_value(type, a));*/
725 LLVMTypeRef vec_type
= lp_build_vec_type(type
);
726 /*LLVMTypeRef int_vec_type = lp_build_int_vec_type(type);*/
728 res
= LLVMBuildSIToFP(bld
->builder
, a
, vec_type
, "");
735 enum lp_build_round_sse41_mode
737 LP_BUILD_ROUND_SSE41_NEAREST
= 0,
738 LP_BUILD_ROUND_SSE41_FLOOR
= 1,
739 LP_BUILD_ROUND_SSE41_CEIL
= 2,
740 LP_BUILD_ROUND_SSE41_TRUNCATE
= 3
744 static INLINE LLVMValueRef
745 lp_build_round_sse41(struct lp_build_context
*bld
,
747 enum lp_build_round_sse41_mode mode
)
749 const struct lp_type type
= bld
->type
;
750 LLVMTypeRef vec_type
= lp_build_vec_type(type
);
751 const char *intrinsic
;
753 assert(type
.floating
);
754 assert(type
.width
*type
.length
== 128);
755 assert(lp_check_value(type
, a
));
756 assert(util_cpu_caps
.has_sse4_1
);
760 intrinsic
= "llvm.x86.sse41.round.ps";
763 intrinsic
= "llvm.x86.sse41.round.pd";
770 return lp_build_intrinsic_binary(bld
->builder
, intrinsic
, vec_type
, a
,
771 LLVMConstInt(LLVMInt32Type(), mode
, 0));
776 lp_build_trunc(struct lp_build_context
*bld
,
779 const struct lp_type type
= bld
->type
;
781 assert(type
.floating
);
782 assert(lp_check_value(type
, a
));
784 if(util_cpu_caps
.has_sse4_1
)
785 return lp_build_round_sse41(bld
, a
, LP_BUILD_ROUND_SSE41_TRUNCATE
);
787 LLVMTypeRef vec_type
= lp_build_vec_type(type
);
788 LLVMTypeRef int_vec_type
= lp_build_int_vec_type(type
);
790 res
= LLVMBuildFPToSI(bld
->builder
, a
, int_vec_type
, "");
791 res
= LLVMBuildSIToFP(bld
->builder
, res
, vec_type
, "");
798 lp_build_round(struct lp_build_context
*bld
,
801 const struct lp_type type
= bld
->type
;
803 assert(type
.floating
);
804 assert(lp_check_value(type
, a
));
806 if(util_cpu_caps
.has_sse4_1
)
807 return lp_build_round_sse41(bld
, a
, LP_BUILD_ROUND_SSE41_NEAREST
);
809 LLVMTypeRef vec_type
= lp_build_vec_type(type
);
811 res
= lp_build_iround(bld
, a
);
812 res
= LLVMBuildSIToFP(bld
->builder
, res
, vec_type
, "");
819 lp_build_floor(struct lp_build_context
*bld
,
822 const struct lp_type type
= bld
->type
;
824 assert(type
.floating
);
826 if(util_cpu_caps
.has_sse4_1
)
827 return lp_build_round_sse41(bld
, a
, LP_BUILD_ROUND_SSE41_FLOOR
);
829 LLVMTypeRef vec_type
= lp_build_vec_type(type
);
831 res
= lp_build_ifloor(bld
, a
);
832 res
= LLVMBuildSIToFP(bld
->builder
, res
, vec_type
, "");
839 lp_build_ceil(struct lp_build_context
*bld
,
842 const struct lp_type type
= bld
->type
;
844 assert(type
.floating
);
845 assert(lp_check_value(type
, a
));
847 if(util_cpu_caps
.has_sse4_1
)
848 return lp_build_round_sse41(bld
, a
, LP_BUILD_ROUND_SSE41_CEIL
);
850 LLVMTypeRef vec_type
= lp_build_vec_type(type
);
852 res
= lp_build_iceil(bld
, a
);
853 res
= LLVMBuildSIToFP(bld
->builder
, res
, vec_type
, "");
860 * Convert to integer, through whichever rounding method that's fastest,
861 * typically truncating toward zero.
864 lp_build_itrunc(struct lp_build_context
*bld
,
867 const struct lp_type type
= bld
->type
;
868 LLVMTypeRef int_vec_type
= lp_build_int_vec_type(type
);
870 assert(type
.floating
);
871 assert(lp_check_value(type
, a
));
873 return LLVMBuildFPToSI(bld
->builder
, a
, int_vec_type
, "");
878 lp_build_iround(struct lp_build_context
*bld
,
881 const struct lp_type type
= bld
->type
;
882 LLVMTypeRef int_vec_type
= lp_build_int_vec_type(type
);
885 assert(type
.floating
);
886 assert(lp_check_value(type
, a
));
888 if(util_cpu_caps
.has_sse4_1
) {
889 res
= lp_build_round_sse41(bld
, a
, LP_BUILD_ROUND_SSE41_NEAREST
);
892 LLVMTypeRef vec_type
= lp_build_vec_type(type
);
893 LLVMValueRef mask
= lp_build_int_const_scalar(type
, (unsigned long long)1 << (type
.width
- 1));
898 sign
= LLVMBuildBitCast(bld
->builder
, a
, int_vec_type
, "");
899 sign
= LLVMBuildAnd(bld
->builder
, sign
, mask
, "");
902 half
= lp_build_const_scalar(type
, 0.5);
903 half
= LLVMBuildBitCast(bld
->builder
, half
, int_vec_type
, "");
904 half
= LLVMBuildOr(bld
->builder
, sign
, half
, "");
905 half
= LLVMBuildBitCast(bld
->builder
, half
, vec_type
, "");
907 res
= LLVMBuildAdd(bld
->builder
, a
, half
, "");
910 res
= LLVMBuildFPToSI(bld
->builder
, res
, int_vec_type
, "");
917 * Convert float[] to int[] with floor().
920 lp_build_ifloor(struct lp_build_context
*bld
,
923 const struct lp_type type
= bld
->type
;
924 LLVMTypeRef int_vec_type
= lp_build_int_vec_type(type
);
927 assert(type
.floating
);
928 assert(lp_check_value(type
, a
));
930 if(util_cpu_caps
.has_sse4_1
) {
931 res
= lp_build_round_sse41(bld
, a
, LP_BUILD_ROUND_SSE41_FLOOR
);
934 /* Take the sign bit and add it to 1 constant */
935 LLVMTypeRef vec_type
= lp_build_vec_type(type
);
936 unsigned mantissa
= lp_mantissa(type
);
937 LLVMValueRef mask
= lp_build_int_const_scalar(type
, (unsigned long long)1 << (type
.width
- 1));
941 /* sign = a < 0 ? ~0 : 0 */
942 sign
= LLVMBuildBitCast(bld
->builder
, a
, int_vec_type
, "");
943 sign
= LLVMBuildAnd(bld
->builder
, sign
, mask
, "");
944 sign
= LLVMBuildAShr(bld
->builder
, sign
, lp_build_int_const_scalar(type
, type
.width
- 1), "");
945 lp_build_name(sign
, "floor.sign");
947 /* offset = -0.99999(9)f */
948 offset
= lp_build_const_scalar(type
, -(double)(((unsigned long long)1 << mantissa
) - 1)/((unsigned long long)1 << mantissa
));
949 offset
= LLVMConstBitCast(offset
, int_vec_type
);
951 /* offset = a < 0 ? -0.99999(9)f : 0.0f */
952 offset
= LLVMBuildAnd(bld
->builder
, offset
, sign
, "");
953 offset
= LLVMBuildBitCast(bld
->builder
, offset
, vec_type
, "");
954 lp_build_name(offset
, "floor.offset");
956 res
= LLVMBuildAdd(bld
->builder
, a
, offset
, "");
957 lp_build_name(res
, "floor.res");
960 res
= LLVMBuildFPToSI(bld
->builder
, res
, int_vec_type
, "");
961 lp_build_name(res
, "floor");
968 lp_build_iceil(struct lp_build_context
*bld
,
971 const struct lp_type type
= bld
->type
;
972 LLVMTypeRef int_vec_type
= lp_build_int_vec_type(type
);
975 assert(type
.floating
);
976 assert(lp_check_value(type
, a
));
978 if(util_cpu_caps
.has_sse4_1
) {
979 res
= lp_build_round_sse41(bld
, a
, LP_BUILD_ROUND_SSE41_CEIL
);
986 res
= LLVMBuildFPToSI(bld
->builder
, res
, int_vec_type
, "");
993 lp_build_sqrt(struct lp_build_context
*bld
,
996 const struct lp_type type
= bld
->type
;
997 LLVMTypeRef vec_type
= lp_build_vec_type(type
);
1000 /* TODO: optimize the constant case */
1001 /* TODO: optimize the constant case */
1003 assert(type
.floating
);
1004 util_snprintf(intrinsic
, sizeof intrinsic
, "llvm.sqrt.v%uf%u", type
.length
, type
.width
);
1006 return lp_build_intrinsic_unary(bld
->builder
, intrinsic
, vec_type
, a
);
1011 lp_build_rcp(struct lp_build_context
*bld
,
1014 const struct lp_type type
= bld
->type
;
1023 assert(type
.floating
);
1025 if(LLVMIsConstant(a
))
1026 return LLVMConstFDiv(bld
->one
, a
);
1028 if(util_cpu_caps
.has_sse
&& type
.width
== 32 && type
.length
== 4)
1029 /* FIXME: improve precision */
1030 return lp_build_intrinsic_unary(bld
->builder
, "llvm.x86.sse.rcp.ps", lp_build_vec_type(type
), a
);
1032 return LLVMBuildFDiv(bld
->builder
, bld
->one
, a
, "");
1037 * Generate 1/sqrt(a)
1040 lp_build_rsqrt(struct lp_build_context
*bld
,
1043 const struct lp_type type
= bld
->type
;
1045 assert(type
.floating
);
1047 if(util_cpu_caps
.has_sse
&& type
.width
== 32 && type
.length
== 4)
1048 return lp_build_intrinsic_unary(bld
->builder
, "llvm.x86.sse.rsqrt.ps", lp_build_vec_type(type
), a
);
1050 return lp_build_rcp(bld
, lp_build_sqrt(bld
, a
));
1058 lp_build_cos(struct lp_build_context
*bld
,
1061 const struct lp_type type
= bld
->type
;
1062 LLVMTypeRef vec_type
= lp_build_vec_type(type
);
1065 /* TODO: optimize the constant case */
1067 assert(type
.floating
);
1068 util_snprintf(intrinsic
, sizeof intrinsic
, "llvm.cos.v%uf%u", type
.length
, type
.width
);
1070 return lp_build_intrinsic_unary(bld
->builder
, intrinsic
, vec_type
, a
);
1078 lp_build_sin(struct lp_build_context
*bld
,
1081 const struct lp_type type
= bld
->type
;
1082 LLVMTypeRef vec_type
= lp_build_vec_type(type
);
1085 /* TODO: optimize the constant case */
1087 assert(type
.floating
);
1088 util_snprintf(intrinsic
, sizeof intrinsic
, "llvm.sin.v%uf%u", type
.length
, type
.width
);
1090 return lp_build_intrinsic_unary(bld
->builder
, intrinsic
, vec_type
, a
);
1095 * Generate pow(x, y)
1098 lp_build_pow(struct lp_build_context
*bld
,
1102 /* TODO: optimize the constant case */
1103 if(LLVMIsConstant(x
) && LLVMIsConstant(y
))
1104 debug_printf("%s: inefficient/imprecise constant arithmetic\n",
1107 return lp_build_exp2(bld
, lp_build_mul(bld
, lp_build_log2(bld
, x
), y
));
1115 lp_build_exp(struct lp_build_context
*bld
,
1118 /* log2(e) = 1/log(2) */
1119 LLVMValueRef log2e
= lp_build_const_scalar(bld
->type
, 1.4426950408889634);
1121 return lp_build_mul(bld
, log2e
, lp_build_exp2(bld
, x
));
1129 lp_build_log(struct lp_build_context
*bld
,
1133 LLVMValueRef log2
= lp_build_const_scalar(bld
->type
, 0.69314718055994529);
1135 return lp_build_mul(bld
, log2
, lp_build_exp2(bld
, x
));
1139 #define EXP_POLY_DEGREE 3
1140 #define LOG_POLY_DEGREE 5
1144 * Generate polynomial.
1145 * Ex: coeffs[0] + x * coeffs[1] + x^2 * coeffs[2].
1148 lp_build_polynomial(struct lp_build_context
*bld
,
1150 const double *coeffs
,
1151 unsigned num_coeffs
)
1153 const struct lp_type type
= bld
->type
;
1154 LLVMValueRef res
= NULL
;
1157 /* TODO: optimize the constant case */
1158 if(LLVMIsConstant(x
))
1159 debug_printf("%s: inefficient/imprecise constant arithmetic\n",
1162 for (i
= num_coeffs
; i
--; ) {
1163 LLVMValueRef coeff
= lp_build_const_scalar(type
, coeffs
[i
]);
1165 res
= lp_build_add(bld
, coeff
, lp_build_mul(bld
, x
, res
));
1178 * Minimax polynomial fit of 2**x, in range [-0.5, 0.5[
1180 const double lp_build_exp2_polynomial
[] = {
1181 #if EXP_POLY_DEGREE == 5
1182 9.9999994e-1, 6.9315308e-1, 2.4015361e-1, 5.5826318e-2, 8.9893397e-3, 1.8775767e-3
1183 #elif EXP_POLY_DEGREE == 4
1184 1.0000026, 6.9300383e-1, 2.4144275e-1, 5.2011464e-2, 1.3534167e-2
1185 #elif EXP_POLY_DEGREE == 3
1186 9.9992520e-1, 6.9583356e-1, 2.2606716e-1, 7.8024521e-2
1187 #elif EXP_POLY_DEGREE == 2
1188 1.0017247, 6.5763628e-1, 3.3718944e-1
1196 lp_build_exp2_approx(struct lp_build_context
*bld
,
1198 LLVMValueRef
*p_exp2_int_part
,
1199 LLVMValueRef
*p_frac_part
,
1200 LLVMValueRef
*p_exp2
)
1202 const struct lp_type type
= bld
->type
;
1203 LLVMTypeRef vec_type
= lp_build_vec_type(type
);
1204 LLVMTypeRef int_vec_type
= lp_build_int_vec_type(type
);
1205 LLVMValueRef ipart
= NULL
;
1206 LLVMValueRef fpart
= NULL
;
1207 LLVMValueRef expipart
= NULL
;
1208 LLVMValueRef expfpart
= NULL
;
1209 LLVMValueRef res
= NULL
;
1211 if(p_exp2_int_part
|| p_frac_part
|| p_exp2
) {
1212 /* TODO: optimize the constant case */
1213 if(LLVMIsConstant(x
))
1214 debug_printf("%s: inefficient/imprecise constant arithmetic\n",
1217 assert(type
.floating
&& type
.width
== 32);
1219 x
= lp_build_min(bld
, x
, lp_build_const_scalar(type
, 129.0));
1220 x
= lp_build_max(bld
, x
, lp_build_const_scalar(type
, -126.99999));
1222 /* ipart = int(x - 0.5) */
1223 ipart
= LLVMBuildSub(bld
->builder
, x
, lp_build_const_scalar(type
, 0.5f
), "");
1224 ipart
= LLVMBuildFPToSI(bld
->builder
, ipart
, int_vec_type
, "");
1226 /* fpart = x - ipart */
1227 fpart
= LLVMBuildSIToFP(bld
->builder
, ipart
, vec_type
, "");
1228 fpart
= LLVMBuildSub(bld
->builder
, x
, fpart
, "");
1231 if(p_exp2_int_part
|| p_exp2
) {
1232 /* expipart = (float) (1 << ipart) */
1233 expipart
= LLVMBuildAdd(bld
->builder
, ipart
, lp_build_int_const_scalar(type
, 127), "");
1234 expipart
= LLVMBuildShl(bld
->builder
, expipart
, lp_build_int_const_scalar(type
, 23), "");
1235 expipart
= LLVMBuildBitCast(bld
->builder
, expipart
, vec_type
, "");
1239 expfpart
= lp_build_polynomial(bld
, fpart
, lp_build_exp2_polynomial
,
1240 Elements(lp_build_exp2_polynomial
));
1242 res
= LLVMBuildMul(bld
->builder
, expipart
, expfpart
, "");
1246 *p_exp2_int_part
= expipart
;
1249 *p_frac_part
= fpart
;
1257 lp_build_exp2(struct lp_build_context
*bld
,
1261 lp_build_exp2_approx(bld
, x
, NULL
, NULL
, &res
);
1267 * Minimax polynomial fit of log2(x)/(x - 1), for x in range [1, 2[
1268 * These coefficients can be generate with
1269 * http://www.boost.org/doc/libs/1_36_0/libs/math/doc/sf_and_dist/html/math_toolkit/toolkit/internals2/minimax.html
1271 const double lp_build_log2_polynomial
[] = {
1272 #if LOG_POLY_DEGREE == 6
1273 3.11578814719469302614, -3.32419399085241980044, 2.59883907202499966007, -1.23152682416275988241, 0.318212422185251071475, -0.0344359067839062357313
1274 #elif LOG_POLY_DEGREE == 5
1275 2.8882704548164776201, -2.52074962577807006663, 1.48116647521213171641, -0.465725644288844778798, 0.0596515482674574969533
1276 #elif LOG_POLY_DEGREE == 4
1277 2.61761038894603480148, -1.75647175389045657003, 0.688243882994381274313, -0.107254423828329604454
1278 #elif LOG_POLY_DEGREE == 3
1279 2.28330284476918490682, -1.04913055217340124191, 0.204446009836232697516
1287 * See http://www.devmaster.net/forums/showthread.php?p=43580
1290 lp_build_log2_approx(struct lp_build_context
*bld
,
1292 LLVMValueRef
*p_exp
,
1293 LLVMValueRef
*p_floor_log2
,
1294 LLVMValueRef
*p_log2
)
1296 const struct lp_type type
= bld
->type
;
1297 LLVMTypeRef vec_type
= lp_build_vec_type(type
);
1298 LLVMTypeRef int_vec_type
= lp_build_int_vec_type(type
);
1300 LLVMValueRef expmask
= lp_build_int_const_scalar(type
, 0x7f800000);
1301 LLVMValueRef mantmask
= lp_build_int_const_scalar(type
, 0x007fffff);
1302 LLVMValueRef one
= LLVMConstBitCast(bld
->one
, int_vec_type
);
1304 LLVMValueRef i
= NULL
;
1305 LLVMValueRef exp
= NULL
;
1306 LLVMValueRef mant
= NULL
;
1307 LLVMValueRef logexp
= NULL
;
1308 LLVMValueRef logmant
= NULL
;
1309 LLVMValueRef res
= NULL
;
1311 if(p_exp
|| p_floor_log2
|| p_log2
) {
1312 /* TODO: optimize the constant case */
1313 if(LLVMIsConstant(x
))
1314 debug_printf("%s: inefficient/imprecise constant arithmetic\n",
1317 assert(type
.floating
&& type
.width
== 32);
1319 i
= LLVMBuildBitCast(bld
->builder
, x
, int_vec_type
, "");
1321 /* exp = (float) exponent(x) */
1322 exp
= LLVMBuildAnd(bld
->builder
, i
, expmask
, "");
1325 if(p_floor_log2
|| p_log2
) {
1326 logexp
= LLVMBuildLShr(bld
->builder
, exp
, lp_build_int_const_scalar(type
, 23), "");
1327 logexp
= LLVMBuildSub(bld
->builder
, logexp
, lp_build_int_const_scalar(type
, 127), "");
1328 logexp
= LLVMBuildSIToFP(bld
->builder
, logexp
, vec_type
, "");
1332 /* mant = (float) mantissa(x) */
1333 mant
= LLVMBuildAnd(bld
->builder
, i
, mantmask
, "");
1334 mant
= LLVMBuildOr(bld
->builder
, mant
, one
, "");
1335 mant
= LLVMBuildBitCast(bld
->builder
, mant
, vec_type
, "");
1337 logmant
= lp_build_polynomial(bld
, mant
, lp_build_log2_polynomial
,
1338 Elements(lp_build_log2_polynomial
));
1340 /* This effectively increases the polynomial degree by one, but ensures that log2(1) == 0*/
1341 logmant
= LLVMBuildMul(bld
->builder
, logmant
, LLVMBuildSub(bld
->builder
, mant
, bld
->one
, ""), "");
1343 res
= LLVMBuildAdd(bld
->builder
, logmant
, logexp
, "");
1350 *p_floor_log2
= logexp
;
1358 lp_build_log2(struct lp_build_context
*bld
,
1362 lp_build_log2_approx(bld
, x
, NULL
, NULL
, &res
);