summaryrefslogtreecommitdiffstats
path: root/zero.c
blob: 9a5c9d58c6b56abe2e9227fc0ed64adb5b61f26e (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
#include <string.h>

#include "macro.h"
#include "zero.h"

static void zero_u8(void *dst, size_t dstr, size_t bytes) {
    uint8_t *d = dst;

    if (dstr == 1)
        memset(dst, 0x80, bytes);
    else {
        for (; bytes > 0; bytes --, d += dstr)
            *d = 0x80;
    }       
}

static void zero_16(void *dst, size_t dstr, size_t bytes) {
    uint16_t *d = dst;
    unsigned n = bytes/sizeof(uint16_t);

    if (dstr == sizeof(uint16_t))
        memset(dst, 0, bytes);
    else {
        for (; n > 0; n --, d += dstr/sizeof(uint16_t))
            *d = 0;
    }       
}

static void zero_32(void *dst, size_t dstr, size_t bytes) {
    uint32_t *d = dst;
    unsigned n = bytes/sizeof(float);

    if (dstr == sizeof(uint32_t))
        memset(dst, 0, bytes);
    else {
        for (; n > 0; n --, d += dstr/sizeof(uint32_t))
            *d = 0;
    }       
}

zero_func_t get_zero_func(sa_pcm_format_t f) {

    static const zero_func_t funcs[_SA_PCM_FORMAT_MAX] = {
        [SA_PCM_FORMAT_U8] = zero_u8,
        [SA_PCM_FORMAT_S16_NE] = zero_16,
        [SA_PCM_FORMAT_S32_NE] = zero_32,
        [SA_PCM_FORMAT_FLOAT32_NE] = zero_32
    };

    sa_assert(f < _SA_PCM_FORMAT_MAX);

    return funcs[f];
}