summaryrefslogtreecommitdiffstats
path: root/sbc/sbc_math.h
blob: c8c72c759b69d2558d6deeabed9c919b7cefd12b (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
/*
 *
 *  Bluetooth low-complexity, subband codec (SBC) library
 *
 *  Copyright (C) 2004-2007  Marcel Holtmann <marcel@holtmann.org>
 *  Copyright (C) 2004-2005  Henryk Ploetz <henryk@ploetzli.ch>
 *  Copyright (C) 2005-2008  Brad Midgley <bmidgley@xmission.com>
 *
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2.1 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */

#define fabs(x) ((x) < 0 ? -(x) : (x))
/* C does not provide an explicit arithmetic shift right but this will
   always be correct and every compiler *should* generate optimal code */
#define ASR(val, bits) ((-2 >> 1 == -1) ? \
		 ((int32_t)(val)) >> (bits) : ((int32_t) (val)) / (1 << (bits)))
#define ASR_64(val, bits) ((-2 >> 1 == -1) ? \
		 ((long long)(val)) >> (bits) : ((long long) (val)) / (1 << (bits)))

#define SCALE_PROTO4_TBL	16
#define SCALE_ANA4_TBL		16
#define SCALE_PROTO8_TBL	16
#define SCALE_ANA8_TBL		16
#define SCALE_SPROTO4_TBL	16
#define SCALE_SPROTO8_TBL	16
#define SCALE_NPROTO4_TBL	10
#define SCALE_NPROTO8_TBL	12
#define SCALE_SAMPLES		14
#define SCALE4_STAGE1_BITS	9
#define SCALE4_STAGE2_BITS	21
#define SCALE4_STAGED1_BITS	18
#define SCALE4_STAGED2_BITS	23
#define SCALE8_STAGE1_BITS	7
#define SCALE8_STAGE2_BITS	24
#define SCALE8_STAGED1_BITS	18
#define SCALE8_STAGED2_BITS	23

typedef int32_t sbc_fixed_t;
typedef long long sbc_extended_t;

#define SCALE4_STAGE1(src)  ASR_64(src, SCALE4_STAGE1_BITS)
#define SCALE4_STAGE2(src)  ASR_64(src, SCALE4_STAGE2_BITS)
#define SCALE4_STAGED1(src) ASR_64(src, SCALE4_STAGED1_BITS)
#define SCALE4_STAGED2(src) ASR_64(src, SCALE4_STAGED2_BITS)
#define SCALE8_STAGE1(src)  ASR_64(src, SCALE8_STAGE1_BITS)
#define SCALE8_STAGE2(src)  ASR_64(src, SCALE8_STAGE2_BITS)
#define SCALE8_STAGED1(src) ASR_64(src, SCALE8_STAGED1_BITS)
#define SCALE8_STAGED2(src) ASR_64(src, SCALE8_STAGED2_BITS)

#define SBC_FIXED_0(val) { val = 0; }
#define ADD(dst, src)    { dst += src; }
#define SUB(dst, src)    { dst -= src; }
#define MUL32(dst, a, b)   { dst = (sbc_fixed_t) (a) * (b); }
#define MULA32(dst, a, b)  { dst += (sbc_fixed_t) (a) * (b); }
#define MUL(dst, a, b)   { dst = (sbc_extended_t) (a) * (b); }
#define MULA(dst, a, b)  { dst += (sbc_extended_t) (a) * (b); }
#define DIV2(dst, src)   { dst = ASR(src, 1); }