File: read_image.cl

package info (click to toggle)
pocl 1.6-5
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 17,816 kB
  • sloc: lisp: 135,476; ansic: 64,403; cpp: 32,196; vhdl: 1,040; sh: 382; python: 336; makefile: 151; pascal: 140; java: 72; xml: 49
file content (1837 lines) | stat: -rw-r--r-- 68,349 bytes parent folder | download
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
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
/* OpenCL built-in library: read_image()

   Copyright (c) 2013 Ville Korhonen
   Copyright (c) 2014 Felix Bytow
   Copyright (c) 2015 Matias Koskela
   Copyright (c) 2017 Michal Babej / Tampere University of Technology

   Permission is hereby granted, free of charge, to any person obtaining a copy
   of this software and associated documentation files (the "Software"), to deal
   in the Software without restriction, including without limitation the rights
   to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
   copies of the Software, and to permit persons to whom the Software is
   furnished to do so, subject to the following conditions:

   The above copyright notice and this permission notice shall be included in
   all copies or substantial portions of the Software.

   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
   IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
   AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
   LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
   OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
   THE SOFTWARE.
*/

/* NOTE: this file is NOT a generic implementation; it works with vectors
   in a lot of places and requires that either device supports unaligned
   vector operations, or that memory backing the images is properly aligned.
   The maximum required alignment is 16bytes (4channels * 32bit color)

   Not all CPUs support unaligned vector operations, but the pthread / basic
   drivers allocate properly aligned memory for backing buffers; therefore
   this should work for everything supported by pthread / basic.
*/

#include "templates.h"
#include "pocl_image_rw_utils.h"

#define CLK_ADDRESS_MASK                                                      \
  (CLK_ADDRESS_CLAMP_TO_EDGE | CLK_ADDRESS_CLAMP | CLK_ADDRESS_REPEAT         \
   | CLK_ADDRESS_MIRRORED_REPEAT)

_CL_READNONE static uint4
map_channels (uint4 color, int order)
{
  switch (order)
    {
    case CLK_ARGB:
      return color.yzwx;
    case CLK_BGRA:
      return color.zyxw;
    case CLK_RGBA:
    default:
      return color;
    }
}

/*************************************************************************/

/* only for CLK_FLOAT, CLK_SNORM_INT8, CLK_UNORM_INT8,
 * CLK_SNORM_INT16, CLK_UNORM_INT16 channel types */
_CL_READONLY static float4
get_float4_pixel (void *data, size_t base_index, int type)
{
  if (type == CLK_FLOAT)
    return ((float4 *)data)[base_index];
  if (type == CLK_HALF_FLOAT)
    {
      return vloada_half4(base_index, data);
    }
  const float4 one_127th = (float4) (1.0f / 127.0f);
  const float4 one_32767th = (float4) (1.0f / 32767.0f);
  const float4 one_255th = ((float4) (1.0f / (float)UCHAR_MAX));
  const float4 one_65535th = ((float4) (1.0f / (float)USHRT_MAX));
  if (type == CLK_SNORM_INT8)
    {
      /*  <I*_MIN, I*_MAX> to <-1.0, 1.0> */
      int4 color = convert_int4 (((char4 *)data)[base_index]);
      float4 colorf = convert_float4 (color);
      return max ((float4) (-1.0f), (one_127th * colorf));
    }
  if (type == CLK_SNORM_INT16)
    {
      int4 color = convert_int4 (((short4 *)data)[base_index]);
      float4 colorf = convert_float4 (color);
      return max ((float4) (-1.0f), (one_32767th * colorf));
    }
  if (type == CLK_UNORM_INT8)
    {
      /* <0, I*_MAX> to <0.0, 1.0> */
      return convert_float4 (((uchar4 *)data)[base_index]) * one_255th;
    }
  if (type == CLK_UNORM_INT16)
    {
      /* <0, I*_MAX> to <0.0, 1.0> */
      return convert_float4 (((ushort4 *)data)[base_index]) * one_65535th;
    }
  return (float4) (123.0f);
}

/* only for CLK_FLOAT, CLK_SNORM_INT8, CLK_UNORM_INT8,
 * CLK_SNORM_INT16, CLK_UNORM_INT16 channel types */
_CL_READONLY static float
get_float_pixel (void *data, size_t base_index, int type)
{
  if (type == CLK_FLOAT)
    return ((float *)data)[base_index];
  const float one_127th = (float)(1.0f / 127.0f);
  const float one_32767th = (float)(1.0f / 32767.0f);
  const float one_255th = ((float)(1.0f / (float)UCHAR_MAX));
  const float one_65535th = ((float)(1.0f / (float)USHRT_MAX));
  if (type == CLK_SNORM_INT8)
    {
      /*  <I*_MIN, I*_MAX> to <-1.0, 1.0> */
      char color = ((char *)data)[base_index];
      float colorf = convert_float (color);
      return max ((-1.0f), (one_127th * colorf));
    }
  if (type == CLK_SNORM_INT16)
    {
      short color = ((short *)data)[base_index];
      float colorf = convert_float (color);
      return max ((-1.0f), (one_32767th * colorf));
    }
  if (type == CLK_UNORM_INT8)
    {
      /* <0, I*_MAX> to <0.0, 1.0> */
      return convert_float (((uchar *)data)[base_index]) * one_255th;
    }
  if (type == CLK_UNORM_INT16)
    {
      return convert_float (((ushort *)data)[base_index]) * one_65535th;
    }

  return 234.0f;
}

/*************************************************************************/

#define BORDER_COLOR (0)
#define BORDER_COLOR_F (0.0f)

/* for use inside filter functions
 * no channel mapping
 * no pointers to img metadata */
_CL_READONLY static uint4
pocl_read_pixel_fast_ui (size_t base_index, int order, int elem_size,
                         void *data)
{
  uint4 color;

  if (order == CLK_A)
    {
      color = (uint4)0;
      if (elem_size == 1)
        color.w = ((uchar *)data)[base_index];
      else if (elem_size == 2)
        color.w = ((ushort *)data)[base_index];
      else if (elem_size == 4)
        color.w = ((uint *)data)[base_index];
      return color;
    }

  if (elem_size == 1)
    {
      return convert_uint4 (((uchar4 *)data)[base_index]);
    }
  else if (elem_size == 2)
    {
      return convert_uint4 (((ushort4 *)data)[base_index]);
    }
  else if (elem_size == 4)
    {
      return ((uint4 *)data)[base_index];
    }

  return (uint4)0;
}

/* for use inside filter functions
 * no channel mapping
 * no pointers to img metadata */
_CL_READONLY static float4
pocl_read_pixel_fast_f (size_t base_index, int channel_type, int order,
                        void *data)
{

  if (order == CLK_A)
    {
      float p = get_float_pixel (data, base_index, channel_type);
      return (float4) (0.0f, 0.0f, 0.0f, p);
    }
  else
    {
      return get_float4_pixel (data, base_index, channel_type);
    }
}

/* for use inside filter functions
 * no channel mapping
 * no pointers to img metadata */
_CL_READONLY static int4
pocl_read_pixel_fast_i (size_t base_index, int order, int elem_size,
                        void *data)
{
  int4 color;

  if (order == CLK_A)
    {
      color = (int4)0;
      if (elem_size == 1)
        color.w = ((char *)data)[base_index];
      else if (elem_size == 2)
        color.w = ((short *)data)[base_index];
      else if (elem_size == 4)
        color.w = ((int *)data)[base_index];
      return color;
    }

  if (elem_size == 1)
    {
      return convert_int4 (((char4 *)data)[base_index]);
    }
  else if (elem_size == 2)
    {
      return convert_int4 (((short4 *)data)[base_index]);
    }
  else if (elem_size == 4)
    {
      return ((int4 *)data)[base_index];
    }
  return (int4)0;
}

/*************************************************************************/

_CL_READONLY static int4
get_image_array_offset (global dev_image_t *img, int4 uvw_after_rint,
                        int4 array_coord)
{
  int4 res = uvw_after_rint;
  if (img->_image_array_size > 0)
    {
      if (img->_height > 0)
        {
          res.z = clamp (array_coord.z, 0, (img->_image_array_size - 1));
          res.w = 0;
        }
      else
        {
          res.y = clamp (array_coord.y, 0, (img->_image_array_size - 1));
          res.z = 0;
          res.w = 0;
        }
    }
  return res;
}

/* array_coord must be unnormalized & repeats removed */
_CL_READONLY static int4
get_image_array_offset2 (global dev_image_t *img, int4 uvw_after_rint,
                         float4 array_coord)
{
  int4 res = uvw_after_rint;
  if (img->_image_array_size > 0)
    {
      if (img->_height > 0)
        {
          res.z = clamp (convert_int (floor (array_coord.z + 0.5f)), 0,
                         (img->_image_array_size - 1));
          res.w = 0;
        }
      else
        {
          res.y = clamp (convert_int (floor (array_coord.y + 0.5f)), 0,
                         (img->_image_array_size - 1));
          res.z = 0;
          res.w = 0;
        }
    }
  return res;
}

/* RET: (int4) (img.x{,y,z}, array_size, 0 {,0 ...} ) */
_CL_READONLY static int4
pocl_get_image_array_size (global dev_image_t *img)
{
  int4 imgsize = (int4) (img->_width, img->_height, img->_depth, 0);
  if (img->_image_array_size > 0)
    {
      if (img->_height > 0)
        imgsize.z = img->_image_array_size;
      else
        imgsize.y = img->_image_array_size;
    }
  return imgsize;
}
/*************************************************************************/

/* full read with channel map conversion etc  */
/* Reads a four element pixel from image pointed by integer coords.
 * Returns Border color (0) for out-of-range reads. This is OK since
 * reads behind border should either return border color, or are undefined */
_CL_READONLY static uint4
pocl_read_pixel (global dev_image_t *img, int4 coord)
{
  uint4 color;
  int width = img->_width;
  int height = img->_height;
  int depth = img->_depth;
  int num_channels = img->_num_channels;
  int order = img->_order;
  int elem_size = img->_elem_size;
  int channel_type = img->_data_type;
  void *data = img->_data;
  size_t elem_bytes = num_channels * elem_size;
  size_t row_pitch = img->_row_pitch / elem_bytes;
  size_t slice_pitch = img->_slice_pitch / elem_bytes;

  if ((coord.x >= width || coord.x < 0)
      || ((height != 0) && (coord.y >= height || coord.y < 0))
      || ((depth != 0) && (coord.z >= depth || coord.z < 0)))
    {
      /* if out of bounds, return BORDER COLOR:
       * since pocl's basic/pthread device only
       * supports CLK_A + CLK_{RGBA combos},
       * the border color is always zeroes. */
      if ((channel_type == CLK_SIGNED_INT8) || (channel_type == CLK_SIGNED_INT16)
          || (channel_type == CLK_SIGNED_INT32)
          || (channel_type == CLK_UNSIGNED_INT8)
          || (channel_type == CLK_UNSIGNED_INT16)
          || (channel_type == CLK_UNSIGNED_INT32))
        return (uint4)BORDER_COLOR;
      else
        return as_uint4 ((float4)BORDER_COLOR_F);
    }

  size_t base_index
      = coord.x + (coord.y * row_pitch) + (coord.z * slice_pitch);

  if ((channel_type == CLK_SIGNED_INT8) || (channel_type == CLK_SIGNED_INT16)
      || (channel_type == CLK_SIGNED_INT32))
    color = as_uint4 (
        pocl_read_pixel_fast_i (base_index, order, elem_size, data));
  else if ((channel_type == CLK_UNSIGNED_INT8)
           || (channel_type == CLK_UNSIGNED_INT16)
           || (channel_type == CLK_UNSIGNED_INT32))
    color = pocl_read_pixel_fast_ui (base_index, order, elem_size, data);
  else // TODO unsupported channel types
    color = as_uint4 (
        pocl_read_pixel_fast_f (base_index, channel_type, order, data));

  return map_channels (color, order);
}

/* Transforms coords based on image addressing mode */
_CL_READONLY static int4
pocl_address_mode (global dev_image_t *img, int4 input_coord,
                   const dev_sampler_t samp)
{
  if ((samp & CLK_ADDRESS_MASK) == CLK_ADDRESS_CLAMP_TO_EDGE)
    {
      int4 max_clamp = max (
          (int4) (img->_width - 1, img->_height - 1, img->_depth - 1, 0),
          (int4)0);
      return clamp (input_coord, (int4) (0), max_clamp);
    }

  if ((samp & CLK_ADDRESS_MASK) == CLK_ADDRESS_CLAMP)
    {
      int4 max_clamp
          = max ((int4) (img->_width, img->_height, img->_depth, 0), (int4)0);
      return clamp (input_coord, (int4) (-1), max_clamp);
    }

  return input_coord;
}

/*************************************************************************/

_CL_READONLY static float4
read_pixel_linear_3d_float (float4 abc, float4 one_m, int4 ijk0, int4 ijk1,
                            int width, int height, int depth, int channel_type,
                            size_t row_pitch, size_t slice_pitch, int order,
                            void *data)
{
  size_t base_index = 0;
  int ijk0_y_OK = (ijk0.y >= 0 && ijk0.y < height);
  int ijk1_y_OK = (ijk1.y >= 0 && ijk1.y < height);
  int ijk0_x_OK = (ijk0.x >= 0 && ijk0.x < width);
  int ijk1_x_OK = (ijk1.x >= 0 && ijk1.x < width);
  // 3D image
  // T = (1 – a) * (1 – b) * (1 – c) * Ti0j0k0
  float4 sum = (float4) (0.0f);

  if (ijk0.z >= 0 && ijk0.z < depth)
    {
      base_index += (ijk0.z * slice_pitch);

      if (ijk0_y_OK)
        {
          base_index += (ijk0.y * row_pitch);

          if (ijk0_x_OK)
            {
              base_index += ijk0.x;
              sum += (one_m.x * one_m.y * one_m.z
                      * pocl_read_pixel_fast_f (base_index, channel_type,
                                                order, data));
              base_index -= ijk0.x;
            }

          // + a * (1 – b) * (1 – c) * Ti1j0k0
          if (ijk1_x_OK)
            {
              base_index += ijk1.x;
              sum += (abc.x * one_m.y * one_m.z
                      * pocl_read_pixel_fast_f (base_index, channel_type,
                                                order, data));
              base_index -= ijk1.x;
            }

          base_index -= (ijk0.y * row_pitch);
        }

      if (ijk1_y_OK)
        {
          base_index += (ijk1.y * row_pitch);

          // + (1 – a) * b * (1 – c) * Ti0j1k0
          if (ijk0_x_OK)
            {
              base_index += ijk0.x;
              sum += (one_m.x * abc.y * one_m.z
                      * pocl_read_pixel_fast_f (base_index, channel_type,
                                                order, data));
              base_index -= ijk0.x;
            }

          // + a * b * (1 – c) * Ti1j1k0
          if (ijk1_x_OK)
            {
              base_index += ijk1.x;
              sum += (abc.x * abc.y * one_m.z
                      * pocl_read_pixel_fast_f (base_index, channel_type,
                                                order, data));
              base_index -= ijk1.x;
            }

          base_index -= (ijk1.y * row_pitch);
        }

      base_index -= (ijk0.z * slice_pitch);
    }

  if (ijk1.z >= 0 && ijk1.z < depth)
    {
      base_index += (ijk1.z * slice_pitch);

      if (ijk0_y_OK)
        {
          base_index += (ijk0.y * row_pitch);

          // + (1 – a) * (1 – b) * c * Ti0j0k1
          if (ijk0_x_OK)
            {
              base_index += ijk0.x;
              sum += (one_m.x * one_m.y * abc.z
                      * pocl_read_pixel_fast_f (base_index, channel_type,
                                                order, data));
              base_index -= ijk0.x;
            }

          // + a * (1 – b) * (1 – c) * Ti1j0k0
          if (ijk1_x_OK)
            {
              base_index += ijk1.x;
              sum += (abc.x * one_m.y * abc.z
                      * pocl_read_pixel_fast_f (base_index, channel_type,
                                                order, data));
              base_index -= ijk1.x;
            }

          base_index -= (ijk0.y * row_pitch);
        }

      if (ijk1_y_OK)
        {
          base_index += (ijk1.y * row_pitch);

          // + (1 – a) * b * (1 – c) * Ti0j1k0
          if (ijk0_x_OK)
            {
              base_index += ijk0.x;
              sum += (one_m.x * abc.y * abc.z
                      * pocl_read_pixel_fast_f (base_index, channel_type,
                                                order, data));
              base_index -= ijk0.x;
            }

          // + a * b * (1 – c) * Ti1j1k0
          if (ijk1_x_OK)
            {
              base_index += ijk1.x;
              sum += (abc.x * abc.y * abc.z
                      * pocl_read_pixel_fast_f (base_index, channel_type,
                                                order, data));
              base_index -= ijk1.x;
            }

          base_index -= (ijk1.y * row_pitch);
        }

      base_index -= (ijk1.z * slice_pitch);
    }

  return sum;
}

/* TODO: float * convert_flaot(UINT32) is imprecise, so reading from images
 * with 32bit channel types may return quite bad results.
 */

_CL_READONLY static uint4
read_pixel_linear_3d_uint (float4 abc, float4 one_m, int4 ijk0, int4 ijk1,
                           int width, int height, int depth, size_t row_pitch,
                           size_t slice_pitch, int order, int elem_size,
                           void *data)
{
  size_t base_index = 0;
  int ijk0_y_OK = (ijk0.y >= 0 && ijk0.y < height);
  int ijk1_y_OK = (ijk1.y >= 0 && ijk1.y < height);
  int ijk0_x_OK = (ijk0.x >= 0 && ijk0.x < width);
  int ijk1_x_OK = (ijk1.x >= 0 && ijk1.x < width);
  // 3D image
  // T = (1 – a) * (1 – b) * (1 – c) * Ti0j0k0
  float4 sum = (float4) (0.0f);

  if (ijk0.z >= 0 && ijk0.z < depth)
    {
      base_index += (ijk0.z * slice_pitch);

      if (ijk0_y_OK)
        {
          base_index += (ijk0.y * row_pitch);

          if (ijk0_x_OK)
            {
              base_index += ijk0.x;
              sum += (one_m.x * one_m.y * one_m.z
                      * convert_float4 (pocl_read_pixel_fast_ui (
                            base_index, order, elem_size, data)));
              base_index -= ijk0.x;
            }

          // + a * (1 – b) * (1 – c) * Ti1j0k0
          if (ijk1_x_OK)
            {
              base_index += ijk1.x;
              sum += (abc.x * one_m.y * one_m.z
                      * convert_float4 (pocl_read_pixel_fast_ui (
                            base_index, order, elem_size, data)));
              base_index -= ijk1.x;
            }

          base_index -= (ijk0.y * row_pitch);
        }

      if (ijk1_y_OK)
        {
          base_index += (ijk1.y * row_pitch);

          // + (1 – a) * b * (1 – c) * Ti0j1k0
          if (ijk0_x_OK)
            {
              base_index += ijk0.x;
              sum += (one_m.x * abc.y * one_m.z
                      * convert_float4 (pocl_read_pixel_fast_ui (
                            base_index, order, elem_size, data)));
              base_index -= ijk0.x;
            }

          // + a * b * (1 – c) * Ti1j1k0
          if (ijk1_x_OK)
            {
              base_index += ijk1.x;
              sum += (abc.x * abc.y * one_m.z
                      * convert_float4 (pocl_read_pixel_fast_ui (
                            base_index, order, elem_size, data)));
              base_index -= ijk1.x;
            }

          base_index -= (ijk1.y * row_pitch);
        }

      base_index -= (ijk0.z * slice_pitch);
    }

  if (ijk1.z >= 0 && ijk1.z < depth)
    {
      base_index += (ijk1.z * slice_pitch);

      if (ijk0_y_OK)
        {
          base_index += (ijk0.y * row_pitch);

          // + (1 – a) * (1 – b) * c * Ti0j0k1
          if (ijk0_x_OK)
            {
              base_index += ijk0.x;
              sum += (one_m.x * one_m.y * abc.z
                      * convert_float4 (pocl_read_pixel_fast_ui (
                            base_index, order, elem_size, data)));
              base_index -= ijk0.x;
            }

          // + a * (1 – b) * (1 – c) * Ti1j0k0
          if (ijk1_x_OK)
            {
              base_index += ijk1.x;
              sum += (abc.x * one_m.y * abc.z
                      * convert_float4 (pocl_read_pixel_fast_ui (
                            base_index, order, elem_size, data)));
              base_index -= ijk1.x;
            }

          base_index -= (ijk0.y * row_pitch);
        }

      if (ijk1_y_OK)
        {
          base_index += (ijk1.y * row_pitch);

          // + (1 – a) * b * (1 – c) * Ti0j1k0
          if (ijk0_x_OK)
            {
              base_index += ijk0.x;
              sum += (one_m.x * abc.y * abc.z
                      * convert_float4 (pocl_read_pixel_fast_ui (
                            base_index, order, elem_size, data)));
              base_index -= ijk0.x;
            }

          // + a * b * (1 – c) * Ti1j1k0
          if (ijk1_x_OK)
            {
              base_index += ijk1.x;
              sum += (abc.x * abc.y * abc.z
                      * convert_float4 (pocl_read_pixel_fast_ui (
                            base_index, order, elem_size, data)));
              base_index -= ijk1.x;
            }

          base_index -= (ijk1.y * row_pitch);
        }

      base_index -= (ijk1.z * slice_pitch);
    }

  return convert_uint4 (sum);
}

_CL_READONLY static int4
read_pixel_linear_3d_int (float4 abc, float4 one_m, int4 ijk0, int4 ijk1,
                          int width, int height, int depth, size_t row_pitch,
                          size_t slice_pitch, int order, int elem_size,
                          void *data)
{
  size_t base_index = 0;
  int ijk0_y_OK = (ijk0.y >= 0 && ijk0.y < height);
  int ijk1_y_OK = (ijk1.y >= 0 && ijk1.y < height);
  int ijk0_x_OK = (ijk0.x >= 0 && ijk0.x < width);
  int ijk1_x_OK = (ijk1.x >= 0 && ijk1.x < width);
  // 3D image
  // T = (1 – a) * (1 – b) * (1 – c) * Ti0j0k0
  float4 sum = (float4) (0.0f);

  if (ijk0.z >= 0 && ijk0.z < depth)
    {
      base_index += (ijk0.z * slice_pitch);

      if (ijk0_y_OK)
        {
          base_index += (ijk0.y * row_pitch);

          if (ijk0_x_OK)
            {
              base_index += ijk0.x;
              sum += (one_m.x * one_m.y * one_m.z
                      * convert_float4 (pocl_read_pixel_fast_i (
                            base_index, order, elem_size, data)));
              base_index -= ijk0.x;
            }

          // + a * (1 – b) * (1 – c) * Ti1j0k0
          if (ijk1_x_OK)
            {
              base_index += ijk1.x;
              sum += (abc.x * one_m.y * one_m.z
                      * convert_float4 (pocl_read_pixel_fast_i (
                            base_index, order, elem_size, data)));
              base_index -= ijk1.x;
            }

          base_index -= (ijk0.y * row_pitch);
        }

      if (ijk1_y_OK)
        {
          base_index += (ijk1.y * row_pitch);

          // + (1 – a) * b * (1 – c) * Ti0j1k0
          if (ijk0_x_OK)
            {
              base_index += ijk0.x;
              sum += (one_m.x * abc.y * one_m.z
                      * convert_float4 (pocl_read_pixel_fast_i (
                            base_index, order, elem_size, data)));
              base_index -= ijk0.x;
            }

          // + a * b * (1 – c) * Ti1j1k0
          if (ijk1_x_OK)
            {
              base_index += ijk1.x;
              sum += (abc.x * abc.y * one_m.z
                      * convert_float4 (pocl_read_pixel_fast_i (
                            base_index, order, elem_size, data)));
              base_index -= ijk1.x;
            }

          base_index -= (ijk1.y * row_pitch);
        }

      base_index -= (ijk0.z * slice_pitch);
    }

  if (ijk1.z >= 0 && ijk1.z < depth)
    {
      base_index += (ijk1.z * slice_pitch);

      if (ijk0_y_OK)
        {
          base_index += (ijk0.y * row_pitch);

          // + (1 – a) * (1 – b) * c * Ti0j0k1
          if (ijk0_x_OK)
            {
              base_index += ijk0.x;
              sum += (one_m.x * one_m.y * abc.z
                      * convert_float4 (pocl_read_pixel_fast_i (
                            base_index, order, elem_size, data)));
              base_index -= ijk0.x;
            }

          // + a * (1 – b) * (1 – c) * Ti1j0k0
          if (ijk1_x_OK)
            {
              base_index += ijk1.x;
              sum += (abc.x * one_m.y * abc.z
                      * convert_float4 (pocl_read_pixel_fast_i (
                            base_index, order, elem_size, data)));
              base_index -= ijk1.x;
            }

          base_index -= (ijk0.y * row_pitch);
        }

      if (ijk1_y_OK)
        {
          base_index += (ijk1.y * row_pitch);

          // + (1 – a) * b * (1 – c) * Ti0j1k0
          if (ijk0_x_OK)
            {
              base_index += ijk0.x;
              sum += (one_m.x * abc.y * abc.z
                      * convert_float4 (pocl_read_pixel_fast_i (
                            base_index, order, elem_size, data)));
              base_index -= ijk0.x;
            }

          // + a * b * (1 – c) * Ti1j1k0
          if (ijk1_x_OK)
            {
              base_index += ijk1.x;
              sum += (abc.x * abc.y * abc.z
                      * convert_float4 (pocl_read_pixel_fast_i (
                            base_index, order, elem_size, data)));
              base_index -= ijk1.x;
            }

          base_index -= (ijk1.y * row_pitch);
        }

      base_index -= (ijk1.z * slice_pitch);
    }

  return convert_int4 (sum);
}

_CL_READONLY static uint4
read_pixel_linear_3d (float4 abc, float4 one_m, int4 ijk0, int4 ijk1,
                      int width, int height, int depth, int channel_type,
                      size_t row_pitch, size_t slice_pitch, int order,
                      int elem_size, void *data)
{
  // TODO unsupported channel types
  if ((channel_type == CLK_SIGNED_INT8) || (channel_type == CLK_SIGNED_INT16)
      || (channel_type == CLK_SIGNED_INT32))
    return as_uint4 (read_pixel_linear_3d_int (
        abc, one_m, ijk0, ijk1, width, height, depth, row_pitch, slice_pitch,
        order, elem_size, data));
  if ((channel_type == CLK_UNSIGNED_INT8) || (channel_type == CLK_UNSIGNED_INT16)
      || (channel_type == CLK_UNSIGNED_INT32))
    return read_pixel_linear_3d_uint (abc, one_m, ijk0, ijk1, width, height,
                                      depth, row_pitch, slice_pitch, order,
                                      elem_size, data);
  return as_uint4 (read_pixel_linear_3d_float (
      abc, one_m, ijk0, ijk1, width, height, depth, channel_type, row_pitch,
      slice_pitch, order, data));
}

/*************************************************************************/

_CL_READONLY static float4
read_pixel_linear_2d_float (float4 abc, float4 one_m, int4 ijk0, int4 ijk1,
                            int array_coord, int width, int height,
                            int channel_type, size_t row_pitch,
                            size_t slice_pitch, int order, void *data)
{
  // 2D image
  size_t base_index = 0;
  int ijk0_x_OK = (ijk0.x >= 0 && ijk0.x < width);
  int ijk1_x_OK = (ijk1.x >= 0 && ijk1.x < width);
  float4 sum = (float4) (0.0f);

  if (array_coord > 0)
    base_index += (array_coord * slice_pitch);

  if (ijk0.y >= 0 && ijk0.y < height)
    {
      base_index += (ijk0.y * row_pitch);

      // T = (1 – a) * (1 – b) * Ti0j0
      if (ijk0_x_OK)
        {
          base_index += ijk0.x;
          sum += (one_m.x * one_m.y * pocl_read_pixel_fast_f (base_index,
                                                              channel_type,
                                                              order, data));
          base_index -= ijk0.x;
        }

      // + a * (1 – b) * Ti1j0
      if (ijk1_x_OK)
        {
          base_index += ijk1.x;
          sum += (abc.x * one_m.y * pocl_read_pixel_fast_f (base_index,
                                                            channel_type,
                                                            order, data));
          base_index -= ijk1.x;
        }

      base_index -= (ijk0.y * row_pitch);
    }

  if (ijk1.y >= 0 && ijk1.y < height)
    {
      base_index += (ijk1.y * row_pitch);

      // + (1 – a) * b * Ti0j1
      if (ijk0_x_OK)
        {
          base_index += ijk0.x;
          sum += (one_m.x * abc.y * pocl_read_pixel_fast_f (base_index,
                                                            channel_type,
                                                            order, data));
          base_index -= ijk0.x;
        }

      // + a * b * Ti1j1
      if (ijk1_x_OK)
        {
          base_index += ijk1.x;
          sum += (abc.x * abc.y * pocl_read_pixel_fast_f (
                                      base_index, channel_type, order, data));
          base_index -= ijk1.x;
        }

      base_index -= (ijk1.y * row_pitch);
    }

  return sum;
}

/* TODO: float * convert_flaot(UINT32) is imprecise, so reading from images
 * with 32bit channel types may return quite bad results.
 */

_CL_READONLY static uint4
read_pixel_linear_2d_uint (float4 abc, float4 one_m, int4 ijk0, int4 ijk1,
                           int array_coord, int width, int height,
                           size_t row_pitch, size_t slice_pitch, int order,
                           int elem_size, void *data)
{
  // 2D image
  size_t base_index = 0;
  int ijk0_x_OK = (ijk0.x >= 0 && ijk0.x < width);
  int ijk1_x_OK = (ijk1.x >= 0 && ijk1.x < width);
  float4 sum = (float4) (0.0f);

  if (array_coord > 0)
    base_index += (array_coord * slice_pitch);

  if (ijk0.y >= 0 && ijk0.y < height)
    {
      base_index += (ijk0.y * row_pitch);

      // T = (1 – a) * (1 – b) * Ti0j0
      if (ijk0_x_OK)
        {
          base_index += ijk0.x;
          sum += (one_m.x * one_m.y
                  * convert_float4 (pocl_read_pixel_fast_ui (
                        base_index, order, elem_size, data)));
          base_index -= ijk0.x;
        }

      // + a * (1 – b) * Ti1j0
      if (ijk1_x_OK)
        {
          base_index += ijk1.x;
          sum += (abc.x * one_m.y * convert_float4 (pocl_read_pixel_fast_ui (
                                        base_index, order, elem_size, data)));
          base_index -= ijk1.x;
        }

      base_index -= (ijk0.y * row_pitch);
    }

  if (ijk1.y >= 0 && ijk1.y < height)
    {
      base_index += (ijk1.y * row_pitch);

      // + (1 – a) * b * Ti0j1
      if (ijk0_x_OK)
        {
          base_index += ijk0.x;
          sum += (one_m.x * abc.y * convert_float4 (pocl_read_pixel_fast_ui (
                                        base_index, order, elem_size, data)));
          base_index -= ijk0.x;
        }

      // + a * b * Ti1j1
      if (ijk1_x_OK)
        {
          base_index += ijk1.x;
          sum += (abc.x * abc.y * convert_float4 (pocl_read_pixel_fast_ui (
                                      base_index, order, elem_size, data)));
          base_index -= ijk1.x;
        }

      base_index -= (ijk1.y * row_pitch);
    }

  return convert_uint4 (sum);
}

_CL_READONLY static int4
read_pixel_linear_2d_int (float4 abc, float4 one_m, int4 ijk0, int4 ijk1,
                          int array_coord, int width, int height,
                          size_t row_pitch, size_t slice_pitch, int order,
                          int elem_size, void *data)
{
  // 2D image
  size_t base_index = 0;
  int ijk0_x_OK = (ijk0.x >= 0 && ijk0.x < width);
  int ijk1_x_OK = (ijk1.x >= 0 && ijk1.x < width);
  float4 sum = (float4) (0.0f);

  if (array_coord > 0)
    base_index += (array_coord * slice_pitch);

  if (ijk0.y >= 0 && ijk0.y < height)
    {
      base_index += (ijk0.y * row_pitch);

      // T = (1 – a) * (1 – b) * Ti0j0
      if (ijk0_x_OK)
        {
          base_index += ijk0.x;
          sum += (one_m.x * one_m.y
                  * convert_float4 (pocl_read_pixel_fast_i (base_index, order,
                                                            elem_size, data)));
          base_index -= ijk0.x;
        }

      // + a * (1 – b) * Ti1j0
      if (ijk1_x_OK)
        {
          base_index += ijk1.x;
          sum += (abc.x * one_m.y * convert_float4 (pocl_read_pixel_fast_i (
                                        base_index, order, elem_size, data)));
          base_index -= ijk1.x;
        }

      base_index -= (ijk0.y * row_pitch);
    }

  if (ijk1.y >= 0 && ijk1.y < height)
    {
      base_index += (ijk1.y * row_pitch);

      // + (1 – a) * b * Ti0j1
      if (ijk0_x_OK)
        {
          base_index += ijk0.x;
          sum += (one_m.x * abc.y * convert_float4 (pocl_read_pixel_fast_i (
                                        base_index, order, elem_size, data)));
          base_index -= ijk0.x;
        }

      // + a * b * Ti1j1
      if (ijk1_x_OK)
        {
          base_index += ijk1.x;
          sum += (abc.x * abc.y * convert_float4 (pocl_read_pixel_fast_i (
                                      base_index, order, elem_size, data)));
          base_index -= ijk1.x;
        }

      base_index -= (ijk1.y * row_pitch);
    }

  return convert_int4 (sum);
}

_CL_READONLY static uint4
read_pixel_linear_2d (float4 abc, float4 one_m, int4 ijk0, int4 ijk1,
                      int array_coord, int width, int height, int channel_type,
                      size_t row_pitch, size_t slice_pitch, int order,
                      int elem_size, void *data)
{
  // TODO unsupported channel types
  if ((channel_type == CLK_SIGNED_INT8) || (channel_type == CLK_SIGNED_INT16)
      || (channel_type == CLK_SIGNED_INT32))
    return as_uint4 (read_pixel_linear_2d_int (
        abc, one_m, ijk0, ijk1, array_coord, width, height, row_pitch,
        slice_pitch, order, elem_size, data));
  if ((channel_type == CLK_UNSIGNED_INT8) || (channel_type == CLK_UNSIGNED_INT16)
      || (channel_type == CLK_UNSIGNED_INT32))
    return read_pixel_linear_2d_uint (abc, one_m, ijk0, ijk1, array_coord,
                                      width, height, row_pitch, slice_pitch,
                                      order, elem_size, data);
  return as_uint4 (read_pixel_linear_2d_float (
      abc, one_m, ijk0, ijk1, array_coord, width, height, channel_type,
      row_pitch, slice_pitch, order, data));
}

/*************************************************************************/

_CL_READONLY static float4
read_pixel_linear_1d_float (float4 abc, float4 one_m, int ijk0, int ijk1,
                            int array_coord, int width, size_t slice_pitch,
                            int channel_type, int order, void *data)
{
  // 1D image
  size_t base_index = 0;
  float4 sum = (float4) (0.0f);

  if (array_coord > 0)
    base_index += (array_coord * slice_pitch);

  // T = (1 – a) * Ti0
  if (ijk0 >= 0 && ijk0 < width)
    {
      base_index += ijk0;
      sum += (one_m.x * pocl_read_pixel_fast_f (base_index, channel_type,
                                                order, data));
      base_index -= ijk0;
    }

  // + a * Ti1
  if (ijk1 >= 0 && ijk1 < width)
    {
      base_index += ijk1;
      sum += (abc.x * pocl_read_pixel_fast_f (base_index, channel_type, order,
                                              data));
      base_index -= ijk1;
    }

  return sum;
}

/* TODO: float * convert_flaot(UINT32) is imprecise, so reading from images
 * with 32bit channel types may return quite bad results.
 */

_CL_READONLY static uint4
read_pixel_linear_1d_uint (float4 abc, float4 one_m, int ijk0, int ijk1,
                           int array_coord, int width, size_t slice_pitch,
                           int order, int elem_size, void *data)
{
  // 1D image
  size_t base_index = 0;
  float4 sum = (float4) (0.0f);

  if (array_coord > 0)
    base_index += (array_coord * slice_pitch);

  // T = (1 – a) * Ti0
  if (ijk0 >= 0 && ijk0 < width)
    {
      base_index += ijk0;
      sum += (one_m.x * convert_float4 (pocl_read_pixel_fast_ui (
                            base_index, order, elem_size, data)));
      base_index -= ijk0;
    }

  // + a * Ti1
  if (ijk1 >= 0 && ijk1 < width)
    {
      base_index += ijk1;
      sum += (abc.x * convert_float4 (pocl_read_pixel_fast_ui (
                          base_index, order, elem_size, data)));
      base_index -= ijk1;
    }

  return convert_uint4 (sum);
}

_CL_READONLY static int4
read_pixel_linear_1d_int (float4 abc, float4 one_m, int ijk0, int ijk1,
                          int array_coord, int width, size_t slice_pitch,
                          int order, int elem_size, void *data)
{
  // 1D image
  size_t base_index = 0;
  float4 sum = (float4) (0.0f);

  if (array_coord > 0)
    base_index += (array_coord * slice_pitch);

  // T = (1 – a) * Ti0
  if (ijk0 >= 0 && ijk0 < width)
    {
      base_index += ijk0;
      sum += (one_m.x * convert_float4 (pocl_read_pixel_fast_i (
                            base_index, order, elem_size, data)));
      base_index -= ijk0;
    }

  // + a * Ti1
  if (ijk1 >= 0 && ijk1 < width)
    {
      base_index += ijk1;
      sum += (abc.x * convert_float4 (pocl_read_pixel_fast_i (
                          base_index, order, elem_size, data)));
      base_index -= ijk1;
    }

  return convert_int4 (sum);
}

_CL_READONLY static uint4
read_pixel_linear_1d (float4 abc, float4 one_m, int ijk0, int ijk1,
                      int array_coord, int width, size_t slice_pitch,
                      int channel_type, int order, int elem_size, void *data)
{
  // TODO unsupported channel types
  if ((channel_type == CLK_SIGNED_INT8) || (channel_type == CLK_SIGNED_INT16)
      || (channel_type == CLK_SIGNED_INT32))
    return as_uint4 (read_pixel_linear_1d_int (abc, one_m, ijk0, ijk1,
                                               array_coord, width, slice_pitch,
                                               order, elem_size, data));
  if ((channel_type == CLK_UNSIGNED_INT8) || (channel_type == CLK_UNSIGNED_INT16)
      || (channel_type == CLK_UNSIGNED_INT32))
    return read_pixel_linear_1d_uint (abc, one_m, ijk0, ijk1, array_coord,
                                      width, slice_pitch, order, elem_size,
                                      data);
  return as_uint4 (read_pixel_linear_1d_float (abc, one_m, ijk0, ijk1,
                                               array_coord, width, slice_pitch,
                                               channel_type, order, data));
}

/*************************************************************************/

/* These magic constant should be converted to some sort of
 * error signaling */
#define INVALID_SAMPLER_ADDRMODE (uint4) (0x1111)
#define INVALID_SAMPLER_FILTER (uint4) (0x2222)
#define INVALID_SAMPLER_NORMAL (uint4) (0x3333)

_CL_READONLY static uint4
nonrepeat_filter (global dev_image_t *img, float4 orig_coord,
                  const dev_sampler_t samp)
{
  float4 coord = orig_coord;
  if (samp & CLK_NORMALIZED_COORDS_TRUE)
    {
      float4 imgsize = convert_float4 (pocl_get_image_array_size (img));
      coord *= imgsize;
    }

  int num_channels = img->_num_channels;
  int elem_size = img->_elem_size;
  int a_index = 0;
  size_t elem_bytes = num_channels * elem_size;
  size_t row_pitch = img->_row_pitch / elem_bytes;
  size_t slice_pitch = img->_slice_pitch / elem_bytes;

  if (samp & CLK_FILTER_NEAREST)
    {
      int4 final_coord
          = pocl_address_mode (img, convert_int4 (floor (coord)), samp);
      int4 array_coord = get_image_array_offset2 (img, final_coord, coord);
      return pocl_read_pixel (img, array_coord);
    }
  else if (samp & CLK_FILTER_LINEAR)
    {
      float4 r0 = floor (coord - (float4) (0.5f)); // ijk0, address mod
      float4 r1 = r0 + (float4) (1.0f);            // ijk1, address mod
      int4 ijk0 = pocl_address_mode (img, convert_int4 (r0), samp);
      int4 ijk1 = pocl_address_mode (img, convert_int4 (r1), samp);
      float4 unused;
      float4 abc = fract ((coord - (float4) (0.5f)), &unused);
      float4 one_m = (float4) (1.0f) - abc;
      uint4 res;
      if (img->_depth != 0)
        {
          res = read_pixel_linear_3d (
              abc, one_m, ijk0, ijk1, img->_width, img->_height, img->_depth,
              img->_data_type, row_pitch, slice_pitch, img->_order,
              img->_elem_size, img->_data);
        }
      else if (img->_height != 0)
        {
          if (img->_image_array_size > 0)
            a_index = clamp (convert_int (floor (coord.z + 0.5f)), 0,
                             (int)(img->_image_array_size - 1));
          res = read_pixel_linear_2d (
              abc, one_m, ijk0, ijk1, a_index, img->_width, img->_height,
              img->_data_type, row_pitch, slice_pitch, img->_order,
              img->_elem_size, img->_data);
        }
      else
        {
          if (img->_image_array_size > 0)
            a_index = clamp (convert_int (floor (coord.y + 0.5f)), 0,
                             (int)(img->_image_array_size - 1));
          res = read_pixel_linear_1d (
              abc, one_m, ijk0.x, ijk1.x, a_index, img->_width, slice_pitch,
              img->_data_type, img->_order, img->_elem_size, img->_data);
        }
      return map_channels (res, img->_order);
    }
  else
    {
      // this should never happen - filter can only be LINEAR/NEAREST
      return INVALID_SAMPLER_FILTER;
    }
}

_CL_READONLY static uint4
repeat_filter (global dev_image_t *img, float4 coord, const dev_sampler_t samp)
{
  int array_size = img->_image_array_size;
  int num_channels = img->_num_channels;
  size_t elem_bytes = num_channels * img->_elem_size;
  size_t row_pitch = img->_row_pitch / elem_bytes;
  size_t slice_pitch = img->_slice_pitch / elem_bytes;

  if (samp & CLK_FILTER_NEAREST)
    {
      /*
         uvw = (str – floor(str)) * whd
         ijk = (int)floor(uvw)
         if (ijk > whd – 1)
           ijk = ijk – whd
         ... same for 3 coords
      */
      int4 maxcoord = pocl_get_image_array_size (img);
      float4 whd = convert_float4 (maxcoord);
      float4 uvw = (coord - floor (coord)) * whd;
      int4 ijk = convert_int4 (floor (uvw));
      int4 final_coord = select (ijk, (ijk - maxcoord), (ijk >= maxcoord));
      int4 array_coord
          = get_image_array_offset2 (img, final_coord, (coord * whd));

      return pocl_read_pixel (img, array_coord);
    }
  else if (samp & CLK_FILTER_LINEAR)
    {
      /*
          u = (s – floor(s)) * wt
          i0 = (int)floor(u – 0.5)
          i1 = i0 + 1
          if (i0 < 0)
           i0 = wt + i0
          if (i1 > wt – 1)
           i1 = i1 – wt
      */
      int a_index = 0;
      int4 maxcoord = (int4) (img->_width, img->_height, img->_depth, 1);
      float4 whd = convert_float4 (maxcoord);
      float4 uvw = (coord - floor (coord)) * whd;
      int4 ijk0 = convert_int4 (floor (uvw - (float4) (0.5f)));
      int4 ijk1 = ijk0 + (int4) (1);
      ijk0 = select (ijk0, (ijk0 + maxcoord), (ijk0 < (int4) (0)));
      maxcoord = max (maxcoord, (int4)1);
      ijk1 = ijk1 % maxcoord;
      float4 unused;
      float arraysize_f = convert_float (array_size);
      float4 abc = fract ((uvw - (float4) (0.5f)), &unused);
      float4 one_m = (float4) (1.0f) - abc;

      uint4 res;
      if (img->_depth != 0)
        {
          res = read_pixel_linear_3d (
              abc, one_m, ijk0, ijk1, img->_width, img->_height, img->_depth,
              img->_data_type, row_pitch, slice_pitch, img->_order,
              img->_elem_size, img->_data);
        }
      else if (img->_height != 0)
        {
          if (array_size > 0)
            a_index
                = clamp (convert_int (floor ((coord.z * arraysize_f) + 0.5f)),
                         0, (array_size - 1));
          res = read_pixel_linear_2d (
              abc, one_m, ijk0, ijk1, a_index, img->_width, img->_height,
              img->_data_type, row_pitch, slice_pitch, img->_order,
              img->_elem_size, img->_data);
        }
      else
        {
          if (array_size > 0)
            a_index
                = clamp (convert_int (floor ((coord.y * arraysize_f) + 0.5f)),
                         0, (array_size - 1));
          res = read_pixel_linear_1d (
              abc, one_m, ijk0.x, ijk1.x, a_index, img->_width, slice_pitch,
              img->_data_type, img->_order, img->_elem_size, img->_data);
        }
      return map_channels (res, img->_order);
    }
  else
    {
      // this should never happen - filter can only be LINEAR/NEAREST
      return INVALID_SAMPLER_FILTER;
    }
}

_CL_READONLY static uint4
mirrored_repeat_filter (global dev_image_t *img, float4 coord,
                        const dev_sampler_t samp)
{
  int array_size = img->_image_array_size;
  int num_channels = img->_num_channels;
  size_t elem_bytes = num_channels * img->_elem_size;
  size_t row_pitch = img->_row_pitch / elem_bytes;
  size_t slice_pitch = img->_slice_pitch / elem_bytes;

  if (samp & CLK_FILTER_NEAREST)
    {
      /*
        s’ = 2.0f * rint(0.5f * s)
        s’ = fabs(s – s’)
        u = s’ * wt
        i = (int)floor(u)
        i = min(i, wt – 1)
      */

      float4 ss = (float4) (2.0f) * rint ((float4) (0.5f) * coord);
      ss = fabs (coord - ss);
      int4 maxcoord = pocl_get_image_array_size (img);
      float4 whd = convert_float4 (maxcoord);
      float4 uvw = ss * whd;
      int4 ijk = convert_int4 (floor (uvw));
      int4 wdt = max ((maxcoord - (int4) (1)), (int4) (0));
      int4 final_coord = select (ijk, wdt, (ijk > wdt));
      int4 array_coord
          = get_image_array_offset2 (img, final_coord, (coord * whd));
      return pocl_read_pixel (img, array_coord);
    }
  else if (samp & CLK_FILTER_LINEAR)
    {
      /*
        s’ = 2.0f * rint(0.5f * s)
        s’ = fabs(s – s’)
        u = s’ * wt
        i0 = (int)floor(u – 0.5f)
        i1 = i0 + 1
        i0 = max(i0, 0)
        i1 = min(i1, wt – 1)
      */
      float4 ss = (float4) (2.0f) * rint ((float4) (0.5f) * coord);
      ss = fabs (coord - ss);
      int4 maxcoord = (int4) (img->_width, img->_height, img->_depth, 1);
      float4 uvw = ss * convert_float4 (maxcoord);
      int4 ijk0 = convert_int4 (floor (uvw - (float4) (0.5f)));
      int4 ijk1 = ijk0 + (int4) (1);
      ijk0 = max (ijk0, (int4)0);
      ijk1 = min (ijk1, (maxcoord - (int4) (1)));
      float4 unused;
      float arraysize_f = convert_float (array_size);
      float4 abc = fract ((uvw - (float4) (0.5f)), &unused);
      float4 one_m = (float4) (1.0f) - abc;
      uint4 res;
      int a_index = 0;
      if (img->_depth != 0)
        {
          res = read_pixel_linear_3d (
              abc, one_m, ijk0, ijk1, img->_width, img->_height, img->_depth,
              img->_data_type, row_pitch, slice_pitch, img->_order,
              img->_elem_size, img->_data);
        }
      else if (img->_height != 0)
        {
          if (array_size > 0)
            a_index
                = clamp (convert_int (floor ((coord.z * arraysize_f) + 0.5f)),
                         0, (array_size - 1));
          res = read_pixel_linear_2d (
              abc, one_m, ijk0, ijk1, a_index, img->_width, img->_height,
              img->_data_type, row_pitch, slice_pitch, img->_order,
              img->_elem_size, img->_data);
        }
      else
        {
          if (array_size > 0)
            a_index
                = clamp (convert_int (floor ((coord.y * arraysize_f) + 0.5f)),
                         0, (array_size - 1));
          res = read_pixel_linear_1d (
              abc, one_m, ijk0.x, ijk1.x, a_index, img->_width, slice_pitch,
              img->_data_type, img->_order, img->_elem_size, img->_data);
        }
      return map_channels (res, img->_order);
    }
  else
    {
      // this should never happen - filter can only be LINEAR/NEAREST
      return INVALID_SAMPLER_FILTER;
    }
}

/*************************************************************************/
/* read pixel with float coordinates */
_CL_READONLY static uint4
pocl_read_pixel_floatc (global dev_image_t *img, float4 coord,
                        const dev_sampler_t samp)
{
  if ((samp & CLK_ADDRESS_MASK) == CLK_ADDRESS_REPEAT)
    return repeat_filter (img, coord, samp);
  else if ((samp & CLK_ADDRESS_MASK) == CLK_ADDRESS_MIRRORED_REPEAT)
    return mirrored_repeat_filter (img, coord, samp);
  else
    return nonrepeat_filter (img, coord, samp);
}

/*************************************************************************/
/* read pixel with int coordinates
 * from Spec:
 *
 * Furthermore, the read_imagei and read_imageui calls that take integer
 * coordinates must use a sampler with normalized coordinates set to
 * CLK_NORMALIZED_COORDS_FALSE and addressing mode set to
 * CLK_ADDRESS_CLAMP_TO_EDGE, CLK_ADDRESS_CLAMP or CLK_ADDRESS_NONE;
 * otherwise the values returned are undefined.
*/

_CL_READONLY static uint4
pocl_read_pixel_intc (global dev_image_t *img, int4 coord,
                      const dev_sampler_t samp)
{
  if (samp & CLK_NORMALIZED_COORDS_TRUE)
    return INVALID_SAMPLER_NORMAL;
  if (((samp & CLK_ADDRESS_MASK) == CLK_ADDRESS_REPEAT)
      || ((samp & CLK_ADDRESS_MASK) == CLK_ADDRESS_MIRRORED_REPEAT))
    return INVALID_SAMPLER_ADDRMODE;

  int4 final_coord = pocl_address_mode (img, coord, samp);
  int4 array_coord = get_image_array_offset (img, final_coord, coord);
  return pocl_read_pixel (img, array_coord);
}

/******************* DONE *************************************************/
/* read pixel with float coordinates, WITHOUT sampler
 * from Spec:
 *
 * The samplerless read image functions behave exactly as the corresponding
 * read image functions that take integer coordinates and a sampler with
 * filter mode set to CLK_FILTER_NEAREST, normalized coordinates set to
 * CLK_NORMALIZED_COORDS_FALSE and addressing mode to CLK_ADDRESS_NONE.
 */

_CL_READONLY static uint4
pocl_read_pixel_intc_samplerless (global dev_image_t *img, int4 coord)
{
  const dev_sampler_t samp
      = CLK_FILTER_NEAREST | CLK_NORMALIZED_COORDS_FALSE | CLK_ADDRESS_NONE;

  int4 final_coord = pocl_address_mode (img, coord, samp);
  int4 array_coord = get_image_array_offset (img, final_coord, coord);
  return pocl_read_pixel (img, array_coord);
}

/*************************************************************************/

#if __clang_major__ > 3
/* After Clang 4.0, the sampler_t is passed as an opaque struct (ptr)
 which we convert to int32 with the LLVM pass HandleSamplerInitialization. */
#define READ_SAMPLER                                                          \
  const dev_sampler_t s                                                       \
      = (dev_sampler_t) (__builtin_astype (sampler, uintptr_t));
#else
/* Before Clang 4.0, the sampler_t was passed as an int32. */
#define READ_SAMPLER                                                          \
  const dev_sampler_t s = (dev_sampler_t) (__builtin_astype (sampler, int));
#endif

/* Implementation for read_image with any image data type and int coordinates
   __IMGTYPE__ = image type (image2d_t, ...)
   __RETVAL__  = return value (int4 or uint4 float4)
   __POSTFIX__ = function name postfix (i, ui, f)
   __COORD__   = coordinate type (int, int2, int4)
*/

#define IMPLEMENT_READ_INT4_IMAGE_INT_COORD(__IMGTYPE__, __RETVAL__,          \
                                            __POSTFIX__, __COORD__)           \
  __RETVAL__ _CL_OVERLOADABLE _CL_READONLY read_image##__POSTFIX__ (                       \
      __IMGTYPE__ image, sampler_t sampler, __COORD__ coord)                  \
  {                                                                           \
    int4 coord4;                                                              \
    INITCOORD##__COORD__ (coord4, coord);                                     \
    global dev_image_t *i_ptr                                                 \
        = __builtin_astype (image, global dev_image_t *);                     \
    READ_SAMPLER                                                              \
    uint4 color = pocl_read_pixel_intc (i_ptr, coord4, s);                    \
    return as_##__RETVAL__ (color);                                           \
  }

#define IMPLEMENT_READ_FLOAT4_IMAGE_INT_COORD(__IMGTYPE__, __COORD__)         \
  float4 _CL_OVERLOADABLE _CL_READONLY read_imagef (__IMGTYPE__ image, sampler_t sampler,  \
                                       __COORD__ coord)                       \
  {                                                                           \
    int4 coord4;                                                              \
    INITCOORD##__COORD__ (coord4, coord);                                     \
    global dev_image_t *i_ptr                                                 \
        = __builtin_astype (image, global dev_image_t *);                     \
    READ_SAMPLER                                                              \
    uint4 color = pocl_read_pixel_intc (i_ptr, coord4, s);                    \
    return as_float4 (color);                                                 \
  }

#define IMPLEMENT_READ_FLOAT4_IMAGE_FLOAT_COORD(__IMGTYPE__, __COORD__)       \
  float4 _CL_OVERLOADABLE _CL_READONLY read_imagef (__IMGTYPE__ image, sampler_t sampler,  \
                                       __COORD__ coord)                       \
  {                                                                           \
    float4 coord4;                                                            \
    INITCOORD##__COORD__ (coord4, coord);                                     \
    global dev_image_t *i_ptr                                                 \
        = __builtin_astype (image, global dev_image_t *);                     \
    READ_SAMPLER                                                              \
    uint4 color = pocl_read_pixel_floatc (i_ptr, coord4, s);                  \
    return as_float4 (color);                                                 \
  }

#define IMPLEMENT_READ_INT4_IMAGE_FLOAT_COORD(__IMGTYPE__, __RETVAL__,        \
                                              __POSTFIX__, __COORD__)         \
  __RETVAL__ _CL_OVERLOADABLE _CL_READONLY read_image##__POSTFIX__ (                       \
      __IMGTYPE__ image, sampler_t sampler, __COORD__ coord)                  \
  {                                                                           \
    float4 coord4;                                                            \
    INITCOORD##__COORD__ (coord4, coord);                                     \
    global dev_image_t *i_ptr                                                 \
        = __builtin_astype (image, global dev_image_t *);                     \
    READ_SAMPLER                                                              \
    uint4 color = pocl_read_pixel_floatc (i_ptr, coord4, s);                  \
    return as_##__RETVAL__ (color);                                           \
  }

/* NO Sampler Implementation for read_image with any image data type
   and int coordinates
   __IMGTYPE__ = image type (image2d_t, ...)
   __RETVAL__  = return value (int4 or uint4 float4)
   __POSTFIX__ = function name postfix (i, ui, f)
   __COORD__   = coordinate type (int, int2, int4)
   OCL 1.2 Spec Says
   The samplerless read image functions behave exactly as the
   corresponding read image functions that take integer
   coordinates and a sampler with filter mode set to
   CLK_FILTER_NEAREST, normalized coordinates set to
   CLK_NORMALIZED_COORDS_FALSE and
   addressing mode to CLK_ADDRESS_NONE.
   CLK_ADDRESS_NONE – for this addressing mode the programmer guarantees that
   the image coordinates used to sample elements of the image refer to a
   location inside the image; otherwise the results are undefined.
   Thus we do not need out of bound check for now.
   If we need sampler for other cases it has to be defined as default sampler:
   const sampler_t defualt_sampler = CLK_NORMALIZED_COORDS_FALSE |
                                  CLK_ADDRESS_NONE |
                                  CLK_FILTER_NEAREST;
*/

#define IMPLEMENT_READ_INT4_IMAGE_INT_COORD_NOSAMPLER(                        \
    __IMGTYPE__, __RETVAL__, __POSTFIX__, __COORD__)                          \
  __RETVAL__ _CL_OVERLOADABLE _CL_READONLY read_image##__POSTFIX__ (__IMGTYPE__ image,     \
                                                       __COORD__ coord)       \
  {                                                                           \
    int4 coord4;                                                              \
    INITCOORD##__COORD__ (coord4, coord);                                     \
    global dev_image_t *i_ptr                                                 \
        = __builtin_astype (image, global dev_image_t *);                     \
    uint4 color = pocl_read_pixel_intc_samplerless (i_ptr, coord4);           \
    return as_##__RETVAL__ (color);                                           \
  }

#define IMPLEMENT_READ_FLOAT4_IMAGE_INT_COORD_NOSAMPLER(__IMGTYPE__,          \
                                                        __COORD__)            \
  float4 _CL_OVERLOADABLE _CL_READONLY read_imagef (__IMGTYPE__ image, __COORD__ coord)    \
  {                                                                           \
    int4 coord4;                                                              \
    INITCOORD##__COORD__ (coord4, coord);                                     \
    global dev_image_t *i_ptr                                                 \
        = __builtin_astype (image, global dev_image_t *);                     \
    uint4 color = pocl_read_pixel_intc_samplerless (i_ptr, coord4);           \
    return as_float4 (color);                                                 \
  }

/* NO sampler */

IMPLEMENT_READ_FLOAT4_IMAGE_INT_COORD_NOSAMPLER (IMG_RO_AQ image1d_t, int)
IMPLEMENT_READ_FLOAT4_IMAGE_INT_COORD_NOSAMPLER (IMG_RO_AQ image1d_buffer_t,
                                                 int)
IMPLEMENT_READ_FLOAT4_IMAGE_INT_COORD_NOSAMPLER (IMG_RO_AQ image1d_array_t, int2)

IMPLEMENT_READ_FLOAT4_IMAGE_INT_COORD_NOSAMPLER (IMG_RO_AQ image2d_t, int2)
IMPLEMENT_READ_FLOAT4_IMAGE_INT_COORD_NOSAMPLER (IMG_RO_AQ image2d_array_t,
                                                 int4)
IMPLEMENT_READ_FLOAT4_IMAGE_INT_COORD_NOSAMPLER (IMG_RO_AQ image3d_t, int4)



IMPLEMENT_READ_INT4_IMAGE_INT_COORD_NOSAMPLER (IMG_RO_AQ image1d_t, uint4, ui,
                                               int)
IMPLEMENT_READ_INT4_IMAGE_INT_COORD_NOSAMPLER (IMG_RO_AQ image1d_t, int4, i,
                                               int)
IMPLEMENT_READ_INT4_IMAGE_INT_COORD_NOSAMPLER (IMG_RO_AQ image1d_array_t, uint4, ui,
                                               int2)
IMPLEMENT_READ_INT4_IMAGE_INT_COORD_NOSAMPLER (IMG_RO_AQ image1d_array_t, int4, i,
                                               int2)
IMPLEMENT_READ_INT4_IMAGE_INT_COORD_NOSAMPLER (IMG_RO_AQ image1d_buffer_t,
                                               uint4, ui, int)
IMPLEMENT_READ_INT4_IMAGE_INT_COORD_NOSAMPLER (IMG_RO_AQ image1d_buffer_t,
                                               int4, i, int)

IMPLEMENT_READ_INT4_IMAGE_INT_COORD_NOSAMPLER (IMG_RO_AQ image2d_t, uint4, ui,
                                               int2)
IMPLEMENT_READ_INT4_IMAGE_INT_COORD_NOSAMPLER (IMG_RO_AQ image2d_t, int4, i,
                                               int2)
IMPLEMENT_READ_INT4_IMAGE_INT_COORD_NOSAMPLER (IMG_RO_AQ image2d_array_t,
                                               uint4, ui, int4)
IMPLEMENT_READ_INT4_IMAGE_INT_COORD_NOSAMPLER (IMG_RO_AQ image2d_array_t, int4,
                                               i, int4)
IMPLEMENT_READ_INT4_IMAGE_INT_COORD_NOSAMPLER (IMG_RO_AQ image3d_t, uint4, ui,
                                               int4)
IMPLEMENT_READ_INT4_IMAGE_INT_COORD_NOSAMPLER (IMG_RO_AQ image3d_t, int4, i,
                                               int4)

/* float4 img + float coords + sampler */

IMPLEMENT_READ_FLOAT4_IMAGE_FLOAT_COORD (IMG_RO_AQ image1d_t, float)
IMPLEMENT_READ_FLOAT4_IMAGE_FLOAT_COORD (IMG_RO_AQ image1d_buffer_t, float)
IMPLEMENT_READ_FLOAT4_IMAGE_FLOAT_COORD (IMG_RO_AQ image1d_array_t, float2)
IMPLEMENT_READ_FLOAT4_IMAGE_FLOAT_COORD (IMG_RO_AQ image2d_t, float2)
IMPLEMENT_READ_FLOAT4_IMAGE_FLOAT_COORD (IMG_RO_AQ image2d_array_t, float4)
IMPLEMENT_READ_FLOAT4_IMAGE_FLOAT_COORD (IMG_RO_AQ image3d_t, float4)

/* float4 img + int coords + sampler */

IMPLEMENT_READ_FLOAT4_IMAGE_INT_COORD (IMG_RO_AQ image1d_t, int)
IMPLEMENT_READ_FLOAT4_IMAGE_INT_COORD (IMG_RO_AQ image1d_buffer_t, int)
IMPLEMENT_READ_FLOAT4_IMAGE_INT_COORD (IMG_RO_AQ image1d_array_t, int2)
IMPLEMENT_READ_FLOAT4_IMAGE_INT_COORD (IMG_RO_AQ image2d_t, int2)
IMPLEMENT_READ_FLOAT4_IMAGE_INT_COORD (IMG_RO_AQ image2d_array_t, int4)
IMPLEMENT_READ_FLOAT4_IMAGE_INT_COORD (IMG_RO_AQ image3d_t, int4)

/* int4 img + float coords + sampler */

IMPLEMENT_READ_INT4_IMAGE_FLOAT_COORD (IMG_RO_AQ image1d_t, uint4, ui, float)
IMPLEMENT_READ_INT4_IMAGE_FLOAT_COORD (IMG_RO_AQ image1d_t, int4, i, float)
IMPLEMENT_READ_INT4_IMAGE_FLOAT_COORD (IMG_RO_AQ image1d_buffer_t, uint4, ui,
                                       float)
IMPLEMENT_READ_INT4_IMAGE_FLOAT_COORD (IMG_RO_AQ image1d_buffer_t, int4, i,
                                       float)
IMPLEMENT_READ_INT4_IMAGE_FLOAT_COORD (IMG_RO_AQ image1d_array_t, uint4, ui,
                                       float2)
IMPLEMENT_READ_INT4_IMAGE_FLOAT_COORD (IMG_RO_AQ image1d_array_t, int4, i,
                                       float2)

IMPLEMENT_READ_INT4_IMAGE_FLOAT_COORD (IMG_RO_AQ image2d_t, uint4, ui, float2)
IMPLEMENT_READ_INT4_IMAGE_FLOAT_COORD (IMG_RO_AQ image2d_t, int4, i, float2)
IMPLEMENT_READ_INT4_IMAGE_FLOAT_COORD (IMG_RO_AQ image2d_array_t, uint4, ui,
                                       float4)
IMPLEMENT_READ_INT4_IMAGE_FLOAT_COORD (IMG_RO_AQ image2d_array_t, int4, i,
                                       float4)
IMPLEMENT_READ_INT4_IMAGE_FLOAT_COORD (IMG_RO_AQ image3d_t, uint4, ui, float4)
IMPLEMENT_READ_INT4_IMAGE_FLOAT_COORD (IMG_RO_AQ image3d_t, int4, i, float4)

/* int4 img + int coords + sampler */

IMPLEMENT_READ_INT4_IMAGE_INT_COORD (IMG_RO_AQ image1d_t, uint4, ui, int)
IMPLEMENT_READ_INT4_IMAGE_INT_COORD (IMG_RO_AQ image1d_t, int4, i, int)
IMPLEMENT_READ_INT4_IMAGE_INT_COORD (IMG_RO_AQ image1d_buffer_t, uint4, ui,
                                     int)
IMPLEMENT_READ_INT4_IMAGE_INT_COORD (IMG_RO_AQ image1d_buffer_t, int4, i, int)
IMPLEMENT_READ_INT4_IMAGE_INT_COORD (IMG_RO_AQ image1d_array_t, uint4, ui,
                                     int2)
IMPLEMENT_READ_INT4_IMAGE_INT_COORD (IMG_RO_AQ image1d_array_t, int4, i, int2)

IMPLEMENT_READ_INT4_IMAGE_INT_COORD (IMG_RO_AQ image2d_t, uint4, ui, int2)
IMPLEMENT_READ_INT4_IMAGE_INT_COORD (IMG_RO_AQ image2d_t, int4, i, int2)
IMPLEMENT_READ_INT4_IMAGE_INT_COORD (IMG_RO_AQ image2d_array_t, uint4, ui,
                                     int4)
IMPLEMENT_READ_INT4_IMAGE_INT_COORD (IMG_RO_AQ image2d_array_t, int4, i, int4)
IMPLEMENT_READ_INT4_IMAGE_INT_COORD (IMG_RO_AQ image3d_t, uint4, ui, int4)
IMPLEMENT_READ_INT4_IMAGE_INT_COORD (IMG_RO_AQ image3d_t, int4, i, int4)

/******************************************************************************/
/******************************************************************************/

#ifdef CLANG_HAS_RW_IMAGES

/* NO sampler */

IMPLEMENT_READ_FLOAT4_IMAGE_INT_COORD_NOSAMPLER (IMG_RW_AQ image1d_t, int)
IMPLEMENT_READ_FLOAT4_IMAGE_INT_COORD_NOSAMPLER (IMG_RW_AQ image1d_buffer_t,
                                                 int)
IMPLEMENT_READ_FLOAT4_IMAGE_INT_COORD_NOSAMPLER (IMG_RW_AQ image1d_array_t, int2)

IMPLEMENT_READ_FLOAT4_IMAGE_INT_COORD_NOSAMPLER (IMG_RW_AQ image2d_t, int2)
IMPLEMENT_READ_FLOAT4_IMAGE_INT_COORD_NOSAMPLER (IMG_RW_AQ image2d_array_t,
                                                 int4)
IMPLEMENT_READ_FLOAT4_IMAGE_INT_COORD_NOSAMPLER (IMG_RW_AQ image3d_t, int4)


IMPLEMENT_READ_INT4_IMAGE_INT_COORD_NOSAMPLER (IMG_RW_AQ image1d_t, uint4, ui,
                                               int)
IMPLEMENT_READ_INT4_IMAGE_INT_COORD_NOSAMPLER (IMG_RW_AQ image1d_t, int4, i,
                                               int)
IMPLEMENT_READ_INT4_IMAGE_INT_COORD_NOSAMPLER (IMG_RW_AQ image1d_array_t, uint4, ui,
                                               int2)
IMPLEMENT_READ_INT4_IMAGE_INT_COORD_NOSAMPLER (IMG_RW_AQ image1d_array_t, int4, i,
                                               int2)
IMPLEMENT_READ_INT4_IMAGE_INT_COORD_NOSAMPLER (IMG_RW_AQ image1d_buffer_t,
                                               uint4, ui, int)
IMPLEMENT_READ_INT4_IMAGE_INT_COORD_NOSAMPLER (IMG_RW_AQ image1d_buffer_t,
                                               int4, i, int)

IMPLEMENT_READ_INT4_IMAGE_INT_COORD_NOSAMPLER (IMG_RW_AQ image2d_t, uint4, ui,
                                               int2)
IMPLEMENT_READ_INT4_IMAGE_INT_COORD_NOSAMPLER (IMG_RW_AQ image2d_t, int4, i,
                                               int2)
IMPLEMENT_READ_INT4_IMAGE_INT_COORD_NOSAMPLER (IMG_RW_AQ image2d_array_t,
                                               uint4, ui, int4)
IMPLEMENT_READ_INT4_IMAGE_INT_COORD_NOSAMPLER (IMG_RW_AQ image2d_array_t, int4,
                                               i, int4)
IMPLEMENT_READ_INT4_IMAGE_INT_COORD_NOSAMPLER (IMG_RW_AQ image3d_t, uint4, ui,
                                               int4)
IMPLEMENT_READ_INT4_IMAGE_INT_COORD_NOSAMPLER (IMG_RW_AQ image3d_t, int4, i,
                                               int4)

/* float4 img + float coords + sampler */

IMPLEMENT_READ_FLOAT4_IMAGE_FLOAT_COORD (IMG_RW_AQ image1d_t, float)
IMPLEMENT_READ_FLOAT4_IMAGE_FLOAT_COORD (IMG_RW_AQ image1d_buffer_t, float)
IMPLEMENT_READ_FLOAT4_IMAGE_FLOAT_COORD (IMG_RW_AQ image1d_array_t, float2)
IMPLEMENT_READ_FLOAT4_IMAGE_FLOAT_COORD (IMG_RW_AQ image2d_t, float2)
IMPLEMENT_READ_FLOAT4_IMAGE_FLOAT_COORD (IMG_RW_AQ image2d_array_t, float4)
IMPLEMENT_READ_FLOAT4_IMAGE_FLOAT_COORD (IMG_RW_AQ image3d_t, float4)

/* float4 img + int coords + sampler */

IMPLEMENT_READ_FLOAT4_IMAGE_INT_COORD (IMG_RW_AQ image1d_t, int)
IMPLEMENT_READ_FLOAT4_IMAGE_INT_COORD (IMG_RW_AQ image1d_buffer_t, int)
IMPLEMENT_READ_FLOAT4_IMAGE_INT_COORD (IMG_RW_AQ image1d_array_t, int2)
IMPLEMENT_READ_FLOAT4_IMAGE_INT_COORD (IMG_RW_AQ image2d_t, int2)
IMPLEMENT_READ_FLOAT4_IMAGE_INT_COORD (IMG_RW_AQ image2d_array_t, int4)
IMPLEMENT_READ_FLOAT4_IMAGE_INT_COORD (IMG_RW_AQ image3d_t, int4)

/* int4 img + float coords + sampler */

IMPLEMENT_READ_INT4_IMAGE_FLOAT_COORD (IMG_RW_AQ image1d_t, uint4, ui, float)
IMPLEMENT_READ_INT4_IMAGE_FLOAT_COORD (IMG_RW_AQ image1d_t, int4, i, float)
IMPLEMENT_READ_INT4_IMAGE_FLOAT_COORD (IMG_RW_AQ image1d_buffer_t, uint4, ui,
                                       float)
IMPLEMENT_READ_INT4_IMAGE_FLOAT_COORD (IMG_RW_AQ image1d_buffer_t, int4, i,
                                       float)
IMPLEMENT_READ_INT4_IMAGE_FLOAT_COORD (IMG_RW_AQ image1d_array_t, uint4, ui,
                                       float2)
IMPLEMENT_READ_INT4_IMAGE_FLOAT_COORD (IMG_RW_AQ image1d_array_t, int4, i,
                                       float2)

IMPLEMENT_READ_INT4_IMAGE_FLOAT_COORD (IMG_RW_AQ image2d_t, uint4, ui, float2)
IMPLEMENT_READ_INT4_IMAGE_FLOAT_COORD (IMG_RW_AQ image2d_t, int4, i, float2)
IMPLEMENT_READ_INT4_IMAGE_FLOAT_COORD (IMG_RW_AQ image2d_array_t, uint4, ui,
                                       float4)
IMPLEMENT_READ_INT4_IMAGE_FLOAT_COORD (IMG_RW_AQ image2d_array_t, int4, i,
                                       float4)
IMPLEMENT_READ_INT4_IMAGE_FLOAT_COORD (IMG_RW_AQ image3d_t, uint4, ui, float4)
IMPLEMENT_READ_INT4_IMAGE_FLOAT_COORD (IMG_RW_AQ image3d_t, int4, i, float4)

/* int4 img + int coords + sampler */

IMPLEMENT_READ_INT4_IMAGE_INT_COORD (IMG_RW_AQ image1d_t, uint4, ui, int)
IMPLEMENT_READ_INT4_IMAGE_INT_COORD (IMG_RW_AQ image1d_t, int4, i, int)
IMPLEMENT_READ_INT4_IMAGE_INT_COORD (IMG_RW_AQ image1d_buffer_t, uint4, ui,
                                     int)
IMPLEMENT_READ_INT4_IMAGE_INT_COORD (IMG_RW_AQ image1d_buffer_t, int4, i, int)
IMPLEMENT_READ_INT4_IMAGE_INT_COORD (IMG_RW_AQ image1d_array_t, uint4, ui,
                                     int2)
IMPLEMENT_READ_INT4_IMAGE_INT_COORD (IMG_RW_AQ image1d_array_t, int4, i, int2)

IMPLEMENT_READ_INT4_IMAGE_INT_COORD (IMG_RW_AQ image2d_t, uint4, ui, int2)
IMPLEMENT_READ_INT4_IMAGE_INT_COORD (IMG_RW_AQ image2d_t, int4, i, int2)
IMPLEMENT_READ_INT4_IMAGE_INT_COORD (IMG_RW_AQ image2d_array_t, uint4, ui,
                                     int4)
IMPLEMENT_READ_INT4_IMAGE_INT_COORD (IMG_RW_AQ image2d_array_t, int4, i, int4)
IMPLEMENT_READ_INT4_IMAGE_INT_COORD (IMG_RW_AQ image3d_t, uint4, ui, int4)
IMPLEMENT_READ_INT4_IMAGE_INT_COORD (IMG_RW_AQ image3d_t, int4, i, int4)

#endif