Debian Codegraph @

core/sound/tremor/misc.h

raw symbols SCIP
1 /********************************************************************
2 * *
3 * THIS FILE IS PART OF THE OggVorbis 'TREMOR' CODEC SOURCE CODE. *
4 * *
5 * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS *
6 * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE *
7 * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. *
8 * *
9 * THE OggVorbis 'TREMOR' SOURCE CODE IS (C) COPYRIGHT 1994-2002 *
10 * BY THE Xiph.Org FOUNDATION http://www.xiph.org/ *
11 * *
12 ********************************************************************
13
14 function: miscellaneous math and prototypes
15
16 ********************************************************************/
17
18 #ifndef _V_RANDOM_H_
19 #define _V_RANDOM_H_
20 #include "ivorbiscodec.h"
21 #include "os.h"
22
23 #include "asm_arm.h"
24 #include <stdlib.h> /* for abs() */
25
26 #if defined(GEKKO) && !defined(__LIBRETRO__)
27 #include <gctypes.h>
28 #endif
29
30 #ifndef _V_WIDE_MATH
31 #define _V_WIDE_MATH
32
33 #ifndef _LOW_ACCURACY_
34 /* 64 bit multiply */
35
36 #if !(defined WIN32 && defined WINCE)
37 #include <sys/types.h>
38 #endif
39
40 #if BYTE_ORDER==BIG_ENDIAN
41 union magic {
42 struct {
43 ogg_int32_t hi;
44 ogg_int32_t lo;
45 } halves;
46 ogg_int64_t whole;
47 };
48 #elif BYTE_ORDER==LITTLE_ENDIAN
49 union magic {
50 struct {
51 ogg_int32_t lo;
52 ogg_int32_t hi;
53 } halves;
54 ogg_int64_t whole;
55 };
56 #endif
57
58 STIN ogg_int32_t MULT32(ogg_int32_t x, ogg_int32_t y) {
59 union magic magic;
60 magic.whole = (ogg_int64_t)x * y;
61 return magic.halves.hi;
62 }
63
64 STIN ogg_int32_t MULT31(ogg_int32_t x, ogg_int32_t y) {
65 return MULT32(x,y)<<1;
66 }
67
68 STIN ogg_int32_t MULT31_SHIFT15(ogg_int32_t x, ogg_int32_t y) {
69 union magic magic;
70 magic.whole = (ogg_int64_t)x * y;
71 return ((ogg_uint32_t)(magic.halves.lo)>>15) | ((magic.halves.hi)<<17);
72 }
73
74 #else
75 /* 32 bit multiply, more portable but less accurate */
76
77 /*
78 * Note: Precision is biased towards the first argument therefore ordering
79 * is important. Shift values were chosen for the best sound quality after
80 * many listening tests.
81 */
82
83 /*
84 * For MULT32 and MULT31: The second argument is always a lookup table
85 * value already preshifted from 31 to 8 bits. We therefore take the
86 * opportunity to save on text space and use unsigned char for those
87 * tables in this case.
88 */
89
90 STIN ogg_int32_t MULT32(ogg_int32_t x, ogg_int32_t y) {
91 return (x >> 9) * y; /* y preshifted >>23 */
92 }
93
94 STIN ogg_int32_t MULT31(ogg_int32_t x, ogg_int32_t y) {
95 return (x >> 8) * y; /* y preshifted >>23 */
96 }
97
98 STIN ogg_int32_t MULT31_SHIFT15(ogg_int32_t x, ogg_int32_t y) {
99 return (x >> 6) * y; /* y preshifted >>9 */
100 }
101
102 #endif
103
104 /*
105 * This should be used as a memory barrier, forcing all cached values in
106 * registers to wr writen back to memory. Might or might not be beneficial
107 * depending on the architecture and compiler.
108 */
109 #define MB()
110
111 /*
112 * The XPROD functions are meant to optimize the cross products found all
113 * over the place in mdct.c by forcing memory operation ordering to avoid
114 * unnecessary register reloads as soon as memory is being written to.
115 * However this is only beneficial on CPUs with a sane number of general
116 * purpose registers which exclude the Intel x86. On Intel, better let the
117 * compiler actually reload registers directly from original memory by using
118 * macros.
119 */
120
121 #ifdef __i386__
122
123 #define XPROD32(_a, _b, _t, _v, _x, _y) \
124 { *(_x)=MULT32(_a,_t)+MULT32(_b,_v); \
125 *(_y)=MULT32(_b,_t)-MULT32(_a,_v); }
126 #define XPROD31(_a, _b, _t, _v, _x, _y) \
127 { *(_x)=MULT31(_a,_t)+MULT31(_b,_v); \
128 *(_y)=MULT31(_b,_t)-MULT31(_a,_v); }
129 #define XNPROD31(_a, _b, _t, _v, _x, _y) \
130 { *(_x)=MULT31(_a,_t)-MULT31(_b,_v); \
131 *(_y)=MULT31(_b,_t)+MULT31(_a,_v); }
132
133 #else
134
135 STIN void XPROD32(ogg_int32_t a, ogg_int32_t b,
136 ogg_int32_t t, ogg_int32_t v,
137 ogg_int32_t *x, ogg_int32_t *y)
138 {
139 *x = MULT32(a, t) + MULT32(b, v);
140 *y = MULT32(b, t) - MULT32(a, v);
141 }
142
143 STIN void XPROD31(ogg_int32_t a, ogg_int32_t b,
144 ogg_int32_t t, ogg_int32_t v,
145 ogg_int32_t *x, ogg_int32_t *y)
146 {
147 *x = MULT31(a, t) + MULT31(b, v);
148 *y = MULT31(b, t) - MULT31(a, v);
149 }
150
151 STIN void XNPROD31(ogg_int32_t a, ogg_int32_t b,
152 ogg_int32_t t, ogg_int32_t v,
153 ogg_int32_t *x, ogg_int32_t *y)
154 {
155 *x = MULT31(a, t) - MULT31(b, v);
156 *y = MULT31(b, t) + MULT31(a, v);
157 }
158
159 #endif
160
161 #endif
162
163 #ifndef _V_CLIP_MATH
164 #define _V_CLIP_MATH
165
166 STIN ogg_int32_t CLIP_TO_15(ogg_int32_t x) {
167 int ret=x;
168 ret-= ((x<=32767)-1)&(x-32767);
169 ret-= ((x>=-32768)-1)&(x+32768);
170 return(ret);
171 }
172
173 #endif
174
175 STIN ogg_int32_t VFLOAT_MULT(ogg_int32_t a,ogg_int32_t ap,
176 ogg_int32_t b,ogg_int32_t bp,
177 ogg_int32_t *p){
178 if(a && b){
179 #ifndef _LOW_ACCURACY_
180 *p=ap+bp+32;
181 return MULT32(a,b);
182 #else
183 *p=ap+bp+31;
184 return (a>>15)*(b>>16);
185 #endif
186 }else
187 return 0;
188 }
189
190 int _ilog(unsigned int);
191
192 STIN ogg_int32_t VFLOAT_MULTI(ogg_int32_t a,ogg_int32_t ap,
193 ogg_int32_t i,
194 ogg_int32_t *p){
195
196 int ip=_ilog(abs(i))-31;
197 return VFLOAT_MULT(a,ap,i<<-ip,ip,p);
198 }
199
200 STIN ogg_int32_t VFLOAT_ADD(ogg_int32_t a,ogg_int32_t ap,
201 ogg_int32_t b,ogg_int32_t bp,
202 ogg_int32_t *p){
203
204 if(!a){
205 *p=bp;
206 return b;
207 }else if(!b){
208 *p=ap;
209 return a;
210 }
211
212 /* yes, this can leak a bit. */
213 if(ap>bp){
214 int shift=ap-bp+1;
215 *p=ap+1;
216 a>>=1;
217 if(shift<32){
218 b=(b+(1<<(shift-1)))>>shift;
219 }else{
220 b=0;
221 }
222 }else{
223 int shift=bp-ap+1;
224 *p=bp+1;
225 b>>=1;
226 if(shift<32){
227 a=(a+(1<<(shift-1)))>>shift;
228 }else{
229 a=0;
230 }
231 }
232
233 a+=b;
234 if((a&0xc0000000)==0xc0000000 ||
235 (a&0xc0000000)==0){
236 a<<=1;
237 (*p)--;
238 }
239 return(a);
240 }
241
242 #endif
243
244
245
246