summaryrefslogtreecommitdiff
path: root/src/runtime/stdlib.c
blob: 90b994a74c70363cdca70682d933b6b1d4f070d1 (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
#include "stdlib.h"

int debug(const char *format, ...);

/* WARNING: Due to some device-specific things in stdlib.h, the bitcode stdlib
 * must only be used by CPUDevice, as it's targeted to the host CPU at Clover's
 * compilation! */

/*
 * Image functions
 */

int clamp(int a, int b, int c)
{
    return (a < b) ? b : ((a > c) ? c : a);
}

int __cpu_get_image_width(void *image);
int __cpu_get_image_height(void *image);
int __cpu_get_image_depth(void *image);
int __cpu_get_image_channel_data_type(void *image);
int __cpu_get_image_channel_order(void *image);
int __cpu_is_image_3d(void *image);
void *__cpu_image_data(void *image, int x, int y, int z, int *order, int *type);

int4 handle_address_mode(image3d_t image, int4 coord, sampler_t sampler)
{
    coord.w = 0;

    int w = get_image_width(image),
        h = get_image_height(image),
        d = get_image_depth(image);

    if ((sampler & 0xf0) ==  CLK_ADDRESS_CLAMP_TO_EDGE)
    {
        coord.x = clamp(coord.x, 0, w - 1);
        coord.y = clamp(coord.y, 0, h - 1);
        coord.z = clamp(coord.z, 0, d - 1);
    }
    else if ((sampler & 0xf0) == CLK_ADDRESS_CLAMP)
    {
        coord.x = clamp(coord.x, 0, w);
        coord.y = clamp(coord.y, 0, h);
        coord.z = clamp(coord.z, 0, d);
    }

    if (coord.x == w ||
        coord.y == h ||
        coord.z == d)
    {
        coord.w = 1;
    }

    return coord;
}

float4 OVERLOAD read_imagef(image2d_t image, sampler_t sampler, int2 coord)
{
    int4 c;
    c.xy = coord;
    c.zw = 0;

    return read_imagef((image3d_t)image, sampler, c);
}

float4 OVERLOAD read_imagef(image3d_t image, sampler_t sampler, int4 coord)
{
    float4 result;

    // Handle address mode
    coord = handle_address_mode(image, coord, sampler);

    if (coord.w != 0)
    {
        // Border color
        switch (get_image_channel_order(image))
        {
            case CLK_R:
            case CLK_RG:
            case CLK_RGB:
            case CLK_LUMINANCE:
                result.xyz = 0.0f;
                result.w = 1.0f;
                return result;
            default:
                result.xyzw = 0.0f;
                return result;
        }
    }

    int order, type;
    void *v_source = __cpu_image_data(image, coord.x, coord.y, coord.z, &order, &type);

#define UNSWIZZLE(order, source, data, m)\
    switch (order)                      \
    {                                   \
        case CLK_R:                     \
        case CLK_Rx:                    \
            data.x = (*source).x;       \
            data.yz = 0;                \
            data.w = m;                 \
            break;                      \
        case CLK_A:                     \
            data.w = (*source).x;       \
            data.xyz = 0;               \
            break;                      \
        case CLK_RG:                    \
        case CLK_RGx:                   \
            data.xy = (*source).xy;     \
            data.z = 0;                 \
            data.w = m;                 \
            break;                      \
        case CLK_RA:                    \
            data.xw = (*source).xy;     \
            data.yz = 0;                \
            break;                      \
        case CLK_RGBA:                  \
            data = *source;             \
            break;                      \
        case CLK_BGRA:                  \
            data.zyxw = (*source).xyzw; \
            break;                      \
        case CLK_ARGB:                  \
            data.wxyz = (*source).xyzw; \
            break;                      \
        case CLK_INTENSITY:             \
            data.xyzw = (*source).x;    \
            break;                      \
        case CLK_LUMINANCE:             \
            data.xyz = (*source).x;     \
            data.w = m;                 \
            break;                      \
    }

    switch (type)
    {
        case CLK_UNORM_INT8:
        {
            uchar4 *source = v_source;
            uchar4 data;

            UNSWIZZLE(order, source, data, 0xff)

            result.x = (float)data.x / 255.0f;
            result.y = (float)data.y / 255.0f;
            result.z = (float)data.z / 255.0f;
            result.w = (float)data.w / 255.0f;
            break;
        }
        case CLK_UNORM_INT16:
        {
            ushort4 *source = v_source;
            ushort4 data;

            UNSWIZZLE(order, source, data, 0xffff)

            result.x = (float)data.x / 65535.0f;
            result.y = (float)data.y / 65535.0f;
            result.z = (float)data.z / 65535.0f;
            result.w = (float)data.w / 65535.0f;
            break;
        }
        case CLK_SNORM_INT8:
        {
            char4 *source = v_source;
            char4 data;

            UNSWIZZLE(order, source, data, 0x7f)

            result.x = (float)data.x / 127.0f;
            result.y = (float)data.y / 127.0f;
            result.z = (float)data.z / 127.0f;
            result.w = (float)data.w / 127.0f;
            break;
        }
        case CLK_SNORM_INT16:
        {
            short4 *source = v_source;
            short4 data;

            UNSWIZZLE(order, source, data, 0x7fff)

            result.x = (float)data.x / 32767.0f;
            result.y = (float)data.y / 32767.0f;
            result.z = (float)data.z / 32767.0f;
            result.w = (float)data.w / 32767.0f;
            break;
        }
        case CLK_FLOAT:
        {
            float4 *source = v_source;
            UNSWIZZLE(order, source, result, 1.0f)
            break;
        }
    }

#undef UNSWIZZLE

    return result;
}

float4 OVERLOAD read_imagef(image2d_t image, sampler_t sampler, float2 coord)
{
    float4 c;

    c.xy = coord;
    c.zw = 0;

    return read_imagef((image3d_t)image, sampler, c);
}

int4 f2i_floor(float4 value)
{
    int4 result = __builtin_ia32_cvtps2dq(value);
    value = __builtin_ia32_psrldi128((int4)value, 31);
    result -= (int4)value;
    return result;
}

float4 f2f_floor(float4 value)
{
    return __builtin_ia32_cvtdq2ps(f2i_floor(value));
}

#define LINEAR_3D(t_max, suf)                           \
    (t_max - a) * (t_max - b) - (t_max - c) *           \
    read_image##suf(image, sampler,                     \
        __builtin_shufflevector(V0, V1, 0, 1, 2, 3)) +  \
    a * (t_max - b) * (t_max - c) *                     \
    read_image##suf(image, sampler,                     \
        __builtin_shufflevector(V0, V1, 4, 1, 2, 3)) +  \
    (t_max - a) * b * (t_max - c) *                     \
    read_image##suf(image, sampler,                     \
        __builtin_shufflevector(V0, V1, 0, 5, 2, 3)) +  \
    a * b * (t_max - c) *                               \
    read_image##suf(image, sampler,                     \
        __builtin_shufflevector(V0, V1, 4, 5, 2, 3)) +  \
    (t_max - a) * (t_max - b) * c *                     \
    read_image##suf(image, sampler,                     \
        __builtin_shufflevector(V0, V1, 0, 1, 6, 3)) +  \
    a * (t_max - b) * c *                               \
    read_image##suf(image, sampler,                     \
        __builtin_shufflevector(V0, V1, 4, 1, 6, 3)) +  \
    (t_max - a) * b * c *                               \
    read_image##suf(image, sampler,                     \
        __builtin_shufflevector(V0, V1, 0, 5, 6, 3)) +  \
    a * b * c *                                         \
    read_image##suf(image, sampler,                     \
        __builtin_shufflevector(V0, V1, 4, 5, 6, 3))

#define LINEAR_2D(t_max, suf)                           \
    (t_max - a) * (t_max - b) *                         \
    read_image##suf(image, sampler,                     \
        __builtin_shufflevector(V0, V1, 0, 1, 2, 2)) +  \
    a * (t_max - b) *                                   \
    read_image##suf(image, sampler,                     \
        __builtin_shufflevector(V0, V1, 4, 1, 2, 2)) +  \
    (t_max - a) * b *                                   \
    read_image##suf(image, sampler,                     \
        __builtin_shufflevector(V0, V1, 0, 5, 2, 2)) +  \
    a * b *                                             \
    read_image##suf(image, sampler,                     \
        __builtin_shufflevector(V0, V1, 4, 5, 2, 2));

#define READ_IMAGE(type, suf, type_max)                                        \
    type##4 result;                                                            \
                                                                               \
    switch (sampler & 0xf0)                                                    \
    {                                                                          \
        case CLK_ADDRESS_NONE:                                                 \
        case CLK_ADDRESS_CLAMP:                                                \
        case CLK_ADDRESS_CLAMP_TO_EDGE:                                        \
            /* Denormalize coords */                                           \
            if ((sampler & 0xf) == CLK_NORMALIZED_COORDS_TRUE)                 \
                coord *= __builtin_ia32_cvtdq2ps(get_image_dim(image));        \
                                                                               \
            switch (sampler & 0xf00)                                           \
            {                                                                  \
                case CLK_FILTER_NEAREST:                                       \
                {                                                              \
                    int4 c = f2i_floor(coord);                                 \
                                                                               \
                    return read_image##suf(image, sampler, c);                 \
                }                                                              \
                case CLK_FILTER_LINEAR:                                        \
                {                                                              \
                    type a, b, c;                                              \
                                                                               \
                    coord -= 0.5f;                                             \
                                                                               \
                    int4 V0, V1;                                               \
                                                                               \
                    V0 = f2i_floor(coord);                                     \
                    V1 = f2i_floor(coord) + 1;                                 \
                                                                               \
                    coord -= f2f_floor(coord);                                 \
                                                                               \
                    a = (type)(coord.x * type_max);                            \
                    b = (type)(coord.y * type_max);                            \
                    c = (type)(coord.z * type_max);                            \
                                                                               \
                    if (__cpu_is_image_3d(image))                              \
                    {                                                          \
                        result = LINEAR_3D(type_max, suf);                     \
                    }                                                          \
                    else                                                       \
                    {                                                          \
                        result = LINEAR_2D(type_max, suf);                     \
                    }                                                          \
                }                                                              \
            }                                                                  \
            break;                                                             \
        case CLK_ADDRESS_REPEAT:                                               \
            switch (sampler & 0xf00)                                           \
            {                                                                  \
                case CLK_FILTER_NEAREST:                                       \
                {                                                              \
                    int4 dim = get_image_dim(image);                           \
                    coord = (coord - f2f_floor(coord)) *                       \
                                __builtin_ia32_cvtdq2ps(dim);                  \
                                                                               \
                    int4 c = f2i_floor(coord);                                 \
                                                                               \
                    /* if (c > dim - 1) c = c - dim */                         \
                    int4 mask = __builtin_ia32_pcmpgtd128(c, dim - 1);         \
                    int4 repl = c - dim;                                       \
                    c = (repl & mask) | (c & ~mask);                           \
                                                                               \
                    return read_image##suf(image, sampler, c);                 \
                }                                                              \
                case CLK_FILTER_LINEAR:                                        \
                {                                                              \
                    type a, b, c;                                              \
                                                                               \
                    int4 dim = get_image_dim(image);                           \
                    coord = (coord - f2f_floor(coord)) *                       \
                                __builtin_ia32_cvtdq2ps(dim);                  \
                                                                               \
                    float4 tmp = coord;                                        \
                    tmp -= 0.5f;                                               \
                    tmp -= f2f_floor(tmp);                                     \
                                                                               \
                    a = (type)(tmp.x * type_max);                              \
                    b = (type)(tmp.y * type_max);                              \
                    c = (type)(tmp.z * type_max);                              \
                                                                               \
                    int4 V0, V1;                                               \
                                                                               \
                    V0 = f2i_floor(coord - 0.5f);                              \
                    V1 = V0 + 1;                                               \
                                                                               \
                    /* if (0 > V0) V0 = dim + V0 */                            \
                    int4 zero = 0;                                             \
                    int4 mask = __builtin_ia32_pcmpgtd128(zero, V0);           \
                    int4 repl = dim + V0;                                      \
                    V0 = (repl & mask) | (V0 & ~mask);                         \
                                                                               \
                    /* if (V1 > dim - 1) V1 = V1 - dim */                      \
                    mask = __builtin_ia32_pcmpgtd128(V1, dim);                 \
                    repl = V1 - dim;                                           \
                    V1 = (repl & mask) | (V0 & ~mask);                         \
                                                                               \
                    if (__cpu_is_image_3d(image))                              \
                    {                                                          \
                        result = LINEAR_3D(type_max, suf);                     \
                    }                                                          \
                    else                                                       \
                    {                                                          \
                        result = LINEAR_2D(type_max, suf);                     \
                    }                                                          \
                }                                                              \
            }                                                                  \
            break;                                                             \
        case CLK_ADDRESS_MIRRORED_REPEAT:                                      \
            switch (sampler & 0xf00)                                           \
            {                                                                  \
                case CLK_FILTER_NEAREST:                                       \
                {                                                              \
                    int4 dim = get_image_dim(image);                           \
                    float4 two = 2.0f;                                         \
                    float4 prim = two * __builtin_ia32_cvtdq2ps(               \
                        __builtin_ia32_cvtps2dq(0.5f * coord));                \
                    prim -= coord;                                             \
                                                                               \
                    /* abs(x) = x & ~{-0, -0, -0, -0} */                       \
                    float4 nzeroes = -0.0f;                                    \
                    prim = (float4)((int4)prim & ~(int4)nzeroes);              \
                                                                               \
                    coord = prim * __builtin_ia32_cvtdq2ps(dim);               \
                    int4 c = f2i_floor(coord);                                 \
                                                                               \
                    /* if (c > dim - 1) c = dim - 1 */                         \
                    int4 repl = dim - 1;                                       \
                    int4 mask = __builtin_ia32_pcmpgtd128(c, repl);            \
                    c = (repl & mask) | (c & ~mask);                           \
                                                                               \
                    return read_image##suf(image, sampler, c);                 \
                }                                                              \
                case CLK_FILTER_LINEAR:                                        \
                {                                                              \
                    type a, b, c;                                              \
                                                                               \
                    int4 dim = get_image_dim(image);                           \
                    float4 two = 2.0f;                                         \
                    float4 prim = two * __builtin_ia32_cvtdq2ps(               \
                        __builtin_ia32_cvtps2dq(0.5f * coord));                \
                    prim -= coord;                                             \
                                                                               \
                    /* abs(x) = x & ~{-0, -0, -0, -0} */                       \
                    float4 nzeroes = -0.0f;                                    \
                    prim = (float4)((int4)prim & ~(int4)nzeroes);              \
                                                                               \
                    coord = prim * __builtin_ia32_cvtdq2ps(dim);               \
                                                                               \
                    float4 tmp = coord;                                        \
                    tmp -= 0.5f;                                               \
                    tmp -= f2f_floor(tmp);                                     \
                                                                               \
                    a = (type)(tmp.x * type_max);                              \
                    b = (type)(tmp.y * type_max);                              \
                    c = (type)(tmp.z * type_max);                              \
                                                                               \
                    int4 V0, V1, zero = 0;                                     \
                                                                               \
                    V0 = f2i_floor(coord - 0.5f);                              \
                    V1 = V0 + 1;                                               \
                                                                               \
                    /* if (0 > V0) V0 = 0 */                                   \
                    int4 mask = __builtin_ia32_pcmpgtd128(V0, zero);           \
                    V0 &= ~mask;                                               \
                                                                               \
                    /* if (V1 > dim - 1) V1 = dim - 1 */                       \
                    int4 repl = dim - 1;                                       \
                    mask = __builtin_ia32_pcmpgtd128(V1, repl);                \
                    V1 = (repl & mask) | (V1 & ~mask);                         \
                                                                               \
                    if (__cpu_is_image_3d(image))                              \
                    {                                                          \
                        result = LINEAR_3D(type_max, suf);                     \
                    }                                                          \
                    else                                                       \
                    {                                                          \
                        result = LINEAR_2D(type_max, suf);                     \
                    }                                                          \
                }                                                              \
            }                                                                  \
            break;                                                             \
    }                                                                          \
                                                                               \
    return result;

float4 OVERLOAD read_imagef(image3d_t image, sampler_t sampler, float4 coord)
{
    READ_IMAGE(float, f, 1.0f)
}

#define UNSWIZZLE_8(source, data, m)    \
        case CLK_ARGB:                  \
            data.wxyz = (*source).xyzw; \
            break;                      \
        case CLK_BGRA:                  \
            data.zyxw = (*source).xyzw; \
            break;

#define UNSWIZZLE_16(source, data, m)   \
        case CLK_INTENSITY:             \
            data.xyzw = (*source).x;    \
            break;                      \
        case CLK_LUMINANCE:             \
            data.xyz = (*source).x;     \
            data.w = m;                 \
            break;

#define UNSWIZZLE_32(source, data, m)   \
        case CLK_R:                     \
        case CLK_Rx:                    \
            data.x = (*source).x;       \
            data.yz = 0;                \
            data.w = m;                 \
            break;                      \
        case CLK_A:                     \
            data.w = (*source).x;       \
            data.xyz = 0;               \
            break;                      \
        case CLK_RG:                    \
        case CLK_RGx:                   \
            data.xy = (*source).xy;     \
            data.z = 0;                 \
            data.w = m;                 \
            break;                      \
        case CLK_RA:                    \
            data.xw = (*source).xy;     \
            data.yz = 0;                \
            break;                      \
        case CLK_RGBA:                  \
            data = *source;             \
            break;

int4 OVERLOAD read_imagei(image2d_t image, sampler_t sampler, int2 coord)
{
    int4 c;
    c.xy = coord;
    c.zw = 0;

    return read_imagei((image3d_t)image, sampler, c);
}

int4 OVERLOAD read_imagei(image3d_t image, sampler_t sampler, int4 coord)
{
    int4 result;

    // Handle address mode
    coord = handle_address_mode(image, coord, sampler);

    if (coord.w != 0)
    {
        // Border color
        switch (get_image_channel_order(image))
        {
            case CLK_R:
            case CLK_RG:
            case CLK_RGB:
            case CLK_LUMINANCE:
                result.xyz = 0;
                result.w = 0x7fffffff;
                return result;
            default:
                result.xyzw = 0;
                return result;
        }
    }

    int order, type;
    void *v_source = __cpu_image_data(image, coord.x, coord.y, coord.z, &order, &type);

    switch (type)
    {
        case CLK_SIGNED_INT8:
        {
            char4 *source = v_source;
            char4 data;

            switch (order)
            {
                UNSWIZZLE_8(source, data, 0x7f)
                UNSWIZZLE_16(source, data, 0x7f)
                UNSWIZZLE_32(source, data, 0x7f)
            }

            result.x = data.x;
            result.y = data.y;
            result.z = data.z;
            result.w = data.w;
            break;
        }
        case CLK_SIGNED_INT16:
        {
            short4 *source = v_source;
            short4 data;

            switch (order)
            {
                UNSWIZZLE_8(source, data, 0x7fff)
                UNSWIZZLE_16(source, data, 0x7fff)
                UNSWIZZLE_32(source, data, 0x7fff)
            }

            result.x = data.x;
            result.y = data.y;
            result.z = data.z;
            result.w = data.w;
            break;
        }
        case CLK_SIGNED_INT32:
        {
            int4 *source = v_source;

            switch (order)
            {
                UNSWIZZLE_8(source, result, 0x7fffffff)
                UNSWIZZLE_16(source, result, 0x7fffffff)
                UNSWIZZLE_32(source, result, 0x7fffffff)
            }
            break;
        }
    }

    return result;
}

int4 OVERLOAD read_imagei(image2d_t image, sampler_t sampler, float2 coord)
{
    float4 c;

    c.xy = coord;
    c.zw = 0;

    return read_imagei((image3d_t)image, sampler, c);
}

int4 OVERLOAD read_imagei(image3d_t image, sampler_t sampler, float4 coord)
{
    READ_IMAGE(int, i, 0x7fffffff)
}

uint4 OVERLOAD read_imageui(image2d_t image, sampler_t sampler, int2 coord)
{
    int4 c;
    c.xy = coord;
    c.zw = 0;

    return read_imageui((image3d_t)image, sampler, c);
}

uint4 OVERLOAD read_imageui(image3d_t image, sampler_t sampler, int4 coord)
{
    uint4 result;

    // Handle address mode
    coord = handle_address_mode(image, coord, sampler);

    if (coord.w != 0)
    {
        // Border color
        switch (get_image_channel_order(image))
        {
            case CLK_R:
            case CLK_RG:
            case CLK_RGB:
            case CLK_LUMINANCE:
                result.xyz = 0;
                result.w = 0xffffffff;
                return result;
            default:
                result.xyzw = 0;
                return result;
        }
    }

    int order, type;
    void *v_source = __cpu_image_data(image, coord.x, coord.y, coord.z, &order, &type);

    switch (type)
    {
        case CLK_UNSIGNED_INT8:
        {
            uchar4 *source = v_source;
            uchar4 data;

            switch (order)
            {
                UNSWIZZLE_8(source, data, 0xff)
                UNSWIZZLE_16(source, data, 0xff)
                UNSWIZZLE_32(source, data, 0xff)
            }

            result.x = data.x;
            result.y = data.y;
            result.z = data.z;
            result.w = data.w;
            break;
        }
        case CLK_UNSIGNED_INT16:
        {
            ushort4 *source = v_source;
            ushort4 data;

            switch (order)
            {
                UNSWIZZLE_8(source, data, 0xffff)
                UNSWIZZLE_16(source, data, 0xffff)
                UNSWIZZLE_32(source, data, 0xffff)
            }

            result.x = data.x;
            result.y = data.y;
            result.z = data.z;
            result.w = data.w;
            break;
        }
        case CLK_UNSIGNED_INT32:
        {
            uint4 *source = v_source;

            switch (order)
            {
                UNSWIZZLE_8(source, result, 0xffffffff)
                UNSWIZZLE_16(source, result, 0xffffffff)
                UNSWIZZLE_32(source, result, 0xffffffff)
            }
            break;
        }
    }

    return result;
}

uint4 OVERLOAD read_imageui(image2d_t image, sampler_t sampler, float2 coord)
{
    float4 c;

    c.xy = coord;
    c.zw = 0;

    return read_imageui((image3d_t)image, sampler, c);
}

uint4 OVERLOAD read_imageui(image3d_t image, sampler_t sampler, float4 coord)
{
    READ_IMAGE(uint, ui, 0xffffffff)
}

#undef UNSWIZZLE_8
#undef UNSWIZZLE_16
#undef UNSWIZZLE_32

void OVERLOAD write_imagef(image2d_t image, int2 coord, float4 color)
{
    int4 c;
    c.xy = coord;
    c.zw = 0;

    write_imagef((image3d_t)image, c, color);
}

void OVERLOAD write_imagef(image3d_t image, int4 coord, float4 color)
{
    int order, type;
    void *v_target = __cpu_image_data(image, coord.x, coord.y, coord.z, &order, &type);

#define SWIZZLE(order, target, data)    \
    switch (order)                      \
    {                                   \
        case CLK_R:                     \
        case CLK_Rx:                    \
            (*target).x = data.x;       \
            break;                      \
        case CLK_A:                     \
            (*target).x = data.w;       \
            break;                      \
        case CLK_RG:                    \
        case CLK_RGx:                   \
            (*target).xy = data.xy;     \
            break;                      \
        case CLK_RA:                    \
            (*target).xy = data.xw;     \
            break;                      \
        case CLK_RGBA:                  \
            *target = data;             \
            break;                      \
        case CLK_BGRA:                  \
            (*target).zyxw = data.xyzw; \
            break;                      \
        case CLK_ARGB:                  \
            (*target).wxyz = data.xyzw; \
            break;                      \
        case CLK_INTENSITY:             \
        case CLK_LUMINANCE:             \
            (*target).x = data.x;       \
            break;                      \
    }

    switch (type)
    {
        case CLK_SNORM_INT8:
        {
            char4 *target = v_target;
            char4 data;

            // Denormalize
            data.x = (color.x * 127.0f);
            data.y = (color.y * 127.0f);
            data.z = (color.z * 127.0f);
            data.w = (color.w * 127.0f);

            SWIZZLE(order, target, data)
            break;
        }
        case CLK_UNORM_INT8:
        {
            uchar4 *target = v_target;
            uchar4 data;

            // Denormalize
            data.x = (color.x * 255.0f);
            data.y = (color.y * 255.0f);
            data.z = (color.z * 255.0f);
            data.w = (color.w * 255.0f);

            SWIZZLE(order, target, data)
            break;
        }
        case CLK_SNORM_INT16:
        {
            short4 *target = v_target;
            short4 data;

            // Denormalize
            data.x = (color.x * 32767.0f);
            data.y = (color.y * 32767.0f);
            data.z = (color.z * 32767.0f);
            data.w = (color.w * 32767.0f);

            SWIZZLE(order, target, data)
            break;
        }
        case CLK_UNORM_INT16:
        {
            ushort4 *target = v_target;
            ushort4 data;

            data.x = (color.x * 65535.0f);
            data.y = (color.y * 65535.0f);
            data.z = (color.z * 65535.0f);
            data.w = (color.w * 65535.0f);

            SWIZZLE(order, target, data)
            break;
        }
        case CLK_FLOAT:
        {
            float4 *target = v_target;

            SWIZZLE(order, target, color)
            break;
        }
    }

#undef SWIZZLE
}

#define SWIZZLE_8(target, data)         \
        case CLK_ARGB:                  \
            (*target).wxyz = data.xyzw; \
            break;                      \
        case CLK_BGRA:                  \
            (*target).zyxw = data.xyzw; \
            break;

#define SWIZZLE_16(target, data)        \
        case CLK_LUMINANCE:             \
        case CLK_INTENSITY:             \
            (*target).x = data.x;       \
            break;

#define SWIZZLE_32(target, data)        \
        case CLK_R:                     \
        case CLK_Rx:                    \
            (*target).x = data.x;       \
            break;                      \
        case CLK_A:                     \
            (*target).x = data.w;       \
            break;                      \
        case CLK_RG:                    \
        case CLK_RGx:                   \
            (*target).xy = data.xy;     \
            break;                      \
        case CLK_RA:                    \
            (*target).xy = data.xw;     \
            break;                      \
        case CLK_RGBA:                  \
            *target = data;             \
            break;

void OVERLOAD write_imagei(image2d_t image, int2 coord, int4 color)
{
    int4 c;
    c.xy = coord;
    c.zw = 0;

    write_imagei((image3d_t)image, c, color);
}

void OVERLOAD write_imagei(image3d_t image, int4 coord, int4 color)
{
    int order, type;
    void *v_target = __cpu_image_data(image, coord.x, coord.y, coord.z, &order, &type);

    switch (type)
    {
        case CLK_SIGNED_INT8:
        {
            char4 *target = v_target;
            char4 data;

            data.x = color.x;
            data.y = color.y;
            data.z = color.z;
            data.w = color.w;

            switch (order)
            {
                SWIZZLE_8(target, data)
                SWIZZLE_16(target, data)
                SWIZZLE_32(target, data)
            }

            break;
        }
        case CLK_SIGNED_INT16:
        {
            short4 *target = v_target;
            short4 data;

            data.x = color.x;
            data.y = color.y;
            data.z = color.z;
            data.w = color.w;

            switch (order)
            {
                SWIZZLE_16(target, data)
                SWIZZLE_32(target, data)
            }

            break;
        }
        case CLK_SIGNED_INT32:
        {
            int4 *target = v_target;

            switch (order)
            {
                SWIZZLE_32(target, color)
            }

            break;
        }
    }
}

void OVERLOAD write_imageui(image2d_t image, int2 coord, uint4 color)
{
    int4 c;
    c.xy = coord;
    c.zw = 0;

    write_imageui((image3d_t)image, c, color);
}

void OVERLOAD write_imageui(image3d_t image, int4 coord, uint4 color)
{
    int order, type;
    void *v_target = __cpu_image_data(image, coord.x, coord.y, coord.z, &order, &type);

    switch (type)
    {
        case CLK_UNSIGNED_INT8:
        {
            uchar4 *target = v_target;
            uchar4 data;

            data.x = color.x;
            data.y = color.y;
            data.z = color.z;
            data.w = color.w;

            switch (order)
            {
                SWIZZLE_8(target, data)
                SWIZZLE_16(target, data)
                SWIZZLE_32(target, data)
            }
        }
        case CLK_UNSIGNED_INT16:
        {
            ushort4 *target = v_target;
            ushort4 data;

            data.x = color.x;
            data.y = color.y;
            data.z = color.z;
            data.w = color.w;

            switch (order)
            {
                SWIZZLE_16(target, data)
                SWIZZLE_32(target, data)
            }
        }
        case CLK_UNSIGNED_INT32:
        {
            uint4 *target = v_target;

            switch (order)
            {
                SWIZZLE_32(target, color)
            }
        }
    }
}

#undef SWIZZLE_8
#undef SWIZZLE_16
#undef SWIZZLE_32

int2 OVERLOAD get_image_dim(image2d_t image)
{
    int2 result;

    result.x = get_image_width(image);
    result.y = get_image_height(image);

    return result;
}

int4 OVERLOAD get_image_dim(image3d_t image)
{
    int4 result;

    result.x = get_image_width(image);
    result.y = get_image_height(image);
    result.z = get_image_depth(image);
    result.w = 0;

    return result;
}

int OVERLOAD get_image_width(image2d_t image)
{
    return __cpu_get_image_width(image);
}

int OVERLOAD get_image_width(image3d_t image)
{
    return __cpu_get_image_width(image);
}

int OVERLOAD get_image_height(image2d_t image)
{
    return __cpu_get_image_height(image);
}

int OVERLOAD get_image_height(image3d_t image)
{
    return __cpu_get_image_height(image);
}

int OVERLOAD get_image_depth(image3d_t image)
{
    return __cpu_get_image_depth(image);
}

int OVERLOAD get_image_channel_data_type(image2d_t image)
{
    return __cpu_get_image_channel_data_type(image);
}

int OVERLOAD get_image_channel_data_type(image3d_t image)
{
    return __cpu_get_image_channel_data_type(image);
}

int OVERLOAD get_image_channel_order(image2d_t image)
{
    return __cpu_get_image_channel_order(image);
}

int OVERLOAD get_image_channel_order(image3d_t image)
{
    return __cpu_get_image_channel_order(image);
}