~ubuntu-branches/ubuntu/natty/mesa/natty-proposed

« back to all changes in this revision

Viewing changes to src/gallium/auxiliary/gallivm/lp_bld_arit.c

  • Committer: Bazaar Package Importer
  • Author(s): Robert Hooker, Robert Hooker, Christopher James Halse Rogers
  • Date: 2010-09-14 08:55:40 UTC
  • mfrom: (1.2.28 upstream)
  • Revision ID: james.westby@ubuntu.com-20100914085540-m4fpl0hdjlfd4jgz
Tags: 7.9~git20100909-0ubuntu1
[ Robert Hooker ]
* New upstream git snapshot up to commit 94118fe2d4b1e5 (LP: #631413)
* New features include ATI HD5xxx series support in r600, and a vastly
  improved glsl compiler.
* Remove pre-generated .pc's, use the ones generated at build time
  instead.
* Remove all references to mesa-utils now that its no longer shipped
  with the mesa source.
* Disable the experimental ARB_fragment_shader option by default on
  i915, it exposes incomplete functionality that breaks KDE compositing
  among other things. It can be enabled via driconf still. (LP: #628930).

[ Christopher James Halse Rogers ]
* debian/patches/04_osmesa_version.diff:
  - Refresh for new upstream
* Bugs fixed in this release:
  - Fixes severe rendering corruption in Unity on radeon (LP: #628727,
    LP: #596292, LP: #599741, LP: #630315, LP: #613694, LP: #599741).
  - Also fixes rendering in gnome-shell (LP: #578619).
  - Flickering in OpenGL apps on radeon (LP: #626943, LP: #610541).
  - Provides preliminary support for new intel chips (LP: #601052).
* debian/rules:
  - Update configure flags to match upstream reshuffling.
  - Explicitly remove gallium DRI drivers that we don't want to ship.
* Update debian/gbp.conf for this Maverick-specific packaging
* libegl1-mesa-dri-x11,kms: There are no longer separate kms or x11 drivers
  for EGL, libegl1-mesa-drivers now contains a single driver that provides
  both backends.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/**************************************************************************
2
2
 *
3
 
 * Copyright 2009 VMware, Inc.
 
3
 * Copyright 2009-2010 VMware, Inc.
4
4
 * All Rights Reserved.
5
5
 *
6
6
 * Permission is hereby granted, free of charge, to any person obtaining a
56
56
#include "lp_bld_intr.h"
57
57
#include "lp_bld_logic.h"
58
58
#include "lp_bld_pack.h"
59
 
#include "lp_bld_debug.h"
60
59
#include "lp_bld_arit.h"
61
60
 
62
61
 
 
62
#define EXP_POLY_DEGREE 3
 
63
 
 
64
#define LOG_POLY_DEGREE 5
 
65
 
 
66
 
63
67
/**
64
68
 * Generate min(a, b)
65
69
 * No checks for special case values of a or b = 1 or 0 are done.
73
77
   const char *intrinsic = NULL;
74
78
   LLVMValueRef cond;
75
79
 
 
80
   assert(lp_check_value(type, a));
 
81
   assert(lp_check_value(type, b));
 
82
 
76
83
   /* TODO: optimize the constant case */
77
84
 
78
85
   if(type.width * type.length == 128) {
119
126
   const char *intrinsic = NULL;
120
127
   LLVMValueRef cond;
121
128
 
 
129
   assert(lp_check_value(type, a));
 
130
   assert(lp_check_value(type, b));
 
131
 
122
132
   /* TODO: optimize the constant case */
123
133
 
124
134
   if(type.width * type.length == 128) {
161
171
{
162
172
   const struct lp_type type = bld->type;
163
173
 
 
174
   assert(lp_check_value(type, a));
 
175
 
164
176
   if(a == bld->one)
165
177
      return bld->zero;
166
178
   if(a == bld->zero)
174
186
   }
175
187
 
176
188
   if(LLVMIsConstant(a))
177
 
      return LLVMConstSub(bld->one, a);
 
189
      if (type.floating)
 
190
          return LLVMConstFSub(bld->one, a);
 
191
      else
 
192
          return LLVMConstSub(bld->one, a);
178
193
   else
179
 
      return LLVMBuildSub(bld->builder, bld->one, a, "");
 
194
      if (type.floating)
 
195
         return LLVMBuildFSub(bld->builder, bld->one, a, "");
 
196
      else
 
197
         return LLVMBuildSub(bld->builder, bld->one, a, "");
180
198
}
181
199
 
182
200
 
191
209
   const struct lp_type type = bld->type;
192
210
   LLVMValueRef res;
193
211
 
 
212
   assert(lp_check_value(type, a));
 
213
   assert(lp_check_value(type, b));
 
214
 
194
215
   if(a == bld->zero)
195
216
      return b;
196
217
   if(b == bld->zero)
218
239
   }
219
240
 
220
241
   if(LLVMIsConstant(a) && LLVMIsConstant(b))
221
 
      res = LLVMConstAdd(a, b);
 
242
      if (type.floating)
 
243
         res = LLVMConstFAdd(a, b);
 
244
      else
 
245
         res = LLVMConstAdd(a, b);
222
246
   else
223
 
      res = LLVMBuildAdd(bld->builder, a, b, "");
 
247
      if (type.floating)
 
248
         res = LLVMBuildFAdd(bld->builder, a, b, "");
 
249
      else
 
250
         res = LLVMBuildAdd(bld->builder, a, b, "");
224
251
 
225
252
   /* clamp to ceiling of 1.0 */
226
253
   if(bld->type.norm && (bld->type.floating || bld->type.fixed))
232
259
}
233
260
 
234
261
 
 
262
/** Return the scalar sum of the elements of a */
 
263
LLVMValueRef
 
264
lp_build_sum_vector(struct lp_build_context *bld,
 
265
                    LLVMValueRef a)
 
266
{
 
267
   const struct lp_type type = bld->type;
 
268
   LLVMValueRef index, res;
 
269
   unsigned i;
 
270
 
 
271
   assert(lp_check_value(type, a));
 
272
 
 
273
   if (type.length == 1) {
 
274
      return a;
 
275
   }
 
276
 
 
277
   assert(!bld->type.norm);
 
278
 
 
279
   index = LLVMConstInt(LLVMInt32Type(), 0, 0);
 
280
   res = LLVMBuildExtractElement(bld->builder, a, index, "");
 
281
 
 
282
   for (i = 1; i < type.length; i++) {
 
283
      index = LLVMConstInt(LLVMInt32Type(), i, 0);
 
284
      if (type.floating)
 
285
         res = LLVMBuildFAdd(bld->builder, res,
 
286
                            LLVMBuildExtractElement(bld->builder,
 
287
                                                    a, index, ""),
 
288
                            "");
 
289
      else
 
290
         res = LLVMBuildAdd(bld->builder, res,
 
291
                            LLVMBuildExtractElement(bld->builder,
 
292
                                                    a, index, ""),
 
293
                            "");
 
294
   }
 
295
 
 
296
   return res;
 
297
}
 
298
 
 
299
 
235
300
/**
236
301
 * Generate a - b
237
302
 */
243
308
   const struct lp_type type = bld->type;
244
309
   LLVMValueRef res;
245
310
 
 
311
   assert(lp_check_value(type, a));
 
312
   assert(lp_check_value(type, b));
 
313
 
246
314
   if(b == bld->zero)
247
315
      return a;
248
316
   if(a == bld->undef || b == bld->undef)
270
338
   }
271
339
 
272
340
   if(LLVMIsConstant(a) && LLVMIsConstant(b))
273
 
      res = LLVMConstSub(a, b);
 
341
      if (type.floating)
 
342
         res = LLVMConstFSub(a, b);
 
343
      else
 
344
         res = LLVMConstSub(a, b);
274
345
   else
275
 
      res = LLVMBuildSub(bld->builder, a, b, "");
 
346
      if (type.floating)
 
347
         res = LLVMBuildFSub(bld->builder, a, b, "");
 
348
      else
 
349
         res = LLVMBuildSub(bld->builder, a, b, "");
276
350
 
277
351
   if(bld->type.norm && (bld->type.floating || bld->type.fixed))
278
352
      res = lp_build_max_simple(bld, res, bld->zero);
330
404
   LLVMValueRef c8;
331
405
   LLVMValueRef ab;
332
406
 
333
 
   c8 = lp_build_int_const_scalar(i16_type, 8);
 
407
   assert(!i16_type.floating);
 
408
   assert(lp_check_value(i16_type, a));
 
409
   assert(lp_check_value(i16_type, b));
 
410
 
 
411
   c8 = lp_build_const_int_vec(i16_type, 8);
334
412
   
335
413
#if 0
336
414
   
337
415
   /* a*b/255 ~= (a*(b + 1)) >> 256 */
338
 
   b = LLVMBuildAdd(builder, b, lp_build_int_const_scalar(i16_type, 1), "");
 
416
   b = LLVMBuildAdd(builder, b, lp_build_const_int_vec(i16_type, 1), "");
339
417
   ab = LLVMBuildMul(builder, a, b, "");
340
418
 
341
419
#else
343
421
   /* ab/255 ~= (ab + (ab >> 8) + 0x80) >> 8 */
344
422
   ab = LLVMBuildMul(builder, a, b, "");
345
423
   ab = LLVMBuildAdd(builder, ab, LLVMBuildLShr(builder, ab, c8, ""), "");
346
 
   ab = LLVMBuildAdd(builder, ab, lp_build_int_const_scalar(i16_type, 0x80), "");
 
424
   ab = LLVMBuildAdd(builder, ab, lp_build_const_int_vec(i16_type, 0x80), "");
347
425
 
348
426
#endif
349
427
   
365
443
   LLVMValueRef shift;
366
444
   LLVMValueRef res;
367
445
 
 
446
   assert(lp_check_value(type, a));
 
447
   assert(lp_check_value(type, b));
 
448
 
368
449
   if(a == bld->zero)
369
450
      return bld->zero;
370
451
   if(a == bld->one)
398
479
   }
399
480
 
400
481
   if(type.fixed)
401
 
      shift = lp_build_int_const_scalar(type, type.width/2);
 
482
      shift = lp_build_const_int_vec(type, type.width/2);
402
483
   else
403
484
      shift = NULL;
404
485
 
405
486
   if(LLVMIsConstant(a) && LLVMIsConstant(b)) {
406
 
      res =  LLVMConstMul(a, b);
 
487
      if (type.floating)
 
488
         res = LLVMConstFMul(a, b);
 
489
      else
 
490
         res = LLVMConstMul(a, b);
407
491
      if(shift) {
408
492
         if(type.sign)
409
493
            res = LLVMConstAShr(res, shift);
412
496
      }
413
497
   }
414
498
   else {
415
 
      res = LLVMBuildMul(bld->builder, a, b, "");
 
499
      if (type.floating)
 
500
         res = LLVMBuildFMul(bld->builder, a, b, "");
 
501
      else
 
502
         res = LLVMBuildMul(bld->builder, a, b, "");
416
503
      if(shift) {
417
504
         if(type.sign)
418
505
            res = LLVMBuildAShr(bld->builder, res, shift, "");
435
522
{
436
523
   LLVMValueRef factor;
437
524
 
 
525
   assert(lp_check_value(bld->type, a));
 
526
 
438
527
   if(b == 0)
439
528
      return bld->zero;
440
529
 
442
531
      return a;
443
532
 
444
533
   if(b == -1)
445
 
      return LLVMBuildNeg(bld->builder, a, "");
 
534
      return lp_build_negate(bld, a);
446
535
 
447
536
   if(b == 2 && bld->type.floating)
448
537
      return lp_build_add(bld, a, a);
449
538
 
450
 
   if(util_is_pot(b)) {
 
539
   if(util_is_power_of_two(b)) {
451
540
      unsigned shift = ffs(b) - 1;
452
541
 
453
542
      if(bld->type.floating) {
460
549
          * for Inf and NaN.
461
550
          */
462
551
         unsigned mantissa = lp_mantissa(bld->type);
463
 
         factor = lp_build_int_const_scalar(bld->type, (unsigned long long)shift << mantissa);
 
552
         factor = lp_build_const_int_vec(bld->type, (unsigned long long)shift << mantissa);
464
553
         a = LLVMBuildBitCast(bld->builder, a, lp_build_int_vec_type(bld->type), "");
465
554
         a = LLVMBuildAdd(bld->builder, a, factor, "");
466
555
         a = LLVMBuildBitCast(bld->builder, a, lp_build_vec_type(bld->type), "");
468
557
#endif
469
558
      }
470
559
      else {
471
 
         factor = lp_build_const_scalar(bld->type, shift);
 
560
         factor = lp_build_const_vec(bld->type, shift);
472
561
         return LLVMBuildShl(bld->builder, a, factor, "");
473
562
      }
474
563
   }
475
564
 
476
 
   factor = lp_build_const_scalar(bld->type, (double)b);
 
565
   factor = lp_build_const_vec(bld->type, (double)b);
477
566
   return lp_build_mul(bld, a, factor);
478
567
}
479
568
 
488
577
{
489
578
   const struct lp_type type = bld->type;
490
579
 
 
580
   assert(lp_check_value(type, a));
 
581
   assert(lp_check_value(type, b));
 
582
 
491
583
   if(a == bld->zero)
492
584
      return bld->zero;
493
585
   if(a == bld->one)
499
591
   if(a == bld->undef || b == bld->undef)
500
592
      return bld->undef;
501
593
 
502
 
   if(LLVMIsConstant(a) && LLVMIsConstant(b))
503
 
      return LLVMConstFDiv(a, b);
 
594
   if(LLVMIsConstant(a) && LLVMIsConstant(b)) {
 
595
      if (type.floating)
 
596
         return LLVMConstFDiv(a, b);
 
597
      else if (type.sign)
 
598
         return LLVMConstSDiv(a, b);
 
599
      else
 
600
         return LLVMConstUDiv(a, b);
 
601
   }
504
602
 
505
603
   if(util_cpu_caps.has_sse && type.width == 32 && type.length == 4)
506
604
      return lp_build_mul(bld, a, lp_build_rcp(bld, b));
507
605
 
508
 
   return LLVMBuildFDiv(bld->builder, a, b, "");
 
606
   if (type.floating)
 
607
      return LLVMBuildFDiv(bld->builder, a, b, "");
 
608
   else if (type.sign)
 
609
      return LLVMBuildSDiv(bld->builder, a, b, "");
 
610
   else
 
611
      return LLVMBuildUDiv(bld->builder, a, b, "");
509
612
}
510
613
 
511
614
 
525
628
   LLVMValueRef delta;
526
629
   LLVMValueRef res;
527
630
 
 
631
   assert(lp_check_value(bld->type, x));
 
632
   assert(lp_check_value(bld->type, v0));
 
633
   assert(lp_check_value(bld->type, v1));
 
634
 
528
635
   delta = lp_build_sub(bld, v1, v0);
529
636
 
530
637
   res = lp_build_mul(bld, x, delta);
536
643
       * but it will be wrong for other uses. Basically we need a more
537
644
       * powerful lp_type, capable of further distinguishing the values
538
645
       * interpretation from the value storage. */
539
 
      res = LLVMBuildAnd(bld->builder, res, lp_build_int_const_scalar(bld->type, (1 << bld->type.width/2) - 1), "");
 
646
      res = LLVMBuildAnd(bld->builder, res, lp_build_const_int_vec(bld->type, (1 << bld->type.width/2) - 1), "");
540
647
 
541
648
   return res;
542
649
}
566
673
             LLVMValueRef a,
567
674
             LLVMValueRef b)
568
675
{
 
676
   assert(lp_check_value(bld->type, a));
 
677
   assert(lp_check_value(bld->type, b));
 
678
 
569
679
   if(a == bld->undef || b == bld->undef)
570
680
      return bld->undef;
571
681
 
594
704
             LLVMValueRef a,
595
705
             LLVMValueRef b)
596
706
{
 
707
   assert(lp_check_value(bld->type, a));
 
708
   assert(lp_check_value(bld->type, b));
 
709
 
597
710
   if(a == bld->undef || b == bld->undef)
598
711
      return bld->undef;
599
712
 
623
736
               LLVMValueRef min,
624
737
               LLVMValueRef max)
625
738
{
 
739
   assert(lp_check_value(bld->type, a));
 
740
   assert(lp_check_value(bld->type, min));
 
741
   assert(lp_check_value(bld->type, max));
 
742
 
626
743
   a = lp_build_min(bld, a, max);
627
744
   a = lp_build_max(bld, a, min);
628
745
   return a;
639
756
   const struct lp_type type = bld->type;
640
757
   LLVMTypeRef vec_type = lp_build_vec_type(type);
641
758
 
 
759
   assert(lp_check_value(type, a));
 
760
 
642
761
   if(!type.sign)
643
762
      return a;
644
763
 
646
765
      /* Mask out the sign bit */
647
766
      LLVMTypeRef int_vec_type = lp_build_int_vec_type(type);
648
767
      unsigned long long absMask = ~(1ULL << (type.width - 1));
649
 
      LLVMValueRef mask = lp_build_int_const_scalar(type, ((unsigned long long) absMask));
 
768
      LLVMValueRef mask = lp_build_const_int_vec(type, ((unsigned long long) absMask));
650
769
      a = LLVMBuildBitCast(bld->builder, a, int_vec_type, "");
651
770
      a = LLVMBuildAnd(bld->builder, a, mask, "");
652
771
      a = LLVMBuildBitCast(bld->builder, a, vec_type, "");
672
791
lp_build_negate(struct lp_build_context *bld,
673
792
                LLVMValueRef a)
674
793
{
675
 
   return LLVMBuildNeg(bld->builder, a, "");
 
794
   assert(lp_check_value(bld->type, a));
 
795
 
 
796
#if HAVE_LLVM >= 0x0207
 
797
   if (bld->type.floating)
 
798
      a = LLVMBuildFNeg(bld->builder, a, "");
 
799
   else
 
800
#endif
 
801
      a = LLVMBuildNeg(bld->builder, a, "");
 
802
 
 
803
   return a;
676
804
}
677
805
 
678
806
 
 
807
/** Return -1, 0 or +1 depending on the sign of a */
679
808
LLVMValueRef
680
809
lp_build_sgn(struct lp_build_context *bld,
681
810
             LLVMValueRef a)
682
811
{
683
812
   const struct lp_type type = bld->type;
684
 
   LLVMTypeRef vec_type = lp_build_vec_type(type);
685
813
   LLVMValueRef cond;
686
814
   LLVMValueRef res;
687
815
 
 
816
   assert(lp_check_value(type, a));
 
817
 
688
818
   /* Handle non-zero case */
689
819
   if(!type.sign) {
690
820
      /* if not zero then sign must be positive */
691
821
      res = bld->one;
692
822
   }
693
823
   else if(type.floating) {
694
 
      /* Take the sign bit and add it to 1 constant */
695
 
      LLVMTypeRef int_vec_type = lp_build_int_vec_type(type);
696
 
      LLVMValueRef mask = lp_build_int_const_scalar(type, (unsigned long long)1 << (type.width - 1));
 
824
      LLVMTypeRef vec_type;
 
825
      LLVMTypeRef int_type;
 
826
      LLVMValueRef mask;
697
827
      LLVMValueRef sign;
698
828
      LLVMValueRef one;
699
 
      sign = LLVMBuildBitCast(bld->builder, a, int_vec_type, "");
 
829
      unsigned long long maskBit = (unsigned long long)1 << (type.width - 1);
 
830
 
 
831
      int_type = lp_build_int_vec_type(type);
 
832
      vec_type = lp_build_vec_type(type);
 
833
      mask = lp_build_const_int_vec(type, maskBit);
 
834
 
 
835
      /* Take the sign bit and add it to 1 constant */
 
836
      sign = LLVMBuildBitCast(bld->builder, a, int_type, "");
700
837
      sign = LLVMBuildAnd(bld->builder, sign, mask, "");
701
 
      one = LLVMConstBitCast(bld->one, int_vec_type);
 
838
      one = LLVMConstBitCast(bld->one, int_type);
702
839
      res = LLVMBuildOr(bld->builder, sign, one, "");
703
840
      res = LLVMBuildBitCast(bld->builder, res, vec_type, "");
704
841
   }
705
842
   else
706
843
   {
707
 
      LLVMValueRef minus_one = lp_build_const_scalar(type, -1.0);
 
844
      LLVMValueRef minus_one = lp_build_const_vec(type, -1.0);
708
845
      cond = lp_build_cmp(bld, PIPE_FUNC_GREATER, a, bld->zero);
709
846
      res = lp_build_select(bld, cond, bld->one, minus_one);
710
847
   }
711
848
 
712
849
   /* Handle zero */
713
850
   cond = lp_build_cmp(bld, PIPE_FUNC_EQUAL, a, bld->zero);
714
 
   res = lp_build_select(bld, cond, bld->zero, bld->one);
 
851
   res = lp_build_select(bld, cond, bld->zero, res);
715
852
 
716
853
   return res;
717
854
}
730
867
   const struct lp_type type = bld->type;
731
868
   LLVMTypeRef int_vec_type = lp_build_int_vec_type(type);
732
869
   LLVMTypeRef vec_type = lp_build_vec_type(type);
733
 
   LLVMValueRef shift = lp_build_int_const_scalar(type, type.width - 1);
734
 
   LLVMValueRef mask = lp_build_int_const_scalar(type,
 
870
   LLVMValueRef shift = lp_build_const_int_vec(type, type.width - 1);
 
871
   LLVMValueRef mask = lp_build_const_int_vec(type,
735
872
                             ~((unsigned long long) 1 << (type.width - 1)));
736
873
   LLVMValueRef val, res;
737
874
 
738
875
   assert(type.floating);
 
876
   assert(lp_check_value(type, a));
739
877
 
740
878
   /* val = reinterpret_cast<int>(a) */
741
879
   val = LLVMBuildBitCast(bld->builder, a, int_vec_type, "");
753
891
 
754
892
 
755
893
/**
756
 
 * Convert vector of int to vector of float.
 
894
 * Convert vector of (or scalar) int to vector of (or scalar) float.
757
895
 */
758
896
LLVMValueRef
759
897
lp_build_int_to_float(struct lp_build_context *bld,
760
898
                      LLVMValueRef a)
761
899
{
762
900
   const struct lp_type type = bld->type;
 
901
   LLVMTypeRef vec_type = lp_build_vec_type(type);
763
902
 
764
903
   assert(type.floating);
765
 
   /*assert(lp_check_value(type, a));*/
766
904
 
767
 
   {
768
 
      LLVMTypeRef vec_type = lp_build_vec_type(type);
769
 
      /*LLVMTypeRef int_vec_type = lp_build_int_vec_type(type);*/
770
 
      LLVMValueRef res;
771
 
      res = LLVMBuildSIToFP(bld->builder, a, vec_type, "");
772
 
      return res;
773
 
   }
 
905
   return LLVMBuildSIToFP(bld->builder, a, vec_type, "");
774
906
}
775
907
 
776
908
 
815
947
}
816
948
 
817
949
 
 
950
/**
 
951
 * Return the integer part of a float (vector) value.  The returned value is
 
952
 * a float (vector).
 
953
 * Ex: trunc(-1.5) = 1.0
 
954
 */
818
955
LLVMValueRef
819
956
lp_build_trunc(struct lp_build_context *bld,
820
957
               LLVMValueRef a)
824
961
   assert(type.floating);
825
962
   assert(lp_check_value(type, a));
826
963
 
827
 
   if(util_cpu_caps.has_sse4_1)
 
964
   if (util_cpu_caps.has_sse4_1 && type.width*type.length == 128)
828
965
      return lp_build_round_sse41(bld, a, LP_BUILD_ROUND_SSE41_TRUNCATE);
829
966
   else {
830
967
      LLVMTypeRef vec_type = lp_build_vec_type(type);
837
974
}
838
975
 
839
976
 
 
977
/**
 
978
 * Return float (vector) rounded to nearest integer (vector).  The returned
 
979
 * value is a float (vector).
 
980
 * Ex: round(0.9) = 1.0
 
981
 * Ex: round(-1.5) = -2.0
 
982
 */
840
983
LLVMValueRef
841
984
lp_build_round(struct lp_build_context *bld,
842
985
               LLVMValueRef a)
846
989
   assert(type.floating);
847
990
   assert(lp_check_value(type, a));
848
991
 
849
 
   if(util_cpu_caps.has_sse4_1)
 
992
   if (util_cpu_caps.has_sse4_1 && type.width*type.length == 128)
850
993
      return lp_build_round_sse41(bld, a, LP_BUILD_ROUND_SSE41_NEAREST);
851
994
   else {
852
995
      LLVMTypeRef vec_type = lp_build_vec_type(type);
858
1001
}
859
1002
 
860
1003
 
 
1004
/**
 
1005
 * Return floor of float (vector), result is a float (vector)
 
1006
 * Ex: floor(1.1) = 1.0
 
1007
 * Ex: floor(-1.1) = -2.0
 
1008
 */
861
1009
LLVMValueRef
862
1010
lp_build_floor(struct lp_build_context *bld,
863
1011
               LLVMValueRef a)
865
1013
   const struct lp_type type = bld->type;
866
1014
 
867
1015
   assert(type.floating);
 
1016
   assert(lp_check_value(type, a));
868
1017
 
869
 
   if(util_cpu_caps.has_sse4_1)
 
1018
   if (util_cpu_caps.has_sse4_1 && type.width*type.length == 128)
870
1019
      return lp_build_round_sse41(bld, a, LP_BUILD_ROUND_SSE41_FLOOR);
871
1020
   else {
872
1021
      LLVMTypeRef vec_type = lp_build_vec_type(type);
878
1027
}
879
1028
 
880
1029
 
 
1030
/**
 
1031
 * Return ceiling of float (vector), returning float (vector).
 
1032
 * Ex: ceil( 1.1) = 2.0
 
1033
 * Ex: ceil(-1.1) = -1.0
 
1034
 */
881
1035
LLVMValueRef
882
1036
lp_build_ceil(struct lp_build_context *bld,
883
1037
              LLVMValueRef a)
887
1041
   assert(type.floating);
888
1042
   assert(lp_check_value(type, a));
889
1043
 
890
 
   if(util_cpu_caps.has_sse4_1)
 
1044
   if (util_cpu_caps.has_sse4_1 && type.width*type.length == 128)
891
1045
      return lp_build_round_sse41(bld, a, LP_BUILD_ROUND_SSE41_CEIL);
892
1046
   else {
893
1047
      LLVMTypeRef vec_type = lp_build_vec_type(type);
900
1054
 
901
1055
 
902
1056
/**
903
 
 * Return fractional part of 'a' computed as a - floor(f)
 
1057
 * Return fractional part of 'a' computed as a - floor(a)
904
1058
 * Typically used in texture coord arithmetic.
905
1059
 */
906
1060
LLVMValueRef
913
1067
 
914
1068
 
915
1069
/**
916
 
 * Convert to integer, through whichever rounding method that's fastest,
917
 
 * typically truncating toward zero.
 
1070
 * Return the integer part of a float (vector) value.  The returned value is
 
1071
 * an integer (vector).
 
1072
 * Ex: itrunc(-1.5) = 1
918
1073
 */
919
1074
LLVMValueRef
920
1075
lp_build_itrunc(struct lp_build_context *bld,
930
1085
}
931
1086
 
932
1087
 
 
1088
/**
 
1089
 * Return float (vector) rounded to nearest integer (vector).  The returned
 
1090
 * value is an integer (vector).
 
1091
 * Ex: iround(0.9) = 1
 
1092
 * Ex: iround(-1.5) = -2
 
1093
 */
933
1094
LLVMValueRef
934
1095
lp_build_iround(struct lp_build_context *bld,
935
1096
                LLVMValueRef a)
939
1100
   LLVMValueRef res;
940
1101
 
941
1102
   assert(type.floating);
 
1103
 
942
1104
   assert(lp_check_value(type, a));
943
1105
 
944
 
   if(util_cpu_caps.has_sse4_1) {
 
1106
   if (util_cpu_caps.has_sse4_1 && type.width*type.length == 128) {
945
1107
      res = lp_build_round_sse41(bld, a, LP_BUILD_ROUND_SSE41_NEAREST);
946
1108
   }
947
1109
   else {
948
1110
      LLVMTypeRef vec_type = lp_build_vec_type(type);
949
 
      LLVMValueRef mask = lp_build_int_const_scalar(type, (unsigned long long)1 << (type.width - 1));
 
1111
      LLVMValueRef mask = lp_build_const_int_vec(type, (unsigned long long)1 << (type.width - 1));
950
1112
      LLVMValueRef sign;
951
1113
      LLVMValueRef half;
952
1114
 
955
1117
      sign = LLVMBuildAnd(bld->builder, sign, mask, "");
956
1118
 
957
1119
      /* sign * 0.5 */
958
 
      half = lp_build_const_scalar(type, 0.5);
 
1120
      half = lp_build_const_vec(type, 0.5);
959
1121
      half = LLVMBuildBitCast(bld->builder, half, int_vec_type, "");
960
1122
      half = LLVMBuildOr(bld->builder, sign, half, "");
961
1123
      half = LLVMBuildBitCast(bld->builder, half, vec_type, "");
962
1124
 
963
 
      res = LLVMBuildAdd(bld->builder, a, half, "");
 
1125
      res = LLVMBuildFAdd(bld->builder, a, half, "");
964
1126
   }
965
1127
 
966
1128
   res = LLVMBuildFPToSI(bld->builder, res, int_vec_type, "");
970
1132
 
971
1133
 
972
1134
/**
973
 
 * Convert float[] to int[] with floor().
 
1135
 * Return floor of float (vector), result is an int (vector)
 
1136
 * Ex: ifloor(1.1) = 1.0
 
1137
 * Ex: ifloor(-1.1) = -2.0
974
1138
 */
975
1139
LLVMValueRef
976
1140
lp_build_ifloor(struct lp_build_context *bld,
983
1147
   assert(type.floating);
984
1148
   assert(lp_check_value(type, a));
985
1149
 
986
 
   if(util_cpu_caps.has_sse4_1) {
 
1150
   if (util_cpu_caps.has_sse4_1 && type.width*type.length == 128) {
987
1151
      res = lp_build_round_sse41(bld, a, LP_BUILD_ROUND_SSE41_FLOOR);
988
1152
   }
989
1153
   else {
990
1154
      /* Take the sign bit and add it to 1 constant */
991
1155
      LLVMTypeRef vec_type = lp_build_vec_type(type);
992
1156
      unsigned mantissa = lp_mantissa(type);
993
 
      LLVMValueRef mask = lp_build_int_const_scalar(type, (unsigned long long)1 << (type.width - 1));
 
1157
      LLVMValueRef mask = lp_build_const_int_vec(type, (unsigned long long)1 << (type.width - 1));
994
1158
      LLVMValueRef sign;
995
1159
      LLVMValueRef offset;
996
1160
 
997
1161
      /* sign = a < 0 ? ~0 : 0 */
998
1162
      sign = LLVMBuildBitCast(bld->builder, a, int_vec_type, "");
999
1163
      sign = LLVMBuildAnd(bld->builder, sign, mask, "");
1000
 
      sign = LLVMBuildAShr(bld->builder, sign, lp_build_int_const_scalar(type, type.width - 1), "");
1001
 
      lp_build_name(sign, "floor.sign");
 
1164
      sign = LLVMBuildAShr(bld->builder, sign, lp_build_const_int_vec(type, type.width - 1), "ifloor.sign");
1002
1165
 
1003
1166
      /* offset = -0.99999(9)f */
1004
 
      offset = lp_build_const_scalar(type, -(double)(((unsigned long long)1 << mantissa) - 1)/((unsigned long long)1 << mantissa));
 
1167
      offset = lp_build_const_vec(type, -(double)(((unsigned long long)1 << mantissa) - 10)/((unsigned long long)1 << mantissa));
1005
1168
      offset = LLVMConstBitCast(offset, int_vec_type);
1006
1169
 
1007
 
      /* offset = a < 0 ? -0.99999(9)f : 0.0f */
 
1170
      /* offset = a < 0 ? offset : 0.0f */
1008
1171
      offset = LLVMBuildAnd(bld->builder, offset, sign, "");
1009
 
      offset = LLVMBuildBitCast(bld->builder, offset, vec_type, "");
1010
 
      lp_build_name(offset, "floor.offset");
 
1172
      offset = LLVMBuildBitCast(bld->builder, offset, vec_type, "ifloor.offset");
1011
1173
 
1012
 
      res = LLVMBuildAdd(bld->builder, a, offset, "");
1013
 
      lp_build_name(res, "floor.res");
 
1174
      res = LLVMBuildFAdd(bld->builder, a, offset, "ifloor.res");
1014
1175
   }
1015
1176
 
1016
 
   res = LLVMBuildFPToSI(bld->builder, res, int_vec_type, "");
1017
 
   lp_build_name(res, "floor");
 
1177
   /* round to nearest (toward zero) */
 
1178
   res = LLVMBuildFPToSI(bld->builder, res, int_vec_type, "ifloor.res");
1018
1179
 
1019
1180
   return res;
1020
1181
}
1021
1182
 
1022
1183
 
 
1184
/**
 
1185
 * Return ceiling of float (vector), returning int (vector).
 
1186
 * Ex: iceil( 1.1) = 2
 
1187
 * Ex: iceil(-1.1) = -1
 
1188
 */
1023
1189
LLVMValueRef
1024
1190
lp_build_iceil(struct lp_build_context *bld,
1025
1191
               LLVMValueRef a)
1031
1197
   assert(type.floating);
1032
1198
   assert(lp_check_value(type, a));
1033
1199
 
1034
 
   if(util_cpu_caps.has_sse4_1) {
 
1200
   if (util_cpu_caps.has_sse4_1 && type.width*type.length == 128) {
1035
1201
      res = lp_build_round_sse41(bld, a, LP_BUILD_ROUND_SSE41_CEIL);
1036
1202
   }
1037
1203
   else {
1038
 
      assert(0);
1039
 
      res = bld->undef;
 
1204
      LLVMTypeRef vec_type = lp_build_vec_type(type);
 
1205
      unsigned mantissa = lp_mantissa(type);
 
1206
      LLVMValueRef mask = lp_build_const_int_vec(type, (unsigned long long)1 << (type.width - 1));
 
1207
      LLVMValueRef sign;
 
1208
      LLVMValueRef offset;
 
1209
 
 
1210
      /* sign = a < 0 ? 0 : ~0 */
 
1211
      sign = LLVMBuildBitCast(bld->builder, a, int_vec_type, "");
 
1212
      sign = LLVMBuildAnd(bld->builder, sign, mask, "");
 
1213
      sign = LLVMBuildAShr(bld->builder, sign, lp_build_const_int_vec(type, type.width - 1), "iceil.sign");
 
1214
      sign = LLVMBuildNot(bld->builder, sign, "iceil.not");
 
1215
 
 
1216
      /* offset = 0.99999(9)f */
 
1217
      offset = lp_build_const_vec(type, (double)(((unsigned long long)1 << mantissa) - 10)/((unsigned long long)1 << mantissa));
 
1218
      offset = LLVMConstBitCast(offset, int_vec_type);
 
1219
 
 
1220
      /* offset = a < 0 ? 0.0 : offset */
 
1221
      offset = LLVMBuildAnd(bld->builder, offset, sign, "");
 
1222
      offset = LLVMBuildBitCast(bld->builder, offset, vec_type, "iceil.offset");
 
1223
 
 
1224
      res = LLVMBuildFAdd(bld->builder, a, offset, "iceil.res");
1040
1225
   }
1041
1226
 
1042
 
   res = LLVMBuildFPToSI(bld->builder, res, int_vec_type, "");
 
1227
   /* round to nearest (toward zero) */
 
1228
   res = LLVMBuildFPToSI(bld->builder, res, int_vec_type, "iceil.res");
1043
1229
 
1044
1230
   return res;
1045
1231
}
1053
1239
   LLVMTypeRef vec_type = lp_build_vec_type(type);
1054
1240
   char intrinsic[32];
1055
1241
 
 
1242
   assert(lp_check_value(type, a));
 
1243
 
1056
1244
   /* TODO: optimize the constant case */
1057
1245
   /* TODO: optimize the constant case */
1058
1246
 
1063
1251
}
1064
1252
 
1065
1253
 
 
1254
/**
 
1255
 * Do one Newton-Raphson step to improve reciprocate precision:
 
1256
 *
 
1257
 *   x_{i+1} = x_i * (2 - a * x_i)
 
1258
 *
 
1259
 * XXX: Unfortunately this won't give IEEE-754 conformant results for 0 or
 
1260
 * +/-Inf, giving NaN instead.  Certain applications rely on this behavior,
 
1261
 * such as Google Earth, which does RCP(RSQRT(0.0) when drawing the Earth's
 
1262
 * halo. It would be necessary to clamp the argument to prevent this.
 
1263
 *
 
1264
 * See also:
 
1265
 * - http://en.wikipedia.org/wiki/Division_(digital)#Newton.E2.80.93Raphson_division
 
1266
 * - http://softwarecommunity.intel.com/articles/eng/1818.htm
 
1267
 */
 
1268
static INLINE LLVMValueRef
 
1269
lp_build_rcp_refine(struct lp_build_context *bld,
 
1270
                    LLVMValueRef a,
 
1271
                    LLVMValueRef rcp_a)
 
1272
{
 
1273
   LLVMValueRef two = lp_build_const_vec(bld->type, 2.0);
 
1274
   LLVMValueRef res;
 
1275
 
 
1276
   res = LLVMBuildFMul(bld->builder, a, rcp_a, "");
 
1277
   res = LLVMBuildFSub(bld->builder, two, res, "");
 
1278
   res = LLVMBuildFMul(bld->builder, rcp_a, res, "");
 
1279
 
 
1280
   return res;
 
1281
}
 
1282
 
 
1283
 
1066
1284
LLVMValueRef
1067
1285
lp_build_rcp(struct lp_build_context *bld,
1068
1286
             LLVMValueRef a)
1069
1287
{
1070
1288
   const struct lp_type type = bld->type;
1071
1289
 
 
1290
   assert(lp_check_value(type, a));
 
1291
 
1072
1292
   if(a == bld->zero)
1073
1293
      return bld->undef;
1074
1294
   if(a == bld->one)
1081
1301
   if(LLVMIsConstant(a))
1082
1302
      return LLVMConstFDiv(bld->one, a);
1083
1303
 
1084
 
   if(util_cpu_caps.has_sse && type.width == 32 && type.length == 4)
1085
 
      /* FIXME: improve precision */
1086
 
      return lp_build_intrinsic_unary(bld->builder, "llvm.x86.sse.rcp.ps", lp_build_vec_type(type), a);
 
1304
   /*
 
1305
    * We don't use RCPPS because:
 
1306
    * - it only has 10bits of precision
 
1307
    * - it doesn't even get the reciprocate of 1.0 exactly
 
1308
    * - doing Newton-Rapshon steps yields wrong (NaN) values for 0.0 or Inf
 
1309
    * - for recent processors the benefit over DIVPS is marginal, a case
 
1310
    *   depedent
 
1311
    *
 
1312
    * We could still use it on certain processors if benchmarks show that the
 
1313
    * RCPPS plus necessary workarounds are still preferrable to DIVPS; or for
 
1314
    * particular uses that require less workarounds.
 
1315
    */
 
1316
 
 
1317
   if (FALSE && util_cpu_caps.has_sse && type.width == 32 && type.length == 4) {
 
1318
      const unsigned num_iterations = 0;
 
1319
      LLVMValueRef res;
 
1320
      unsigned i;
 
1321
 
 
1322
      res = lp_build_intrinsic_unary(bld->builder, "llvm.x86.sse.rcp.ps", bld->vec_type, a);
 
1323
 
 
1324
      for (i = 0; i < num_iterations; ++i) {
 
1325
         res = lp_build_rcp_refine(bld, a, res);
 
1326
      }
 
1327
 
 
1328
      return res;
 
1329
   }
1087
1330
 
1088
1331
   return LLVMBuildFDiv(bld->builder, bld->one, a, "");
1089
1332
}
1090
1333
 
1091
1334
 
1092
1335
/**
 
1336
 * Do one Newton-Raphson step to improve rsqrt precision:
 
1337
 *
 
1338
 *   x_{i+1} = 0.5 * x_i * (3.0 - a * x_i * x_i)
 
1339
 *
 
1340
 * See also:
 
1341
 * - http://softwarecommunity.intel.com/articles/eng/1818.htm
 
1342
 */
 
1343
static INLINE LLVMValueRef
 
1344
lp_build_rsqrt_refine(struct lp_build_context *bld,
 
1345
                      LLVMValueRef a,
 
1346
                      LLVMValueRef rsqrt_a)
 
1347
{
 
1348
   LLVMValueRef half = lp_build_const_vec(bld->type, 0.5);
 
1349
   LLVMValueRef three = lp_build_const_vec(bld->type, 3.0);
 
1350
   LLVMValueRef res;
 
1351
 
 
1352
   res = LLVMBuildFMul(bld->builder, rsqrt_a, rsqrt_a, "");
 
1353
   res = LLVMBuildFMul(bld->builder, a, res, "");
 
1354
   res = LLVMBuildFSub(bld->builder, three, res, "");
 
1355
   res = LLVMBuildFMul(bld->builder, rsqrt_a, res, "");
 
1356
   res = LLVMBuildFMul(bld->builder, half, res, "");
 
1357
 
 
1358
   return res;
 
1359
}
 
1360
 
 
1361
 
 
1362
/**
1093
1363
 * Generate 1/sqrt(a)
1094
1364
 */
1095
1365
LLVMValueRef
1098
1368
{
1099
1369
   const struct lp_type type = bld->type;
1100
1370
 
 
1371
   assert(lp_check_value(type, a));
 
1372
 
1101
1373
   assert(type.floating);
1102
1374
 
1103
 
   if(util_cpu_caps.has_sse && type.width == 32 && type.length == 4)
1104
 
      return lp_build_intrinsic_unary(bld->builder, "llvm.x86.sse.rsqrt.ps", lp_build_vec_type(type), a);
 
1375
   if (util_cpu_caps.has_sse && type.width == 32 && type.length == 4) {
 
1376
      const unsigned num_iterations = 0;
 
1377
      LLVMValueRef res;
 
1378
      unsigned i;
 
1379
 
 
1380
      res = lp_build_intrinsic_unary(bld->builder, "llvm.x86.sse.rsqrt.ps", bld->vec_type, a);
 
1381
 
 
1382
      for (i = 0; i < num_iterations; ++i) {
 
1383
         res = lp_build_rsqrt_refine(bld, a, res);
 
1384
      }
 
1385
 
 
1386
      return res;
 
1387
   }
1105
1388
 
1106
1389
   return lp_build_rcp(bld, lp_build_sqrt(bld, a));
1107
1390
}
1108
1391
 
1109
1392
 
1110
 
/**
1111
 
 * Generate cos(a)
 
1393
static inline LLVMValueRef
 
1394
lp_build_const_v4si(unsigned long value)
 
1395
{
 
1396
   LLVMValueRef element = LLVMConstInt(LLVMInt32Type(), value, 0);
 
1397
   LLVMValueRef elements[4] = { element, element, element, element };
 
1398
   return LLVMConstVector(elements, 4);
 
1399
}
 
1400
 
 
1401
static inline LLVMValueRef
 
1402
lp_build_const_v4sf(float value)
 
1403
{
 
1404
   LLVMValueRef element = LLVMConstReal(LLVMFloatType(), value);
 
1405
   LLVMValueRef elements[4] = { element, element, element, element };
 
1406
   return LLVMConstVector(elements, 4);
 
1407
}
 
1408
 
 
1409
 
 
1410
/**
 
1411
 * Generate sin(a) using SSE2
 
1412
 */
 
1413
LLVMValueRef
 
1414
lp_build_sin(struct lp_build_context *bld,
 
1415
             LLVMValueRef a)
 
1416
{
 
1417
   struct lp_type int_type = lp_int_type(bld->type);
 
1418
   LLVMBuilderRef b = bld->builder;
 
1419
   LLVMTypeRef v4sf = LLVMVectorType(LLVMFloatType(), 4);
 
1420
   LLVMTypeRef v4si = LLVMVectorType(LLVMInt32Type(), 4);
 
1421
 
 
1422
   /*
 
1423
    *  take the absolute value,
 
1424
    *  x = _mm_and_ps(x, *(v4sf*)_ps_inv_sign_mask);
 
1425
    */
 
1426
 
 
1427
   LLVMValueRef inv_sig_mask = lp_build_const_v4si(~0x80000000);
 
1428
   LLVMValueRef a_v4si = LLVMBuildBitCast(b, a, v4si, "a_v4si");
 
1429
 
 
1430
   LLVMValueRef absi = LLVMBuildAnd(b, a_v4si, inv_sig_mask, "absi");
 
1431
   LLVMValueRef x_abs = LLVMBuildBitCast(b, absi, v4sf, "x_abs");
 
1432
 
 
1433
   /*
 
1434
    * extract the sign bit (upper one)
 
1435
    * sign_bit = _mm_and_ps(sign_bit, *(v4sf*)_ps_sign_mask);
 
1436
    */
 
1437
   LLVMValueRef sig_mask = lp_build_const_v4si(0x80000000);
 
1438
   LLVMValueRef sign_bit_i = LLVMBuildAnd(b, a_v4si, sig_mask, "sign_bit_i");
 
1439
 
 
1440
   /*
 
1441
    * scale by 4/Pi
 
1442
    * y = _mm_mul_ps(x, *(v4sf*)_ps_cephes_FOPI);
 
1443
    */
 
1444
   
 
1445
   LLVMValueRef FOPi = lp_build_const_v4sf(1.27323954473516);
 
1446
   LLVMValueRef scale_y = LLVMBuildFMul(b, x_abs, FOPi, "scale_y");
 
1447
 
 
1448
   /*
 
1449
    * store the integer part of y in mm0
 
1450
    * emm2 = _mm_cvttps_epi32(y);
 
1451
    */
 
1452
   
 
1453
   LLVMValueRef emm2_i = LLVMBuildFPToSI(b, scale_y, v4si, "emm2_i");
 
1454
 
 
1455
   /*
 
1456
    * j=(j+1) & (~1) (see the cephes sources)
 
1457
    * emm2 = _mm_add_epi32(emm2, *(v4si*)_pi32_1);
 
1458
    */
 
1459
 
 
1460
   LLVMValueRef all_one = lp_build_const_v4si(1);
 
1461
   LLVMValueRef emm2_add =  LLVMBuildAdd(b, emm2_i, all_one, "emm2_add");
 
1462
   /*
 
1463
    * emm2 = _mm_and_si128(emm2, *(v4si*)_pi32_inv1);
 
1464
    */
 
1465
   LLVMValueRef inv_one = lp_build_const_v4si(~1);
 
1466
   LLVMValueRef emm2_and =  LLVMBuildAnd(b, emm2_add, inv_one, "emm2_and");
 
1467
 
 
1468
   /*
 
1469
    * y = _mm_cvtepi32_ps(emm2);
 
1470
    */
 
1471
   LLVMValueRef y_2 = LLVMBuildSIToFP(b, emm2_and, v4sf, "y_2");
 
1472
 
 
1473
   /* get the swap sign flag
 
1474
    * emm0 = _mm_and_si128(emm2, *(v4si*)_pi32_4);
 
1475
    */
 
1476
   LLVMValueRef pi32_4 = lp_build_const_v4si(4);
 
1477
   LLVMValueRef emm0_and =  LLVMBuildAnd(b, emm2_add, pi32_4, "emm0_and");
 
1478
   
 
1479
   /*
 
1480
    * emm2 = _mm_slli_epi32(emm0, 29);
 
1481
    */  
 
1482
   LLVMValueRef const_29 = lp_build_const_v4si(29);
 
1483
   LLVMValueRef swap_sign_bit = LLVMBuildShl(b, emm0_and, const_29, "swap_sign_bit");
 
1484
 
 
1485
   /*
 
1486
    * get the polynom selection mask 
 
1487
    * there is one polynom for 0 <= x <= Pi/4
 
1488
    * and another one for Pi/4<x<=Pi/2
 
1489
    * Both branches will be computed.
 
1490
    *  
 
1491
    * emm2 = _mm_and_si128(emm2, *(v4si*)_pi32_2);
 
1492
    * emm2 = _mm_cmpeq_epi32(emm2, _mm_setzero_si128());
 
1493
    */
 
1494
 
 
1495
   LLVMValueRef pi32_2 = lp_build_const_v4si(2);
 
1496
   LLVMValueRef emm2_3 =  LLVMBuildAnd(b, emm2_and, pi32_2, "emm2_3");
 
1497
   LLVMValueRef poly_mask = lp_build_compare(b, int_type, PIPE_FUNC_EQUAL,
 
1498
                                             emm2_3, lp_build_const_v4si(0));
 
1499
   /*
 
1500
    *   sign_bit = _mm_xor_ps(sign_bit, swap_sign_bit);
 
1501
    */
 
1502
   LLVMValueRef sign_bit_1 =  LLVMBuildXor(b, sign_bit_i, swap_sign_bit, "sign_bit");
 
1503
 
 
1504
   /*
 
1505
    * _PS_CONST(minus_cephes_DP1, -0.78515625);
 
1506
    * _PS_CONST(minus_cephes_DP2, -2.4187564849853515625e-4);
 
1507
    * _PS_CONST(minus_cephes_DP3, -3.77489497744594108e-8);
 
1508
    */
 
1509
   LLVMValueRef DP1 = lp_build_const_v4sf(-0.78515625);
 
1510
   LLVMValueRef DP2 = lp_build_const_v4sf(-2.4187564849853515625e-4);
 
1511
   LLVMValueRef DP3 = lp_build_const_v4sf(-3.77489497744594108e-8);
 
1512
 
 
1513
   /*
 
1514
    * The magic pass: "Extended precision modular arithmetic" 
 
1515
    * x = ((x - y * DP1) - y * DP2) - y * DP3; 
 
1516
    * xmm1 = _mm_mul_ps(y, xmm1);
 
1517
    * xmm2 = _mm_mul_ps(y, xmm2);
 
1518
    * xmm3 = _mm_mul_ps(y, xmm3);
 
1519
    */
 
1520
   LLVMValueRef xmm1 = LLVMBuildFMul(b, y_2, DP1, "xmm1");
 
1521
   LLVMValueRef xmm2 = LLVMBuildFMul(b, y_2, DP2, "xmm2");
 
1522
   LLVMValueRef xmm3 = LLVMBuildFMul(b, y_2, DP3, "xmm3");
 
1523
 
 
1524
   /*
 
1525
    * x = _mm_add_ps(x, xmm1);
 
1526
    * x = _mm_add_ps(x, xmm2);
 
1527
    * x = _mm_add_ps(x, xmm3);
 
1528
    */ 
 
1529
 
 
1530
   LLVMValueRef x_1 = LLVMBuildFAdd(b, x_abs, xmm1, "x_1");
 
1531
   LLVMValueRef x_2 = LLVMBuildFAdd(b, x_1, xmm2, "x_2");
 
1532
   LLVMValueRef x_3 = LLVMBuildFAdd(b, x_2, xmm3, "x_3");
 
1533
 
 
1534
   /*
 
1535
    * Evaluate the first polynom  (0 <= x <= Pi/4)
 
1536
    *
 
1537
    * z = _mm_mul_ps(x,x);
 
1538
    */
 
1539
   LLVMValueRef z = LLVMBuildFMul(b, x_3, x_3, "z");
 
1540
 
 
1541
   /*
 
1542
    * _PS_CONST(coscof_p0,  2.443315711809948E-005);
 
1543
    * _PS_CONST(coscof_p1, -1.388731625493765E-003);
 
1544
    * _PS_CONST(coscof_p2,  4.166664568298827E-002);
 
1545
    */
 
1546
   LLVMValueRef coscof_p0 = lp_build_const_v4sf(2.443315711809948E-005);
 
1547
   LLVMValueRef coscof_p1 = lp_build_const_v4sf(-1.388731625493765E-003);
 
1548
   LLVMValueRef coscof_p2 = lp_build_const_v4sf(4.166664568298827E-002);
 
1549
 
 
1550
   /*
 
1551
    * y = *(v4sf*)_ps_coscof_p0;
 
1552
    * y = _mm_mul_ps(y, z);
 
1553
    */
 
1554
   LLVMValueRef y_3 = LLVMBuildFMul(b, z, coscof_p0, "y_3");
 
1555
   LLVMValueRef y_4 = LLVMBuildFAdd(b, y_3, coscof_p1, "y_4");
 
1556
   LLVMValueRef y_5 = LLVMBuildFMul(b, y_4, z, "y_5");
 
1557
   LLVMValueRef y_6 = LLVMBuildFAdd(b, y_5, coscof_p2, "y_6");
 
1558
   LLVMValueRef y_7 = LLVMBuildFMul(b, y_6, z, "y_7");
 
1559
   LLVMValueRef y_8 = LLVMBuildFMul(b, y_7, z, "y_8");
 
1560
 
 
1561
 
 
1562
   /*
 
1563
    * tmp = _mm_mul_ps(z, *(v4sf*)_ps_0p5);
 
1564
    * y = _mm_sub_ps(y, tmp);
 
1565
    * y = _mm_add_ps(y, *(v4sf*)_ps_1);
 
1566
    */ 
 
1567
   LLVMValueRef half = lp_build_const_v4sf(0.5);
 
1568
   LLVMValueRef tmp = LLVMBuildFMul(b, z, half, "tmp");
 
1569
   LLVMValueRef y_9 = LLVMBuildFSub(b, y_8, tmp, "y_8");
 
1570
   LLVMValueRef one = lp_build_const_v4sf(1.0);
 
1571
   LLVMValueRef y_10 = LLVMBuildFAdd(b, y_9, one, "y_9");
 
1572
 
 
1573
   /*
 
1574
    * _PS_CONST(sincof_p0, -1.9515295891E-4);
 
1575
    * _PS_CONST(sincof_p1,  8.3321608736E-3);
 
1576
    * _PS_CONST(sincof_p2, -1.6666654611E-1);
 
1577
    */
 
1578
   LLVMValueRef sincof_p0 = lp_build_const_v4sf(-1.9515295891E-4);
 
1579
   LLVMValueRef sincof_p1 = lp_build_const_v4sf(8.3321608736E-3);
 
1580
   LLVMValueRef sincof_p2 = lp_build_const_v4sf(-1.6666654611E-1);
 
1581
 
 
1582
   /*
 
1583
    * Evaluate the second polynom  (Pi/4 <= x <= 0)
 
1584
    *
 
1585
    * y2 = *(v4sf*)_ps_sincof_p0;
 
1586
    * y2 = _mm_mul_ps(y2, z);
 
1587
    * y2 = _mm_add_ps(y2, *(v4sf*)_ps_sincof_p1);
 
1588
    * y2 = _mm_mul_ps(y2, z);
 
1589
    * y2 = _mm_add_ps(y2, *(v4sf*)_ps_sincof_p2);
 
1590
    * y2 = _mm_mul_ps(y2, z);
 
1591
    * y2 = _mm_mul_ps(y2, x);
 
1592
    * y2 = _mm_add_ps(y2, x);
 
1593
    */
 
1594
 
 
1595
   LLVMValueRef y2_3 = LLVMBuildFMul(b, z, sincof_p0, "y2_3");
 
1596
   LLVMValueRef y2_4 = LLVMBuildFAdd(b, y2_3, sincof_p1, "y2_4");
 
1597
   LLVMValueRef y2_5 = LLVMBuildFMul(b, y2_4, z, "y2_5");
 
1598
   LLVMValueRef y2_6 = LLVMBuildFAdd(b, y2_5, sincof_p2, "y2_6");
 
1599
   LLVMValueRef y2_7 = LLVMBuildFMul(b, y2_6, z, "y2_7");
 
1600
   LLVMValueRef y2_8 = LLVMBuildFMul(b, y2_7, x_3, "y2_8");
 
1601
   LLVMValueRef y2_9 = LLVMBuildFAdd(b, y2_8, x_3, "y2_9");
 
1602
 
 
1603
   /*
 
1604
    * select the correct result from the two polynoms
 
1605
    * xmm3 = poly_mask;
 
1606
    * y2 = _mm_and_ps(xmm3, y2); //, xmm3);
 
1607
    * y = _mm_andnot_ps(xmm3, y);
 
1608
    * y = _mm_add_ps(y,y2);
 
1609
    */
 
1610
   LLVMValueRef y2_i = LLVMBuildBitCast(b, y2_9, v4si, "y2_i");
 
1611
   LLVMValueRef y_i = LLVMBuildBitCast(b, y_10, v4si, "y_i");
 
1612
   LLVMValueRef y2_and = LLVMBuildAnd(b, y2_i, poly_mask, "y2_and");
 
1613
   LLVMValueRef inv = lp_build_const_v4si(~0);
 
1614
   LLVMValueRef poly_mask_inv = LLVMBuildXor(b, poly_mask, inv, "poly_mask_inv");
 
1615
   LLVMValueRef y_and = LLVMBuildAnd(b, y_i, poly_mask_inv, "y_and");
 
1616
   LLVMValueRef y_combine = LLVMBuildAdd(b, y_and, y2_and, "y_combine");
 
1617
 
 
1618
   /*
 
1619
    * update the sign
 
1620
    * y = _mm_xor_ps(y, sign_bit);
 
1621
    */
 
1622
   LLVMValueRef y_sign = LLVMBuildXor(b, y_combine, sign_bit_1, "y_sin");
 
1623
   LLVMValueRef y_result = LLVMBuildBitCast(b, y_sign, v4sf, "y_result");
 
1624
   return y_result;
 
1625
}
 
1626
 
 
1627
 
 
1628
/**
 
1629
 * Generate cos(a) using SSE2
1112
1630
 */
1113
1631
LLVMValueRef
1114
1632
lp_build_cos(struct lp_build_context *bld,
1115
 
              LLVMValueRef a)
1116
 
{
1117
 
   const struct lp_type type = bld->type;
1118
 
   LLVMTypeRef vec_type = lp_build_vec_type(type);
1119
 
   char intrinsic[32];
1120
 
 
1121
 
   /* TODO: optimize the constant case */
1122
 
 
1123
 
   assert(type.floating);
1124
 
   util_snprintf(intrinsic, sizeof intrinsic, "llvm.cos.v%uf%u", type.length, type.width);
1125
 
 
1126
 
   return lp_build_intrinsic_unary(bld->builder, intrinsic, vec_type, a);
1127
 
}
1128
 
 
1129
 
 
1130
 
/**
1131
 
 * Generate sin(a)
1132
 
 */
1133
 
LLVMValueRef
1134
 
lp_build_sin(struct lp_build_context *bld,
1135
 
              LLVMValueRef a)
1136
 
{
1137
 
   const struct lp_type type = bld->type;
1138
 
   LLVMTypeRef vec_type = lp_build_vec_type(type);
1139
 
   char intrinsic[32];
1140
 
 
1141
 
   /* TODO: optimize the constant case */
1142
 
 
1143
 
   assert(type.floating);
1144
 
   util_snprintf(intrinsic, sizeof intrinsic, "llvm.sin.v%uf%u", type.length, type.width);
1145
 
 
1146
 
   return lp_build_intrinsic_unary(bld->builder, intrinsic, vec_type, a);
 
1633
             LLVMValueRef a)
 
1634
{
 
1635
   struct lp_type int_type = lp_int_type(bld->type);
 
1636
   LLVMBuilderRef b = bld->builder;
 
1637
   LLVMTypeRef v4sf = LLVMVectorType(LLVMFloatType(), 4);
 
1638
   LLVMTypeRef v4si = LLVMVectorType(LLVMInt32Type(), 4);
 
1639
 
 
1640
   /*
 
1641
    *  take the absolute value,
 
1642
    *  x = _mm_and_ps(x, *(v4sf*)_ps_inv_sign_mask);
 
1643
    */
 
1644
 
 
1645
   LLVMValueRef inv_sig_mask = lp_build_const_v4si(~0x80000000);
 
1646
   LLVMValueRef a_v4si = LLVMBuildBitCast(b, a, v4si, "a_v4si");
 
1647
 
 
1648
   LLVMValueRef absi = LLVMBuildAnd(b, a_v4si, inv_sig_mask, "absi");
 
1649
   LLVMValueRef x_abs = LLVMBuildBitCast(b, absi, v4sf, "x_abs");
 
1650
 
 
1651
   /*
 
1652
    * scale by 4/Pi
 
1653
    * y = _mm_mul_ps(x, *(v4sf*)_ps_cephes_FOPI);
 
1654
    */
 
1655
   
 
1656
   LLVMValueRef FOPi = lp_build_const_v4sf(1.27323954473516);
 
1657
   LLVMValueRef scale_y = LLVMBuildFMul(b, x_abs, FOPi, "scale_y");
 
1658
 
 
1659
   /*
 
1660
    * store the integer part of y in mm0
 
1661
    * emm2 = _mm_cvttps_epi32(y);
 
1662
    */
 
1663
   
 
1664
   LLVMValueRef emm2_i = LLVMBuildFPToSI(b, scale_y, v4si, "emm2_i");
 
1665
 
 
1666
   /*
 
1667
    * j=(j+1) & (~1) (see the cephes sources)
 
1668
    * emm2 = _mm_add_epi32(emm2, *(v4si*)_pi32_1);
 
1669
    */
 
1670
 
 
1671
   LLVMValueRef all_one = lp_build_const_v4si(1);
 
1672
   LLVMValueRef emm2_add =  LLVMBuildAdd(b, emm2_i, all_one, "emm2_add");
 
1673
   /*
 
1674
    * emm2 = _mm_and_si128(emm2, *(v4si*)_pi32_inv1);
 
1675
    */
 
1676
   LLVMValueRef inv_one = lp_build_const_v4si(~1);
 
1677
   LLVMValueRef emm2_and =  LLVMBuildAnd(b, emm2_add, inv_one, "emm2_and");
 
1678
 
 
1679
   /*
 
1680
    * y = _mm_cvtepi32_ps(emm2);
 
1681
    */
 
1682
   LLVMValueRef y_2 = LLVMBuildSIToFP(b, emm2_and, v4sf, "y_2");
 
1683
 
 
1684
 
 
1685
   /*
 
1686
    * emm2 = _mm_sub_epi32(emm2, *(v4si*)_pi32_2);
 
1687
    */
 
1688
   LLVMValueRef const_2 = lp_build_const_v4si(2);
 
1689
   LLVMValueRef emm2_2 = LLVMBuildSub(b, emm2_and, const_2, "emm2_2");
 
1690
 
 
1691
 
 
1692
   /* get the swap sign flag
 
1693
    * emm0 = _mm_andnot_si128(emm2, *(v4si*)_pi32_4);
 
1694
    */
 
1695
   LLVMValueRef inv = lp_build_const_v4si(~0);
 
1696
   LLVMValueRef emm0_not = LLVMBuildXor(b, emm2_2, inv, "emm0_not");
 
1697
   LLVMValueRef pi32_4 = lp_build_const_v4si(4);
 
1698
   LLVMValueRef emm0_and =  LLVMBuildAnd(b, emm0_not, pi32_4, "emm0_and");
 
1699
   
 
1700
   /*
 
1701
    * emm2 = _mm_slli_epi32(emm0, 29);
 
1702
    */  
 
1703
   LLVMValueRef const_29 = lp_build_const_v4si(29);
 
1704
   LLVMValueRef sign_bit = LLVMBuildShl(b, emm0_and, const_29, "sign_bit");
 
1705
 
 
1706
   /*
 
1707
    * get the polynom selection mask 
 
1708
    * there is one polynom for 0 <= x <= Pi/4
 
1709
    * and another one for Pi/4<x<=Pi/2
 
1710
    * Both branches will be computed.
 
1711
    *  
 
1712
    * emm2 = _mm_and_si128(emm2, *(v4si*)_pi32_2);
 
1713
    * emm2 = _mm_cmpeq_epi32(emm2, _mm_setzero_si128());
 
1714
    */
 
1715
 
 
1716
   LLVMValueRef pi32_2 = lp_build_const_v4si(2);
 
1717
   LLVMValueRef emm2_3 =  LLVMBuildAnd(b, emm2_2, pi32_2, "emm2_3");
 
1718
   LLVMValueRef poly_mask = lp_build_compare(b, int_type, PIPE_FUNC_EQUAL,
 
1719
                                             emm2_3, lp_build_const_v4si(0));
 
1720
 
 
1721
   /*
 
1722
    * _PS_CONST(minus_cephes_DP1, -0.78515625);
 
1723
    * _PS_CONST(minus_cephes_DP2, -2.4187564849853515625e-4);
 
1724
    * _PS_CONST(minus_cephes_DP3, -3.77489497744594108e-8);
 
1725
    */
 
1726
   LLVMValueRef DP1 = lp_build_const_v4sf(-0.78515625);
 
1727
   LLVMValueRef DP2 = lp_build_const_v4sf(-2.4187564849853515625e-4);
 
1728
   LLVMValueRef DP3 = lp_build_const_v4sf(-3.77489497744594108e-8);
 
1729
 
 
1730
   /*
 
1731
    * The magic pass: "Extended precision modular arithmetic" 
 
1732
    * x = ((x - y * DP1) - y * DP2) - y * DP3; 
 
1733
    * xmm1 = _mm_mul_ps(y, xmm1);
 
1734
    * xmm2 = _mm_mul_ps(y, xmm2);
 
1735
    * xmm3 = _mm_mul_ps(y, xmm3);
 
1736
    */
 
1737
   LLVMValueRef xmm1 = LLVMBuildFMul(b, y_2, DP1, "xmm1");
 
1738
   LLVMValueRef xmm2 = LLVMBuildFMul(b, y_2, DP2, "xmm2");
 
1739
   LLVMValueRef xmm3 = LLVMBuildFMul(b, y_2, DP3, "xmm3");
 
1740
 
 
1741
   /*
 
1742
    * x = _mm_add_ps(x, xmm1);
 
1743
    * x = _mm_add_ps(x, xmm2);
 
1744
    * x = _mm_add_ps(x, xmm3);
 
1745
    */ 
 
1746
 
 
1747
   LLVMValueRef x_1 = LLVMBuildFAdd(b, x_abs, xmm1, "x_1");
 
1748
   LLVMValueRef x_2 = LLVMBuildFAdd(b, x_1, xmm2, "x_2");
 
1749
   LLVMValueRef x_3 = LLVMBuildFAdd(b, x_2, xmm3, "x_3");
 
1750
 
 
1751
   /*
 
1752
    * Evaluate the first polynom  (0 <= x <= Pi/4)
 
1753
    *
 
1754
    * z = _mm_mul_ps(x,x);
 
1755
    */
 
1756
   LLVMValueRef z = LLVMBuildFMul(b, x_3, x_3, "z");
 
1757
 
 
1758
   /*
 
1759
    * _PS_CONST(coscof_p0,  2.443315711809948E-005);
 
1760
    * _PS_CONST(coscof_p1, -1.388731625493765E-003);
 
1761
    * _PS_CONST(coscof_p2,  4.166664568298827E-002);
 
1762
    */
 
1763
   LLVMValueRef coscof_p0 = lp_build_const_v4sf(2.443315711809948E-005);
 
1764
   LLVMValueRef coscof_p1 = lp_build_const_v4sf(-1.388731625493765E-003);
 
1765
   LLVMValueRef coscof_p2 = lp_build_const_v4sf(4.166664568298827E-002);
 
1766
 
 
1767
   /*
 
1768
    * y = *(v4sf*)_ps_coscof_p0;
 
1769
    * y = _mm_mul_ps(y, z);
 
1770
    */
 
1771
   LLVMValueRef y_3 = LLVMBuildFMul(b, z, coscof_p0, "y_3");
 
1772
   LLVMValueRef y_4 = LLVMBuildFAdd(b, y_3, coscof_p1, "y_4");
 
1773
   LLVMValueRef y_5 = LLVMBuildFMul(b, y_4, z, "y_5");
 
1774
   LLVMValueRef y_6 = LLVMBuildFAdd(b, y_5, coscof_p2, "y_6");
 
1775
   LLVMValueRef y_7 = LLVMBuildFMul(b, y_6, z, "y_7");
 
1776
   LLVMValueRef y_8 = LLVMBuildFMul(b, y_7, z, "y_8");
 
1777
 
 
1778
 
 
1779
   /*
 
1780
    * tmp = _mm_mul_ps(z, *(v4sf*)_ps_0p5);
 
1781
    * y = _mm_sub_ps(y, tmp);
 
1782
    * y = _mm_add_ps(y, *(v4sf*)_ps_1);
 
1783
    */ 
 
1784
   LLVMValueRef half = lp_build_const_v4sf(0.5);
 
1785
   LLVMValueRef tmp = LLVMBuildFMul(b, z, half, "tmp");
 
1786
   LLVMValueRef y_9 = LLVMBuildFSub(b, y_8, tmp, "y_8");
 
1787
   LLVMValueRef one = lp_build_const_v4sf(1.0);
 
1788
   LLVMValueRef y_10 = LLVMBuildFAdd(b, y_9, one, "y_9");
 
1789
 
 
1790
   /*
 
1791
    * _PS_CONST(sincof_p0, -1.9515295891E-4);
 
1792
    * _PS_CONST(sincof_p1,  8.3321608736E-3);
 
1793
    * _PS_CONST(sincof_p2, -1.6666654611E-1);
 
1794
    */
 
1795
   LLVMValueRef sincof_p0 = lp_build_const_v4sf(-1.9515295891E-4);
 
1796
   LLVMValueRef sincof_p1 = lp_build_const_v4sf(8.3321608736E-3);
 
1797
   LLVMValueRef sincof_p2 = lp_build_const_v4sf(-1.6666654611E-1);
 
1798
 
 
1799
   /*
 
1800
    * Evaluate the second polynom  (Pi/4 <= x <= 0)
 
1801
    *
 
1802
    * y2 = *(v4sf*)_ps_sincof_p0;
 
1803
    * y2 = _mm_mul_ps(y2, z);
 
1804
    * y2 = _mm_add_ps(y2, *(v4sf*)_ps_sincof_p1);
 
1805
    * y2 = _mm_mul_ps(y2, z);
 
1806
    * y2 = _mm_add_ps(y2, *(v4sf*)_ps_sincof_p2);
 
1807
    * y2 = _mm_mul_ps(y2, z);
 
1808
    * y2 = _mm_mul_ps(y2, x);
 
1809
    * y2 = _mm_add_ps(y2, x);
 
1810
    */
 
1811
 
 
1812
   LLVMValueRef y2_3 = LLVMBuildFMul(b, z, sincof_p0, "y2_3");
 
1813
   LLVMValueRef y2_4 = LLVMBuildFAdd(b, y2_3, sincof_p1, "y2_4");
 
1814
   LLVMValueRef y2_5 = LLVMBuildFMul(b, y2_4, z, "y2_5");
 
1815
   LLVMValueRef y2_6 = LLVMBuildFAdd(b, y2_5, sincof_p2, "y2_6");
 
1816
   LLVMValueRef y2_7 = LLVMBuildFMul(b, y2_6, z, "y2_7");
 
1817
   LLVMValueRef y2_8 = LLVMBuildFMul(b, y2_7, x_3, "y2_8");
 
1818
   LLVMValueRef y2_9 = LLVMBuildFAdd(b, y2_8, x_3, "y2_9");
 
1819
 
 
1820
   /*
 
1821
    * select the correct result from the two polynoms
 
1822
    * xmm3 = poly_mask;
 
1823
    * y2 = _mm_and_ps(xmm3, y2); //, xmm3);
 
1824
    * y = _mm_andnot_ps(xmm3, y);
 
1825
    * y = _mm_add_ps(y,y2);
 
1826
    */
 
1827
   LLVMValueRef y2_i = LLVMBuildBitCast(b, y2_9, v4si, "y2_i");
 
1828
   LLVMValueRef y_i = LLVMBuildBitCast(b, y_10, v4si, "y_i");
 
1829
   LLVMValueRef y2_and = LLVMBuildAnd(b, y2_i, poly_mask, "y2_and");
 
1830
   LLVMValueRef poly_mask_inv = LLVMBuildXor(b, poly_mask, inv, "poly_mask_inv");
 
1831
   LLVMValueRef y_and = LLVMBuildAnd(b, y_i, poly_mask_inv, "y_and");
 
1832
   LLVMValueRef y_combine = LLVMBuildAdd(b, y_and, y2_and, "y_combine");
 
1833
 
 
1834
   /*
 
1835
    * update the sign
 
1836
    * y = _mm_xor_ps(y, sign_bit);
 
1837
    */
 
1838
   LLVMValueRef y_sign = LLVMBuildXor(b, y_combine, sign_bit, "y_sin");
 
1839
   LLVMValueRef y_result = LLVMBuildBitCast(b, y_sign, v4sf, "y_result");
 
1840
   return y_result;
1147
1841
}
1148
1842
 
1149
1843
 
1172
1866
             LLVMValueRef x)
1173
1867
{
1174
1868
   /* log2(e) = 1/log(2) */
1175
 
   LLVMValueRef log2e = lp_build_const_scalar(bld->type, 1.4426950408889634);
 
1869
   LLVMValueRef log2e = lp_build_const_vec(bld->type, 1.4426950408889634);
 
1870
 
 
1871
   assert(lp_check_value(bld->type, x));
1176
1872
 
1177
1873
   return lp_build_mul(bld, log2e, lp_build_exp2(bld, x));
1178
1874
}
1186
1882
             LLVMValueRef x)
1187
1883
{
1188
1884
   /* log(2) */
1189
 
   LLVMValueRef log2 = lp_build_const_scalar(bld->type, 0.69314718055994529);
 
1885
   LLVMValueRef log2 = lp_build_const_vec(bld->type, 0.69314718055994529);
 
1886
 
 
1887
   assert(lp_check_value(bld->type, x));
1190
1888
 
1191
1889
   return lp_build_mul(bld, log2, lp_build_exp2(bld, x));
1192
1890
}
1193
1891
 
1194
1892
 
1195
 
#define EXP_POLY_DEGREE 3
1196
 
#define LOG_POLY_DEGREE 5
1197
 
 
1198
 
 
1199
1893
/**
1200
1894
 * Generate polynomial.
1201
1895
 * Ex:  coeffs[0] + x * coeffs[1] + x^2 * coeffs[2].
1210
1904
   LLVMValueRef res = NULL;
1211
1905
   unsigned i;
1212
1906
 
 
1907
   assert(lp_check_value(bld->type, x));
 
1908
 
1213
1909
   /* TODO: optimize the constant case */
1214
1910
   if(LLVMIsConstant(x))
1215
1911
      debug_printf("%s: inefficient/imprecise constant arithmetic\n",
1216
1912
                   __FUNCTION__);
1217
1913
 
1218
1914
   for (i = num_coeffs; i--; ) {
1219
 
      LLVMValueRef coeff = lp_build_const_scalar(type, coeffs[i]);
 
1915
      LLVMValueRef coeff;
 
1916
 
 
1917
      coeff = lp_build_const_vec(type, coeffs[i]);
 
1918
 
1220
1919
      if(res)
1221
1920
         res = lp_build_add(bld, coeff, lp_build_mul(bld, x, res));
1222
1921
      else
1231
1930
 
1232
1931
 
1233
1932
/**
1234
 
 * Minimax polynomial fit of 2**x, in range [-0.5, 0.5[
 
1933
 * Minimax polynomial fit of 2**x, in range [0, 1[
1235
1934
 */
1236
1935
const double lp_build_exp2_polynomial[] = {
1237
1936
#if EXP_POLY_DEGREE == 5
1238
 
   9.9999994e-1, 6.9315308e-1, 2.4015361e-1, 5.5826318e-2, 8.9893397e-3, 1.8775767e-3
 
1937
   0.999999999690134838155,
 
1938
   0.583974334321735217258,
 
1939
   0.164553105719676828492,
 
1940
   0.0292811063701710962255,
 
1941
   0.00354944426657875141846,
 
1942
   0.000296253726543423377365
1239
1943
#elif EXP_POLY_DEGREE == 4
1240
 
   1.0000026, 6.9300383e-1, 2.4144275e-1, 5.2011464e-2, 1.3534167e-2
 
1944
   1.00000001502262084505,
 
1945
   0.563586057338685991394,
 
1946
   0.150436017652442413623,
 
1947
   0.0243220604213317927308,
 
1948
   0.0025359088446580436489
1241
1949
#elif EXP_POLY_DEGREE == 3
1242
 
   9.9992520e-1, 6.9583356e-1, 2.2606716e-1, 7.8024521e-2
 
1950
   0.999925218562710312959,
 
1951
   0.695833540494823811697,
 
1952
   0.226067155427249155588,
 
1953
   0.0780245226406372992967
1243
1954
#elif EXP_POLY_DEGREE == 2
1244
 
   1.0017247, 6.5763628e-1, 3.3718944e-1
 
1955
   1.00172476321474503578,
 
1956
   0.657636275736077639316,
 
1957
   0.33718943461968720704
1245
1958
#else
1246
1959
#error
1247
1960
#endif
1264
1977
   LLVMValueRef expfpart = NULL;
1265
1978
   LLVMValueRef res = NULL;
1266
1979
 
 
1980
   assert(lp_check_value(bld->type, x));
 
1981
 
1267
1982
   if(p_exp2_int_part || p_frac_part || p_exp2) {
1268
1983
      /* TODO: optimize the constant case */
1269
1984
      if(LLVMIsConstant(x))
1272
1987
 
1273
1988
      assert(type.floating && type.width == 32);
1274
1989
 
1275
 
      x = lp_build_min(bld, x, lp_build_const_scalar(type,  129.0));
1276
 
      x = lp_build_max(bld, x, lp_build_const_scalar(type, -126.99999));
 
1990
      x = lp_build_min(bld, x, lp_build_const_vec(type,  129.0));
 
1991
      x = lp_build_max(bld, x, lp_build_const_vec(type, -126.99999));
1277
1992
 
1278
 
      /* ipart = int(x - 0.5) */
1279
 
      ipart = LLVMBuildSub(bld->builder, x, lp_build_const_scalar(type, 0.5f), "");
1280
 
      ipart = LLVMBuildFPToSI(bld->builder, ipart, int_vec_type, "");
 
1993
      /* ipart = floor(x) */
 
1994
      ipart = lp_build_floor(bld, x);
1281
1995
 
1282
1996
      /* fpart = x - ipart */
1283
 
      fpart = LLVMBuildSIToFP(bld->builder, ipart, vec_type, "");
1284
 
      fpart = LLVMBuildSub(bld->builder, x, fpart, "");
 
1997
      fpart = LLVMBuildFSub(bld->builder, x, ipart, "");
1285
1998
   }
1286
1999
 
1287
2000
   if(p_exp2_int_part || p_exp2) {
1288
2001
      /* expipart = (float) (1 << ipart) */
1289
 
      expipart = LLVMBuildAdd(bld->builder, ipart, lp_build_int_const_scalar(type, 127), "");
1290
 
      expipart = LLVMBuildShl(bld->builder, expipart, lp_build_int_const_scalar(type, 23), "");
 
2002
      ipart = LLVMBuildFPToSI(bld->builder, ipart, int_vec_type, "");
 
2003
      expipart = LLVMBuildAdd(bld->builder, ipart, lp_build_const_int_vec(type, 127), "");
 
2004
      expipart = LLVMBuildShl(bld->builder, expipart, lp_build_const_int_vec(type, 23), "");
1291
2005
      expipart = LLVMBuildBitCast(bld->builder, expipart, vec_type, "");
1292
2006
   }
1293
2007
 
1295
2009
      expfpart = lp_build_polynomial(bld, fpart, lp_build_exp2_polynomial,
1296
2010
                                     Elements(lp_build_exp2_polynomial));
1297
2011
 
1298
 
      res = LLVMBuildMul(bld->builder, expipart, expfpart, "");
 
2012
      res = LLVMBuildFMul(bld->builder, expipart, expfpart, "");
1299
2013
   }
1300
2014
 
1301
2015
   if(p_exp2_int_part)
1326
2040
 */
1327
2041
const double lp_build_log2_polynomial[] = {
1328
2042
#if LOG_POLY_DEGREE == 6
1329
 
   3.11578814719469302614, -3.32419399085241980044, 2.59883907202499966007, -1.23152682416275988241, 0.318212422185251071475, -0.0344359067839062357313
 
2043
   3.11578814719469302614,
 
2044
   -3.32419399085241980044,
 
2045
   2.59883907202499966007,
 
2046
   -1.23152682416275988241,
 
2047
   0.318212422185251071475,
 
2048
   -0.0344359067839062357313
1330
2049
#elif LOG_POLY_DEGREE == 5
1331
 
   2.8882704548164776201, -2.52074962577807006663, 1.48116647521213171641, -0.465725644288844778798, 0.0596515482674574969533
 
2050
   2.8882704548164776201,
 
2051
   -2.52074962577807006663,
 
2052
   1.48116647521213171641,
 
2053
   -0.465725644288844778798,
 
2054
   0.0596515482674574969533
1332
2055
#elif LOG_POLY_DEGREE == 4
1333
 
   2.61761038894603480148, -1.75647175389045657003, 0.688243882994381274313, -0.107254423828329604454
 
2056
   2.61761038894603480148,
 
2057
   -1.75647175389045657003,
 
2058
   0.688243882994381274313,
 
2059
   -0.107254423828329604454
1334
2060
#elif LOG_POLY_DEGREE == 3
1335
 
   2.28330284476918490682, -1.04913055217340124191, 0.204446009836232697516
 
2061
   2.28330284476918490682,
 
2062
   -1.04913055217340124191,
 
2063
   0.204446009836232697516
1336
2064
#else
1337
2065
#error
1338
2066
#endif
1353
2081
   LLVMTypeRef vec_type = lp_build_vec_type(type);
1354
2082
   LLVMTypeRef int_vec_type = lp_build_int_vec_type(type);
1355
2083
 
1356
 
   LLVMValueRef expmask = lp_build_int_const_scalar(type, 0x7f800000);
1357
 
   LLVMValueRef mantmask = lp_build_int_const_scalar(type, 0x007fffff);
 
2084
   LLVMValueRef expmask = lp_build_const_int_vec(type, 0x7f800000);
 
2085
   LLVMValueRef mantmask = lp_build_const_int_vec(type, 0x007fffff);
1358
2086
   LLVMValueRef one = LLVMConstBitCast(bld->one, int_vec_type);
1359
2087
 
1360
2088
   LLVMValueRef i = NULL;
1364
2092
   LLVMValueRef logmant = NULL;
1365
2093
   LLVMValueRef res = NULL;
1366
2094
 
 
2095
   assert(lp_check_value(bld->type, x));
 
2096
 
1367
2097
   if(p_exp || p_floor_log2 || p_log2) {
1368
2098
      /* TODO: optimize the constant case */
1369
2099
      if(LLVMIsConstant(x))
1379
2109
   }
1380
2110
 
1381
2111
   if(p_floor_log2 || p_log2) {
1382
 
      logexp = LLVMBuildLShr(bld->builder, exp, lp_build_int_const_scalar(type, 23), "");
1383
 
      logexp = LLVMBuildSub(bld->builder, logexp, lp_build_int_const_scalar(type, 127), "");
 
2112
      logexp = LLVMBuildLShr(bld->builder, exp, lp_build_const_int_vec(type, 23), "");
 
2113
      logexp = LLVMBuildSub(bld->builder, logexp, lp_build_const_int_vec(type, 127), "");
1384
2114
      logexp = LLVMBuildSIToFP(bld->builder, logexp, vec_type, "");
1385
2115
   }
1386
2116
 
1394
2124
                                    Elements(lp_build_log2_polynomial));
1395
2125
 
1396
2126
      /* This effectively increases the polynomial degree by one, but ensures that log2(1) == 0*/
1397
 
      logmant = LLVMBuildMul(bld->builder, logmant, LLVMBuildSub(bld->builder, mant, bld->one, ""), "");
 
2127
      logmant = LLVMBuildFMul(bld->builder, logmant, LLVMBuildFSub(bld->builder, mant, bld->one, ""), "");
1398
2128
 
1399
 
      res = LLVMBuildAdd(bld->builder, logmant, logexp, "");
 
2129
      res = LLVMBuildFAdd(bld->builder, logmant, logexp, "");
1400
2130
   }
1401
2131
 
1402
 
   if(p_exp)
 
2132
   if(p_exp) {
 
2133
      exp = LLVMBuildBitCast(bld->builder, exp, vec_type, "");
1403
2134
      *p_exp = exp;
 
2135
   }
1404
2136
 
1405
2137
   if(p_floor_log2)
1406
2138
      *p_floor_log2 = logexp;