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