summaryrefslogtreecommitdiffstats
path: root/tests/conversion_routines.c
blob: dfd2d6d1504f5ec52e97ec21cbc3751b475baf35 (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
#include <stdint.h>

static void kst_split_aft(
    uint32_t* pAfloat,
    int32_t*  exp,
    int64_t*  mant,
    int32_t*  sign)
{
    uint32_t uAft = *pAfloat;

    *exp  = (uAft >> 25) & 0x3F;
    *mant =  uAft & 0x1FFFFFF;
    *sign =  uAft >> 31;
    if (*exp || *mant)
    {
        *mant |= 1 << 25;
    }
}

static void kst_aft_to_dbl(
    void* pDouble,
    void* pAfloat)
{
    uint64_t uDbl;
    int32_t  exp;
    int32_t  sign;
    int64_t  mant;

    kst_split_aft((uint32_t*)pAfloat, &exp, &mant, &sign);
    if (exp || mant)
    {
        uDbl = ((uint64_t)sign << 63) |
               ((uint64_t)(exp + (1023 - (1 << 5))) << 52) |
               ((uint64_t)(mant & ((1 << 25) - 1)) << (52 - 25));
    }
    else
    {
        uDbl = (uint64_t)sign << 63;
    }
    *((uint64_t*)pDouble) = uDbl;
}

void kst_float_to_q15_vector(
    void*    pDst,
    void*    pSrc,
    uint32_t elCnt)
{
    uint32_t* pSrcT;
    int16_t*  pDstT;
    uint32_t  idx;
    double    smp;

    pSrcT = (uint32_t*)pSrc;
    pDstT = (int16_t*)pDst;
    for (idx = 0; idx<elCnt; idx++)
    {
        kst_aft_to_dbl(&smp, &(pSrcT[idx]));
        smp = smp * 32768.0;
        pDstT[idx] = ((smp < 32767.0) ? ((smp > -32768.0) ? ((int16_t)smp) : -32768) : 32767);
    }
}

void kst_float_to_IEEE_float(
    void*    pDst,
    void*    pSrc)
{
    double dst;

    kst_aft_to_dbl(&dst, pSrc);
    *((float*)pDst) = (float)dst;
}