blob: 4176eb6c19929cf8c7e40634acacb03bfa67cade [file] [log] [blame]
Gloria Wang37fe1582010-03-12 14:53:20 -08001/************************************************************************
Gloria Wang2da723a2010-03-18 15:56:16 -07002 * Copyright (C) 2002-2009, Xiph.org Foundation
3 * Copyright (C) 2010, Robin Watts for Pinknoise Productions Ltd
Gloria Wang37fe1582010-03-12 14:53:20 -08004 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
Gloria Wang2da723a2010-03-18 15:56:16 -07007 * modification, are permitted provided that the following conditions
8 * are met:
Gloria Wang37fe1582010-03-12 14:53:20 -08009 *
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following disclaimer
14 * in the documentation and/or other materials provided with the
15 * distribution.
Gloria Wang2da723a2010-03-18 15:56:16 -070016 * * Neither the names of the Xiph.org Foundation nor Pinknoise
17 * Productions Ltd nor the names of its contributors may be used to
18 * endorse or promote products derived from this software without
19 * specific prior written permission.
Gloria Wang37fe1582010-03-12 14:53:20 -080020 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 ************************************************************************
Gloria Wang79130732010-02-08 14:41:04 -080033
34 function: miscellaneous math and prototypes
35
Gloria Wang37fe1582010-03-12 14:53:20 -080036 ************************************************************************/
Gloria Wang79130732010-02-08 14:41:04 -080037
38#ifndef _V_RANDOM_H_
39#define _V_RANDOM_H_
40#include "ivorbiscodec.h"
41#include "os_types.h"
42
43/*#define _VDBG_GRAPHFILE "_0.m"*/
44
45
46#ifdef _VDBG_GRAPHFILE
47extern void *_VDBG_malloc(void *ptr,long bytes,char *file,long line);
48extern void _VDBG_free(void *ptr,char *file,long line);
49
50#undef _ogg_malloc
51#undef _ogg_calloc
52#undef _ogg_realloc
53#undef _ogg_free
54
55#define _ogg_malloc(x) _VDBG_malloc(NULL,(x),__FILE__,__LINE__)
56#define _ogg_calloc(x,y) _VDBG_malloc(NULL,(x)*(y),__FILE__,__LINE__)
57#define _ogg_realloc(x,y) _VDBG_malloc((x),(y),__FILE__,__LINE__)
58#define _ogg_free(x) _VDBG_free((x),__FILE__,__LINE__)
59#endif
60
61#include "asm_arm.h"
62
63#ifndef _V_WIDE_MATH
64#define _V_WIDE_MATH
65
66#ifndef _LOW_ACCURACY_
67/* 64 bit multiply */
68
69#include <sys/types.h>
70
71#if BYTE_ORDER==LITTLE_ENDIAN
72union magic {
73 struct {
74 ogg_int32_t lo;
75 ogg_int32_t hi;
76 } halves;
77 ogg_int64_t whole;
78};
79#endif
80
81#if BYTE_ORDER==BIG_ENDIAN
82union magic {
83 struct {
84 ogg_int32_t hi;
85 ogg_int32_t lo;
86 } halves;
87 ogg_int64_t whole;
88};
89#endif
90
91static inline ogg_int32_t MULT32(ogg_int32_t x, ogg_int32_t y) {
92 union magic magic;
93 magic.whole = (ogg_int64_t)x * y;
94 return magic.halves.hi;
95}
96
97static inline ogg_int32_t MULT31(ogg_int32_t x, ogg_int32_t y) {
98 return MULT32(x,y)<<1;
99}
100
101static inline ogg_int32_t MULT31_SHIFT15(ogg_int32_t x, ogg_int32_t y) {
102 union magic magic;
103 magic.whole = (ogg_int64_t)x * y;
104 return ((ogg_uint32_t)(magic.halves.lo)>>15) | ((magic.halves.hi)<<17);
105}
106
107#else
108/* 32 bit multiply, more portable but less accurate */
109
110/*
111 * Note: Precision is biased towards the first argument therefore ordering
112 * is important. Shift values were chosen for the best sound quality after
113 * many listening tests.
114 */
115
116/*
117 * For MULT32 and MULT31: The second argument is always a lookup table
118 * value already preshifted from 31 to 8 bits. We therefore take the
119 * opportunity to save on text space and use unsigned char for those
120 * tables in this case.
121 */
122
123static inline ogg_int32_t MULT32(ogg_int32_t x, ogg_int32_t y) {
124 return (x >> 9) * y; /* y preshifted >>23 */
125}
126
127static inline ogg_int32_t MULT31(ogg_int32_t x, ogg_int32_t y) {
128 return (x >> 8) * y; /* y preshifted >>23 */
129}
130
131static inline ogg_int32_t MULT31_SHIFT15(ogg_int32_t x, ogg_int32_t y) {
132 return (x >> 6) * y; /* y preshifted >>9 */
133}
134
135#endif
136
137/*
138 * This should be used as a memory barrier, forcing all cached values in
139 * registers to wr writen back to memory. Might or might not be beneficial
140 * depending on the architecture and compiler.
141 */
142#define MB()
143
144/*
145 * The XPROD functions are meant to optimize the cross products found all
146 * over the place in mdct.c by forcing memory operation ordering to avoid
147 * unnecessary register reloads as soon as memory is being written to.
148 * However this is only beneficial on CPUs with a sane number of general
149 * purpose registers which exclude the Intel x86. On Intel, better let the
150 * compiler actually reload registers directly from original memory by using
151 * macros.
152 */
153
154#ifdef __i386__
155
156#define XPROD32(_a, _b, _t, _v, _x, _y) \
157 { *(_x)=MULT32(_a,_t)+MULT32(_b,_v); \
158 *(_y)=MULT32(_b,_t)-MULT32(_a,_v); }
159#define XPROD31(_a, _b, _t, _v, _x, _y) \
160 { *(_x)=MULT31(_a,_t)+MULT31(_b,_v); \
161 *(_y)=MULT31(_b,_t)-MULT31(_a,_v); }
162#define XNPROD31(_a, _b, _t, _v, _x, _y) \
163 { *(_x)=MULT31(_a,_t)-MULT31(_b,_v); \
164 *(_y)=MULT31(_b,_t)+MULT31(_a,_v); }
165
166#else
167
168static inline void XPROD32(ogg_int32_t a, ogg_int32_t b,
169 ogg_int32_t t, ogg_int32_t v,
170 ogg_int32_t *x, ogg_int32_t *y)
171{
172 *x = MULT32(a, t) + MULT32(b, v);
173 *y = MULT32(b, t) - MULT32(a, v);
174}
175
176static inline void XPROD31(ogg_int32_t a, ogg_int32_t b,
177 ogg_int32_t t, ogg_int32_t v,
178 ogg_int32_t *x, ogg_int32_t *y)
179{
180 *x = MULT31(a, t) + MULT31(b, v);
181 *y = MULT31(b, t) - MULT31(a, v);
182}
183
184static inline void XNPROD31(ogg_int32_t a, ogg_int32_t b,
185 ogg_int32_t t, ogg_int32_t v,
186 ogg_int32_t *x, ogg_int32_t *y)
187{
188 *x = MULT31(a, t) - MULT31(b, v);
189 *y = MULT31(b, t) + MULT31(a, v);
190}
191
192#endif
193
194#endif
195
196#ifndef _V_CLIP_MATH
197#define _V_CLIP_MATH
198
199static inline ogg_int32_t CLIP_TO_15(ogg_int32_t x) {
200 int ret=x;
201 ret-= ((x<=32767)-1)&(x-32767);
202 ret-= ((x>=-32768)-1)&(x+32768);
203 return(ret);
204}
205
206#endif
207
208#endif
209
210
211
212