summaryrefslogtreecommitdiff
path: root/driver/runtime/arch/generic.c
blob: e178ed5882a38ffc0454a92b24146965c3b75fa3 (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
/*
 * Copyright (C) 2012 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */


#include "rs_core.rsh"

extern short __attribute__((overloadable, always_inline)) rsClamp(short amount, short low, short high);
extern uchar4 __attribute__((overloadable)) convert_uchar4(short4);
extern uchar4 __attribute__((overloadable)) convert_uchar4(float4);
extern float4 __attribute__((overloadable)) convert_float4(uchar4);
extern float __attribute__((overloadable)) sqrt(float);

/*
 * CLAMP
 */
#define _CLAMP(T) \
extern T __attribute__((overloadable)) clamp(T amount, T low, T high) {             \
    return amount < low ? low : (amount > high ? high : amount);                    \
}                                                                                   \
                                                                                    \
extern T##2 __attribute__((overloadable)) clamp(T##2 amount, T##2 low, T##2 high) { \
    T##2 r;                                                                         \
    r.x = amount.x < low.x ? low.x : (amount.x > high.x ? high.x : amount.x);       \
    r.y = amount.y < low.y ? low.y : (amount.y > high.y ? high.y : amount.y);       \
    return r;                                                                       \
}                                                                                   \
                                                                                    \
extern T##3 __attribute__((overloadable)) clamp(T##3 amount, T##3 low, T##3 high) { \
    T##3 r;                                                                         \
    r.x = amount.x < low.x ? low.x : (amount.x > high.x ? high.x : amount.x);       \
    r.y = amount.y < low.y ? low.y : (amount.y > high.y ? high.y : amount.y);       \
    r.z = amount.z < low.z ? low.z : (amount.z > high.z ? high.z : amount.z);       \
    return r;                                                                       \
}                                                                                   \
                                                                                    \
extern T##4 __attribute__((overloadable)) clamp(T##4 amount, T##4 low, T##4 high) { \
    T##4 r;                                                                         \
    r.x = amount.x < low.x ? low.x : (amount.x > high.x ? high.x : amount.x);       \
    r.y = amount.y < low.y ? low.y : (amount.y > high.y ? high.y : amount.y);       \
    r.z = amount.z < low.z ? low.z : (amount.z > high.z ? high.z : amount.z);       \
    r.w = amount.w < low.w ? low.w : (amount.w > high.w ? high.w : amount.w);       \
    return r;                                                                       \
}                                                                                   \
                                                                                    \
extern T##2 __attribute__((overloadable)) clamp(T##2 amount, T low, T high) {       \
    T##2 r;                                                                         \
    r.x = amount.x < low ? low : (amount.x > high ? high : amount.x);               \
    r.y = amount.y < low ? low : (amount.y > high ? high : amount.y);               \
    return r;                                                                       \
}                                                                                   \
                                                                                    \
extern T##3 __attribute__((overloadable)) clamp(T##3 amount, T low, T high) {       \
    T##3 r;                                                                         \
    r.x = amount.x < low ? low : (amount.x > high ? high : amount.x);               \
    r.y = amount.y < low ? low : (amount.y > high ? high : amount.y);               \
    r.z = amount.z < low ? low : (amount.z > high ? high : amount.z);               \
    return r;                                                                       \
}                                                                                   \
                                                                                    \
extern T##4 __attribute__((overloadable)) clamp(T##4 amount, T low, T high) {       \
    T##4 r;                                                                         \
    r.x = amount.x < low ? low : (amount.x > high ? high : amount.x);               \
    r.y = amount.y < low ? low : (amount.y > high ? high : amount.y);               \
    r.z = amount.z < low ? low : (amount.z > high ? high : amount.z);               \
    r.w = amount.w < low ? low : (amount.w > high ? high : amount.w);               \
    return r;                                                                       \
}

#if !defined(__i386__) && !defined(__x86_64__)

_CLAMP(float);

#else

extern float __attribute__((overloadable)) clamp(float amount, float low, float high);
extern float2 __attribute__((overloadable)) clamp(float2 amount, float2 low, float2 high);
extern float3 __attribute__((overloadable)) clamp(float3 amount, float3 low, float3 high);
extern float4 __attribute__((overloadable)) clamp(float4 amount, float4 low, float4 high);
extern float2 __attribute__((overloadable)) clamp(float2 amount, float low, float high);
extern float3 __attribute__((overloadable)) clamp(float3 amount, float low, float high);
extern float4 __attribute__((overloadable)) clamp(float4 amount, float low, float high);

#endif // !defined(__i386__) && !defined(__x86_64__)

_CLAMP(double);
_CLAMP(char);
_CLAMP(uchar);
_CLAMP(short);
_CLAMP(ushort);
_CLAMP(int);
_CLAMP(uint);
_CLAMP(long);
_CLAMP(ulong);

#undef _CLAMP

/*
 * FMAX
 */

extern float __attribute__((overloadable)) fmax(float v1, float v2) {
    return v1 > v2 ? v1 : v2;
}

extern float2 __attribute__((overloadable)) fmax(float2 v1, float2 v2) {
    float2 r;
    r.x = v1.x > v2.x ? v1.x : v2.x;
    r.y = v1.y > v2.y ? v1.y : v2.y;
    return r;
}

extern float3 __attribute__((overloadable)) fmax(float3 v1, float3 v2) {
    float3 r;
    r.x = v1.x > v2.x ? v1.x : v2.x;
    r.y = v1.y > v2.y ? v1.y : v2.y;
    r.z = v1.z > v2.z ? v1.z : v2.z;
    return r;
}

extern float4 __attribute__((overloadable)) fmax(float4 v1, float4 v2) {
    float4 r;
    r.x = v1.x > v2.x ? v1.x : v2.x;
    r.y = v1.y > v2.y ? v1.y : v2.y;
    r.z = v1.z > v2.z ? v1.z : v2.z;
    r.w = v1.w > v2.w ? v1.w : v2.w;
    return r;
}

extern float2 __attribute__((overloadable)) fmax(float2 v1, float v2) {
    float2 r;
    r.x = v1.x > v2 ? v1.x : v2;
    r.y = v1.y > v2 ? v1.y : v2;
    return r;
}

extern float3 __attribute__((overloadable)) fmax(float3 v1, float v2) {
    float3 r;
    r.x = v1.x > v2 ? v1.x : v2;
    r.y = v1.y > v2 ? v1.y : v2;
    r.z = v1.z > v2 ? v1.z : v2;
    return r;
}

extern float4 __attribute__((overloadable)) fmax(float4 v1, float v2) {
    float4 r;
    r.x = v1.x > v2 ? v1.x : v2;
    r.y = v1.y > v2 ? v1.y : v2;
    r.z = v1.z > v2 ? v1.z : v2;
    r.w = v1.w > v2 ? v1.w : v2;
    return r;
}

extern float __attribute__((overloadable)) fmin(float v1, float v2) {
    return v1 < v2 ? v1 : v2;
}


/*
 * FMIN
 */
extern float2 __attribute__((overloadable)) fmin(float2 v1, float2 v2) {
    float2 r;
    r.x = v1.x < v2.x ? v1.x : v2.x;
    r.y = v1.y < v2.y ? v1.y : v2.y;
    return r;
}

extern float3 __attribute__((overloadable)) fmin(float3 v1, float3 v2) {
    float3 r;
    r.x = v1.x < v2.x ? v1.x : v2.x;
    r.y = v1.y < v2.y ? v1.y : v2.y;
    r.z = v1.z < v2.z ? v1.z : v2.z;
    return r;
}

extern float4 __attribute__((overloadable)) fmin(float4 v1, float4 v2) {
    float4 r;
    r.x = v1.x < v2.x ? v1.x : v2.x;
    r.y = v1.y < v2.y ? v1.y : v2.y;
    r.z = v1.z < v2.z ? v1.z : v2.z;
    r.w = v1.w < v2.w ? v1.w : v2.w;
    return r;
}

extern float2 __attribute__((overloadable)) fmin(float2 v1, float v2) {
    float2 r;
    r.x = v1.x < v2 ? v1.x : v2;
    r.y = v1.y < v2 ? v1.y : v2;
    return r;
}

extern float3 __attribute__((overloadable)) fmin(float3 v1, float v2) {
    float3 r;
    r.x = v1.x < v2 ? v1.x : v2;
    r.y = v1.y < v2 ? v1.y : v2;
    r.z = v1.z < v2 ? v1.z : v2;
    return r;
}

extern float4 __attribute__((overloadable)) fmin(float4 v1, float v2) {
    float4 r;
    r.x = v1.x < v2 ? v1.x : v2;
    r.y = v1.y < v2 ? v1.y : v2;
    r.z = v1.z < v2 ? v1.z : v2;
    r.w = v1.w < v2 ? v1.w : v2;
    return r;
}


/*
 * MAX
 */

extern char __attribute__((overloadable)) max(char v1, char v2) {
    return v1 > v2 ? v1 : v2;
}

extern char2 __attribute__((overloadable)) max(char2 v1, char2 v2) {
    char2 r;
    r.x = v1.x > v2.x ? v1.x : v2.x;
    r.y = v1.y > v2.y ? v1.y : v2.y;
    return r;
}

extern char3 __attribute__((overloadable)) max(char3 v1, char3 v2) {
    char3 r;
    r.x = v1.x > v2.x ? v1.x : v2.x;
    r.y = v1.y > v2.y ? v1.y : v2.y;
    r.z = v1.z > v2.z ? v1.z : v2.z;
    return r;
}

extern char4 __attribute__((overloadable)) max(char4 v1, char4 v2) {
    char4 r;
    r.x = v1.x > v2.x ? v1.x : v2.x;
    r.y = v1.y > v2.y ? v1.y : v2.y;
    r.z = v1.z > v2.z ? v1.z : v2.z;
    r.w = v1.w > v2.w ? v1.w : v2.w;
    return r;
}

extern short __attribute__((overloadable)) max(short v1, short v2) {
    return v1 > v2 ? v1 : v2;
}

extern short2 __attribute__((overloadable)) max(short2 v1, short2 v2) {
    short2 r;
    r.x = v1.x > v2.x ? v1.x : v2.x;
    r.y = v1.y > v2.y ? v1.y : v2.y;
    return r;
}

extern short3 __attribute__((overloadable)) max(short3 v1, short3 v2) {
    short3 r;
    r.x = v1.x > v2.x ? v1.x : v2.x;
    r.y = v1.y > v2.y ? v1.y : v2.y;
    r.z = v1.z > v2.z ? v1.z : v2.z;
    return r;
}

extern short4 __attribute__((overloadable)) max(short4 v1, short4 v2) {
    short4 r;
    r.x = v1.x > v2.x ? v1.x : v2.x;
    r.y = v1.y > v2.y ? v1.y : v2.y;
    r.z = v1.z > v2.z ? v1.z : v2.z;
    r.w = v1.w > v2.w ? v1.w : v2.w;
    return r;
}

extern int __attribute__((overloadable)) max(int v1, int v2) {
    return v1 > v2 ? v1 : v2;
}

extern int2 __attribute__((overloadable)) max(int2 v1, int2 v2) {
    int2 r;
    r.x = v1.x > v2.x ? v1.x : v2.x;
    r.y = v1.y > v2.y ? v1.y : v2.y;
    return r;
}

extern int3 __attribute__((overloadable)) max(int3 v1, int3 v2) {
    int3 r;
    r.x = v1.x > v2.x ? v1.x : v2.x;
    r.y = v1.y > v2.y ? v1.y : v2.y;
    r.z = v1.z > v2.z ? v1.z : v2.z;
    return r;
}

extern int4 __attribute__((overloadable)) max(int4 v1, int4 v2) {
    int4 r;
    r.x = v1.x > v2.x ? v1.x : v2.x;
    r.y = v1.y > v2.y ? v1.y : v2.y;
    r.z = v1.z > v2.z ? v1.z : v2.z;
    r.w = v1.w > v2.w ? v1.w : v2.w;
    return r;
}

extern uchar __attribute__((overloadable)) max(uchar v1, uchar v2) {
    return v1 > v2 ? v1 : v2;
}

extern uchar2 __attribute__((overloadable)) max(uchar2 v1, uchar2 v2) {
    uchar2 r;
    r.x = v1.x > v2.x ? v1.x : v2.x;
    r.y = v1.y > v2.y ? v1.y : v2.y;
    return r;
}

extern uchar3 __attribute__((overloadable)) max(uchar3 v1, uchar3 v2) {
    uchar3 r;
    r.x = v1.x > v2.x ? v1.x : v2.x;
    r.y = v1.y > v2.y ? v1.y : v2.y;
    r.z = v1.z > v2.z ? v1.z : v2.z;
    return r;
}

extern uchar4 __attribute__((overloadable)) max(uchar4 v1, uchar4 v2) {
    uchar4 r;
    r.x = v1.x > v2.x ? v1.x : v2.x;
    r.y = v1.y > v2.y ? v1.y : v2.y;
    r.z = v1.z > v2.z ? v1.z : v2.z;
    r.w = v1.w > v2.w ? v1.w : v2.w;
    return r;
}

extern ushort __attribute__((overloadable)) max(ushort v1, ushort v2) {
    return v1 > v2 ? v1 : v2;
}

extern ushort2 __attribute__((overloadable)) max(ushort2 v1, ushort2 v2) {
    ushort2 r;
    r.x = v1.x > v2.x ? v1.x : v2.x;
    r.y = v1.y > v2.y ? v1.y : v2.y;
    return r;
}

extern ushort3 __attribute__((overloadable)) max(ushort3 v1, ushort3 v2) {
    ushort3 r;
    r.x = v1.x > v2.x ? v1.x : v2.x;
    r.y = v1.y > v2.y ? v1.y : v2.y;
    r.z = v1.z > v2.z ? v1.z : v2.z;
    return r;
}

extern ushort4 __attribute__((overloadable)) max(ushort4 v1, ushort4 v2) {
    ushort4 r;
    r.x = v1.x > v2.x ? v1.x : v2.x;
    r.y = v1.y > v2.y ? v1.y : v2.y;
    r.z = v1.z > v2.z ? v1.z : v2.z;
    r.w = v1.w > v2.w ? v1.w : v2.w;
    return r;
}

extern uint __attribute__((overloadable)) max(uint v1, uint v2) {
    return v1 > v2 ? v1 : v2;
}

extern uint2 __attribute__((overloadable)) max(uint2 v1, uint2 v2) {
    uint2 r;
    r.x = v1.x > v2.x ? v1.x : v2.x;
    r.y = v1.y > v2.y ? v1.y : v2.y;
    return r;
}

extern uint3 __attribute__((overloadable)) max(uint3 v1, uint3 v2) {
    uint3 r;
    r.x = v1.x > v2.x ? v1.x : v2.x;
    r.y = v1.y > v2.y ? v1.y : v2.y;
    r.z = v1.z > v2.z ? v1.z : v2.z;
    return r;
}

extern uint4 __attribute__((overloadable)) max(uint4 v1, uint4 v2) {
    uint4 r;
    r.x = v1.x > v2.x ? v1.x : v2.x;
    r.y = v1.y > v2.y ? v1.y : v2.y;
    r.z = v1.z > v2.z ? v1.z : v2.z;
    r.w = v1.w > v2.w ? v1.w : v2.w;
    return r;
}

extern float __attribute__((overloadable)) max(float v1, float v2) {
    return fmax(v1, v2);
}

extern float2 __attribute__((overloadable)) max(float2 v1, float2 v2) {
    return fmax(v1, v2);
}

extern float2 __attribute__((overloadable)) max(float2 v1, float v2) {
    return fmax(v1, v2);
}

extern float3 __attribute__((overloadable)) max(float3 v1, float3 v2) {
    return fmax(v1, v2);
}

extern float3 __attribute__((overloadable)) max(float3 v1, float v2) {
    return fmax(v1, v2);
}

extern float4 __attribute__((overloadable)) max(float4 v1, float4 v2) {
    return fmax(v1, v2);
}

extern float4 __attribute__((overloadable)) max(float4 v1, float v2) {
    return fmax(v1, v2);
}


/*
 * MIN
 */

extern int8_t __attribute__((overloadable)) min(int8_t v1, int8_t v2) {
    return v1 < v2 ? v1 : v2;
}

extern char2 __attribute__((overloadable)) min(char2 v1, char2 v2) {
    char2 r;
    r.x = v1.x < v2.x ? v1.x : v2.x;
    r.y = v1.y < v2.y ? v1.y : v2.y;
    return r;
}

extern char3 __attribute__((overloadable)) min(char3 v1, char3 v2) {
    char3 r;
    r.x = v1.x < v2.x ? v1.x : v2.x;
    r.y = v1.y < v2.y ? v1.y : v2.y;
    r.z = v1.z < v2.z ? v1.z : v2.z;
    return r;
}

extern char4 __attribute__((overloadable)) min(char4 v1, char4 v2) {
    char4 r;
    r.x = v1.x < v2.x ? v1.x : v2.x;
    r.y = v1.y < v2.y ? v1.y : v2.y;
    r.z = v1.z < v2.z ? v1.z : v2.z;
    r.w = v1.w < v2.w ? v1.w : v2.w;
    return r;
}

extern int16_t __attribute__((overloadable)) min(int16_t v1, int16_t v2) {
    return v1 < v2 ? v1 : v2;
}

extern short2 __attribute__((overloadable)) min(short2 v1, short2 v2) {
    short2 r;
    r.x = v1.x < v2.x ? v1.x : v2.x;
    r.y = v1.y < v2.y ? v1.y : v2.y;
    return r;
}

extern short3 __attribute__((overloadable)) min(short3 v1, short3 v2) {
    short3 r;
    r.x = v1.x < v2.x ? v1.x : v2.x;
    r.y = v1.y < v2.y ? v1.y : v2.y;
    r.z = v1.z < v2.z ? v1.z : v2.z;
    return r;
}

extern short4 __attribute__((overloadable)) min(short4 v1, short4 v2) {
    short4 r;
    r.x = v1.x < v2.x ? v1.x : v2.x;
    r.y = v1.y < v2.y ? v1.y : v2.y;
    r.z = v1.z < v2.z ? v1.z : v2.z;
    r.w = v1.w < v2.w ? v1.w : v2.w;
    return r;
}

extern int32_t __attribute__((overloadable)) min(int32_t v1, int32_t v2) {
    return v1 < v2 ? v1 : v2;
}

extern int2 __attribute__((overloadable)) min(int2 v1, int2 v2) {
    int2 r;
    r.x = v1.x < v2.x ? v1.x : v2.x;
    r.y = v1.y < v2.y ? v1.y : v2.y;
    return r;
}

extern int3 __attribute__((overloadable)) min(int3 v1, int3 v2) {
    int3 r;
    r.x = v1.x < v2.x ? v1.x : v2.x;
    r.y = v1.y < v2.y ? v1.y : v2.y;
    r.z = v1.z < v2.z ? v1.z : v2.z;
    return r;
}

extern int4 __attribute__((overloadable)) min(int4 v1, int4 v2) {
    int4 r;
    r.x = v1.x < v2.x ? v1.x : v2.x;
    r.y = v1.y < v2.y ? v1.y : v2.y;
    r.z = v1.z < v2.z ? v1.z : v2.z;
    r.w = v1.w < v2.w ? v1.w : v2.w;
    return r;
}

extern uchar __attribute__((overloadable)) min(uchar v1, uchar v2) {
    return v1 < v2 ? v1 : v2;
}

extern uchar2 __attribute__((overloadable)) min(uchar2 v1, uchar2 v2) {
    uchar2 r;
    r.x = v1.x < v2.x ? v1.x : v2.x;
    r.y = v1.y < v2.y ? v1.y : v2.y;
    return r;
}

extern uchar3 __attribute__((overloadable)) min(uchar3 v1, uchar3 v2) {
    uchar3 r;
    r.x = v1.x < v2.x ? v1.x : v2.x;
    r.y = v1.y < v2.y ? v1.y : v2.y;
    r.z = v1.z < v2.z ? v1.z : v2.z;
    return r;
}

extern uchar4 __attribute__((overloadable)) min(uchar4 v1, uchar4 v2) {
    uchar4 r;
    r.x = v1.x < v2.x ? v1.x : v2.x;
    r.y = v1.y < v2.y ? v1.y : v2.y;
    r.z = v1.z < v2.z ? v1.z : v2.z;
    r.w = v1.w < v2.w ? v1.w : v2.w;
    return r;
}

extern ushort __attribute__((overloadable)) min(ushort v1, ushort v2) {
    return v1 < v2 ? v1 : v2;
}

extern ushort2 __attribute__((overloadable)) min(ushort2 v1, ushort2 v2) {
    ushort2 r;
    r.x = v1.x < v2.x ? v1.x : v2.x;
    r.y = v1.y < v2.y ? v1.y : v2.y;
    return r;
}

extern ushort3 __attribute__((overloadable)) min(ushort3 v1, ushort3 v2) {
    ushort3 r;
    r.x = v1.x < v2.x ? v1.x : v2.x;
    r.y = v1.y < v2.y ? v1.y : v2.y;
    r.z = v1.z < v2.z ? v1.z : v2.z;
    return r;
}

extern ushort4 __attribute__((overloadable)) min(ushort4 v1, ushort4 v2) {
    ushort4 r;
    r.x = v1.x < v2.x ? v1.x : v2.x;
    r.y = v1.y < v2.y ? v1.y : v2.y;
    r.z = v1.z < v2.z ? v1.z : v2.z;
    r.w = v1.w < v2.w ? v1.w : v2.w;
    return r;
}

extern uint __attribute__((overloadable)) min(uint v1, uint v2) {
    return v1 < v2 ? v1 : v2;
}

extern uint2 __attribute__((overloadable)) min(uint2 v1, uint2 v2) {
    uint2 r;
    r.x = v1.x < v2.x ? v1.x : v2.x;
    r.y = v1.y < v2.y ? v1.y : v2.y;
    return r;
}

extern uint3 __attribute__((overloadable)) min(uint3 v1, uint3 v2) {
    uint3 r;
    r.x = v1.x < v2.x ? v1.x : v2.x;
    r.y = v1.y < v2.y ? v1.y : v2.y;
    r.z = v1.z < v2.z ? v1.z : v2.z;
    return r;
}

extern uint4 __attribute__((overloadable)) min(uint4 v1, uint4 v2) {
    uint4 r;
    r.x = v1.x < v2.x ? v1.x : v2.x;
    r.y = v1.y < v2.y ? v1.y : v2.y;
    r.z = v1.z < v2.z ? v1.z : v2.z;
    r.w = v1.w < v2.w ? v1.w : v2.w;
    return r;
}

extern float __attribute__((overloadable)) min(float v1, float v2) {
    return fmin(v1, v2);
}

extern float2 __attribute__((overloadable)) min(float2 v1, float2 v2) {
    return fmin(v1, v2);
}

extern float2 __attribute__((overloadable)) min(float2 v1, float v2) {
    return fmin(v1, v2);
}

extern float3 __attribute__((overloadable)) min(float3 v1, float3 v2) {
    return fmin(v1, v2);
}

extern float3 __attribute__((overloadable)) min(float3 v1, float v2) {
    return fmin(v1, v2);
}

extern float4 __attribute__((overloadable)) min(float4 v1, float4 v2) {
    return fmin(v1, v2);
}

extern float4 __attribute__((overloadable)) min(float4 v1, float v2) {
    return fmin(v1, v2);
}

/*
 * YUV
 */

extern uchar4 __attribute__((overloadable)) rsYuvToRGBA_uchar4(uchar y, uchar u, uchar v) {
    short Y = ((short)y) - 16;
    short U = ((short)u) - 128;
    short V = ((short)v) - 128;

    short4 p;
    p.r = (Y * 298 + V * 409 + 128) >> 8;
    p.g = (Y * 298 - U * 100 - V * 208 + 128) >> 8;
    p.b = (Y * 298 + U * 516 + 128) >> 8;
    p.a = 255;
    p.r = rsClamp(p.r, (short)0, (short)255);
    p.g = rsClamp(p.g, (short)0, (short)255);
    p.b = rsClamp(p.b, (short)0, (short)255);

    return convert_uchar4(p);
}

/*
 * half_RECIP
 */

extern float2 __attribute__((overloadable)) half_recip(float2 v) {
    return ((float2) 1.f) / v;
}

extern float3 __attribute__((overloadable)) half_recip(float3 v) {
    return ((float3) 1.f) / v;
}

extern float4 __attribute__((overloadable)) half_recip(float4 v) {
    return ((float4) 1.f) / v;
}



/*
 * half_rsqrt
 */

extern float __attribute__((overloadable)) half_rsqrt(float v) {
    return 1.f / sqrt(v);
}

extern float2 __attribute__((overloadable)) half_rsqrt(float2 v) {
    float2 r;
    r.x = half_rsqrt(v.x);
    r.y = half_rsqrt(v.y);
    return r;
}

extern float3 __attribute__((overloadable)) half_rsqrt(float3 v) {
    float3 r;
    r.x = half_rsqrt(v.x);
    r.y = half_rsqrt(v.y);
    r.z = half_rsqrt(v.z);
    return r;
}

extern float4 __attribute__((overloadable)) half_rsqrt(float4 v) {
    float4 r;
    r.x = half_rsqrt(v.x);
    r.y = half_rsqrt(v.y);
    r.z = half_rsqrt(v.z);
    r.w = half_rsqrt(v.w);
    return r;
}

/**
 * matrix ops
 */

extern float4 __attribute__((overloadable))
rsMatrixMultiply(const rs_matrix4x4 *m, float4 in) {
    float4 ret;
    ret.x = (m->m[0] * in.x) + (m->m[4] * in.y) + (m->m[8] * in.z) + (m->m[12] * in.w);
    ret.y = (m->m[1] * in.x) + (m->m[5] * in.y) + (m->m[9] * in.z) + (m->m[13] * in.w);
    ret.z = (m->m[2] * in.x) + (m->m[6] * in.y) + (m->m[10] * in.z) + (m->m[14] * in.w);
    ret.w = (m->m[3] * in.x) + (m->m[7] * in.y) + (m->m[11] * in.z) + (m->m[15] * in.w);
    return ret;
}

extern float4 __attribute__((overloadable))
rsMatrixMultiply(const rs_matrix4x4 *m, float3 in) {
    float4 ret;
    ret.x = (m->m[0] * in.x) + (m->m[4] * in.y) + (m->m[8] * in.z) + m->m[12];
    ret.y = (m->m[1] * in.x) + (m->m[5] * in.y) + (m->m[9] * in.z) + m->m[13];
    ret.z = (m->m[2] * in.x) + (m->m[6] * in.y) + (m->m[10] * in.z) + m->m[14];
    ret.w = (m->m[3] * in.x) + (m->m[7] * in.y) + (m->m[11] * in.z) + m->m[15];
    return ret;
}

extern float4 __attribute__((overloadable))
rsMatrixMultiply(const rs_matrix4x4 *m, float2 in) {
    float4 ret;
    ret.x = (m->m[0] * in.x) + (m->m[4] * in.y) + m->m[12];
    ret.y = (m->m[1] * in.x) + (m->m[5] * in.y) + m->m[13];
    ret.z = (m->m[2] * in.x) + (m->m[6] * in.y) + m->m[14];
    ret.w = (m->m[3] * in.x) + (m->m[7] * in.y) + m->m[15];
    return ret;
}

extern float3 __attribute__((overloadable))
rsMatrixMultiply(const rs_matrix3x3 *m, float3 in) {
    float3 ret;
    ret.x = (m->m[0] * in.x) + (m->m[3] * in.y) + (m->m[6] * in.z);
    ret.y = (m->m[1] * in.x) + (m->m[4] * in.y) + (m->m[7] * in.z);
    ret.z = (m->m[2] * in.x) + (m->m[5] * in.y) + (m->m[8] * in.z);
    return ret;
}

extern float3 __attribute__((overloadable))
rsMatrixMultiply(const rs_matrix3x3 *m, float2 in) {
    float3 ret;
    ret.x = (m->m[0] * in.x) + (m->m[3] * in.y);
    ret.y = (m->m[1] * in.x) + (m->m[4] * in.y);
    ret.z = (m->m[2] * in.x) + (m->m[5] * in.y);
    return ret;
}

/**
 * Pixel Ops
 */
extern uchar4 __attribute__((overloadable)) rsPackColorTo8888(float r, float g, float b)
{
    uchar4 c;
    c.x = (uchar)clamp((r * 255.f + 0.5f), 0.f, 255.f);
    c.y = (uchar)clamp((g * 255.f + 0.5f), 0.f, 255.f);
    c.z = (uchar)clamp((b * 255.f + 0.5f), 0.f, 255.f);
    c.w = 255;
    return c;
}

extern uchar4 __attribute__((overloadable)) rsPackColorTo8888(float r, float g, float b, float a)
{
    uchar4 c;
    c.x = (uchar)clamp((r * 255.f + 0.5f), 0.f, 255.f);
    c.y = (uchar)clamp((g * 255.f + 0.5f), 0.f, 255.f);
    c.z = (uchar)clamp((b * 255.f + 0.5f), 0.f, 255.f);
    c.w = (uchar)clamp((a * 255.f + 0.5f), 0.f, 255.f);
    return c;
}

extern uchar4 __attribute__((overloadable)) rsPackColorTo8888(float3 color)
{
    color *= 255.f;
    color += 0.5f;
    color = clamp(color, 0.f, 255.f);
    uchar4 c = {color.x, color.y, color.z, 255};
    return c;
}

extern uchar4 __attribute__((overloadable)) rsPackColorTo8888(float4 color)
{
    color *= 255.f;
    color += 0.5f;
    color = clamp(color, 0.f, 255.f);
    uchar4 c = {color.x, color.y, color.z, color.w};
    return c;
}