1 #include <stdint.h>
2
kst_split_aft(uint32_t * pAfloat,int32_t * exp,int64_t * mant,int32_t * sign)3 static void kst_split_aft(
4 uint32_t* pAfloat,
5 int32_t* exp,
6 int64_t* mant,
7 int32_t* sign)
8 {
9 uint32_t uAft = *pAfloat;
10
11 *exp = (uAft >> 25) & 0x3F;
12 *mant = uAft & 0x1FFFFFF;
13 *sign = uAft >> 31;
14 if (*exp || *mant)
15 {
16 *mant |= 1 << 25;
17 }
18 }
19
kst_aft_to_dbl(void * pDouble,void * pAfloat)20 static void kst_aft_to_dbl(
21 void* pDouble,
22 void* pAfloat)
23 {
24 uint64_t uDbl;
25 int32_t exp;
26 int32_t sign;
27 int64_t mant;
28
29 kst_split_aft((uint32_t*)pAfloat, &exp, &mant, &sign);
30 if (exp || mant)
31 {
32 uDbl = ((uint64_t)sign << 63) |
33 ((uint64_t)(exp + (1023 - (1 << 5))) << 52) |
34 ((uint64_t)(mant & ((1 << 25) - 1)) << (52 - 25));
35 }
36 else
37 {
38 uDbl = (uint64_t)sign << 63;
39 }
40 *((uint64_t*)pDouble) = uDbl;
41 }
42
kst_float_to_q15_vector(void * pDst,void * pSrc,uint32_t elCnt)43 void kst_float_to_q15_vector(
44 void* pDst,
45 void* pSrc,
46 uint32_t elCnt)
47 {
48 uint32_t* pSrcT;
49 int16_t* pDstT;
50 uint32_t idx;
51 double smp;
52
53 pSrcT = (uint32_t*)pSrc;
54 pDstT = (int16_t*)pDst;
55 for (idx = 0; idx<elCnt; idx++)
56 {
57 kst_aft_to_dbl(&smp, &(pSrcT[idx]));
58 smp = smp * 32768.0;
59 pDstT[idx] = ((smp < 32767.0) ? ((smp > -32768.0) ? ((int16_t)smp) : -32768) : 32767);
60 }
61 }
62
kst_float_to_IEEE_float(void * pDst,void * pSrc)63 void kst_float_to_IEEE_float(
64 void* pDst,
65 void* pSrc)
66 {
67 double dst;
68
69 kst_aft_to_dbl(&dst, pSrc);
70 *((float*)pDst) = (float)dst;
71 }