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

#include "arm_compute/core/AccessWindowStatic.h"
#include "arm_compute/core/Error.h"
#include "arm_compute/core/Helpers.h"
#include "arm_compute/core/ITensor.h"
#include "arm_compute/core/TensorInfo.h"
#include "arm_compute/core/Types.h"
#include "arm_compute/core/Utils.h"
#include "arm_compute/core/Validate.h"

#include <arm_neon.h>
#include <cstddef>
#include <cstdint>
#include <tuple>

using namespace arm_compute;

namespace arm_compute
{
class Coordinates;
} // namespace arm_compute

namespace
{
constexpr int NO_EDGE = 0;
constexpr int EDGE    = 255;
constexpr int MAYBE   = 127;
} // namespace

#ifdef ARM_COMPUTE_ENABLE_FP16
namespace fp16
{
inline uint8x8_t phase_quantization(const float32x4x2_t &gx, const float32x4x2_t &gy)
{
    // Constant use for evaluating score1 and score3
    static const float32x4_t const45 = vdupq_n_f32(0.70710678118655f);
    static const float32x4_t zero    = vdupq_n_f32(0.0f);
    static const float32x4_t one     = vdupq_n_f32(1.0f);
    static const float32x4_t two     = vdupq_n_f32(2.0f);
    static const float32x4_t three   = vdupq_n_f32(3.0f);

    // Score0: (1, 0)
    const float32x4x2_t score0 =
    {
        vabsq_f32(gx.val[0]),
        vabsq_f32(gx.val[1])
    };

    // Score2: ( 0, 1 )
    const float32x4x2_t score2 =
    {
        vabsq_f32(gy.val[0]),
        vabsq_f32(gy.val[1])
    };

    // Score1 and Score3: ( sqrt(2) / 2, sqrt(2) / 2 ) - ( -sqrt(2) / 2, sqrt(2) / 2 )
    float32x4x2_t score1 =
    {
        vmulq_f32(gy.val[0], const45),
        vmulq_f32(gy.val[1], const45)
    };

    float32x4x2_t score3 = score1;

    score1.val[0] = vmlaq_f32(score1.val[0], gx.val[0], const45);
    score1.val[1] = vmlaq_f32(score1.val[1], gx.val[1], const45);
    score3.val[0] = vmlsq_f32(score3.val[0], gx.val[0], const45);
    score3.val[1] = vmlsq_f32(score3.val[1], gx.val[1], const45);

    score1.val[0] = vabsq_f32(score1.val[0]);
    score1.val[1] = vabsq_f32(score1.val[1]);
    score3.val[0] = vabsq_f32(score3.val[0]);
    score3.val[1] = vabsq_f32(score3.val[1]);

    float32x4x2_t phase =
    {
        zero,
        zero
    };

    float32x4x2_t old_score = score0;

    // score1 > old_score?
    uint32x4x2_t mask =
    {
        vcgtq_f32(score1.val[0], old_score.val[0]),
        vcgtq_f32(score1.val[1], old_score.val[1])
    };

    phase.val[0]     = vbslq_f32(mask.val[0], one, phase.val[0]);
    phase.val[1]     = vbslq_f32(mask.val[1], one, phase.val[1]);
    old_score.val[0] = vbslq_f32(mask.val[0], score1.val[0], old_score.val[0]);
    old_score.val[1] = vbslq_f32(mask.val[1], score1.val[1], old_score.val[1]);

    // score2 > old_score?
    mask.val[0] = vcgtq_f32(score2.val[0], old_score.val[0]);
    mask.val[1] = vcgtq_f32(score2.val[1], old_score.val[1]);

    phase.val[0]     = vbslq_f32(mask.val[0], two, phase.val[0]);
    phase.val[1]     = vbslq_f32(mask.val[1], two, phase.val[1]);
    old_score.val[0] = vbslq_f32(mask.val[0], score2.val[0], old_score.val[0]);
    old_score.val[1] = vbslq_f32(mask.val[1], score2.val[1], old_score.val[1]);

    // score3 > old_score?
    mask.val[0] = vcgtq_f32(score3.val[0], old_score.val[0]);
    mask.val[1] = vcgtq_f32(score3.val[1], old_score.val[1]);

    phase.val[0]     = vbslq_f32(mask.val[0], three, phase.val[0]);
    phase.val[1]     = vbslq_f32(mask.val[1], three, phase.val[1]);
    old_score.val[0] = vbslq_f32(mask.val[0], score3.val[0], old_score.val[0]);
    old_score.val[1] = vbslq_f32(mask.val[1], score3.val[1], old_score.val[1]);

    // Convert from float32x4_t to uint8x8_t
    return vmovn_u16(vcombine_u16(vmovn_u32(vcvtq_u32_f32(phase.val[0])),
                                  vmovn_u32(vcvtq_u32_f32(phase.val[1]))));
}

inline uint8x8_t phase_quantization(float16x8_t gx, float16x8_t gy)
{
    // Constant use for evaluating score1 and score3
    static const float16x8_t const45 = vdupq_n_f16(0.70710678118655f);
    static const float16x8_t zero    = vdupq_n_f16(0.0f);
    static const float16x8_t one     = vdupq_n_f16(1.0f);
    static const float16x8_t two     = vdupq_n_f16(2.0f);
    static const float16x8_t three   = vdupq_n_f16(3.0f);

    // Score0: (1, 0)
    const float16x8_t score0 = vabsq_f16(gx);

    // Score2: ( 0, 1 )
    const float16x8_t score2 = vabsq_f16(gy);

    // Score1 and Score3: ( sqrt(2) / 2, sqrt(2) / 2 ) - ( -sqrt(2) / 2, sqrt(2) / 2 )
    float16x8_t score1 = vmulq_f16(gy, const45);
    float16x8_t score3 = score1;

    score1 = vfmaq_f16(score1, gx, const45);
    score3 = vfmsq_f16(score3, gx, const45);

    score1 = vabsq_f16(score1);
    score3 = vabsq_f16(score3);

    float16x8_t phase     = zero;
    float16x8_t old_score = score0;

    // score1 > old_score?
    uint16x8_t mask = vcgtq_f16(score1, old_score);

    phase     = vbslq_f16(mask, one, phase);
    old_score = vbslq_f16(mask, score1, old_score);

    // score2 > old_score?
    mask = vcgtq_f16(score2, old_score);

    phase     = vbslq_f16(mask, two, phase);
    old_score = vbslq_f16(mask, score2, old_score);

    // score3 > old_score?
    mask = vcgtq_f16(score3, old_score);

    phase = vbslq_f16(mask, three, phase);

    // Convert from float16x8_t to uint8x8_t
    return vmovn_u16(vcvtq_u16_f16(phase));
}

/** Computes the gradient phase if gradient_size = 3 or 5. The output is quantized.
 *         0 = 0°, 1 = 45°, 2 = 90°, 3 = 135°
 *
 * @param[in] gx Gx component
 * @param[in] gy Gy component
 *
 * @return quantized phase for 8 pixels
 */
inline uint8x8_t phase_quantization_S16_S16(int16x8_t gx, int16x8_t gy)
{
    return phase_quantization(vcvtq_f16_s16(gx), vcvtq_f16_s16(gy));
}

/** Computes the gradient phase if gradient_size = 7. The output is quantized.
 *         0 = 0°, 1 = 45°, 2 = 90°, 3 = 135°
 *
 * @param[in] gx Gx component
 * @param[in] gy Gy component
 *
 * @return quantized phase for 8 pixels
 */
inline uint8x8_t phase_quantization_S32_S32(const int32x4x2_t &gx, const int32x4x2_t &gy)
{
    // Convert to float
    const float32x4x2_t gx_f32 =
    {
        vcvtq_f32_s32(gx.val[0]),
        vcvtq_f32_s32(gx.val[1])
    };

    const float32x4x2_t gy_f32 =
    {
        vcvtq_f32_s32(gy.val[0]),
        vcvtq_f32_s32(gy.val[1])
    };

    return phase_quantization(gx_f32, gy_f32);
}

/** Computes the magnitude using the L1-norm type if gradient_size = 3 or 5
 *
 * @param[in] gx Gx component
 * @param[in] gy Gy component
 *
 * @return magnitude for 8 pixels
 */
inline uint16x8_t mag_l1_S16_S16(int16x8_t gx, int16x8_t gy)
{
    return vaddq_u16(vreinterpretq_u16_s16(vabsq_s16(gx)),
                     vreinterpretq_u16_s16(vabsq_s16(gy)));
}

/** Computes the magnitude using the L1-norm type if gradient_size = 7
 *
 * @param[in] gx Gx component
 * @param[in] gy Gy component
 *
 * @return magnitude for 8 pixels
 */
inline uint32x4x2_t mag_l1_S32_S32(const int32x4x2_t &gx, const int32x4x2_t &gy)
{
    const uint32x4x2_t gx_abs =
    {
        vreinterpretq_u32_s32(vabsq_s32(gx.val[0])),
        vreinterpretq_u32_s32(vabsq_s32(gx.val[1]))
    };

    const uint32x4x2_t gy_abs =
    {
        vreinterpretq_u32_s32(vabsq_s32(gy.val[0])),
        vreinterpretq_u32_s32(vabsq_s32(gy.val[1]))
    };

    const uint32x4x2_t out =
    {
        vaddq_u32(gx_abs.val[0], gy_abs.val[0]),
        vaddq_u32(gx_abs.val[1], gy_abs.val[1])
    };

    return out;
}

inline float32x4x2_t mag_l2(const float32x4x2_t &gx, const float32x4x2_t &gy)
{
    // x^2 ...
    float32x4x2_t mag =
    {
        vmulq_f32(gx.val[0], gx.val[0]),
        vmulq_f32(gx.val[1], gx.val[1])
    };

    // ... + y^2
    mag.val[0] = vmlaq_f32(mag.val[0], gy.val[0], gy.val[0]);
    mag.val[1] = vmlaq_f32(mag.val[1], gy.val[1], gy.val[1]);

    // sqrt(...)
    mag.val[0] = vmulq_f32(vrsqrteq_f32(mag.val[0]), mag.val[0]);
    mag.val[1] = vmulq_f32(vrsqrteq_f32(mag.val[1]), mag.val[1]);

    return mag;
}

inline float16x8_t mag_l2(float16x8_t gx, float16x8_t gy)
{
    // x^2 ...
    float16x8_t mag = vmulq_f16(gx, gx);

    // ... + y^2
    mag = vfmaq_f16(mag, gy, gy);

    // sqrt(...)
    mag = vmulq_f16(vrsqrteq_f16(mag), mag);

    return mag;
}

/** Computes the magnitude using L2-norm if gradient_size = 3 or 5
 *
 * @param[in] gx Gx component
 * @param[in] gy Gy component
 *
 * @return magnitude for 8 pixels
 */
inline uint16x8_t mag_l2_S16_S16(int16x8_t gx, int16x8_t gy)
{
    /* Compute magnitude using L2 normalization */
    const float16x8_t gx2 = vcvtq_f16_s16(gx);
    const float16x8_t gy2 = vcvtq_f16_s16(gy);
    const float16x8_t mag = mag_l2(gx2, gy2);

    /* Store magnitude - Convert to uint16x8 */
    return vcvtq_u16_f16(mag);
}

/** Computes the magnitude using L2-norm if gradient_size = 7
 *
 * @param[in] gx Gx component
 * @param[in] gy Gy component
 *
 * @return magnitude for 8 pixels
 */
inline uint32x4x2_t mag_l2_S32_S32(const int32x4x2_t &gx, const int32x4x2_t &gy)
{
    // Compute magnitude using L2 normalization
    float32x4x2_t gx2 =
    {
        vcvtq_f32_s32(gx.val[0]),
        vcvtq_f32_s32(gx.val[1])
    };

    float32x4x2_t gy2 =
    {
        vcvtq_f32_s32(gy.val[0]),
        vcvtq_f32_s32(gy.val[1])
    };

    const float32x4x2_t mag = mag_l2(gx2, gy2);
    const uint32x4x2_t  mag32 =
    {
        vcvtq_u32_f32(mag.val[0]),
        vcvtq_u32_f32(mag.val[1])
    };

    return mag32;
}

/** Gradient function used when the gradient size = 3 or 5 and when the norm_type = L1-norm
 *
 * @param[in]  in1_ptr  Pointer to source image. Gx image. Data type supported S16
 * @param[in]  in2_ptr  Pointer to source image. Gy image. Data type supported S16
 * @param[out] out1_ptr Pointer to destination image. Magnitude. Data type supported U16
 * @param[out] out2_ptr Pointer to destination image. Quantized phase. Data type supported U8
 */
void mag_phase_l1norm_S16_S16_U16_U8(const void *__restrict in1_ptr, const void *__restrict in2_ptr, void *__restrict out1_ptr, void *__restrict out2_ptr)
{
    const auto in1  = static_cast<const int16_t *__restrict>(in1_ptr);
    const auto in2  = static_cast<const int16_t *__restrict>(in2_ptr);
    const auto out1 = static_cast<uint16_t *__restrict>(out1_ptr);
    const auto out2 = static_cast<uint8_t *__restrict>(out2_ptr);

    const int16x8x4_t gx =
    {
        vld1q_s16(in1),
        vld1q_s16(in1 + 8),
        vld1q_s16(in1 + 16),
        vld1q_s16(in1 + 24)
    };

    const int16x8x4_t gy =
    {
        vld1q_s16(in2),
        vld1q_s16(in2 + 8),
        vld1q_s16(in2 + 16),
        vld1q_s16(in2 + 24)
    };

    // Compute and store phase
    vst1_u8(out2 + 0, phase_quantization_S16_S16(gx.val[0], gy.val[0]));
    vst1_u8(out2 + 8, phase_quantization_S16_S16(gx.val[1], gy.val[1]));
    vst1_u8(out2 + 16, phase_quantization_S16_S16(gx.val[2], gy.val[2]));
    vst1_u8(out2 + 24, phase_quantization_S16_S16(gx.val[3], gy.val[3]));

    // Compute ans store magnitude using L1 normalization
    vst1q_u16(out1 + 0, mag_l1_S16_S16(gx.val[0], gy.val[0]));
    vst1q_u16(out1 + 8, mag_l1_S16_S16(gx.val[1], gy.val[1]));
    vst1q_u16(out1 + 16, mag_l1_S16_S16(gx.val[2], gy.val[2]));
    vst1q_u16(out1 + 24, mag_l1_S16_S16(gx.val[3], gy.val[3]));
}

/** Gradient function used when the gradient size = 3 or 5 and when the norm_type = L2-norm
 *
 * @param[in]  in1_ptr  Pointer to source image. Gx image. Data type supported S16
 * @param[in]  in2_ptr  Pointer to source image. Gy image. Data type supported S16
 * @param[out] out1_ptr Pointer to destination image. Magnitude. Data type supported U16
 * @param[out] out2_ptr Pointer to destination image. Quantized phase. Data type supported U8
 */
void mag_phase_l2norm_S16_S16_U16_U8(const void *__restrict in1_ptr, const void *__restrict in2_ptr, void *__restrict out1_ptr, void *__restrict out2_ptr)
{
    const auto in1  = static_cast<const int16_t *__restrict>(in1_ptr);
    const auto in2  = static_cast<const int16_t *__restrict>(in2_ptr);
    const auto out1 = static_cast<uint16_t *__restrict>(out1_ptr);
    const auto out2 = static_cast<uint8_t *__restrict>(out2_ptr);

    const int16x8x4_t gx =
    {
        vld1q_s16(in1),
        vld1q_s16(in1 + 8),
        vld1q_s16(in1 + 16),
        vld1q_s16(in1 + 24)
    };

    const int16x8x4_t gy =
    {
        vld1q_s16(in2),
        vld1q_s16(in2 + 8),
        vld1q_s16(in2 + 16),
        vld1q_s16(in2 + 24)
    };

    // Compute and store phase
    vst1_u8(out2 + 0, phase_quantization_S16_S16(gx.val[0], gy.val[0]));
    vst1_u8(out2 + 8, phase_quantization_S16_S16(gx.val[1], gy.val[1]));
    vst1_u8(out2 + 16, phase_quantization_S16_S16(gx.val[2], gy.val[2]));
    vst1_u8(out2 + 24, phase_quantization_S16_S16(gx.val[3], gy.val[3]));

    // Compute and store magnitude using L2 normalization
    vst1q_u16(out1 + 0, mag_l2_S16_S16(gx.val[0], gy.val[0]));
    vst1q_u16(out1 + 8, mag_l2_S16_S16(gx.val[1], gy.val[1]));
    vst1q_u16(out1 + 16, mag_l2_S16_S16(gx.val[2], gy.val[2]));
    vst1q_u16(out1 + 24, mag_l2_S16_S16(gx.val[3], gy.val[3]));
}

/** Gradient function used when the gradient size = 7 and when the norm_type = L1-norm
 *
 * @param[in]  in1_ptr  Pointer to source image. Gx image. Data type supported S32
 * @param[in]  in2_ptr  Pointer to source image. Gy image. Data type supported S32
 * @param[out] out1_ptr Pointer to destination image. Magnitude. Data type supported U32
 * @param[out] out2_ptr Pointer to destination image. Quantized phase. Data type supported U8
 */
void mag_phase_l1norm_S32_S32_U32_U8(const void *__restrict in1_ptr, const void *__restrict in2_ptr, void *__restrict out1_ptr, void *__restrict out2_ptr)
{
    auto in1  = static_cast<const int32_t *__restrict>(in1_ptr);
    auto in2  = static_cast<const int32_t *__restrict>(in2_ptr);
    auto out1 = static_cast<uint32_t *__restrict>(out1_ptr);
    auto out2 = static_cast<uint8_t *__restrict>(out2_ptr);

    // Process low and high part
    for(size_t i = 0; i < 2; ++i, in1 += 16, in2 += 16, out1 += 16, out2 += 16)
    {
        const int32x4x2_t gx0 =
        {
            vld1q_s32(in1 + 0),
            vld1q_s32(in1 + 4)
        };

        const int32x4x2_t gx1 =
        {
            vld1q_s32(in1 + 8),
            vld1q_s32(in1 + 12)
        };

        const int32x4x2_t gy0 =
        {
            vld1q_s32(in2 + 0),
            vld1q_s32(in2 + 4)
        };

        const int32x4x2_t gy1 =
        {
            vld1q_s32(in2 + 8),
            vld1q_s32(in2 + 12)
        };

        // Compute and store phase
        vst1_u8(out2 + 0, phase_quantization_S32_S32(gx0, gy0));
        vst1_u8(out2 + 8, phase_quantization_S32_S32(gx1, gy1));

        // Compute magnitude using L1 normalization
        const uint32x4x2_t mag0 = mag_l1_S32_S32(gx0, gy0);
        const uint32x4x2_t mag1 = mag_l1_S32_S32(gx1, gy1);

        // Store magnitude
        vst1q_u32(out1 + 0, mag0.val[0]);
        vst1q_u32(out1 + 4, mag0.val[1]);
        vst1q_u32(out1 + 8, mag1.val[0]);
        vst1q_u32(out1 + 12, mag1.val[1]);
    }
}

/** Gradient function used when the gradient size = 7 and when the norm_type = L2-norm
 *
 * @param[in]  in1_ptr  Pointer to source image. Gx image. Data type supported S32
 * @param[in]  in2_ptr  Pointer to source image. Gy image. Data type supported S32
 * @param[out] out1_ptr Pointer to destination image. Magnitude. Data type supported U32
 * @param[out] out2_ptr Pointer to destination image. Quantized phase. Data type supported U8
 */
void mag_phase_l2norm_S32_S32_U32_U8(const void *__restrict in1_ptr, const void *__restrict in2_ptr, void *__restrict out1_ptr, void *__restrict out2_ptr)
{
    auto in1  = static_cast<const int32_t *__restrict>(in1_ptr);
    auto in2  = static_cast<const int32_t *__restrict>(in2_ptr);
    auto out1 = static_cast<uint32_t *__restrict>(out1_ptr);
    auto out2 = static_cast<uint8_t *__restrict>(out2_ptr);

    // Process low and high part
    for(size_t i = 0; i < 2; ++i, in1 += 16, in2 += 16, out1 += 16, out2 += 16)
    {
        const int32x4x2_t gx0 =
        {
            vld1q_s32(in1 + 0),
            vld1q_s32(in1 + 4)
        };

        const int32x4x2_t gx1 =
        {
            vld1q_s32(in1 + 8),
            vld1q_s32(in1 + 12)
        };

        const int32x4x2_t gy0 =
        {
            vld1q_s32(in2 + 0),
            vld1q_s32(in2 + 4)
        };

        const int32x4x2_t gy1 =
        {
            vld1q_s32(in2 + 8),
            vld1q_s32(in2 + 12)
        };

        // Compute and store phase
        vst1_u8(out2 + 0, phase_quantization_S32_S32(gx0, gy0));
        vst1_u8(out2 + 8, phase_quantization_S32_S32(gx1, gy1));

        // Compute magnitude using L2 normalization
        const uint32x4x2_t mag0 = mag_l2_S32_S32(gx0, gy0);
        const uint32x4x2_t mag1 = mag_l2_S32_S32(gx1, gy1);

        // Store magnitude
        vst1q_u32(out1 + 0, mag0.val[0]);
        vst1q_u32(out1 + 4, mag0.val[1]);
        vst1q_u32(out1 + 8, mag1.val[0]);
        vst1q_u32(out1 + 12, mag1.val[1]);
    }
}

inline uint16x4_t non_max_U32_helper(const uint32_t *in, const uint16x4_t pc, const uint32_t stride_mag, const int32_t lower_thr, const int32_t upper_thr)
{
    // Phase for 4 pixel
    const uint32x4_t pc32 = vmovl_u16(pc);

    // Get magnitude for 4 pixel
    uint32x4_t mc = vld1q_u32(in);

    // Angle_quantized: 0 = 0°, 1 = 45°, 2 = 90°, 3 = 135°
    // 0 degree
    const uint32x4_t mk0_0 = vld1q_u32(in - 1);
    const uint32x4_t mk0_1 = vld1q_u32(in + 1);
    uint32x4_t       mask0 = vceqq_u32(pc32, vdupq_n_u32(0));
    mask0                  = vandq_u32(mask0, vcgeq_u32(mc, mk0_0));
    mask0                  = vandq_u32(mask0, vcgeq_u32(mc, mk0_1));

    // 45 degree
    const uint32x4_t mk45_0 = vld1q_u32(in - stride_mag - 1);
    const uint32x4_t mk45_1 = vld1q_u32(in + stride_mag + 1);
    uint32x4_t       mask1  = vceqq_u32(pc32, vdupq_n_u32(1));
    mask1                   = vandq_u32(mask1, vcgeq_u32(mc, mk45_0));
    mask1                   = vandq_u32(mask1, vcgeq_u32(mc, mk45_1));

    // 90 degree
    const uint32x4_t mk90_0 = vld1q_u32(in - stride_mag);
    const uint32x4_t mk90_1 = vld1q_u32(in + stride_mag);
    uint32x4_t       mask2  = vceqq_u32(pc32, vdupq_n_u32(2));
    mask2                   = vandq_u32(mask2, vcgeq_u32(mc, mk90_0));
    mask2                   = vandq_u32(mask2, vcgeq_u32(mc, mk90_1));

    // 135 degree
    const uint32x4_t mk135_0 = vld1q_u32(in - stride_mag + 1);
    const uint32x4_t mk135_1 = vld1q_u32(in + stride_mag - 1);
    uint32x4_t       mask3   = vceqq_u32(pc32, vdupq_n_u32(3));
    mask3                    = vandq_u32(mask3, vcgeq_u32(mc, mk135_0));
    mask3                    = vandq_u32(mask3, vcgeq_u32(mc, mk135_1));

    // Merge masks
    mask0 = vorrq_u32(mask0, mask1);
    mask2 = vorrq_u32(mask2, mask3);
    mask0 = vorrq_u32(mask0, mask2);

    mc = vbslq_u32(mask0, mc, vdupq_n_u32(0));

    // mc > upper_thr
    mask0 = vcgtq_u32(mc, vdupq_n_u32(upper_thr));

    // mc <= lower_thr
    mask1 = vcleq_u32(mc, vdupq_n_u32(lower_thr));

    // mc <= upper_thr && mc > lower_thr
    mask2 = vcleq_u32(mc, vdupq_n_u32(upper_thr));
    mask2 = vandq_u32(mask2, vcgtq_u32(mc, vdupq_n_u32(lower_thr)));

    mc = vbslq_u32(mask0, vdupq_n_u32(EDGE), mc);
    mc = vbslq_u32(mask1, vdupq_n_u32(NO_EDGE), mc);
    mc = vbslq_u32(mask2, vdupq_n_u32(MAYBE), mc);

    return vmovn_u32(mc);
}

/** Computes edge tracing when is called by edge_trace_U8_U8 recursively
 *
 * @param[in]  in         Pointer to source image. Data type supported U8
 * @param[out] out        Pointer to destination image. Data type supported U8
 * @param[in]  in_stride  Stride of the input image
 * @param[in]  out_stride Stride of the output image
 */
void edge_trace_recursive_U8_U8(uint8_t *__restrict in, uint8_t *__restrict out, const int32_t in_stride, const int32_t out_stride)
{
    // Look for MAYBE pixels in 8 directions
    *out = EDGE;

    // (-1, 0)
    uint8_t pixel = *(in - 1);

    if(pixel == MAYBE)
    {
        // Touched a MAYBE point. MAYBE becomes EDGE
        *(in - 1) = EDGE;

        edge_trace_recursive_U8_U8(in - 1, out - 1, in_stride, out_stride);
    }

    // (+1, 0)
    pixel = *(in + 1);

    if(pixel == MAYBE)
    {
        // Touched a MAYBE point. MAYBE becomes EDGE
        *(in + 1) = EDGE;

        edge_trace_recursive_U8_U8(in + 1, out + 1, in_stride, out_stride);
    }

    in -= in_stride;
    out -= out_stride;

    // (-1, -1)
    pixel = *(in - 1);

    if(pixel == MAYBE)
    {
        // Touched a MAYBE point. MAYBE becomes EDGE
        *(in - 1) = EDGE;

        edge_trace_recursive_U8_U8(in - 1, out - 1, in_stride, out_stride);
    }

    // (0, -1)
    pixel = *in;

    if(pixel == MAYBE)
    {
        // Touched a MAYBE point. MAYBE becomes EDGE
        *in = EDGE;

        edge_trace_recursive_U8_U8(in, out, in_stride, out_stride);
    }

    // (+1, -1)
    pixel = *(in + 1);

    if(pixel == MAYBE)
    {
        // Touched a MAYBE point. MAYBE becomes EDGE
        *(in + 1) = EDGE;

        edge_trace_recursive_U8_U8(in + 1, out + 1, in_stride, out_stride);
    }

    in += in_stride * 2;
    out += out_stride * 2;

    // (-1, +1)
    pixel = *(in - 1);

    if(pixel == MAYBE)
    {
        // Touched a MAYBE point. MAYBE becomes EDGE
        *(in - 1) = EDGE;

        edge_trace_recursive_U8_U8(in - 1, out - 1, in_stride, out_stride);
    }

    // (0, +1)
    pixel = *in;

    if(pixel == MAYBE)
    {
        // Touched a MAYBE point. MAYBE becomes EDGE
        *in = EDGE;

        edge_trace_recursive_U8_U8(in, out, in_stride, out_stride);
    }

    // (+1, +1)
    pixel = *(in + 1);

    if(pixel == MAYBE)
    {
        // Touched a MAYBE point. MAYBE becomes EDGE
        *(in + 1) = EDGE;

        edge_trace_recursive_U8_U8(in + 1, out + 1, in_stride, out_stride);
    }
}
} // namespace fp16

void NEGradientFP16Kernel::configure(const ITensor *gx, const ITensor *gy, ITensor *magnitude, ITensor *phase, int32_t norm_type)
{
    ARM_COMPUTE_ERROR_ON_NULLPTR(gx, gy, magnitude, phase);

    set_shape_if_empty(*magnitude->info(), gx->info()->tensor_shape());
    set_shape_if_empty(*phase->info(), gx->info()->tensor_shape());

    Format magnitude_format = gx->info()->data_type() == DataType::S16 ? Format::U16 : Format::U32;
    set_format_if_unknown(*magnitude->info(), magnitude_format);
    set_format_if_unknown(*phase->info(), Format::U8);

    ARM_COMPUTE_ERROR_ON_MISMATCHING_SHAPES(gx, gy, magnitude, phase);
    ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(gx, 1, DataType::S16, DataType::S32);
    ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(gy, 1, DataType::S16, DataType::S32);
    ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(magnitude, 1, DataType::U16, DataType::U32);
    ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(phase, 1, DataType::U8);
    ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(gx, gy);
    ARM_COMPUTE_ERROR_ON_MSG(element_size_from_data_type(gx->info()->data_type()) != element_size_from_data_type(magnitude->info()->data_type()), "Magnitude must have the same element size as Gx and Gy");

    _gx        = gx;
    _gy        = gy;
    _magnitude = magnitude;
    _phase     = phase;

    if(_gx->info()->data_type() == DataType::S16)
    {
        if(norm_type == 1)
        {
            _func = &fp16::mag_phase_l1norm_S16_S16_U16_U8;
        }
        else
        {
            _func = &fp16::mag_phase_l2norm_S16_S16_U16_U8;
        }
    }
    else
    {
        if(norm_type == 1)
        {
            _func = &fp16::mag_phase_l1norm_S32_S32_U32_U8;
        }
        else
        {
            _func = &fp16::mag_phase_l2norm_S32_S32_U32_U8;
        }
    }

    constexpr unsigned int num_elems_processed_per_iteration = 32;

    // Configure kernel window
    Window win = calculate_max_window(*_gx->info(), Steps(num_elems_processed_per_iteration));

    AccessWindowHorizontal gx_access(_gx->info(), 0, num_elems_processed_per_iteration);
    AccessWindowHorizontal gy_access(_gy->info(), 0, num_elems_processed_per_iteration);
    AccessWindowHorizontal mag_access(_magnitude->info(), 0, num_elems_processed_per_iteration);
    AccessWindowHorizontal phase_access(_phase->info(), 0, num_elems_processed_per_iteration);

    update_window_and_padding(win, gx_access, gy_access, mag_access, phase_access);

    mag_access.set_valid_region(win, _gx->info()->valid_region());
    phase_access.set_valid_region(win, _gx->info()->valid_region());

    INEKernel::configure(win);
}
#endif

namespace
{
inline uint8x8_t phase_quantization(const float32x4x2_t &gx, const float32x4x2_t &gy)
{
    // Constant use for evaluating score1 and score3
    static const float32x4_t const45 = vdupq_n_f32(0.70710678118655f);
    static const float32x4_t zero    = vdupq_n_f32(0.0f);
    static const float32x4_t one     = vdupq_n_f32(1.0f);
    static const float32x4_t two     = vdupq_n_f32(2.0f);
    static const float32x4_t three   = vdupq_n_f32(3.0f);

    // Score0: (1, 0)
    const float32x4x2_t score0 =
    {
        {
            vabsq_f32(gx.val[0]),
            vabsq_f32(gx.val[1])
        }
    };

    // Score2: ( 0, 1 )
    const float32x4x2_t score2 =
    {
        {
            vabsq_f32(gy.val[0]),
            vabsq_f32(gy.val[1])
        }
    };

    // Score1 and Score3: ( sqrt(2) / 2, sqrt(2) / 2 ) - ( -sqrt(2) / 2, sqrt(2) / 2 )
    float32x4x2_t score1 =
    {
        {
            vmulq_f32(gy.val[0], const45),
            vmulq_f32(gy.val[1], const45)
        }
    };

    float32x4x2_t score3 = score1;

    score1.val[0] = vmlaq_f32(score1.val[0], gx.val[0], const45);
    score1.val[1] = vmlaq_f32(score1.val[1], gx.val[1], const45);
    score3.val[0] = vmlsq_f32(score3.val[0], gx.val[0], const45);
    score3.val[1] = vmlsq_f32(score3.val[1], gx.val[1], const45);

    score1.val[0] = vabsq_f32(score1.val[0]);
    score1.val[1] = vabsq_f32(score1.val[1]);
    score3.val[0] = vabsq_f32(score3.val[0]);
    score3.val[1] = vabsq_f32(score3.val[1]);

    float32x4x2_t phase =
    {
        {
            zero,
            zero
        }
    };

    float32x4x2_t old_score = score0;

    // score1 > old_score?
    uint32x4x2_t mask =
    {
        {
            vcgtq_f32(score1.val[0], old_score.val[0]),
            vcgtq_f32(score1.val[1], old_score.val[1])
        }
    };

    phase.val[0]     = vbslq_f32(mask.val[0], one, phase.val[0]);
    phase.val[1]     = vbslq_f32(mask.val[1], one, phase.val[1]);
    old_score.val[0] = vbslq_f32(mask.val[0], score1.val[0], old_score.val[0]);
    old_score.val[1] = vbslq_f32(mask.val[1], score1.val[1], old_score.val[1]);

    // score2 > old_score?
    mask.val[0] = vcgtq_f32(score2.val[0], old_score.val[0]);
    mask.val[1] = vcgtq_f32(score2.val[1], old_score.val[1]);

    phase.val[0]     = vbslq_f32(mask.val[0], two, phase.val[0]);
    phase.val[1]     = vbslq_f32(mask.val[1], two, phase.val[1]);
    old_score.val[0] = vbslq_f32(mask.val[0], score2.val[0], old_score.val[0]);
    old_score.val[1] = vbslq_f32(mask.val[1], score2.val[1], old_score.val[1]);

    // score3 > old_score?
    mask.val[0] = vcgtq_f32(score3.val[0], old_score.val[0]);
    mask.val[1] = vcgtq_f32(score3.val[1], old_score.val[1]);

    phase.val[0]     = vbslq_f32(mask.val[0], three, phase.val[0]);
    phase.val[1]     = vbslq_f32(mask.val[1], three, phase.val[1]);
    old_score.val[0] = vbslq_f32(mask.val[0], score3.val[0], old_score.val[0]);
    old_score.val[1] = vbslq_f32(mask.val[1], score3.val[1], old_score.val[1]);

    // Convert from float32x4_t to uint8x8_t
    return vmovn_u16(vcombine_u16(vmovn_u32(vcvtq_u32_f32(phase.val[0])),
                                  vmovn_u32(vcvtq_u32_f32(phase.val[1]))));
}

/* Computes the gradient phase if gradient_size = 3 or 5. The output is quantized.
 * 0 = 0°, 1 = 45°, 2 = 90°, 3 = 135°
 *
 * @param[in] gx Gx component
 * @param[in] gy Gy component
 *
 * @return quantized phase for 8 pixels
 */
inline uint8x8_t phase_quantization_S16_S16(int16x8_t gx, int16x8_t gy)
{
    // Convert to float
    const float32x4x2_t gx_f32 =
    {
        {
            vcvtq_f32_s32(vmovl_s16(vget_low_s16(gx))),
            vcvtq_f32_s32(vmovl_s16(vget_high_s16(gx)))
        }
    };

    const float32x4x2_t gy_f32 =
    {
        {
            vcvtq_f32_s32(vmovl_s16(vget_low_s16(gy))),
            vcvtq_f32_s32(vmovl_s16(vget_high_s16(gy)))
        }
    };

    return phase_quantization(gx_f32, gy_f32);
}

/* Computes the gradient phase if gradient_size = 7. The output is quantized.
 * 0 = 0°, 1 = 45°, 2 = 90°, 3 = 135°
 *
 * @param[in] gx Gx component
 * @param[in] gy Gy component
 *
 * @return quantized phase for 8 pixels
 */
inline uint8x8_t phase_quantization_S32_S32(const int32x4x2_t &gx, const int32x4x2_t &gy)
{
    // Convert to float
    const float32x4x2_t gx_f32 =
    {
        {
            vcvtq_f32_s32(gx.val[0]),
            vcvtq_f32_s32(gx.val[1])
        }
    };

    const float32x4x2_t gy_f32 =
    {
        {
            vcvtq_f32_s32(gy.val[0]),
            vcvtq_f32_s32(gy.val[1])
        }
    };

    return phase_quantization(gx_f32, gy_f32);
}

/* Computes the magnitude using the L1-norm type if gradient_size = 3 or 5
 *
 * @param[in] gx Gx component
 * @param[in] gy Gy component
 *
 * @return magnitude for 8 pixels
 */
inline uint16x8_t mag_l1_S16_S16(int16x8_t gx, int16x8_t gy)
{
    return vaddq_u16(vreinterpretq_u16_s16(vabsq_s16(gx)),
                     vreinterpretq_u16_s16(vabsq_s16(gy)));
}

/* Computes the magnitude using the L1-norm type if gradient_size = 7
 *
 * @param[in] gx Gx component
 * @param[in] gy Gy component
 *
 * @return magnitude for 8 pixels
 */
inline uint32x4x2_t mag_l1_S32_S32(const int32x4x2_t &gx, const int32x4x2_t &gy)
{
    const uint32x4x2_t gx_abs =
    {
        {
            vreinterpretq_u32_s32(vabsq_s32(gx.val[0])),
            vreinterpretq_u32_s32(vabsq_s32(gx.val[1]))
        }
    };

    const uint32x4x2_t gy_abs =
    {
        {
            vreinterpretq_u32_s32(vabsq_s32(gy.val[0])),
            vreinterpretq_u32_s32(vabsq_s32(gy.val[1]))
        }
    };

    const uint32x4x2_t output =
    {
        {
            vaddq_u32(gx_abs.val[0], gy_abs.val[0]),
            vaddq_u32(gx_abs.val[1], gy_abs.val[1])
        }
    };

    return output;
}

inline float32x4x2_t mag_l2(const float32x4x2_t &gx, const float32x4x2_t &gy)
{
    // x^2 ...
    float32x4x2_t magnitude =
    {
        {
            vmulq_f32(gx.val[0], gx.val[0]),
            vmulq_f32(gx.val[1], gx.val[1])
        }
    };

    // ... + y^2
    magnitude.val[0] = vmlaq_f32(magnitude.val[0], gy.val[0], gy.val[0]);
    magnitude.val[1] = vmlaq_f32(magnitude.val[1], gy.val[1], gy.val[1]);

    // sqrt(...)
    magnitude.val[0] = vmulq_f32(vrsqrteq_f32(magnitude.val[0]), magnitude.val[0]);
    magnitude.val[1] = vmulq_f32(vrsqrteq_f32(magnitude.val[1]), magnitude.val[1]);

    return magnitude;
}

/* Computes the magnitude using L2-norm if gradient_size = 3 or 5
 *
 * @param[in] gx Gx component
 * @param[in] gy Gy component
 *
 * @return magnitude for 8 pixels
 */
inline uint16x8_t mag_l2_S16_S16(int16x8_t gx, int16x8_t gy)
{
    // Compute magnitude using L2 normalization
    const float32x4x2_t gx2 =
    {
        {
            vcvtq_f32_s32(vmovl_s16(vget_low_s16(gx))),
            vcvtq_f32_s32(vmovl_s16(vget_high_s16(gx)))
        }
    };

    const float32x4x2_t gy2 =
    {
        {
            vcvtq_f32_s32(vmovl_s16(vget_low_s16(gy))),
            vcvtq_f32_s32(vmovl_s16(vget_high_s16(gy)))
        }
    };

    const float32x4x2_t magnitude = mag_l2(gx2, gy2);

    // Store magnitude - Convert to uint16x8
    return vcombine_u16(vmovn_u32(vcvtq_u32_f32(magnitude.val[0])),
                        vmovn_u32(vcvtq_u32_f32(magnitude.val[1])));
}

/* Computes the magnitude using L2-norm if gradient_size = 7
 *
 * @param[in] gx Gx component
 * @param[in] gy Gy component
 *
 * @return magnitude for 8 pixels
 */
inline uint32x4x2_t mag_l2_S32_S32(const int32x4x2_t &gx, const int32x4x2_t &gy)
{
    // Compute magnitude using L2 normalization
    float32x4x2_t gx2 =
    {
        {
            vcvtq_f32_s32(gx.val[0]),
            vcvtq_f32_s32(gx.val[1])
        }
    };

    float32x4x2_t gy2 =
    {
        {
            vcvtq_f32_s32(gy.val[0]),
            vcvtq_f32_s32(gy.val[1])
        }
    };

    const float32x4x2_t magnitude = mag_l2(gx2, gy2);
    const uint32x4x2_t  mag32 =
    {
        {
            vcvtq_u32_f32(magnitude.val[0]),
            vcvtq_u32_f32(magnitude.val[1])
        }
    };

    return mag32;
}

/* Gradient function used when the gradient size = 3 or 5 and when the norm_type = L1-norm
 *
 * @param[in]  gx_ptr        Pointer to source image. Gx image. Data type supported S16
 * @param[in]  gy_ptr        Pointer to source image. Gy image. Data type supported S16
 * @param[out] magnitude_ptr Pointer to destination image. Magnitude. Data type supported U16
 * @param[out] phase_ptr     Pointer to destination image. Quantized phase. Data type supported U8
 */
void mag_phase_l1norm_S16_S16_U16_U8(const void *__restrict gx_ptr, const void *__restrict gy_ptr, void *__restrict magnitude_ptr, void *__restrict phase_ptr)
{
    const auto gx        = static_cast<const int16_t *__restrict>(gx_ptr);
    const auto gy        = static_cast<const int16_t *__restrict>(gy_ptr);
    const auto magnitude = static_cast<uint16_t *__restrict>(magnitude_ptr);
    const auto phase     = static_cast<uint8_t *__restrict>(phase_ptr);

    const int16x8x4_t gx_val =
    {
        {
            vld1q_s16(gx),
            vld1q_s16(gx + 8),
            vld1q_s16(gx + 16),
            vld1q_s16(gx + 24)
        }
    };

    const int16x8x4_t gy_val =
    {
        {
            vld1q_s16(gy),
            vld1q_s16(gy + 8),
            vld1q_s16(gy + 16),
            vld1q_s16(gy + 24)
        }
    };

    // Compute and store phase
    vst1_u8(phase + 0, phase_quantization_S16_S16(gx_val.val[0], gy_val.val[0]));
    vst1_u8(phase + 8, phase_quantization_S16_S16(gx_val.val[1], gy_val.val[1]));
    vst1_u8(phase + 16, phase_quantization_S16_S16(gx_val.val[2], gy_val.val[2]));
    vst1_u8(phase + 24, phase_quantization_S16_S16(gx_val.val[3], gy_val.val[3]));

    // Compute ans store magnitude using L1 normalization
    vst1q_u16(magnitude + 0, mag_l1_S16_S16(gx_val.val[0], gy_val.val[0]));
    vst1q_u16(magnitude + 8, mag_l1_S16_S16(gx_val.val[1], gy_val.val[1]));
    vst1q_u16(magnitude + 16, mag_l1_S16_S16(gx_val.val[2], gy_val.val[2]));
    vst1q_u16(magnitude + 24, mag_l1_S16_S16(gx_val.val[3], gy_val.val[3]));
}

/* Gradient function used when the gradient size = 3 or 5 and when the norm_type = L2-norm
 *
 * @param[in]  gx_ptr        Pointer to source image. Gx image. Data type supported S16
 * @param[in]  gy_ptr        Pointer to source image. Gy image. Data type supported S16
 * @param[out] magnitude_ptr Pointer to destination image. Magnitude. Data type supported U16
 * @param[out] phase_ptr     Pointer to destination image. Quantized phase. Data type supported U8
 */
void mag_phase_l2norm_S16_S16_U16_U8(const void *__restrict gx_ptr, const void *__restrict gy_ptr, void *__restrict magnitude_ptr, void *__restrict phase_ptr)
{
    const auto gx        = static_cast<const int16_t *__restrict>(gx_ptr);
    const auto gy        = static_cast<const int16_t *__restrict>(gy_ptr);
    const auto magnitude = static_cast<uint16_t *__restrict>(magnitude_ptr);
    const auto phase     = static_cast<uint8_t *__restrict>(phase_ptr);

    const int16x8x4_t gx_val =
    {
        {
            vld1q_s16(gx),
            vld1q_s16(gx + 8),
            vld1q_s16(gx + 16),
            vld1q_s16(gx + 24)
        }
    };

    const int16x8x4_t gy_val =
    {
        {
            vld1q_s16(gy),
            vld1q_s16(gy + 8),
            vld1q_s16(gy + 16),
            vld1q_s16(gy + 24)
        }
    };

    // Compute and store phase
    vst1_u8(phase + 0, phase_quantization_S16_S16(gx_val.val[0], gy_val.val[0]));
    vst1_u8(phase + 8, phase_quantization_S16_S16(gx_val.val[1], gy_val.val[1]));
    vst1_u8(phase + 16, phase_quantization_S16_S16(gx_val.val[2], gy_val.val[2]));
    vst1_u8(phase + 24, phase_quantization_S16_S16(gx_val.val[3], gy_val.val[3]));

    // Compute and store magnitude using L2 normalization
    vst1q_u16(magnitude + 0, mag_l2_S16_S16(gx_val.val[0], gy_val.val[0]));
    vst1q_u16(magnitude + 8, mag_l2_S16_S16(gx_val.val[1], gy_val.val[1]));
    vst1q_u16(magnitude + 16, mag_l2_S16_S16(gx_val.val[2], gy_val.val[2]));
    vst1q_u16(magnitude + 24, mag_l2_S16_S16(gx_val.val[3], gy_val.val[3]));
}

/* Gradient function used when the gradient size = 7 and when the norm_type = L1-norm
 *
 * @param[in]  gx_ptr        Pointer to source image. Gx image. Data type supported S32
 * @param[in]  gy_ptr        Pointer to source image. Gy image. Data type supported S32
 * @param[out] magnitude_ptr Pointer to destination image. Magnitude. Data type supported U32
 * @param[out] phase_ptr     Pointer to destination image. Quantized phase. Data type support U8
 */
void mag_phase_l1norm_S32_S32_U32_U8(const void *__restrict gx_ptr, const void *__restrict gy_ptr, void *__restrict magnitude_ptr, void *__restrict phase_ptr)
{
    auto gx        = static_cast<const int32_t *__restrict>(gx_ptr);
    auto gy        = static_cast<const int32_t *__restrict>(gy_ptr);
    auto magnitude = static_cast<uint32_t *__restrict>(magnitude_ptr);
    auto phase     = static_cast<uint8_t *__restrict>(phase_ptr);

    // Process low and high part
    for(size_t i = 0; i < 2; ++i, gx += 16, gy += 16, magnitude += 16, phase += 16)
    {
        const int32x4x2_t gx0 =
        {
            {
                vld1q_s32(gx + 0),
                vld1q_s32(gx + 4)
            }
        };

        const int32x4x2_t gx1 =
        {
            {
                vld1q_s32(gx + 8),
                vld1q_s32(gx + 12)
            }
        };

        const int32x4x2_t gy0 =
        {
            {
                vld1q_s32(gy + 0),
                vld1q_s32(gy + 4)
            }
        };

        const int32x4x2_t gy1 =
        {
            {
                vld1q_s32(gy + 8),
                vld1q_s32(gy + 12)
            }
        };

        // Compute and store phase
        vst1_u8(phase + 0, phase_quantization_S32_S32(gx0, gy0));
        vst1_u8(phase + 8, phase_quantization_S32_S32(gx1, gy1));

        // Compute magnitude using L1 normalization
        const uint32x4x2_t mag0 = mag_l1_S32_S32(gx0, gy0);
        const uint32x4x2_t mag1 = mag_l1_S32_S32(gx1, gy1);

        // Store magnitude
        vst1q_u32(magnitude + 0, mag0.val[0]);
        vst1q_u32(magnitude + 4, mag0.val[1]);
        vst1q_u32(magnitude + 8, mag1.val[0]);
        vst1q_u32(magnitude + 12, mag1.val[1]);
    }
}

/* Gradient function used when the gradient size = 7 and when the norm_type = L2-norm
 *
 * @param[in]  gx_ptr        Pointer to source image. Gx image. Data type supported S32
 * @param[in]  gy_ptr        Pointer to source image. Gy image. Data type supported S32
 * @param[out] magnitude_ptr Pointer to destination image. Magnitude. Data type supported U32
 * @param[out] phase_ptr     Pointer to destination image. Quantized phase. Data type supported U8
 */
void mag_phase_l2norm_S32_S32_U32_U8(const void *__restrict gx_ptr, const void *__restrict gy_ptr, void *__restrict magnitude_ptr, void *__restrict phase_ptr)
{
    auto gx        = static_cast<const int32_t *__restrict>(gx_ptr);
    auto gy        = static_cast<const int32_t *__restrict>(gy_ptr);
    auto magnitude = static_cast<uint32_t *__restrict>(magnitude_ptr);
    auto phase     = static_cast<uint8_t *__restrict>(phase_ptr);

    // Process low and high part
    for(size_t i = 0; i < 2; ++i, gx += 16, gy += 16, magnitude += 16, phase += 16)
    {
        const int32x4x2_t gx0 =
        {
            {
                vld1q_s32(gx + 0),
                vld1q_s32(gx + 4)
            }
        };

        const int32x4x2_t gx1 =
        {
            {
                vld1q_s32(gx + 8),
                vld1q_s32(gx + 12)
            }
        };

        const int32x4x2_t gy0 =
        {
            {
                vld1q_s32(gy + 0),
                vld1q_s32(gy + 4)
            }
        };

        const int32x4x2_t gy1 =
        {
            {
                vld1q_s32(gy + 8),
                vld1q_s32(gy + 12)
            }
        };

        // Compute and store phase
        vst1_u8(phase + 0, phase_quantization_S32_S32(gx0, gy0));
        vst1_u8(phase + 8, phase_quantization_S32_S32(gx1, gy1));

        // Compute magnitude using L2 normalization
        const uint32x4x2_t mag0 = mag_l2_S32_S32(gx0, gy0);
        const uint32x4x2_t mag1 = mag_l2_S32_S32(gx1, gy1);

        // Store magnitude
        vst1q_u32(magnitude + 0, mag0.val[0]);
        vst1q_u32(magnitude + 4, mag0.val[1]);
        vst1q_u32(magnitude + 8, mag1.val[0]);
        vst1q_u32(magnitude + 12, mag1.val[1]);
    }
}

/* Computes non-maxima suppression and hysteresis when the gradient size = 3 or 5
 *
 * @param[in]  magnitude_ptr Pointer to source image. Magnitude. Data type supported U16
 * @param[in]  phase_ptr     Pointer to source image. Quantized phase. Data type supported U8
 * @param[out] output_ptr    Pointer to output image. Data type supported U8
 * @param[in]  stride_mag    Stride of magnitude image
 * @param[in]  lower_thr     Lower threshold used for the hysteresis
 * @param[in]  upper_thr     Upper threshold used for the hysteresis
 */
void non_max_suppression_U16_U8_U8(const void *__restrict magnitude_ptr, const void *__restrict phase_ptr, void *__restrict output_ptr, const uint32_t stride_mag, const int32_t lower_thr,
                                   const int32_t upper_thr)
{
    const auto magnitude = static_cast<const uint16_t *__restrict>(magnitude_ptr);
    const auto phase     = static_cast<const uint8_t *__restrict>(phase_ptr);
    const auto output    = static_cast<uint8_t *__restrict>(output_ptr);

    // Get magnitude and phase of the centre pixels
    uint16x8_t mc = vld1q_u16(magnitude);

    // Angle_quantized: 0 = 0°, 1 = 45°, 2 = 90°, 3 = 135°
    const uint16x8_t pc16 = vmovl_u8(vld1_u8(phase));

    // 0 degree
    const uint16x8_t mk0_0 = vld1q_u16(magnitude - 1);
    const uint16x8_t mk0_1 = vld1q_u16(magnitude + 1);
    uint16x8_t       mask0 = vceqq_u16(pc16, vdupq_n_u16(0));
    mask0                  = vandq_u16(mask0, vcgeq_u16(mc, mk0_0));
    mask0                  = vandq_u16(mask0, vcgeq_u16(mc, mk0_1));

    // 45 degree
    const uint16x8_t mk45_0 = vld1q_u16(magnitude - stride_mag - 1);
    const uint16x8_t mk45_1 = vld1q_u16(magnitude + stride_mag + 1);
    uint16x8_t       mask1  = vceqq_u16(pc16, vdupq_n_u16(1));
    mask1                   = vandq_u16(mask1, vcgeq_u16(mc, mk45_0));
    mask1                   = vandq_u16(mask1, vcgeq_u16(mc, mk45_1));

    // 90 degree
    const uint16x8_t mk90_0 = vld1q_u16(magnitude - stride_mag);
    const uint16x8_t mk90_1 = vld1q_u16(magnitude + stride_mag);
    uint16x8_t       mask2  = vceqq_u16(pc16, vdupq_n_u16(2));
    mask2                   = vandq_u16(mask2, vcgeq_u16(mc, mk90_0));
    mask2                   = vandq_u16(mask2, vcgeq_u16(mc, mk90_1));

    // 135 degree
    const uint16x8_t mk135_0 = vld1q_u16(magnitude - stride_mag + 1);
    const uint16x8_t mk135_1 = vld1q_u16(magnitude + stride_mag - 1);
    uint16x8_t       mask3   = vceqq_u16(pc16, vdupq_n_u16(3));
    mask3                    = vandq_u16(mask3, vcgeq_u16(mc, mk135_0));
    mask3                    = vandq_u16(mask3, vcgeq_u16(mc, mk135_1));

    // Merge masks
    mask0 = vorrq_u16(mask0, mask1);
    mask2 = vorrq_u16(mask2, mask3);
    mask0 = vorrq_u16(mask0, mask2);

    mc = vbslq_u16(mask0, mc, vdupq_n_u16(0));

    // mc > upper_thr
    mask0 = vcgtq_u16(mc, vdupq_n_u16(upper_thr));

    // mc <= lower_thr
    mask1 = vcleq_u16(mc, vdupq_n_u16(lower_thr));

    // mc <= upper_thr && mc > lower_thr
    mask2 = vcleq_u16(mc, vdupq_n_u16(upper_thr));
    mask2 = vandq_u16(mask2, vcgtq_u16(mc, vdupq_n_u16(lower_thr)));

    mc = vbslq_u16(mask0, vdupq_n_u16(EDGE), mc);
    mc = vbslq_u16(mask1, vdupq_n_u16(NO_EDGE), mc);
    mc = vbslq_u16(mask2, vdupq_n_u16(MAYBE), mc);

    vst1_u8(output, vmovn_u16(mc));
}

inline uint16x4_t non_max_U32_helper(const uint32_t *input, const uint16x4_t pc, const uint32_t stride_mag, const int32_t lower_thr, const int32_t upper_thr)
{
    // Phase for 4 pixel
    const uint32x4_t pc32 = vmovl_u16(pc);

    // Get magnitude for 4 pixel
    uint32x4_t mc = vld1q_u32(input);

    // Angle_quantized: 0 = 0°, 1 = 45°, 2 = 90°, 3 = 135°
    // 0 degree
    const uint32x4_t mk0_0 = vld1q_u32(input - 1);
    const uint32x4_t mk0_1 = vld1q_u32(input + 1);
    uint32x4_t       mask0 = vceqq_u32(pc32, vdupq_n_u32(0));
    mask0                  = vandq_u32(mask0, vcgeq_u32(mc, mk0_0));
    mask0                  = vandq_u32(mask0, vcgeq_u32(mc, mk0_1));

    // 45 degree
    const uint32x4_t mk45_0 = vld1q_u32(input - stride_mag - 1);
    const uint32x4_t mk45_1 = vld1q_u32(input + stride_mag + 1);
    uint32x4_t       mask1  = vceqq_u32(pc32, vdupq_n_u32(1));
    mask1                   = vandq_u32(mask1, vcgeq_u32(mc, mk45_0));
    mask1                   = vandq_u32(mask1, vcgeq_u32(mc, mk45_1));

    // 90 degree
    const uint32x4_t mk90_0 = vld1q_u32(input - stride_mag);
    const uint32x4_t mk90_1 = vld1q_u32(input + stride_mag);
    uint32x4_t       mask2  = vceqq_u32(pc32, vdupq_n_u32(2));
    mask2                   = vandq_u32(mask2, vcgeq_u32(mc, mk90_0));
    mask2                   = vandq_u32(mask2, vcgeq_u32(mc, mk90_1));

    // 135 degree
    const uint32x4_t mk135_0 = vld1q_u32(input - stride_mag + 1);
    const uint32x4_t mk135_1 = vld1q_u32(input + stride_mag - 1);
    uint32x4_t       mask3   = vceqq_u32(pc32, vdupq_n_u32(3));
    mask3                    = vandq_u32(mask3, vcgeq_u32(mc, mk135_0));
    mask3                    = vandq_u32(mask3, vcgeq_u32(mc, mk135_1));

    // Merge masks
    mask0 = vorrq_u32(mask0, mask1);
    mask2 = vorrq_u32(mask2, mask3);
    mask0 = vorrq_u32(mask0, mask2);

    mc = vbslq_u32(mask0, mc, vdupq_n_u32(0));

    // mc > upper_thr
    mask0 = vcgtq_u32(mc, vdupq_n_u32(upper_thr));

    // mc <= lower_thr
    mask1 = vcleq_u32(mc, vdupq_n_u32(lower_thr));

    // mc <= upper_thr && mc > lower_thr
    mask2 = vcleq_u32(mc, vdupq_n_u32(upper_thr));
    mask2 = vandq_u32(mask2, vcgtq_u32(mc, vdupq_n_u32(lower_thr)));

    mc = vbslq_u32(mask0, vdupq_n_u32(EDGE), mc);
    mc = vbslq_u32(mask1, vdupq_n_u32(NO_EDGE), mc);
    mc = vbslq_u32(mask2, vdupq_n_u32(MAYBE), mc);

    return vmovn_u32(mc);
}

/* Computes non-maxima suppression and hysteresis when the gradient_size = 7
 *
 * @param[in]  magnitude_ptr Pointer to source image. Magnitude. Data type supported U32
 * @param[in]  phase_ptr     Pointer to source image. Quantized phase. Data type supported U8
 * @param[out] output_ptr    Pointer to destination image. Data type supported U8
 * @param[in]  stride_mag    Stride of magnitude image
 * @param[in]  lower_thr     Lower threshold used for the hysteresis
 * @param[in]  upper_thr     Upper threshold used for the hysteresis
 */
void non_max_suppression_U32_U8_U8(const void *__restrict magnitude_ptr, const void *__restrict phase_ptr, void *__restrict output_ptr, const uint32_t stride_mag, const int32_t lower_thr,
                                   const int32_t upper_thr)
{
    const auto magnitude = static_cast<const uint32_t *__restrict>(magnitude_ptr);
    const auto phase     = static_cast<const uint8_t *__restrict>(phase_ptr);
    const auto output    = static_cast<uint8_t *__restrict>(output_ptr);

    // Get phase for 8 pixel
    const uint16x8_t pc16 = vmovl_u8(vld1_u8(phase));

    // Compute non maxima suppression
    const uint16x4x2_t res =
    {
        {
            non_max_U32_helper(magnitude, vget_low_u16(pc16), stride_mag, lower_thr, upper_thr),
            non_max_U32_helper(magnitude + 4, vget_high_u16(pc16), stride_mag, lower_thr, upper_thr)
        }
    };

    // Store result
    vst1_u8(output, vmovn_u16(vcombine_u16(res.val[0], res.val[1])));
}

/* Computes edge tracing when is called by edge_trace_U8_U8 recursively
 *
 * @param[in]  input         Pointer to source image. Data type supported U8
 * @param[out] output        Pointer to destination image. Data type supported U8
 * @param[in]  input_stride  Stride of the input image
 * @param[in]  output_stride Stride of the output image
 */
void edge_trace_recursive_U8_U8(uint8_t *__restrict input, uint8_t *__restrict output, const int32_t input_stride, const int32_t output_stride)
{
    // Look for MAYBE pixels in 8 directions
    *output = EDGE;

    // (-1, 0)
    uint8_t pixel = *(input - 1);

    if(pixel == MAYBE)
    {
        // Touched a MAYBE point. MAYBE becomes EDGE
        *(input - 1) = EDGE;

        edge_trace_recursive_U8_U8(input - 1, output - 1, input_stride, output_stride);
    }

    // (+1, 0)
    pixel = *(input + 1);

    if(pixel == MAYBE)
    {
        // Touched a MAYBE point. MAYBE becomes EDGE
        *(input + 1) = EDGE;

        edge_trace_recursive_U8_U8(input + 1, output + 1, input_stride, output_stride);
    }

    input -= input_stride;
    output -= output_stride;

    // (-1, -1)
    pixel = *(input - 1);

    if(pixel == MAYBE)
    {
        // Touched a MAYBE point. MAYBE becomes EDGE
        *(input - 1) = EDGE;

        edge_trace_recursive_U8_U8(input - 1, output - 1, input_stride, output_stride);
    }

    // (0, -1)
    pixel = *input;

    if(pixel == MAYBE)
    {
        // Touched a MAYBE point. MAYBE becomes EDGE
        *input = EDGE;

        edge_trace_recursive_U8_U8(input, output, input_stride, output_stride);
    }

    // (+1, -1)
    pixel = *(input + 1);

    if(pixel == MAYBE)
    {
        // Touched a MAYBE point. MAYBE becomes EDGE
        *(input + 1) = EDGE;

        edge_trace_recursive_U8_U8(input + 1, output + 1, input_stride, output_stride);
    }

    input += input_stride * 2;
    output += output_stride * 2;

    // (-1, +1)
    pixel = *(input - 1);

    if(pixel == MAYBE)
    {
        // Touched a MAYBE point. MAYBE becomes EDGE
        *(input - 1) = EDGE;

        edge_trace_recursive_U8_U8(input - 1, output - 1, input_stride, output_stride);
    }

    // (0, +1)
    pixel = *input;

    if(pixel == MAYBE)
    {
        // Touched a MAYBE point. MAYBE becomes EDGE
        *input = EDGE;

        edge_trace_recursive_U8_U8(input, output, input_stride, output_stride);
    }

    // (+1, +1)
    pixel = *(input + 1);

    if(pixel == MAYBE)
    {
        // Touched a MAYBE point. MAYBE becomes EDGE
        *(input + 1) = EDGE;

        edge_trace_recursive_U8_U8(input + 1, output + 1, input_stride, output_stride);
    }
}

/* Computes edge tracing
 *
 * @param[in]  input         Pointer to source image. Data type supported U8
 * @param[out] output        Pointer to destination image. Data type supported U8
 * @param[in]  input_stride  Stride of the input image
 * @param[in]  output_stride Stride of the output image
 */
void edge_trace_U8_U8(uint8_t *__restrict input, uint8_t *__restrict output, const int32_t input_stride, const int32_t output_stride)
{
    if(*input == NO_EDGE)
    {
        *output = NO_EDGE;
    }
    // Check if EDGE and not yet touched
    else if((*input == EDGE) && (*output == NO_EDGE))
    {
        edge_trace_recursive_U8_U8(input, output, input_stride, output_stride);
    }
}
} // namespace

NEGradientKernel::NEGradientKernel()
    : _func(nullptr), _gx(nullptr), _gy(nullptr), _magnitude(nullptr), _phase(nullptr)
{
}

void NEGradientKernel::configure(const ITensor *gx, const ITensor *gy, ITensor *magnitude, ITensor *phase, int32_t norm_type)
{
    ARM_COMPUTE_ERROR_ON_NULLPTR(gx, gy, magnitude, phase);

    set_shape_if_empty(*magnitude->info(), gx->info()->tensor_shape());
    set_shape_if_empty(*phase->info(), gx->info()->tensor_shape());

    Format magnitude_format = gx->info()->data_type() == DataType::S16 ? Format::U16 : Format::U32;
    set_format_if_unknown(*magnitude->info(), magnitude_format);
    set_format_if_unknown(*phase->info(), Format::U8);

    ARM_COMPUTE_ERROR_ON_MISMATCHING_SHAPES(gx, gy, magnitude, phase);
    ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(gx, 1, DataType::S16, DataType::S32);
    ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(gy, 1, DataType::S16, DataType::S32);
    ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(magnitude, 1, DataType::U16, DataType::U32);
    ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(phase, 1, DataType::U8);
    ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(gx, gy);
    ARM_COMPUTE_ERROR_ON_MSG(element_size_from_data_type(gx->info()->data_type()) != element_size_from_data_type(magnitude->info()->data_type()), "Magnitude must have the same element size as Gx and Gy");

    _gx        = gx;
    _gy        = gy;
    _magnitude = magnitude;
    _phase     = phase;

    if(_gx->info()->data_type() == DataType::S16)
    {
        if(norm_type == 1)
        {
            _func = &mag_phase_l1norm_S16_S16_U16_U8;
        }
        else
        {
            _func = &mag_phase_l2norm_S16_S16_U16_U8;
        }
    }
    else
    {
        if(norm_type == 1)
        {
            _func = &mag_phase_l1norm_S32_S32_U32_U8;
        }
        else
        {
            _func = &mag_phase_l2norm_S32_S32_U32_U8;
        }
    }

    constexpr unsigned int num_elems_processed_per_iteration = 32;

    // Configure kernel window
    Window win = calculate_max_window(*_gx->info(), Steps(num_elems_processed_per_iteration));

    AccessWindowHorizontal gx_access(_gx->info(), 0, num_elems_processed_per_iteration);
    AccessWindowHorizontal gy_access(_gy->info(), 0, num_elems_processed_per_iteration);
    AccessWindowHorizontal mag_access(_magnitude->info(), 0, num_elems_processed_per_iteration);
    AccessWindowHorizontal phase_access(_phase->info(), 0, num_elems_processed_per_iteration);

    update_window_and_padding(win, gx_access, gy_access, mag_access, phase_access);

    mag_access.set_valid_region(win, _gx->info()->valid_region());
    phase_access.set_valid_region(win, _gx->info()->valid_region());

    INEKernel::configure(win);
}

void NEGradientKernel::run(const Window &window)
{
    ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
    ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window);
    ARM_COMPUTE_ERROR_ON(_func == nullptr);
    Iterator gx(_gx, window);
    Iterator gy(_gy, window);
    Iterator magnitude(_magnitude, window);
    Iterator phase(_phase, window);

    execute_window_loop(window, [&](const Coordinates & id)
    {
        (*_func)(gx.ptr(), gy.ptr(), magnitude.ptr(), phase.ptr());
    },
    gx, gy, magnitude, phase);
}

NEEdgeNonMaxSuppressionKernel::NEEdgeNonMaxSuppressionKernel()
    : _func(nullptr), _magnitude(nullptr), _phase(nullptr), _output(nullptr), _lower_thr(0), _upper_thr(0)
{
}

BorderSize NEEdgeNonMaxSuppressionKernel::border_size() const
{
    return BorderSize(1);
}

void NEEdgeNonMaxSuppressionKernel::configure(const ITensor *magnitude, const ITensor *phase, ITensor *output,
                                              int32_t upper_thr, int32_t lower_thr, bool border_undefined)
{
    ARM_COMPUTE_ERROR_ON_NULLPTR(magnitude, phase, output);

    set_shape_if_empty(*output->info(), magnitude->info()->tensor_shape());

    set_format_if_unknown(*phase->info(), Format::U8);
    set_format_if_unknown(*output->info(), Format::U8);

    ARM_COMPUTE_ERROR_ON_MISMATCHING_SHAPES(magnitude, phase, output);
    ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(magnitude, 1, DataType::U16, DataType::U32);
    ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(phase, 1, DataType::U8);
    ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::U8);
    ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(phase, output);

    _magnitude = magnitude;
    _phase     = phase;
    _output    = output;

    switch(_magnitude->info()->data_type())
    {
        case DataType::U16:
            _func = &non_max_suppression_U16_U8_U8;
            break;
        case DataType::U32:
            _func = &non_max_suppression_U32_U8_U8;
            break;
        default:
            ARM_COMPUTE_ERROR("Unsupported data type!");
    }

    // Set thresholds
    _lower_thr = lower_thr;
    _upper_thr = upper_thr;

    constexpr unsigned int num_elems_processed_per_iteration = 8;
    constexpr unsigned int num_elems_read_per_iteration      = 10;
    constexpr unsigned int num_rows_read_per_iteration       = 3;

    // Configure kernel window
    Window win = calculate_max_window(*_magnitude->info(), Steps(num_elems_processed_per_iteration), border_undefined, border_size());

    AccessWindowRectangle  mag_access(_magnitude->info(), -border_size().left, -border_size().top, num_elems_read_per_iteration, num_rows_read_per_iteration);
    AccessWindowHorizontal phase_access(_phase->info(), 0, num_elems_processed_per_iteration);
    AccessWindowHorizontal output_access(_output->info(), 0, num_elems_processed_per_iteration);

    update_window_and_padding(win, mag_access, phase_access, output_access);

    output_access.set_valid_region(win, _magnitude->info()->valid_region(), border_undefined, border_size());

    INEKernel::configure(win);
}

void NEEdgeNonMaxSuppressionKernel::run(const Window &window)
{
    ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
    ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window);
    ARM_COMPUTE_ERROR_ON(_func == nullptr);
    Iterator magnitude(_magnitude, window);
    Iterator phase(_phase, window);
    Iterator output(_output, window);

    const size_t input1_stride        = _magnitude->info()->strides_in_bytes()[1];
    const size_t input1_stride_ushort = input1_stride / data_size_from_type(_magnitude->info()->data_type());

    execute_window_loop(window, [&](const Coordinates & id)
    {
        (*_func)(magnitude.ptr(), phase.ptr(), output.ptr(), input1_stride_ushort, _lower_thr, _upper_thr);
    },
    magnitude, phase, output);
}

NEEdgeTraceKernel::NEEdgeTraceKernel()
    : _input(nullptr), _output(nullptr)
{
}

BorderSize NEEdgeTraceKernel::border_size() const
{
    return BorderSize(1);
}

bool NEEdgeTraceKernel::is_parallelisable() const
{
    return false;
}

void NEEdgeTraceKernel::configure(ITensor *input, ITensor *output)
{
    ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);

    set_shape_if_empty(*output->info(), input->info()->tensor_shape());

    set_format_if_unknown(*input->info(), Format::U8);
    set_format_if_unknown(*output->info(), Format::U8);

    ARM_COMPUTE_ERROR_ON_MISMATCHING_SHAPES(input, output);
    ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::U8);
    ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::U8);
    ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);

    _input  = input;
    _output = output;

    constexpr unsigned int num_elems_processed_per_iteration = 1;

    // Configure kernel window
    Window win = calculate_max_window(*_input->info(), Steps(num_elems_processed_per_iteration));

    const ValidRegion &input_valid_region  = input->info()->valid_region();
    const ValidRegion &output_valid_region = output->info()->valid_region();

    // Reads can occur within the valid region of the input + border
    AccessWindowStatic input_access(input->info(),
                                    input_valid_region.anchor[0] - border_size().left,
                                    input_valid_region.anchor[1] - border_size().top,
                                    input_valid_region.anchor[0] + input_valid_region.shape[0] + border_size().right,
                                    input_valid_region.anchor[1] + input_valid_region.shape[1] + border_size().bottom);

    // Writes can occur within the valid region of the output + border
    AccessWindowStatic output_access(output->info(),
                                     output_valid_region.anchor[0] - border_size().left,
                                     output_valid_region.anchor[1] - border_size().top,
                                     output_valid_region.anchor[0] + output_valid_region.shape[0] + border_size().right,
                                     output_valid_region.anchor[1] + output_valid_region.shape[1] + border_size().bottom);

    update_window_and_padding(win, input_access, output_access);

    output_access.set_valid_region(win, _input->info()->valid_region());

    INEKernel::configure(win);
}

void NEEdgeTraceKernel::run(const Window &window)
{
    ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
    ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window);
    Iterator input(_input, window);
    Iterator output(_output, window);

    const size_t input_stride  = _input->info()->strides_in_bytes()[1];
    const size_t output_stride = _output->info()->strides_in_bytes()[1];

    execute_window_loop(window, [&](const Coordinates & id)
    {
        edge_trace_U8_U8(input.ptr(), output.ptr(), input_stride, output_stride);
    },
    input, output);
}