Welcome to mirror list, hosted at ThFree Co, Russian Federation.

ssef.h « util « cycles « intern - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 1e2bfa90354af27cf08c1f8a749d8906dbd1aef7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
/* SPDX-License-Identifier: Apache-2.0
 * Copyright 2011-2013 Intel Corporation
 * Modifications Copyright 2014-2022 Blender Foundation. */

#ifndef __UTIL_SSEF_H__
#define __UTIL_SSEF_H__

#include <math.h>

#include "util/ssei.h"

CCL_NAMESPACE_BEGIN

#ifdef __KERNEL_SSE2__

struct sseb;
struct ssef;

/*! 4-wide SSE float type. */
struct ssef {
  typedef sseb Mask;   // mask type
  typedef ssei Int;    // int type
  typedef ssef Float;  // float type

  enum { size = 4 };  // number of SIMD elements
  union {
    __m128 m128;
    float f[4];
    int i[4];
  };  // data

  ////////////////////////////////////////////////////////////////////////////////
  /// Constructors, Assignment & Cast Operators
  ////////////////////////////////////////////////////////////////////////////////

  __forceinline ssef()
  {
  }
  __forceinline ssef(const ssef &other)
  {
    m128 = other.m128;
  }
  __forceinline ssef &operator=(const ssef &other)
  {
    m128 = other.m128;
    return *this;
  }

  __forceinline ssef(const __m128 a) : m128(a)
  {
  }
  __forceinline operator const __m128 &() const
  {
    return m128;
  }
  __forceinline operator __m128 &()
  {
    return m128;
  }

  __forceinline ssef(float a) : m128(_mm_set1_ps(a))
  {
  }
  __forceinline ssef(float a, float b, float c, float d) : m128(_mm_setr_ps(a, b, c, d))
  {
  }

  __forceinline explicit ssef(const __m128i a) : m128(_mm_cvtepi32_ps(a))
  {
  }

  ////////////////////////////////////////////////////////////////////////////////
  /// Loads and Stores
  ////////////////////////////////////////////////////////////////////////////////

#  if defined(__KERNEL_AVX__)
  static __forceinline ssef broadcast(const void *const a)
  {
    return _mm_broadcast_ss((float *)a);
  }
#  else
  static __forceinline ssef broadcast(const void *const a)
  {
    return _mm_set1_ps(*(float *)a);
  }
#  endif

  ////////////////////////////////////////////////////////////////////////////////
  /// Array Access
  ////////////////////////////////////////////////////////////////////////////////

  __forceinline const float &operator[](const size_t i) const
  {
    assert(i < 4);
    return f[i];
  }
  __forceinline float &operator[](const size_t i)
  {
    assert(i < 4);
    return f[i];
  }
};

////////////////////////////////////////////////////////////////////////////////
/// Unary Operators
////////////////////////////////////////////////////////////////////////////////

__forceinline const ssef cast(const __m128i &a)
{
  return _mm_castsi128_ps(a);
}
__forceinline const ssef operator+(const ssef &a)
{
  return a;
}
__forceinline const ssef operator-(const ssef &a)
{
  return _mm_xor_ps(a.m128, _mm_castsi128_ps(_mm_set1_epi32(0x80000000)));
}
__forceinline const ssef abs(const ssef &a)
{
  return _mm_and_ps(a.m128, _mm_castsi128_ps(_mm_set1_epi32(0x7fffffff)));
}
#  if defined(__KERNEL_SSE41__)
__forceinline const ssef sign(const ssef &a)
{
  return _mm_blendv_ps(ssef(1.0f), -ssef(1.0f), _mm_cmplt_ps(a, ssef(0.0f)));
}
#  endif
__forceinline const ssef signmsk(const ssef &a)
{
  return _mm_and_ps(a.m128, _mm_castsi128_ps(_mm_set1_epi32(0x80000000)));
}

__forceinline const ssef rcp(const ssef &a)
{
  const ssef r = _mm_rcp_ps(a.m128);
  return _mm_sub_ps(_mm_add_ps(r, r), _mm_mul_ps(_mm_mul_ps(r, r), a));
}
__forceinline const ssef sqr(const ssef &a)
{
  return _mm_mul_ps(a, a);
}
__forceinline const ssef mm_sqrt(const ssef &a)
{
  return _mm_sqrt_ps(a.m128);
}
__forceinline const ssef rsqrt(const ssef &a)
{
  const ssef r = _mm_rsqrt_ps(a.m128);
  return _mm_add_ps(
      _mm_mul_ps(_mm_set_ps(1.5f, 1.5f, 1.5f, 1.5f), r),
      _mm_mul_ps(_mm_mul_ps(_mm_mul_ps(a, _mm_set_ps(-0.5f, -0.5f, -0.5f, -0.5f)), r),
                 _mm_mul_ps(r, r)));
}

////////////////////////////////////////////////////////////////////////////////
/// Binary Operators
////////////////////////////////////////////////////////////////////////////////

__forceinline const ssef operator+(const ssef &a, const ssef &b)
{
  return _mm_add_ps(a.m128, b.m128);
}
__forceinline const ssef operator+(const ssef &a, const float &b)
{
  return a + ssef(b);
}
__forceinline const ssef operator+(const float &a, const ssef &b)
{
  return ssef(a) + b;
}

__forceinline const ssef operator-(const ssef &a, const ssef &b)
{
  return _mm_sub_ps(a.m128, b.m128);
}
__forceinline const ssef operator-(const ssef &a, const float &b)
{
  return a - ssef(b);
}
__forceinline const ssef operator-(const float &a, const ssef &b)
{
  return ssef(a) - b;
}

__forceinline const ssef operator*(const ssef &a, const ssef &b)
{
  return _mm_mul_ps(a.m128, b.m128);
}
__forceinline const ssef operator*(const ssef &a, const float &b)
{
  return a * ssef(b);
}
__forceinline const ssef operator*(const float &a, const ssef &b)
{
  return ssef(a) * b;
}

__forceinline const ssef operator/(const ssef &a, const ssef &b)
{
  return _mm_div_ps(a.m128, b.m128);
}
__forceinline const ssef operator/(const ssef &a, const float &b)
{
  return a / ssef(b);
}
__forceinline const ssef operator/(const float &a, const ssef &b)
{
  return ssef(a) / b;
}

__forceinline const ssef operator^(const ssef &a, const ssef &b)
{
  return _mm_xor_ps(a.m128, b.m128);
}
__forceinline const ssef operator^(const ssef &a, const ssei &b)
{
  return _mm_xor_ps(a.m128, _mm_castsi128_ps(b.m128));
}

__forceinline const ssef operator&(const ssef &a, const ssef &b)
{
  return _mm_and_ps(a.m128, b.m128);
}
__forceinline const ssef operator&(const ssef &a, const ssei &b)
{
  return _mm_and_ps(a.m128, _mm_castsi128_ps(b.m128));
}

__forceinline const ssef operator|(const ssef &a, const ssef &b)
{
  return _mm_or_ps(a.m128, b.m128);
}
__forceinline const ssef operator|(const ssef &a, const ssei &b)
{
  return _mm_or_ps(a.m128, _mm_castsi128_ps(b.m128));
}

__forceinline const ssef andnot(const ssef &a, const ssef &b)
{
  return _mm_andnot_ps(a.m128, b.m128);
}

__forceinline const ssef min(const ssef &a, const ssef &b)
{
  return _mm_min_ps(a.m128, b.m128);
}
__forceinline const ssef min(const ssef &a, const float &b)
{
  return _mm_min_ps(a.m128, ssef(b));
}
__forceinline const ssef min(const float &a, const ssef &b)
{
  return _mm_min_ps(ssef(a), b.m128);
}

__forceinline const ssef max(const ssef &a, const ssef &b)
{
  return _mm_max_ps(a.m128, b.m128);
}
__forceinline const ssef max(const ssef &a, const float &b)
{
  return _mm_max_ps(a.m128, ssef(b));
}
__forceinline const ssef max(const float &a, const ssef &b)
{
  return _mm_max_ps(ssef(a), b.m128);
}

#  if defined(__KERNEL_SSE41__)
__forceinline ssef mini(const ssef &a, const ssef &b)
{
  const ssei ai = _mm_castps_si128(a);
  const ssei bi = _mm_castps_si128(b);
  const ssei ci = _mm_min_epi32(ai, bi);
  return _mm_castsi128_ps(ci);
}
#  endif

#  if defined(__KERNEL_SSE41__)
__forceinline ssef maxi(const ssef &a, const ssef &b)
{
  const ssei ai = _mm_castps_si128(a);
  const ssei bi = _mm_castps_si128(b);
  const ssei ci = _mm_max_epi32(ai, bi);
  return _mm_castsi128_ps(ci);
}
#  endif

////////////////////////////////////////////////////////////////////////////////
/// Ternary Operators
////////////////////////////////////////////////////////////////////////////////

__forceinline const ssef madd(const ssef &a, const ssef &b, const ssef &c)
{
#  if defined(__KERNEL_NEON__)
  return vfmaq_f32(c, a, b);
#  elif defined(__KERNEL_AVX2__)
  return _mm_fmadd_ps(a, b, c);
#  else
  return a * b + c;
#  endif
}
__forceinline const ssef msub(const ssef &a, const ssef &b, const ssef &c)
{
#  if defined(__KERNEL_NEON__)
  return vfmaq_f32(vnegq_f32(c), a, b);
#  elif defined(__KERNEL_AVX2__)
  return _mm_fmsub_ps(a, b, c);
#  else
  return a * b - c;
#  endif
}
__forceinline const ssef nmadd(const ssef &a, const ssef &b, const ssef &c)
{
#  if defined(__KERNEL_NEON__)
  return vfmsq_f32(c, a, b);
#  elif defined(__KERNEL_AVX2__)
  return _mm_fnmadd_ps(a, b, c);
#  else
  return c - a * b;
#  endif
}
__forceinline const ssef nmsub(const ssef &a, const ssef &b, const ssef &c)
{
#  if defined(__KERNEL_NEON__)
  return vfmsq_f32(vnegq_f32(c), a, b);
#  elif defined(__KERNEL_AVX2__)
  return _mm_fnmsub_ps(a, b, c);
#  else
  return -a * b - c;
#  endif
}

////////////////////////////////////////////////////////////////////////////////
/// Assignment Operators
////////////////////////////////////////////////////////////////////////////////

__forceinline ssef &operator+=(ssef &a, const ssef &b)
{
  return a = a + b;
}
__forceinline ssef &operator+=(ssef &a, const float &b)
{
  return a = a + b;
}

__forceinline ssef &operator-=(ssef &a, const ssef &b)
{
  return a = a - b;
}
__forceinline ssef &operator-=(ssef &a, const float &b)
{
  return a = a - b;
}

__forceinline ssef &operator*=(ssef &a, const ssef &b)
{
  return a = a * b;
}
__forceinline ssef &operator*=(ssef &a, const float &b)
{
  return a = a * b;
}

__forceinline ssef &operator/=(ssef &a, const ssef &b)
{
  return a = a / b;
}
__forceinline ssef &operator/=(ssef &a, const float &b)
{
  return a = a / b;
}

////////////////////////////////////////////////////////////////////////////////
/// Comparison Operators + Select
////////////////////////////////////////////////////////////////////////////////

__forceinline const sseb operator==(const ssef &a, const ssef &b)
{
  return _mm_cmpeq_ps(a.m128, b.m128);
}
__forceinline const sseb operator==(const ssef &a, const float &b)
{
  return a == ssef(b);
}
__forceinline const sseb operator==(const float &a, const ssef &b)
{
  return ssef(a) == b;
}

__forceinline const sseb operator!=(const ssef &a, const ssef &b)
{
  return _mm_cmpneq_ps(a.m128, b.m128);
}
__forceinline const sseb operator!=(const ssef &a, const float &b)
{
  return a != ssef(b);
}
__forceinline const sseb operator!=(const float &a, const ssef &b)
{
  return ssef(a) != b;
}

__forceinline const sseb operator<(const ssef &a, const ssef &b)
{
  return _mm_cmplt_ps(a.m128, b.m128);
}
__forceinline const sseb operator<(const ssef &a, const float &b)
{
  return a < ssef(b);
}
__forceinline const sseb operator<(const float &a, const ssef &b)
{
  return ssef(a) < b;
}

__forceinline const sseb operator>=(const ssef &a, const ssef &b)
{
  return _mm_cmpnlt_ps(a.m128, b.m128);
}
__forceinline const sseb operator>=(const ssef &a, const float &b)
{
  return a >= ssef(b);
}
__forceinline const sseb operator>=(const float &a, const ssef &b)
{
  return ssef(a) >= b;
}

__forceinline const sseb operator>(const ssef &a, const ssef &b)
{
  return _mm_cmpnle_ps(a.m128, b.m128);
}
__forceinline const sseb operator>(const ssef &a, const float &b)
{
  return a > ssef(b);
}
__forceinline const sseb operator>(const float &a, const ssef &b)
{
  return ssef(a) > b;
}

__forceinline const sseb operator<=(const ssef &a, const ssef &b)
{
  return _mm_cmple_ps(a.m128, b.m128);
}
__forceinline const sseb operator<=(const ssef &a, const float &b)
{
  return a <= ssef(b);
}
__forceinline const sseb operator<=(const float &a, const ssef &b)
{
  return ssef(a) <= b;
}

__forceinline const ssef select(const sseb &m, const ssef &t, const ssef &f)
{
#  ifdef __KERNEL_SSE41__
  return _mm_blendv_ps(f, t, m);
#  else
  return _mm_or_ps(_mm_and_ps(m, t), _mm_andnot_ps(m, f));
#  endif
}

__forceinline const ssef select(const ssef &m, const ssef &t, const ssef &f)
{
#  ifdef __KERNEL_SSE41__
  return _mm_blendv_ps(f, t, m);
#  else
  return _mm_or_ps(_mm_and_ps(m, t), _mm_andnot_ps(m, f));
#  endif
}

__forceinline const ssef select(const int mask, const ssef &t, const ssef &f)
{
#  if defined(__KERNEL_SSE41__) && \
      ((!defined(__clang__) && !defined(_MSC_VER)) || defined(__INTEL_COMPILER))
  return _mm_blend_ps(f, t, mask);
#  else
  return select(sseb(mask), t, f);
#  endif
}

////////////////////////////////////////////////////////////////////////////////
/// Rounding Functions
////////////////////////////////////////////////////////////////////////////////

#  if defined(__KERNEL_SSE41__)
__forceinline const ssef round_even(const ssef &a)
{
#    ifdef __KERNEL_NEON__
  return vrndnq_f32(a);
#    else
  return _mm_round_ps(a, _MM_FROUND_TO_NEAREST_INT);
#    endif
}
__forceinline const ssef round_down(const ssef &a)
{
#    ifdef __KERNEL_NEON__
  return vrndmq_f32(a);
#    else
  return _mm_round_ps(a, _MM_FROUND_TO_NEG_INF);
#    endif
}
__forceinline const ssef round_up(const ssef &a)
{
#    ifdef __KERNEL_NEON__
  return vrndpq_f32(a);
#    else
  return _mm_round_ps(a, _MM_FROUND_TO_POS_INF);
#    endif
}
__forceinline const ssef round_zero(const ssef &a)
{
#    ifdef __KERNEL_NEON__
  return vrndq_f32(a);
#    else
  return _mm_round_ps(a, _MM_FROUND_TO_ZERO);
#    endif
}
__forceinline const ssef floor(const ssef &a)
{
#    ifdef __KERNEL_NEON__
  return vrndmq_f32(a);
#    else
  return _mm_round_ps(a, _MM_FROUND_TO_NEG_INF);
#    endif
}
__forceinline const ssef ceil(const ssef &a)
{
#    ifdef __KERNEL_NEON__
  return vrndpq_f32(a);
#    else
  return _mm_round_ps(a, _MM_FROUND_TO_POS_INF);
#    endif
}
#  else
/* Non-SSE4.1 fallback, needed for floorfrac. */
__forceinline const ssef floor(const ssef &a)
{
  return _mm_set_ps(floorf(a.f[3]), floorf(a.f[2]), floorf(a.f[1]), floorf(a.f[0]));
}
#  endif

__forceinline ssei truncatei(const ssef &a)
{
  return _mm_cvttps_epi32(a.m128);
}

__forceinline ssef floorfrac(const ssef &x, ssei *i)
{
  ssef f = floor(x);
  *i = truncatei(f);
  return x - f;
}

////////////////////////////////////////////////////////////////////////////////
/// Common Functions
////////////////////////////////////////////////////////////////////////////////

__forceinline ssef mix(const ssef &a, const ssef &b, const ssef &t)
{
  return madd(t, b, (ssef(1.0f) - t) * a);
}

////////////////////////////////////////////////////////////////////////////////
/// Movement/Shifting/Shuffling Functions
////////////////////////////////////////////////////////////////////////////////

__forceinline ssef unpacklo(const ssef &a, const ssef &b)
{
  return _mm_unpacklo_ps(a.m128, b.m128);
}
__forceinline ssef unpackhi(const ssef &a, const ssef &b)
{
  return _mm_unpackhi_ps(a.m128, b.m128);
}

template<size_t i0, size_t i1, size_t i2, size_t i3>
__forceinline const ssef shuffle(const ssef &b)
{
#  ifdef __KERNEL_NEON__
  return shuffle_neon<float32x4_t, i0, i1, i2, i3>(b.m128);
#  else
  return _mm_castsi128_ps(_mm_shuffle_epi32(_mm_castps_si128(b), _MM_SHUFFLE(i3, i2, i1, i0)));
#  endif
}

template<> __forceinline const ssef shuffle<0, 1, 0, 1>(const ssef &a)
{
  return _mm_movelh_ps(a, a);
}

template<> __forceinline const ssef shuffle<2, 3, 2, 3>(const ssef &a)
{
  return _mm_movehl_ps(a, a);
}

template<size_t i0, size_t i1, size_t i2, size_t i3>
__forceinline const ssef shuffle(const ssef &a, const ssef &b)
{
#  ifdef __KERNEL_NEON__
  return shuffle_neon<float32x4_t, i0, i1, i2, i3>(a, b);
#  else
  return _mm_shuffle_ps(a, b, _MM_SHUFFLE(i3, i2, i1, i0));
#  endif
}

template<size_t i0> __forceinline const ssef shuffle(const ssef &a, const ssef &b)
{
#  ifdef __KERNEL_NEON__
  return shuffle_neon<float32x4_t, i0, i0, i0, i0>(a, b);
#  else
  return _mm_shuffle_ps(a, b, _MM_SHUFFLE(i0, i0, i0, i0));
#  endif
}

#  ifndef __KERNEL_NEON__
template<> __forceinline const ssef shuffle<0, 1, 0, 1>(const ssef &a, const ssef &b)
{
  return _mm_movelh_ps(a, b);
}

template<> __forceinline const ssef shuffle<2, 3, 2, 3>(const ssef &a, const ssef &b)
{
  return _mm_movehl_ps(b, a);
}
#  endif

#  if defined(__KERNEL_SSSE3__)
__forceinline const ssef shuffle8(const ssef &a, const ssei &shuf)
{
  return _mm_castsi128_ps(_mm_shuffle_epi8(_mm_castps_si128(a), shuf));
}
#  endif

#  if defined(__KERNEL_SSE3__)
template<> __forceinline const ssef shuffle<0, 0, 2, 2>(const ssef &b)
{
  return _mm_moveldup_ps(b);
}
template<> __forceinline const ssef shuffle<1, 1, 3, 3>(const ssef &b)
{
  return _mm_movehdup_ps(b);
}
#  endif

template<size_t i0> __forceinline const ssef shuffle(const ssef &b)
{
  return shuffle<i0, i0, i0, i0>(b);
}

#  if defined(__KERNEL_AVX__)
__forceinline const ssef shuffle(const ssef &a, const ssei &shuf)
{
  return _mm_permutevar_ps(a, shuf);
}
#  endif

template<size_t i> __forceinline float extract(const ssef &a)
{
  return _mm_cvtss_f32(shuffle<i, i, i, i>(a));
}
template<> __forceinline float extract<0>(const ssef &a)
{
  return _mm_cvtss_f32(a);
}

#  if defined(__KERNEL_SSE41__)
template<size_t dst, size_t src, size_t clr>
__forceinline const ssef insert(const ssef &a, const ssef &b)
{
#    ifdef __KERNEL_NEON__
  ssef res = a;
  if (clr)
    res[dst] = 0;
  else
    res[dst] = b[src];
  return res;
#    else
  return _mm_insert_ps(a, b, (dst << 4) | (src << 6) | clr);
#    endif
}
template<size_t dst, size_t src> __forceinline const ssef insert(const ssef &a, const ssef &b)
{
  return insert<dst, src, 0>(a, b);
}
template<size_t dst> __forceinline const ssef insert(const ssef &a, const float b)
{
  return insert<dst, 0>(a, _mm_set_ss(b));
}
#  else
template<size_t dst> __forceinline const ssef insert(const ssef &a, const float b)
{
  ssef c = a;
  c[dst] = b;
  return c;
}
#  endif

////////////////////////////////////////////////////////////////////////////////
/// Transpose
////////////////////////////////////////////////////////////////////////////////

__forceinline void transpose(const ssef &r0,
                             const ssef &r1,
                             const ssef &r2,
                             const ssef &r3,
                             ssef &c0,
                             ssef &c1,
                             ssef &c2,
                             ssef &c3)
{
  ssef l02 = unpacklo(r0, r2);
  ssef h02 = unpackhi(r0, r2);
  ssef l13 = unpacklo(r1, r3);
  ssef h13 = unpackhi(r1, r3);
  c0 = unpacklo(l02, l13);
  c1 = unpackhi(l02, l13);
  c2 = unpacklo(h02, h13);
  c3 = unpackhi(h02, h13);
}

__forceinline void transpose(
    const ssef &r0, const ssef &r1, const ssef &r2, const ssef &r3, ssef &c0, ssef &c1, ssef &c2)
{
  ssef l02 = unpacklo(r0, r2);
  ssef h02 = unpackhi(r0, r2);
  ssef l13 = unpacklo(r1, r3);
  ssef h13 = unpackhi(r1, r3);
  c0 = unpacklo(l02, l13);
  c1 = unpackhi(l02, l13);
  c2 = unpacklo(h02, h13);
}

////////////////////////////////////////////////////////////////////////////////
/// Reductions
////////////////////////////////////////////////////////////////////////////////

__forceinline const ssef vreduce_min(const ssef &v)
{
#  ifdef __KERNEL_NEON__
  return vdupq_n_f32(vminvq_f32(v));
#  else
  ssef h = min(shuffle<1, 0, 3, 2>(v), v);
  return min(shuffle<2, 3, 0, 1>(h), h);
#  endif
}
__forceinline const ssef vreduce_max(const ssef &v)
{
#  ifdef __KERNEL_NEON__
  return vdupq_n_f32(vmaxvq_f32(v));
#  else
  ssef h = max(shuffle<1, 0, 3, 2>(v), v);
  return max(shuffle<2, 3, 0, 1>(h), h);
#  endif
}
__forceinline const ssef vreduce_add(const ssef &v)
{
#  ifdef __KERNEL_NEON__
  return vdupq_n_f32(vaddvq_f32(v));
#  else
  ssef h = shuffle<1, 0, 3, 2>(v) + v;
  return shuffle<2, 3, 0, 1>(h) + h;
#  endif
}

__forceinline float reduce_min(const ssef &v)
{
#  ifdef __KERNEL_NEON__
  return vminvq_f32(v);
#  else
  return _mm_cvtss_f32(vreduce_min(v));
#  endif
}
__forceinline float reduce_max(const ssef &v)
{
#  ifdef __KERNEL_NEON__
  return vmaxvq_f32(v);
#  else
  return _mm_cvtss_f32(vreduce_max(v));
#  endif
}
__forceinline float reduce_add(const ssef &v)
{
#  ifdef __KERNEL_NEON__
  return vaddvq_f32(v);
#  else
  return _mm_cvtss_f32(vreduce_add(v));
#  endif
}

__forceinline uint32_t select_min(const ssef &v)
{
  return __bsf(movemask(v == vreduce_min(v)));
}
__forceinline uint32_t select_max(const ssef &v)
{
  return __bsf(movemask(v == vreduce_max(v)));
}

__forceinline uint32_t select_min(const sseb &valid, const ssef &v)
{
  const ssef a = select(valid, v, ssef(pos_inf));
  return __bsf(movemask(valid & (a == vreduce_min(a))));
}
__forceinline uint32_t select_max(const sseb &valid, const ssef &v)
{
  const ssef a = select(valid, v, ssef(neg_inf));
  return __bsf(movemask(valid & (a == vreduce_max(a))));
}

__forceinline uint32_t movemask(const ssef &a)
{
  return _mm_movemask_ps(a);
}

////////////////////////////////////////////////////////////////////////////////
/// Memory load and store operations
////////////////////////////////////////////////////////////////////////////////

__forceinline ssef load4f(const float4 &a)
{
#  ifdef __KERNEL_WITH_SSE_ALIGN__
  return _mm_load_ps(&a.x);
#  else
  return _mm_loadu_ps(&a.x);
#  endif
}

__forceinline ssef load4f(const float3 &a)
{
#  ifdef __KERNEL_WITH_SSE_ALIGN__
  return _mm_load_ps(&a.x);
#  else
  return _mm_loadu_ps(&a.x);
#  endif
}

__forceinline ssef load4f(const void *const a)
{
  return _mm_load_ps((float *)a);
}

__forceinline ssef load1f_first(const float a)
{
  return _mm_set_ss(a);
}

__forceinline void store4f(void *ptr, const ssef &v)
{
  _mm_store_ps((float *)ptr, v);
}

__forceinline ssef loadu4f(const void *const a)
{
  return _mm_loadu_ps((float *)a);
}

__forceinline void storeu4f(void *ptr, const ssef &v)
{
  _mm_storeu_ps((float *)ptr, v);
}

__forceinline void store4f(const sseb &mask, void *ptr, const ssef &f)
{
#  if defined(__KERNEL_AVX__)
  _mm_maskstore_ps((float *)ptr, (__m128i)mask, f);
#  else
  *(ssef *)ptr = select(mask, f, *(ssef *)ptr);
#  endif
}

__forceinline ssef load4f_nt(void *ptr)
{
#  if defined(__KERNEL_SSE41__)
  return _mm_castsi128_ps(_mm_stream_load_si128((__m128i *)ptr));
#  else
  return _mm_load_ps((float *)ptr);
#  endif
}

__forceinline void store4f_nt(void *ptr, const ssef &v)
{
#  if defined(__KERNEL_SSE41__)
  _mm_stream_ps((float *)ptr, v);
#  else
  _mm_store_ps((float *)ptr, v);
#  endif
}

////////////////////////////////////////////////////////////////////////////////
/// Euclidean Space Operators
////////////////////////////////////////////////////////////////////////////////

__forceinline float dot(const ssef &a, const ssef &b)
{
  return reduce_add(a * b);
}

/* calculate shuffled cross product, useful when order of components does not matter */
__forceinline ssef cross_zxy(const ssef &a, const ssef &b)
{
  const ssef a0 = a;
  const ssef b0 = shuffle<1, 2, 0, 3>(b);
  const ssef a1 = shuffle<1, 2, 0, 3>(a);
  const ssef b1 = b;
  return msub(a0, b0, a1 * b1);
}

__forceinline ssef cross(const ssef &a, const ssef &b)
{
  return shuffle<1, 2, 0, 3>(cross_zxy(a, b));
}

ccl_device_inline const ssef dot3_splat(const ssef &a, const ssef &b)
{
#  ifdef __KERNEL_SSE41__
  return _mm_dp_ps(a.m128, b.m128, 0x7f);
#  else
  ssef t = a * b;
  return ssef(((float *)&t)[0] + ((float *)&t)[1] + ((float *)&t)[2]);
#  endif
}

/* squared length taking only specified axes into account */
template<size_t X, size_t Y, size_t Z, size_t W> ccl_device_inline float len_squared(const ssef &a)
{
#  ifndef __KERNEL_SSE41__
  float4 &t = (float4 &)a;
  return (X ? t.x * t.x : 0.0f) + (Y ? t.y * t.y : 0.0f) + (Z ? t.z * t.z : 0.0f) +
         (W ? t.w * t.w : 0.0f);
#  else
  return extract<0>(
      ssef(_mm_dp_ps(a.m128, a.m128, (X << 4) | (Y << 5) | (Z << 6) | (W << 7) | 0xf)));
#  endif
}

ccl_device_inline float dot3(const ssef &a, const ssef &b)
{
#  ifdef __KERNEL_SSE41__
  return extract<0>(ssef(_mm_dp_ps(a.m128, b.m128, 0x7f)));
#  else
  ssef t = a * b;
  return ((float *)&t)[0] + ((float *)&t)[1] + ((float *)&t)[2];
#  endif
}

ccl_device_inline const ssef len3_squared_splat(const ssef &a)
{
  return dot3_splat(a, a);
}

ccl_device_inline float len3_squared(const ssef &a)
{
  return dot3(a, a);
}

ccl_device_inline float len3(const ssef &a)
{
  return extract<0>(mm_sqrt(dot3_splat(a, a)));
}

/* SSE shuffle utility functions */

#  ifdef __KERNEL_SSSE3__

/* faster version for SSSE3 */
typedef ssei shuffle_swap_t;

ccl_device_inline shuffle_swap_t shuffle_swap_identity()
{
  return _mm_set_epi8(15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0);
}

ccl_device_inline shuffle_swap_t shuffle_swap_swap()
{
  return _mm_set_epi8(7, 6, 5, 4, 3, 2, 1, 0, 15, 14, 13, 12, 11, 10, 9, 8);
}

ccl_device_inline const ssef shuffle_swap(const ssef &a, const shuffle_swap_t &shuf)
{
  return cast(_mm_shuffle_epi8(cast(a), shuf));
}

#  else

/* somewhat slower version for SSE2 */
typedef int shuffle_swap_t;

ccl_device_inline shuffle_swap_t shuffle_swap_identity()
{
  return 0;
}

ccl_device_inline shuffle_swap_t shuffle_swap_swap()
{
  return 1;
}

ccl_device_inline const ssef shuffle_swap(const ssef &a, shuffle_swap_t shuf)
{
  /* shuffle value must be a constant, so we need to branch */
  if (shuf)
    return shuffle<1, 0, 3, 2>(a);
  else
    return shuffle<3, 2, 1, 0>(a);
}

#  endif

#  if defined(__KERNEL_SSE41__) && !defined(__KERNEL_NEON__)

ccl_device_inline void gen_idirsplat_swap(const ssef &pn,
                                          const shuffle_swap_t &shuf_identity,
                                          const shuffle_swap_t &shuf_swap,
                                          const float3 &idir,
                                          ssef idirsplat[3],
                                          shuffle_swap_t shufflexyz[3])
{
  const __m128 idirsplat_raw[] = {_mm_set_ps1(idir.x), _mm_set_ps1(idir.y), _mm_set_ps1(idir.z)};
  idirsplat[0] = _mm_xor_ps(idirsplat_raw[0], pn);
  idirsplat[1] = _mm_xor_ps(idirsplat_raw[1], pn);
  idirsplat[2] = _mm_xor_ps(idirsplat_raw[2], pn);

  const ssef signmask = cast(ssei(0x80000000));
  const ssef shuf_identity_f = cast(shuf_identity);
  const ssef shuf_swap_f = cast(shuf_swap);

  shufflexyz[0] = _mm_castps_si128(
      _mm_blendv_ps(shuf_identity_f, shuf_swap_f, _mm_and_ps(idirsplat_raw[0], signmask)));
  shufflexyz[1] = _mm_castps_si128(
      _mm_blendv_ps(shuf_identity_f, shuf_swap_f, _mm_and_ps(idirsplat_raw[1], signmask)));
  shufflexyz[2] = _mm_castps_si128(
      _mm_blendv_ps(shuf_identity_f, shuf_swap_f, _mm_and_ps(idirsplat_raw[2], signmask)));
}

#  else

ccl_device_inline void gen_idirsplat_swap(const ssef &pn,
                                          const shuffle_swap_t &shuf_identity,
                                          const shuffle_swap_t &shuf_swap,
                                          const float3 &idir,
                                          ssef idirsplat[3],
                                          shuffle_swap_t shufflexyz[3])
{
  idirsplat[0] = ssef(idir.x) ^ pn;
  idirsplat[1] = ssef(idir.y) ^ pn;
  idirsplat[2] = ssef(idir.z) ^ pn;

  shufflexyz[0] = (idir.x >= 0) ? shuf_identity : shuf_swap;
  shufflexyz[1] = (idir.y >= 0) ? shuf_identity : shuf_swap;
  shufflexyz[2] = (idir.z >= 0) ? shuf_identity : shuf_swap;
}

#  endif

ccl_device_inline const ssef uint32_to_float(const ssei &in)
{
  ssei a = _mm_srli_epi32(in, 16);
  ssei b = _mm_and_si128(in, _mm_set1_epi32(0x0000ffff));
  ssei c = _mm_or_si128(a, _mm_set1_epi32(0x53000000));
  ssef d = _mm_cvtepi32_ps(b);
  ssef e = _mm_sub_ps(_mm_castsi128_ps(c), _mm_castsi128_ps(_mm_set1_epi32(0x53000000)));
  return _mm_add_ps(e, d);
}

template<size_t S1, size_t S2, size_t S3, size_t S4>
ccl_device_inline const ssef set_sign_bit(const ssef &a)
{
  return cast(cast(a) ^ ssei(S1 << 31, S2 << 31, S3 << 31, S4 << 31));
}

////////////////////////////////////////////////////////////////////////////////
/// Debug Functions
////////////////////////////////////////////////////////////////////////////////

ccl_device_inline void print_ssef(const char *label, const ssef &a)
{
  printf(
      "%s: %.8f %.8f %.8f %.8f\n", label, (double)a[0], (double)a[1], (double)a[2], (double)a[3]);
}

#endif

CCL_NAMESPACE_END

#endif