Skip to content

Commit 7cc974c

Browse files
singalsulgirdwood
authored andcommitted
Test: ztest: Add test for function sofm_atan2_32b()
This patch adds the test for the function. The input values table for atan are Q1.31 (x, y) pair numbers and reference angle values in Q3.29 radians. The table is computed with the double precision atan2() function in Octave and converted to fractional integers. See script atan2_tables.m. Signed-off-by: Seppo Ingalsuo <seppo.ingalsuo@linux.intel.com>
1 parent ab6bc66 commit 7cc974c

4 files changed

Lines changed: 283 additions & 0 deletions

File tree

test/ztest/unit/math/basic/trigonometry/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ target_sources(app PRIVATE
2121
trig_test.c
2222
${SOF_ROOT}/src/math/trig.c
2323
${SOF_ROOT}/src/math/lut_trig.c
24+
${SOF_ROOT}/src/math/atan2.c
2425
)
2526

2627
# Link math library for standard math functions
Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
/* SPDX-License-Identifier: BSD-3-Clause
2+
*
3+
* Copyright(c) 2026 Intel Corporation.
4+
*/
5+
6+
#ifndef __ATAN2_TABLES_H__
7+
#define __ATAN2_TABLES_H__
8+
9+
#include <stdint.h>
10+
11+
#define ATAN2_TEST_TABLE_SIZE 256
12+
13+
static const int32_t atan2_test_y[ATAN2_TEST_TABLE_SIZE] = {
14+
0, 593678131, -532114458, 826669466, 85578803, 2092045542,
15+
-1879009088, -1576694240, 1539058893, 1401024205, -1764925184, 0,
16+
0, 0, 0, 0, 0, 0,
17+
0, 0, 0, -1002953741, 562543872, 1322188032,
18+
-1091606067, -400921101, 2147483647, -1690120538, 2050372122, -350646912,
19+
-1880522864, 320130202, 1583472563, 1870898586, 1777558605, 1275437645,
20+
2147483647, 645138560, 1016824371, 1133248230, -1409419475, -65799642,
21+
751321472, -554752, -481011942, -1824835690, 1120701542, 1219868109,
22+
1276479565, -1402484230, 1094266906, 345378458, 1388947226, 1235927757,
23+
-67347315, -113975488, 352132070, 646445466, -175333594, 1313648794,
24+
498544640, 689375667, 1101318170, -1891416384, 973726336, 260722458,
25+
2049027200, 1619159731, 659320781, 811575142, 2062582016, 1804117709,
26+
-556148173, 2147483647, -1581181395, -1384873248, -2147483648, 1919463322,
27+
-715849728, 1482341555, -2106111198, 2147483647, -1581934605, -2147483648,
28+
2147483647, -1977711542, -1219287520, -1913594461, -1213455936, 1750786893,
29+
-420132698, -222367974, -435172250, -2087889971, -919525235, 834603827,
30+
-1669661664, 2147483647, 1584016051, 2082185882, 212862848, 1926648627,
31+
-1889370525, -1133371149, 2147483647, -125891533, 629086234, -580819290,
32+
-814994048, -940839821, -2147483648, -11732723, -499195981, 2003249459,
33+
-1797551043, 1543991962, 703381043, -200345024, 1618129075, 1901467674,
34+
2147483647, 747880038, 2017725389, 1628990106, -2147483648, 2147483647,
35+
-451489280, 486361498, -2132152282, -58571955, 157509299, -834756314,
36+
1251017293, 1892762010, -1455901638, 90604237, -957637261, -2147483648,
37+
752198656, -1829826099, -1725792499, -569240883, 42635648, -1498449850,
38+
-437911232, -23190746, -661963034, -324850381, -2134133197, 297851418,
39+
980486426, -357129907, 1688064717, -819664806, 1316280909, 13885414,
40+
-1496825299, 18689510, 1180361037, -324324774, 370182912, 2146612582,
41+
-652058458, -259263629, 277880346, -1507396774, -1420640883, 1603635123,
42+
1512286899, 925569638, 64723789, 2101077862, 2046962227, -1243819526,
43+
1021511040, -1659139469, 1906461286, 1168691814, 1900425472, 195734528,
44+
-689034931, 510480538, -1956886624, 965214694, -1640061773, -2146964829,
45+
-1588224, -1125154624, 34522470, 1535756570, 692508749, 292941158,
46+
-143428173, 715886899, 1614379853, 33759053, -1082890266, 610783923,
47+
1470741325, 1262575846, -53151718, 2033863603, 844302131, 19598797,
48+
654380390, -411747354, -1558739706, -1846029434, 1652960461, -2147483648,
49+
-587736512, -595937408, 1554227840, 562723533, 1592959667, -46749261,
50+
1489178803, 582155622, -2126878934, -1168122701, -2147483648, 1081324570,
51+
1864094003, 1138184397, -547145984, 772395853, -41048269, 1008931123,
52+
-844305651, 1003180570, 2147483647, 2147483647, -715760602, -2035354781,
53+
-1371158131, 1678904832, 494394701, -1407855398, 440926182, 2082209818,
54+
-1291104390, 1887003290, 744169114, -1480452019, 2087889126, 23236224,
55+
-1745024160, -72790784, 523330227, -115015155, 1655730278, 923114086,
56+
880698086, 1312830746, 132706534, -978800064,
57+
};
58+
59+
static const int32_t atan2_test_x[ATAN2_TEST_TABLE_SIZE] = {
60+
0, 0, 0, 0, 0, 0,
61+
0, 0, 0, 0, 0, -1399816141,
62+
-230097050, -1261376090, -2081157232, -440471258, -876176998, -1642130829,
63+
-171532698, 104774067, -928553894, 803256115, 1159264128, -451441549,
64+
1477543373, -1407978810, -155422643, -1755391194, 1797471949, -2131587568,
65+
-251177766, -2147483648, -868828787, 1321866445, -550897318, -288342067,
66+
158300877, 370411853, 1242460877, 560164634, 560658278, 1170291584,
67+
-1531339462, 1861627750, 881152307, -76744870, 1266329293, -808628198,
68+
779498650, -1042297485, 29521254, -2147483648, -1831276832, 315334554,
69+
1677842637, -314975091, 1873312179, 1370099174, -853027507, 1308034816,
70+
1094235366, 2147483647, 268095872, 2147483647, -834663949, 838375578,
71+
-574429504, -707173350, 1876573952, 874547379, 1613107021, -2076715221,
72+
1954203187, -1230503296, 1975324314, 1574426445, 150468173, -2147483648,
73+
-2045228592, 102846515, -1659941536, 2147483647, 523171686, -919468352,
74+
1801804365, 25905792, -2147483648, 2100982400, -2147483648, -592549478,
75+
774778470, -794157056, -687915571, 1915112320, -1895780832, -651402752,
76+
-1753523904, -2127834438, -1292520768, -72287987, 471997645, -1600389824,
77+
1050780826, 909749350, 1526121907, -480663181, 1690915917, 152633958,
78+
1151843968, -1504568947, -2135539402, 799874662, 1630831770, -643009382,
79+
-1334604269, -2147483648, -1856544518, 1010088781, 1164046259, 914998093,
80+
2147483647, 560258406, 1061672550, 1741415526, 730181760, -1559917498,
81+
1348011034, 280000230, 247261696, 1805741133, -998184000, 2003341542,
82+
182103398, -343834726, -665013325, 681587456, -965692570, 773658266,
83+
-854945766, -1014991155, -663706278, 200810086, -2147483648, 1065857690,
84+
-1146915827, -2147483648, 1094864461, 1700262502, -1050449382, -713241830,
85+
-1989792816, -1679438253, -2144952374, 1737513395, -76085786, -999872192,
86+
1268036915, 1857466829, 1334412570, 351298253, 1767924506, -206440256,
87+
-2147483648, -1185212653, 2030329805, 2147483647, 1363327539, 848855885,
88+
1660833434, 1357533338, -1650400646, 991000525, 2106705357, 2147483647,
89+
-1964598170, 1936573338, 799362995, 406677427, -395381466, 2147483647,
90+
-1469184218, 204290944, 1538024576, 2093231642, 2147483647, -1031910246,
91+
-2147483648, 2147483647, -172979558, 1774660941, 795639117, 1787846861,
92+
1848797773, 282311603, -379224384, -889636070, -1874511760, -1696152198,
93+
-96912922, 1257014528, 1355642675, -28044262, -1442693542, -2147483648,
94+
586790195, 170231987, -474024038, 1662995277, -139357645, -447027750,
95+
-774900826, 2147483647, 1843413581, 1234652954, -991492621, -2147483648,
96+
-272780570, -1956468659, 1327163059, 1548914611, 1038220902, 2147483647,
97+
-1708905722, 550796928, -1898930634, 1387736064, -1010688026, -1955962237,
98+
2147483647, 1687800013, -538678554, -2051015613, 1152012083, -1856525088,
99+
-784337101, 361366016, 1829781043, -926315456, 584568653, -421916493,
100+
1094186931, 1752872986, -1590665542, -714551270, 1503289216, -2001796262,
101+
-554735245, -1757686726, -904190426, -2083870624, -561217114, -885333222,
102+
-314317274, 1836398643, -1568309389, 2094263142,
103+
};
104+
105+
static const int32_t atan2_test_ref[ATAN2_TEST_TABLE_SIZE] = {
106+
0, 843314857, -843314857, 843314857, 843314857, 843314857,
107+
-843314857, -843314857, 843314857, 843314857, -843314857, 1686629713,
108+
1686629713, 1686629713, 1686629713, 1686629713, 1686629713, 1686629713,
109+
1686629713, 0, 1686629713, -480774686, 242550644, 1019958505,
110+
-341607303, -1537697774, 882102887, -1275141377, 456892678, -1599098184,
111+
-914601725, 1607182223, 1112734724, 513084281, 1004661107, 962680342,
112+
803811086, 563496816, 368216934, 596849638, -640053917, -30153818,
113+
1441750136, -159984, -268264737, -865880079, 388944604, 1157587081,
114+
548989894, -1186440684, 828834606, 1601018220, 1338258195, 709198926,
115+
-21538024, -1500229594, 99753206, 236681525, -1577795578, 422807062,
116+
229517388, 166764936, 715116877, -387665176, 1223768840, 161868893,
117+
990055806, 1064389455, 181392893, 401616028, 486983396, 1302621340,
118+
-148853162, 1122663950, -362401539, -387315937, -805759192, 1295041388,
119+
-1505874184, 806125749, -1201663508, 421657428, -671841804, -1060503213,
120+
468530021, -836282855, -1409396260, -396616142, -1410500034, 1018520658,
121+
-266761868, -1540056775, -1383811666, -444815486, -1444175423, 1199116908,
122+
-1278122081, 1262504863, 1210753289, 861946112, 227456622, 1215451470,
123+
-570823003, -480185572, 511613552, -1549105769, 191218374, -705349581,
124+
-330593881, -1386609757, -1263475094, -7874366, -159473626, 1010064033,
125+
-1186190715, 1351971671, 1492198979, -105120786, 508515064, 602528322,
126+
421657428, 498137329, 583272798, 403755854, -667353222, 1180588320,
127+
-173509607, 562873277, -781331817, -17408120, 1602606516, -211958872,
128+
765710697, 939789547, -1073348055, 70950927, -1267220800, -657671271,
129+
1299248585, -1115207171, -1040423529, -661241101, 1675972201, -511381404,
130+
-1490815052, -1680832252, -291950578, -101352536, -1088879133, 1474249986,
131+
1440830194, -1574140544, 1328665659, -236647292, 874313438, 1679174565,
132+
-465981743, 5401720, 388810509, -400234846, 110813576, 894787567,
133+
-1528364782, -1571011137, 73024931, -328578059, -432708387, 581946141,
134+
396542676, 321267954, 1665586022, 606704001, 413936026, -281848594,
135+
1429206964, -380316068, 630163376, 663532294, 953439316, 48798797,
136+
-1451193270, 638942546, -485695493, 231958476, -350157942, -1083853093,
137+
-1686232657, -259102718, 1580872968, 382979880, 384511044, 87192252,
138+
-41566736, 641651025, 967182272, 1666266823, -1405387221, 1501063583,
139+
878640321, 422842424, -21038732, 850717120, 1402364287, 1681730150,
140+
450864858, -632841657, -1001810229, -449635784, 888470538, -953498287,
141+
-1338255336, -145327507, 376072239, 229590563, 1142214026, -1674944244,
142+
940578011, 1531360468, -543807078, -346902051, -601547020, 250433321,
143+
1241668689, 601343996, -1536018589, 272669402, -1664837116, 1430957794,
144+
-201109884, 287908177, 975262018, 1252638885, -298470119, -1240320708,
145+
-1122270737, 729495693, 141675968, -1155748018, 346943281, 950647349,
146+
-465878468, 441432301, 1451702422, -1084733681, 508293499, 1680398164,
147+
-1008559815, -1664409066, 1404948145, -1657028209, 1018764798, 1253757970,
148+
1027356695, 333210160, 1641308961, -234723315,
149+
};
150+
151+
#endif /* __ATAN2_TABLES_H__ */
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
% SPDX-License-Identifier: BSD-3-Clause
2+
%
3+
% Copyright(c) 2026 Intel Corporation.
4+
5+
function atan2_tables()
6+
7+
% Create random test points with bias to min/max values
8+
q31_scale = 2^31;
9+
q29_scale = 2^29;
10+
num_points = 256;
11+
rand('seed', 42);
12+
x = max(min(2.2 * rand(num_points, 1) - 1.1, 1), -1);
13+
y = max(min(2.2 * rand(num_points, 1) - 1.1, 1), -1);
14+
15+
% Force 1st test point to be (0, 0) to ensure it is tested
16+
x(1) = 0;
17+
y(1) = 0;
18+
19+
% Then force 10 test points to have x = 0, the reference values are 0 or pi
20+
x(2:11) = 0;
21+
22+
% And force 10 test points to have y = 0, the reference values are +/-pi
23+
y(12:21) = 0;
24+
25+
% Quantize input values
26+
ref_x = int32(x * q31_scale);
27+
ref_y = int32(y * q31_scale);
28+
x = double(ref_x)/q31_scale;
29+
y = double(ref_y)/q31_scale;
30+
a = atan2(y, x);
31+
ref_a = int32(a * q29_scale);
32+
33+
figure(1)
34+
subplot(2,1,1)
35+
plot(x, y, 'o');
36+
grid on;
37+
subplot(2,1,2)
38+
plot(a, 'o');
39+
grid on;
40+
41+
fn = 'atan2_tables.h';
42+
fh = export_headerfile(fn);
43+
dn = 'ATAN2_TEST_TABLE_SIZE';
44+
vl = 6;
45+
fu = export_ifndef(fh, fn);
46+
export_define(fh, dn, num_points);
47+
export_array(fh, 'atan2_test_y', dn, vl, ref_y);
48+
export_array(fh, 'atan2_test_x', dn, vl, ref_x);
49+
export_array(fh, 'atan2_test_ref', dn, vl, ref_a);
50+
export_fclose(fh, fu);
51+
52+
end
53+
54+
function [fh] = export_headerfile(headerfn)
55+
fh = fopen(headerfn, 'w');
56+
fprintf(fh, '/* SPDX-License-Identifier: BSD-3-Clause\n');
57+
fprintf(fh, ' *\n');
58+
fprintf(fh, ' * Copyright(c) %s Intel Corporation.\n', ...
59+
datestr(now, 'yyyy'));
60+
fprintf(fh, ' */\n\n');
61+
end
62+
63+
function fu = export_ifndef(fh, fn)
64+
fu = sprintf('__%s__', upper(strrep(fn, '.', '_')));
65+
fprintf(fh, '#ifndef %s\n', fu);
66+
fprintf(fh, '#define %s\n\n', fu);
67+
fprintf(fh, '#include <stdint.h>\n\n');
68+
end
69+
70+
function export_define(fh, dn, val)
71+
fprintf(fh, '#define %s %d\n', dn, val);
72+
end
73+
74+
function export_array(fh, vn, size_str, vl, data)
75+
fprintf(fh, '\n');
76+
fprintf(fh, 'static const int32_t %s[%s] = {\n', vn, size_str);
77+
78+
n = length(data);
79+
k = 0;
80+
for i = 1:vl:n
81+
fprintf(fh, '\t');
82+
for j = 1:min(vl, n-k)
83+
k = k + 1;
84+
fprintf(fh, '%12d,', data(k));
85+
end
86+
fprintf(fh, '\n');
87+
end
88+
89+
fprintf(fh, '};\n');
90+
end
91+
92+
function export_fclose(fh, fu)
93+
fprintf(fh, "\n#endif /* %s */\n", fu);
94+
fclose(fh);
95+
end

test/ztest/unit/math/basic/trigonometry/trig_test.c

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include <sof/math/cordic.h>
3232
#include <sof/math/lut_trig.h>
3333
#include "trig_tables.h"
34+
#include "atan2_tables.h"
3435

3536
/* Define M_PI if not available */
3637
#ifndef M_PI
@@ -47,6 +48,7 @@
4748
#define CMP_TOLERANCE_ASIN_16B 0.0001152158f
4849
#define CMP_TOLERANCE_ACOS_16B 0.0001196862f
4950
#define CMP_TOLERANCE_SIN 3.1e-5f
51+
#define CMP_TOLERANCE_ATAN2 2.0e-5 /* ~0.001 degrees in radians */
5052

5153
/*
5254
* Helper function for rounding double values to nearest integer
@@ -230,4 +232,38 @@ ZTEST(trigonometry, test_sin_lut_16b_fixed)
230232
}
231233
}
232234

235+
/* Test sofm_atan2_32b function */
236+
ZTEST(trigonometry, test_atan2)
237+
{
238+
double reference;
239+
double result;
240+
double delta;
241+
double delta_max = 0.0;
242+
int32_t result_q29_max = 0;
243+
int32_t result_q29;
244+
int i_max = 0;
245+
int i;
246+
247+
/* Note that the first atan2_test_y[], atan2_test_x[] have forced to
248+
* zero y or x, values to test edge cases where x == 0 or y == 0. See
249+
* script atan2_tables.m for details.
250+
*/
251+
for (i = 0; i < ATAN2_TEST_TABLE_SIZE; ++i) {
252+
result_q29 = sofm_atan2_32b(atan2_test_y[i], atan2_test_x[i]);
253+
result = Q_CONVERT_QTOD(result_q29, 29);
254+
reference = Q_CONVERT_QTOD(atan2_test_ref[i], 29);
255+
delta = fabs(reference - result);
256+
if (i == 0 || delta > delta_max) {
257+
result_q29_max = result_q29;
258+
delta_max = delta;
259+
i_max = i;
260+
}
261+
zassert_true(delta <= CMP_TOLERANCE_ATAN2,
262+
"sofm_atan2_32b failed for input %d: (%d, %d) result %d, expected %d",
263+
i, atan2_test_y[i], atan2_test_x[i], result_q29, atan2_test_ref[i]);
264+
}
265+
printf(" INFO - Maximum delta for atan2 test %d: %.6e (%d, %d) result %d\n",
266+
i_max, delta_max, atan2_test_y[i_max], atan2_test_x[i_max], result_q29_max);
267+
}
268+
233269
ZTEST_SUITE(trigonometry, NULL, NULL, NULL, NULL, NULL);

0 commit comments

Comments
 (0)