OSDN Git Service

Merge branch 'mesa_7_7_branch'
[android-x86/external-mesa.git] / src / gallium / drivers / llvmpipe / lp_bld_arit.c
1 /**************************************************************************
2  *
3  * Copyright 2009 VMware, Inc.
4  * All Rights Reserved.
5  *
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:
13  *
14  * The above copyright notice and this permission notice (including the
15  * next paragraph) shall be included in all copies or substantial portions
16  * of the Software.
17  *
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.
25  *
26  **************************************************************************/
27
28
29 /**
30  * @file
31  * Helper
32  *
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.
37  *
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.
43  *
44  * @author Jose Fonseca <jfonseca@vmware.com>
45  */
46
47
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"
53
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"
61
62
63 /**
64  * Generate min(a, b)
65  * No checks for special case values of a or b = 1 or 0 are done.
66  */
67 static LLVMValueRef
68 lp_build_min_simple(struct lp_build_context *bld,
69                     LLVMValueRef a,
70                     LLVMValueRef b)
71 {
72    const struct lp_type type = bld->type;
73    const char *intrinsic = NULL;
74    LLVMValueRef cond;
75
76    /* TODO: optimize the constant case */
77
78    if(type.width * type.length == 128) {
79       if(type.floating) {
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";
84       }
85       else {
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";
98       }
99    }
100
101    if(intrinsic)
102       return lp_build_intrinsic_binary(bld->builder, intrinsic, lp_build_vec_type(bld->type), a, b);
103
104    cond = lp_build_cmp(bld, PIPE_FUNC_LESS, a, b);
105    return lp_build_select(bld, cond, a, b);
106 }
107
108
109 /**
110  * Generate max(a, b)
111  * No checks for special case values of a or b = 1 or 0 are done.
112  */
113 static LLVMValueRef
114 lp_build_max_simple(struct lp_build_context *bld,
115                     LLVMValueRef a,
116                     LLVMValueRef b)
117 {
118    const struct lp_type type = bld->type;
119    const char *intrinsic = NULL;
120    LLVMValueRef cond;
121
122    /* TODO: optimize the constant case */
123
124    if(type.width * type.length == 128) {
125       if(type.floating) {
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";
130       }
131       else {
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";
144       }
145    }
146
147    if(intrinsic)
148       return lp_build_intrinsic_binary(bld->builder, intrinsic, lp_build_vec_type(bld->type), a, b);
149
150    cond = lp_build_cmp(bld, PIPE_FUNC_GREATER, a, b);
151    return lp_build_select(bld, cond, a, b);
152 }
153
154
155 /**
156  * Generate 1 - a, or ~a depending on bld->type.
157  */
158 LLVMValueRef
159 lp_build_comp(struct lp_build_context *bld,
160               LLVMValueRef a)
161 {
162    const struct lp_type type = bld->type;
163
164    if(a == bld->one)
165       return bld->zero;
166    if(a == bld->zero)
167       return bld->one;
168
169    if(type.norm && !type.floating && !type.fixed && !type.sign) {
170       if(LLVMIsConstant(a))
171          return LLVMConstNot(a);
172       else
173          return LLVMBuildNot(bld->builder, a, "");
174    }
175
176    if(LLVMIsConstant(a))
177       return LLVMConstSub(bld->one, a);
178    else
179       return LLVMBuildSub(bld->builder, bld->one, a, "");
180 }
181
182
183 /**
184  * Generate a + b
185  */
186 LLVMValueRef
187 lp_build_add(struct lp_build_context *bld,
188              LLVMValueRef a,
189              LLVMValueRef b)
190 {
191    const struct lp_type type = bld->type;
192    LLVMValueRef res;
193
194    if(a == bld->zero)
195       return b;
196    if(b == bld->zero)
197       return a;
198    if(a == bld->undef || b == bld->undef)
199       return bld->undef;
200
201    if(bld->type.norm) {
202       const char *intrinsic = NULL;
203
204       if(a == bld->one || b == bld->one)
205         return bld->one;
206
207       if(util_cpu_caps.has_sse2 &&
208          type.width * type.length == 128 &&
209          !type.floating && !type.fixed) {
210          if(type.width == 8)
211             intrinsic = type.sign ? "llvm.x86.sse2.padds.b" : "llvm.x86.sse2.paddus.b";
212          if(type.width == 16)
213             intrinsic = type.sign ? "llvm.x86.sse2.padds.w" : "llvm.x86.sse2.paddus.w";
214       }
215    
216       if(intrinsic)
217          return lp_build_intrinsic_binary(bld->builder, intrinsic, lp_build_vec_type(bld->type), a, b);
218    }
219
220    if(LLVMIsConstant(a) && LLVMIsConstant(b))
221       res = LLVMConstAdd(a, b);
222    else
223       res = LLVMBuildAdd(bld->builder, a, b, "");
224
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);
228
229    /* XXX clamp to floor of -1 or 0??? */
230
231    return res;
232 }
233
234
235 /**
236  * Generate a - b
237  */
238 LLVMValueRef
239 lp_build_sub(struct lp_build_context *bld,
240              LLVMValueRef a,
241              LLVMValueRef b)
242 {
243    const struct lp_type type = bld->type;
244    LLVMValueRef res;
245
246    if(b == bld->zero)
247       return a;
248    if(a == bld->undef || b == bld->undef)
249       return bld->undef;
250    if(a == b)
251       return bld->zero;
252
253    if(bld->type.norm) {
254       const char *intrinsic = NULL;
255
256       if(b == bld->one)
257         return bld->zero;
258
259       if(util_cpu_caps.has_sse2 &&
260          type.width * type.length == 128 &&
261          !type.floating && !type.fixed) {
262          if(type.width == 8)
263             intrinsic = type.sign ? "llvm.x86.sse2.psubs.b" : "llvm.x86.sse2.psubus.b";
264          if(type.width == 16)
265             intrinsic = type.sign ? "llvm.x86.sse2.psubs.w" : "llvm.x86.sse2.psubus.w";
266       }
267    
268       if(intrinsic)
269          return lp_build_intrinsic_binary(bld->builder, intrinsic, lp_build_vec_type(bld->type), a, b);
270    }
271
272    if(LLVMIsConstant(a) && LLVMIsConstant(b))
273       res = LLVMConstSub(a, b);
274    else
275       res = LLVMBuildSub(bld->builder, a, b, "");
276
277    if(bld->type.norm && (bld->type.floating || bld->type.fixed))
278       res = lp_build_max_simple(bld, res, bld->zero);
279
280    return res;
281 }
282
283
284 /**
285  * Normalized 8bit multiplication.
286  *
287  * - alpha plus one
288  *
289  *     makes the following approximation to the division (Sree)
290  *    
291  *       a*b/255 ~= (a*(b + 1)) >> 256
292  *    
293  *     which is the fastest method that satisfies the following OpenGL criteria
294  *    
295  *       0*0 = 0 and 255*255 = 255
296  *
297  * - geometric series
298  *
299  *     takes the geometric series approximation to the division
300  *
301  *       t/255 = (t >> 8) + (t >> 16) + (t >> 24) ..
302  *
303  *     in this case just the first two terms to fit in 16bit arithmetic
304  *
305  *       t/255 ~= (t + (t >> 8)) >> 8
306  *
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
309  *     must be used
310  *
311  * - geometric series plus rounding
312  *
313  *     when using a geometric series division instead of truncating the result
314  *     use roundoff in the approximation (Jim Blinn)
315  *
316  *       t/255 ~= (t + (t >> 8) + 0x80) >> 8
317  *
318  *     achieving the exact results
319  *
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
324  */
325 static LLVMValueRef
326 lp_build_mul_u8n(LLVMBuilderRef builder,
327                  struct lp_type i16_type,
328                  LLVMValueRef a, LLVMValueRef b)
329 {
330    LLVMValueRef c8;
331    LLVMValueRef ab;
332
333    c8 = lp_build_int_const_scalar(i16_type, 8);
334    
335 #if 0
336    
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, "");
340
341 #else
342    
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), "");
347
348 #endif
349    
350    ab = LLVMBuildLShr(builder, ab, c8, "");
351
352    return ab;
353 }
354
355
356 /**
357  * Generate a * b
358  */
359 LLVMValueRef
360 lp_build_mul(struct lp_build_context *bld,
361              LLVMValueRef a,
362              LLVMValueRef b)
363 {
364    const struct lp_type type = bld->type;
365    LLVMValueRef shift;
366    LLVMValueRef res;
367
368    if(a == bld->zero)
369       return bld->zero;
370    if(a == bld->one)
371       return b;
372    if(b == bld->zero)
373       return bld->zero;
374    if(b == bld->one)
375       return a;
376    if(a == bld->undef || b == bld->undef)
377       return bld->undef;
378
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;
383
384          lp_build_unpack2(bld->builder, type, i16_type, a, &al, &ah);
385          lp_build_unpack2(bld->builder, type, i16_type, b, &bl, &bh);
386
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);
390
391          ab = lp_build_pack2(bld->builder, i16_type, type, abl, abh);
392          
393          return ab;
394       }
395
396       /* FIXME */
397       assert(0);
398    }
399
400    if(type.fixed)
401       shift = lp_build_int_const_scalar(type, type.width/2);
402    else
403       shift = NULL;
404
405    if(LLVMIsConstant(a) && LLVMIsConstant(b)) {
406       res =  LLVMConstMul(a, b);
407       if(shift) {
408          if(type.sign)
409             res = LLVMConstAShr(res, shift);
410          else
411             res = LLVMConstLShr(res, shift);
412       }
413    }
414    else {
415       res = LLVMBuildMul(bld->builder, a, b, "");
416       if(shift) {
417          if(type.sign)
418             res = LLVMBuildAShr(bld->builder, res, shift, "");
419          else
420             res = LLVMBuildLShr(bld->builder, res, shift, "");
421       }
422    }
423
424    return res;
425 }
426
427
428 /**
429  * Small vector x scale multiplication optimization.
430  */
431 LLVMValueRef
432 lp_build_mul_imm(struct lp_build_context *bld,
433                  LLVMValueRef a,
434                  int b)
435 {
436    LLVMValueRef factor;
437
438    if(b == 0)
439       return bld->zero;
440
441    if(b == 1)
442       return a;
443
444    if(b == -1)
445       return LLVMBuildNeg(bld->builder, a, "");
446
447    if(b == 2 && bld->type.floating)
448       return lp_build_add(bld, a, a);
449
450    if(util_is_pot(b)) {
451       unsigned shift = ffs(b) - 1;
452
453       if(bld->type.floating) {
454 #if 0
455          /*
456           * Power of two multiplication by directly manipulating the mantissa.
457           *
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
460           * for Inf and NaN.
461           */
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), "");
467          return a;
468 #endif
469       }
470       else {
471          factor = lp_build_const_scalar(bld->type, shift);
472          return LLVMBuildShl(bld->builder, a, factor, "");
473       }
474    }
475
476    factor = lp_build_const_scalar(bld->type, (double)b);
477    return lp_build_mul(bld, a, factor);
478 }
479
480
481 /**
482  * Generate a / b
483  */
484 LLVMValueRef
485 lp_build_div(struct lp_build_context *bld,
486              LLVMValueRef a,
487              LLVMValueRef b)
488 {
489    const struct lp_type type = bld->type;
490
491    if(a == bld->zero)
492       return bld->zero;
493    if(a == bld->one)
494       return lp_build_rcp(bld, b);
495    if(b == bld->zero)
496       return bld->undef;
497    if(b == bld->one)
498       return a;
499    if(a == bld->undef || b == bld->undef)
500       return bld->undef;
501
502    if(LLVMIsConstant(a) && LLVMIsConstant(b))
503       return LLVMConstFDiv(a, b);
504
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));
507
508    return LLVMBuildFDiv(bld->builder, a, b, "");
509 }
510
511
512 /**
513  * Linear interpolation.
514  *
515  * This also works for integer values with a few caveats.
516  *
517  * @sa http://www.stereopsis.com/doubleblend.html
518  */
519 LLVMValueRef
520 lp_build_lerp(struct lp_build_context *bld,
521               LLVMValueRef x,
522               LLVMValueRef v0,
523               LLVMValueRef v1)
524 {
525    LLVMValueRef delta;
526    LLVMValueRef res;
527
528    delta = lp_build_sub(bld, v1, v0);
529
530    res = lp_build_mul(bld, x, delta);
531
532    res = lp_build_add(bld, v0, res);
533
534    if(bld->type.fixed)
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), "");
540
541    return res;
542 }
543
544
545 LLVMValueRef
546 lp_build_lerp_2d(struct lp_build_context *bld,
547                  LLVMValueRef x,
548                  LLVMValueRef y,
549                  LLVMValueRef v00,
550                  LLVMValueRef v01,
551                  LLVMValueRef v10,
552                  LLVMValueRef v11)
553 {
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);
557 }
558
559
560 /**
561  * Generate min(a, b)
562  * Do checks for special cases.
563  */
564 LLVMValueRef
565 lp_build_min(struct lp_build_context *bld,
566              LLVMValueRef a,
567              LLVMValueRef b)
568 {
569    if(a == bld->undef || b == bld->undef)
570       return bld->undef;
571
572    if(a == b)
573       return a;
574
575    if(bld->type.norm) {
576       if(a == bld->zero || b == bld->zero)
577          return bld->zero;
578       if(a == bld->one)
579          return b;
580       if(b == bld->one)
581          return a;
582    }
583
584    return lp_build_min_simple(bld, a, b);
585 }
586
587
588 /**
589  * Generate max(a, b)
590  * Do checks for special cases.
591  */
592 LLVMValueRef
593 lp_build_max(struct lp_build_context *bld,
594              LLVMValueRef a,
595              LLVMValueRef b)
596 {
597    if(a == bld->undef || b == bld->undef)
598       return bld->undef;
599
600    if(a == b)
601       return a;
602
603    if(bld->type.norm) {
604       if(a == bld->one || b == bld->one)
605          return bld->one;
606       if(a == bld->zero)
607          return b;
608       if(b == bld->zero)
609          return a;
610    }
611
612    return lp_build_max_simple(bld, a, b);
613 }
614
615
616 /**
617  * Generate abs(a)
618  */
619 LLVMValueRef
620 lp_build_abs(struct lp_build_context *bld,
621              LLVMValueRef a)
622 {
623    const struct lp_type type = bld->type;
624    LLVMTypeRef vec_type = lp_build_vec_type(type);
625
626    if(!type.sign)
627       return a;
628
629    if(type.floating) {
630       /* Mask out the sign bit */
631       LLVMTypeRef int_vec_type = lp_build_int_vec_type(type);
632       unsigned long absMask = ~(1 << (type.width - 1));
633       LLVMValueRef mask = lp_build_int_const_scalar(type, ((unsigned long long) absMask));
634       a = LLVMBuildBitCast(bld->builder, a, int_vec_type, "");
635       a = LLVMBuildAnd(bld->builder, a, mask, "");
636       a = LLVMBuildBitCast(bld->builder, a, vec_type, "");
637       return a;
638    }
639
640    if(type.width*type.length == 128 && util_cpu_caps.has_ssse3) {
641       switch(type.width) {
642       case 8:
643          return lp_build_intrinsic_unary(bld->builder, "llvm.x86.ssse3.pabs.b.128", vec_type, a);
644       case 16:
645          return lp_build_intrinsic_unary(bld->builder, "llvm.x86.ssse3.pabs.w.128", vec_type, a);
646       case 32:
647          return lp_build_intrinsic_unary(bld->builder, "llvm.x86.ssse3.pabs.d.128", vec_type, a);
648       }
649    }
650
651    return lp_build_max(bld, a, LLVMBuildNeg(bld->builder, a, ""));
652 }
653
654
655 LLVMValueRef
656 lp_build_sgn(struct lp_build_context *bld,
657              LLVMValueRef a)
658 {
659    const struct lp_type type = bld->type;
660    LLVMTypeRef vec_type = lp_build_vec_type(type);
661    LLVMValueRef cond;
662    LLVMValueRef res;
663
664    /* Handle non-zero case */
665    if(!type.sign) {
666       /* if not zero then sign must be positive */
667       res = bld->one;
668    }
669    else if(type.floating) {
670       /* Take the sign bit and add it to 1 constant */
671       LLVMTypeRef int_vec_type = lp_build_int_vec_type(type);
672       LLVMValueRef mask = lp_build_int_const_scalar(type, (unsigned long long)1 << (type.width - 1));
673       LLVMValueRef sign;
674       LLVMValueRef one;
675       sign = LLVMBuildBitCast(bld->builder, a, int_vec_type, "");
676       sign = LLVMBuildAnd(bld->builder, sign, mask, "");
677       one = LLVMConstBitCast(bld->one, int_vec_type);
678       res = LLVMBuildOr(bld->builder, sign, one, "");
679       res = LLVMBuildBitCast(bld->builder, res, vec_type, "");
680    }
681    else
682    {
683       LLVMValueRef minus_one = lp_build_const_scalar(type, -1.0);
684       cond = lp_build_cmp(bld, PIPE_FUNC_GREATER, a, bld->zero);
685       res = lp_build_select(bld, cond, bld->one, minus_one);
686    }
687
688    /* Handle zero */
689    cond = lp_build_cmp(bld, PIPE_FUNC_EQUAL, a, bld->zero);
690    res = lp_build_select(bld, cond, bld->zero, bld->one);
691
692    return res;
693 }
694
695
696 enum lp_build_round_sse41_mode
697 {
698    LP_BUILD_ROUND_SSE41_NEAREST = 0,
699    LP_BUILD_ROUND_SSE41_FLOOR = 1,
700    LP_BUILD_ROUND_SSE41_CEIL = 2,
701    LP_BUILD_ROUND_SSE41_TRUNCATE = 3
702 };
703
704
705 static INLINE LLVMValueRef
706 lp_build_round_sse41(struct lp_build_context *bld,
707                      LLVMValueRef a,
708                      enum lp_build_round_sse41_mode mode)
709 {
710    const struct lp_type type = bld->type;
711    LLVMTypeRef vec_type = lp_build_vec_type(type);
712    const char *intrinsic;
713
714    assert(type.floating);
715    assert(type.width*type.length == 128);
716    assert(lp_check_value(type, a));
717    assert(util_cpu_caps.has_sse4_1);
718
719    switch(type.width) {
720    case 32:
721       intrinsic = "llvm.x86.sse41.round.ps";
722       break;
723    case 64:
724       intrinsic = "llvm.x86.sse41.round.pd";
725       break;
726    default:
727       assert(0);
728       return bld->undef;
729    }
730
731    return lp_build_intrinsic_binary(bld->builder, intrinsic, vec_type, a,
732                                     LLVMConstInt(LLVMInt32Type(), mode, 0));
733 }
734
735
736 LLVMValueRef
737 lp_build_trunc(struct lp_build_context *bld,
738                LLVMValueRef a)
739 {
740    const struct lp_type type = bld->type;
741
742    assert(type.floating);
743    assert(lp_check_value(type, a));
744
745    if(util_cpu_caps.has_sse4_1)
746       return lp_build_round_sse41(bld, a, LP_BUILD_ROUND_SSE41_TRUNCATE);
747    else {
748       LLVMTypeRef vec_type = lp_build_vec_type(type);
749       LLVMTypeRef int_vec_type = lp_build_int_vec_type(type);
750       LLVMValueRef res;
751       res = LLVMBuildFPToSI(bld->builder, a, int_vec_type, "");
752       res = LLVMBuildSIToFP(bld->builder, res, vec_type, "");
753       return res;
754    }
755 }
756
757
758 LLVMValueRef
759 lp_build_round(struct lp_build_context *bld,
760                LLVMValueRef a)
761 {
762    const struct lp_type type = bld->type;
763
764    assert(type.floating);
765    assert(lp_check_value(type, a));
766
767    if(util_cpu_caps.has_sse4_1)
768       return lp_build_round_sse41(bld, a, LP_BUILD_ROUND_SSE41_NEAREST);
769    else {
770       LLVMTypeRef vec_type = lp_build_vec_type(type);
771       LLVMValueRef res;
772       res = lp_build_iround(bld, a);
773       res = LLVMBuildSIToFP(bld->builder, res, vec_type, "");
774       return res;
775    }
776 }
777
778
779 LLVMValueRef
780 lp_build_floor(struct lp_build_context *bld,
781                LLVMValueRef a)
782 {
783    const struct lp_type type = bld->type;
784
785    assert(type.floating);
786
787    if(util_cpu_caps.has_sse4_1)
788       return lp_build_round_sse41(bld, a, LP_BUILD_ROUND_SSE41_FLOOR);
789    else {
790       LLVMTypeRef vec_type = lp_build_vec_type(type);
791       LLVMValueRef res;
792       res = lp_build_ifloor(bld, a);
793       res = LLVMBuildSIToFP(bld->builder, res, vec_type, "");
794       return res;
795    }
796 }
797
798
799 LLVMValueRef
800 lp_build_ceil(struct lp_build_context *bld,
801               LLVMValueRef a)
802 {
803    const struct lp_type type = bld->type;
804
805    assert(type.floating);
806    assert(lp_check_value(type, a));
807
808    if(util_cpu_caps.has_sse4_1)
809       return lp_build_round_sse41(bld, a, LP_BUILD_ROUND_SSE41_CEIL);
810    else {
811       LLVMTypeRef vec_type = lp_build_vec_type(type);
812       LLVMValueRef res;
813       res = lp_build_iceil(bld, a);
814       res = LLVMBuildSIToFP(bld->builder, res, vec_type, "");
815       return res;
816    }
817 }
818
819
820 /**
821  * Convert to integer, through whichever rounding method that's fastest,
822  * typically truncating to zero.
823  */
824 LLVMValueRef
825 lp_build_itrunc(struct lp_build_context *bld,
826                 LLVMValueRef a)
827 {
828    const struct lp_type type = bld->type;
829    LLVMTypeRef int_vec_type = lp_build_int_vec_type(type);
830
831    assert(type.floating);
832    assert(lp_check_value(type, a));
833
834    return LLVMBuildFPToSI(bld->builder, a, int_vec_type, "");
835 }
836
837
838 LLVMValueRef
839 lp_build_iround(struct lp_build_context *bld,
840                 LLVMValueRef a)
841 {
842    const struct lp_type type = bld->type;
843    LLVMTypeRef int_vec_type = lp_build_int_vec_type(type);
844    LLVMValueRef res;
845
846    assert(type.floating);
847    assert(lp_check_value(type, a));
848
849    if(util_cpu_caps.has_sse4_1) {
850       res = lp_build_round_sse41(bld, a, LP_BUILD_ROUND_SSE41_NEAREST);
851    }
852    else {
853       LLVMTypeRef vec_type = lp_build_vec_type(type);
854       LLVMValueRef mask = lp_build_int_const_scalar(type, (unsigned long long)1 << (type.width - 1));
855       LLVMValueRef sign;
856       LLVMValueRef half;
857
858       /* get sign bit */
859       sign = LLVMBuildBitCast(bld->builder, a, int_vec_type, "");
860       sign = LLVMBuildAnd(bld->builder, sign, mask, "");
861
862       /* sign * 0.5 */
863       half = lp_build_const_scalar(type, 0.5);
864       half = LLVMBuildBitCast(bld->builder, half, int_vec_type, "");
865       half = LLVMBuildOr(bld->builder, sign, half, "");
866       half = LLVMBuildBitCast(bld->builder, half, vec_type, "");
867
868       res = LLVMBuildAdd(bld->builder, a, half, "");
869    }
870
871    res = LLVMBuildFPToSI(bld->builder, res, int_vec_type, "");
872
873    return res;
874 }
875
876
877 LLVMValueRef
878 lp_build_ifloor(struct lp_build_context *bld,
879                 LLVMValueRef a)
880 {
881    const struct lp_type type = bld->type;
882    LLVMTypeRef int_vec_type = lp_build_int_vec_type(type);
883    LLVMValueRef res;
884
885    assert(type.floating);
886    assert(lp_check_value(type, a));
887
888    if(util_cpu_caps.has_sse4_1) {
889       res = lp_build_round_sse41(bld, a, LP_BUILD_ROUND_SSE41_FLOOR);
890    }
891    else {
892       /* Take the sign bit and add it to 1 constant */
893       LLVMTypeRef vec_type = lp_build_vec_type(type);
894       unsigned mantissa = lp_mantissa(type);
895       LLVMValueRef mask = lp_build_int_const_scalar(type, (unsigned long long)1 << (type.width - 1));
896       LLVMValueRef sign;
897       LLVMValueRef offset;
898
899       /* sign = a < 0 ? ~0 : 0 */
900       sign = LLVMBuildBitCast(bld->builder, a, int_vec_type, "");
901       sign = LLVMBuildAnd(bld->builder, sign, mask, "");
902       sign = LLVMBuildAShr(bld->builder, sign, lp_build_int_const_scalar(type, type.width - 1), "");
903
904       /* offset = -0.99999(9)f */
905       offset = lp_build_const_scalar(type, -(double)(((unsigned long long)1 << mantissa) - 1)/((unsigned long long)1 << mantissa));
906       offset = LLVMConstBitCast(offset, int_vec_type);
907
908       /* offset = a < 0 ? -0.99999(9)f : 0.0f */
909       offset = LLVMBuildAnd(bld->builder, offset, sign, "");
910       offset = LLVMBuildBitCast(bld->builder, offset, vec_type, "");
911
912       res = LLVMBuildAdd(bld->builder, a, offset, "");
913    }
914
915    res = LLVMBuildFPToSI(bld->builder, res, int_vec_type, "");
916
917    return res;
918 }
919
920
921 LLVMValueRef
922 lp_build_iceil(struct lp_build_context *bld,
923                LLVMValueRef a)
924 {
925    const struct lp_type type = bld->type;
926    LLVMTypeRef int_vec_type = lp_build_int_vec_type(type);
927    LLVMValueRef res;
928
929    assert(type.floating);
930    assert(lp_check_value(type, a));
931
932    if(util_cpu_caps.has_sse4_1) {
933       res = lp_build_round_sse41(bld, a, LP_BUILD_ROUND_SSE41_CEIL);
934    }
935    else {
936       assert(0);
937       res = bld->undef;
938    }
939
940    res = LLVMBuildFPToSI(bld->builder, res, int_vec_type, "");
941
942    return res;
943 }
944
945
946 LLVMValueRef
947 lp_build_sqrt(struct lp_build_context *bld,
948               LLVMValueRef a)
949 {
950    const struct lp_type type = bld->type;
951    LLVMTypeRef vec_type = lp_build_vec_type(type);
952    char intrinsic[32];
953
954    /* TODO: optimize the constant case */
955    /* TODO: optimize the constant case */
956
957    assert(type.floating);
958    util_snprintf(intrinsic, sizeof intrinsic, "llvm.sqrt.v%uf%u", type.length, type.width);
959
960    return lp_build_intrinsic_unary(bld->builder, intrinsic, vec_type, a);
961 }
962
963
964 LLVMValueRef
965 lp_build_rcp(struct lp_build_context *bld,
966              LLVMValueRef a)
967 {
968    const struct lp_type type = bld->type;
969
970    if(a == bld->zero)
971       return bld->undef;
972    if(a == bld->one)
973       return bld->one;
974    if(a == bld->undef)
975       return bld->undef;
976
977    assert(type.floating);
978
979    if(LLVMIsConstant(a))
980       return LLVMConstFDiv(bld->one, a);
981
982    if(util_cpu_caps.has_sse && type.width == 32 && type.length == 4)
983       /* FIXME: improve precision */
984       return lp_build_intrinsic_unary(bld->builder, "llvm.x86.sse.rcp.ps", lp_build_vec_type(type), a);
985
986    return LLVMBuildFDiv(bld->builder, bld->one, a, "");
987 }
988
989
990 /**
991  * Generate 1/sqrt(a)
992  */
993 LLVMValueRef
994 lp_build_rsqrt(struct lp_build_context *bld,
995                LLVMValueRef a)
996 {
997    const struct lp_type type = bld->type;
998
999    assert(type.floating);
1000
1001    if(util_cpu_caps.has_sse && type.width == 32 && type.length == 4)
1002       return lp_build_intrinsic_unary(bld->builder, "llvm.x86.sse.rsqrt.ps", lp_build_vec_type(type), a);
1003
1004    return lp_build_rcp(bld, lp_build_sqrt(bld, a));
1005 }
1006
1007
1008 /**
1009  * Generate cos(a)
1010  */
1011 LLVMValueRef
1012 lp_build_cos(struct lp_build_context *bld,
1013               LLVMValueRef a)
1014 {
1015    const struct lp_type type = bld->type;
1016    LLVMTypeRef vec_type = lp_build_vec_type(type);
1017    char intrinsic[32];
1018
1019    /* TODO: optimize the constant case */
1020
1021    assert(type.floating);
1022    util_snprintf(intrinsic, sizeof intrinsic, "llvm.cos.v%uf%u", type.length, type.width);
1023
1024    return lp_build_intrinsic_unary(bld->builder, intrinsic, vec_type, a);
1025 }
1026
1027
1028 /**
1029  * Generate sin(a)
1030  */
1031 LLVMValueRef
1032 lp_build_sin(struct lp_build_context *bld,
1033               LLVMValueRef a)
1034 {
1035    const struct lp_type type = bld->type;
1036    LLVMTypeRef vec_type = lp_build_vec_type(type);
1037    char intrinsic[32];
1038
1039    /* TODO: optimize the constant case */
1040
1041    assert(type.floating);
1042    util_snprintf(intrinsic, sizeof intrinsic, "llvm.sin.v%uf%u", type.length, type.width);
1043
1044    return lp_build_intrinsic_unary(bld->builder, intrinsic, vec_type, a);
1045 }
1046
1047
1048 /**
1049  * Generate pow(x, y)
1050  */
1051 LLVMValueRef
1052 lp_build_pow(struct lp_build_context *bld,
1053              LLVMValueRef x,
1054              LLVMValueRef y)
1055 {
1056    /* TODO: optimize the constant case */
1057    if(LLVMIsConstant(x) && LLVMIsConstant(y))
1058       debug_printf("%s: inefficient/imprecise constant arithmetic\n",
1059                    __FUNCTION__);
1060
1061    return lp_build_exp2(bld, lp_build_mul(bld, lp_build_log2(bld, x), y));
1062 }
1063
1064
1065 /**
1066  * Generate exp(x)
1067  */
1068 LLVMValueRef
1069 lp_build_exp(struct lp_build_context *bld,
1070              LLVMValueRef x)
1071 {
1072    /* log2(e) = 1/log(2) */
1073    LLVMValueRef log2e = lp_build_const_scalar(bld->type, 1.4426950408889634);
1074
1075    return lp_build_mul(bld, log2e, lp_build_exp2(bld, x));
1076 }
1077
1078
1079 /**
1080  * Generate log(x)
1081  */
1082 LLVMValueRef
1083 lp_build_log(struct lp_build_context *bld,
1084              LLVMValueRef x)
1085 {
1086    /* log(2) */
1087    LLVMValueRef log2 = lp_build_const_scalar(bld->type, 0.69314718055994529);
1088
1089    return lp_build_mul(bld, log2, lp_build_exp2(bld, x));
1090 }
1091
1092
1093 #define EXP_POLY_DEGREE 3
1094 #define LOG_POLY_DEGREE 5
1095
1096
1097 /**
1098  * Generate polynomial.
1099  * Ex:  coeffs[0] + x * coeffs[1] + x^2 * coeffs[2].
1100  */
1101 static LLVMValueRef
1102 lp_build_polynomial(struct lp_build_context *bld,
1103                     LLVMValueRef x,
1104                     const double *coeffs,
1105                     unsigned num_coeffs)
1106 {
1107    const struct lp_type type = bld->type;
1108    LLVMValueRef res = NULL;
1109    unsigned i;
1110
1111    /* TODO: optimize the constant case */
1112    if(LLVMIsConstant(x))
1113       debug_printf("%s: inefficient/imprecise constant arithmetic\n",
1114                    __FUNCTION__);
1115
1116    for (i = num_coeffs; i--; ) {
1117       LLVMValueRef coeff = lp_build_const_scalar(type, coeffs[i]);
1118       if(res)
1119          res = lp_build_add(bld, coeff, lp_build_mul(bld, x, res));
1120       else
1121          res = coeff;
1122    }
1123
1124    if(res)
1125       return res;
1126    else
1127       return bld->undef;
1128 }
1129
1130
1131 /**
1132  * Minimax polynomial fit of 2**x, in range [-0.5, 0.5[
1133  */
1134 const double lp_build_exp2_polynomial[] = {
1135 #if EXP_POLY_DEGREE == 5
1136    9.9999994e-1, 6.9315308e-1, 2.4015361e-1, 5.5826318e-2, 8.9893397e-3, 1.8775767e-3
1137 #elif EXP_POLY_DEGREE == 4
1138    1.0000026, 6.9300383e-1, 2.4144275e-1, 5.2011464e-2, 1.3534167e-2
1139 #elif EXP_POLY_DEGREE == 3
1140    9.9992520e-1, 6.9583356e-1, 2.2606716e-1, 7.8024521e-2
1141 #elif EXP_POLY_DEGREE == 2
1142    1.0017247, 6.5763628e-1, 3.3718944e-1
1143 #else
1144 #error
1145 #endif
1146 };
1147
1148
1149 void
1150 lp_build_exp2_approx(struct lp_build_context *bld,
1151                      LLVMValueRef x,
1152                      LLVMValueRef *p_exp2_int_part,
1153                      LLVMValueRef *p_frac_part,
1154                      LLVMValueRef *p_exp2)
1155 {
1156    const struct lp_type type = bld->type;
1157    LLVMTypeRef vec_type = lp_build_vec_type(type);
1158    LLVMTypeRef int_vec_type = lp_build_int_vec_type(type);
1159    LLVMValueRef ipart = NULL;
1160    LLVMValueRef fpart = NULL;
1161    LLVMValueRef expipart = NULL;
1162    LLVMValueRef expfpart = NULL;
1163    LLVMValueRef res = NULL;
1164
1165    if(p_exp2_int_part || p_frac_part || p_exp2) {
1166       /* TODO: optimize the constant case */
1167       if(LLVMIsConstant(x))
1168          debug_printf("%s: inefficient/imprecise constant arithmetic\n",
1169                       __FUNCTION__);
1170
1171       assert(type.floating && type.width == 32);
1172
1173       x = lp_build_min(bld, x, lp_build_const_scalar(type,  129.0));
1174       x = lp_build_max(bld, x, lp_build_const_scalar(type, -126.99999));
1175
1176       /* ipart = int(x - 0.5) */
1177       ipart = LLVMBuildSub(bld->builder, x, lp_build_const_scalar(type, 0.5f), "");
1178       ipart = LLVMBuildFPToSI(bld->builder, ipart, int_vec_type, "");
1179
1180       /* fpart = x - ipart */
1181       fpart = LLVMBuildSIToFP(bld->builder, ipart, vec_type, "");
1182       fpart = LLVMBuildSub(bld->builder, x, fpart, "");
1183    }
1184
1185    if(p_exp2_int_part || p_exp2) {
1186       /* expipart = (float) (1 << ipart) */
1187       expipart = LLVMBuildAdd(bld->builder, ipart, lp_build_int_const_scalar(type, 127), "");
1188       expipart = LLVMBuildShl(bld->builder, expipart, lp_build_int_const_scalar(type, 23), "");
1189       expipart = LLVMBuildBitCast(bld->builder, expipart, vec_type, "");
1190    }
1191
1192    if(p_exp2) {
1193       expfpart = lp_build_polynomial(bld, fpart, lp_build_exp2_polynomial,
1194                                      Elements(lp_build_exp2_polynomial));
1195
1196       res = LLVMBuildMul(bld->builder, expipart, expfpart, "");
1197    }
1198
1199    if(p_exp2_int_part)
1200       *p_exp2_int_part = expipart;
1201
1202    if(p_frac_part)
1203       *p_frac_part = fpart;
1204
1205    if(p_exp2)
1206       *p_exp2 = res;
1207 }
1208
1209
1210 LLVMValueRef
1211 lp_build_exp2(struct lp_build_context *bld,
1212               LLVMValueRef x)
1213 {
1214    LLVMValueRef res;
1215    lp_build_exp2_approx(bld, x, NULL, NULL, &res);
1216    return res;
1217 }
1218
1219
1220 /**
1221  * Minimax polynomial fit of log2(x)/(x - 1), for x in range [1, 2[
1222  * These coefficients can be generate with
1223  * http://www.boost.org/doc/libs/1_36_0/libs/math/doc/sf_and_dist/html/math_toolkit/toolkit/internals2/minimax.html
1224  */
1225 const double lp_build_log2_polynomial[] = {
1226 #if LOG_POLY_DEGREE == 6
1227    3.11578814719469302614, -3.32419399085241980044, 2.59883907202499966007, -1.23152682416275988241, 0.318212422185251071475, -0.0344359067839062357313
1228 #elif LOG_POLY_DEGREE == 5
1229    2.8882704548164776201, -2.52074962577807006663, 1.48116647521213171641, -0.465725644288844778798, 0.0596515482674574969533
1230 #elif LOG_POLY_DEGREE == 4
1231    2.61761038894603480148, -1.75647175389045657003, 0.688243882994381274313, -0.107254423828329604454
1232 #elif LOG_POLY_DEGREE == 3
1233    2.28330284476918490682, -1.04913055217340124191, 0.204446009836232697516
1234 #else
1235 #error
1236 #endif
1237 };
1238
1239
1240 /**
1241  * See http://www.devmaster.net/forums/showthread.php?p=43580
1242  */
1243 void
1244 lp_build_log2_approx(struct lp_build_context *bld,
1245                      LLVMValueRef x,
1246                      LLVMValueRef *p_exp,
1247                      LLVMValueRef *p_floor_log2,
1248                      LLVMValueRef *p_log2)
1249 {
1250    const struct lp_type type = bld->type;
1251    LLVMTypeRef vec_type = lp_build_vec_type(type);
1252    LLVMTypeRef int_vec_type = lp_build_int_vec_type(type);
1253
1254    LLVMValueRef expmask = lp_build_int_const_scalar(type, 0x7f800000);
1255    LLVMValueRef mantmask = lp_build_int_const_scalar(type, 0x007fffff);
1256    LLVMValueRef one = LLVMConstBitCast(bld->one, int_vec_type);
1257
1258    LLVMValueRef i = NULL;
1259    LLVMValueRef exp = NULL;
1260    LLVMValueRef mant = NULL;
1261    LLVMValueRef logexp = NULL;
1262    LLVMValueRef logmant = NULL;
1263    LLVMValueRef res = NULL;
1264
1265    if(p_exp || p_floor_log2 || p_log2) {
1266       /* TODO: optimize the constant case */
1267       if(LLVMIsConstant(x))
1268          debug_printf("%s: inefficient/imprecise constant arithmetic\n",
1269                       __FUNCTION__);
1270
1271       assert(type.floating && type.width == 32);
1272
1273       i = LLVMBuildBitCast(bld->builder, x, int_vec_type, "");
1274
1275       /* exp = (float) exponent(x) */
1276       exp = LLVMBuildAnd(bld->builder, i, expmask, "");
1277    }
1278
1279    if(p_floor_log2 || p_log2) {
1280       logexp = LLVMBuildLShr(bld->builder, exp, lp_build_int_const_scalar(type, 23), "");
1281       logexp = LLVMBuildSub(bld->builder, logexp, lp_build_int_const_scalar(type, 127), "");
1282       logexp = LLVMBuildSIToFP(bld->builder, logexp, vec_type, "");
1283    }
1284
1285    if(p_log2) {
1286       /* mant = (float) mantissa(x) */
1287       mant = LLVMBuildAnd(bld->builder, i, mantmask, "");
1288       mant = LLVMBuildOr(bld->builder, mant, one, "");
1289       mant = LLVMBuildBitCast(bld->builder, mant, vec_type, "");
1290
1291       logmant = lp_build_polynomial(bld, mant, lp_build_log2_polynomial,
1292                                     Elements(lp_build_log2_polynomial));
1293
1294       /* This effectively increases the polynomial degree by one, but ensures that log2(1) == 0*/
1295       logmant = LLVMBuildMul(bld->builder, logmant, LLVMBuildSub(bld->builder, mant, bld->one, ""), "");
1296
1297       res = LLVMBuildAdd(bld->builder, logmant, logexp, "");
1298    }
1299
1300    if(p_exp)
1301       *p_exp = exp;
1302
1303    if(p_floor_log2)
1304       *p_floor_log2 = logexp;
1305
1306    if(p_log2)
1307       *p_log2 = res;
1308 }
1309
1310
1311 LLVMValueRef
1312 lp_build_log2(struct lp_build_context *bld,
1313               LLVMValueRef x)
1314 {
1315    LLVMValueRef res;
1316    lp_build_log2_approx(bld, x, NULL, NULL, &res);
1317    return res;
1318 }