Skip to content

Commit bf4faba

Browse files
Correct issue #287
1 parent 2f24a05 commit bf4faba

File tree

5 files changed

+32
-16
lines changed

5 files changed

+32
-16
lines changed

Include/dsp/utils.h

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,13 @@ __STATIC_INLINE void arm_norm_64_to_32u(uint64_t in, int32_t * normalized, int3
219219
}
220220
}
221221

222+
/**
223+
* arm_div_int64_to_int32 function performs division of a 64-bit integer by a 32-bit integer
224+
* and returns a 32-bit integer result. The function saturates the result to fit within the 32-bit integer range.
225+
* @param num The 64-bit integer numerator.
226+
* @param den The 32-bit integer denominator.
227+
* @return The saturated 32-bit integer result of the division.
228+
*/
222229
__STATIC_INLINE int32_t arm_div_int64_to_int32(int64_t num, int32_t den)
223230
{
224231
int32_t result;
@@ -230,9 +237,9 @@ __STATIC_INLINE int32_t arm_div_int64_to_int32(int64_t num, int32_t den)
230237
* if sum fits in 32bits
231238
* avoid costly 64-bit division
232239
*/
233-
if (num == (int64_t)LONG_MIN)
240+
if (num == LLONG_MIN)
234241
{
235-
absNum = LONG_MAX;
242+
absNum = LLONG_MAX;
236243
}
237244
else
238245
{
@@ -248,7 +255,7 @@ __STATIC_INLINE int32_t arm_div_int64_to_int32(int64_t num, int32_t den)
248255
/*
249256
* 64-bit division
250257
*/
251-
result = (int32_t) (num / den);
258+
result = clip_q63_to_q31(num / den);
252259

253260
return result;
254261
}

Testing/PatternGeneration/FastMath.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,13 @@ def cdiv(a,b):
232232

233233
return(d)
234234

235+
def sat_q63_to_q31(v):
236+
if v > 0x7FFFFFFF:
237+
return(0x7FFFFFFF)
238+
if v < -0x80000000:
239+
return(-0x80000000)
240+
return(v)
241+
235242
def testInt64(config):
236243
theInput=[0x1000000080000000,
237244
0x0000000080000000,
@@ -264,13 +271,14 @@ def testInt64(config):
264271
( -0x0000000080000000,2)
265272
]
266273

267-
res = [tocint32(cdiv(x,y)) for (x,y) in allCombinations]
274+
res = [sat_q63_to_q31(cdiv(x,y)) for (x,y) in allCombinations]
268275

269276
allCombinations=np.array(allCombinations,dtype=np.int64).flatten()
277+
config.setOverwrite(True)
270278
config.writeInputS64(1,allCombinations[0::2],"DivDenInput")
271279
config.writeInputS32(1,allCombinations[1::2],"DivNumInput")
272280

273-
config.writeReferenceU32(1, res,"DivRef")
281+
config.writeReferenceS32(1, res,"DivRef")
274282
config.setOverwrite(False)
275283

276284

Original file line numberDiff line numberDiff line change
@@ -1,26 +1,26 @@
11
W
22
12
3-
// 4294967295
4-
0xFFFFFFFF
5-
// 0
6-
0x00000000
7-
// 2147483649
8-
0x80000001
93
// 2147483647
104
0x7FFFFFFF
5+
// -2147483648
6+
0x80000000
7+
// 2147483647
8+
0x7FFFFFFF
9+
// -2147483648
10+
0x80000000
1111
// 1073741824
1212
0x40000000
13-
// 3221225472
13+
// -1073741824
1414
0xC0000000
1515
// 536870912
1616
0x20000000
17-
// 3758096384
17+
// -536870912
1818
0xE0000000
1919
// 1073741824
2020
0x40000000
21-
// 3221225472
21+
// -1073741824
2222
0xC0000000
2323
// 536870912
2424
0x20000000
25-
// 3221225472
25+
// -1073741824
2626
0xC0000000

Testing/Source/Tests/FastMathQ63.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ Reference patterns are generated with
1313
a double precision computation.
1414
1515
*/
16+
1617
#define ABS_ERROR ((q63_t)0)
1718

1819
void FastMathQ63::test_norm_64_to_32u()

Testing/desc.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1955,7 +1955,7 @@ group Root {
19551955

19561956
Pattern DIV_DEN_INPUT1_S64_ID : DivDenInput1_s64.txt
19571957
Pattern DIV_NUM_INPUT1_S32_ID : DivNumInput1_s32.txt
1958-
Pattern DIV_REF_S32_ID : DivRef1_u32.txt
1958+
Pattern DIV_REF_S32_ID : DivRef1_s32.txt
19591959

19601960
Output OUT_S32_ID : Output
19611961
Output NORMS_S16_ID : Output

0 commit comments

Comments
 (0)