Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
alexbevi
GitHub Repository: alexbevi/BizHawk
Path: blob/master/genplus-gx32/core/tremor/misc.h
2 views
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
#ifdef GEKKO
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==LITTLE_ENDIAN
41
union magic {
42
struct {
43
ogg_int32_t lo;
44
ogg_int32_t hi;
45
} halves;
46
ogg_int64_t whole;
47
};
48
#endif
49
50
#if BYTE_ORDER==BIG_ENDIAN
51
union magic {
52
struct {
53
ogg_int32_t hi;
54
ogg_int32_t lo;
55
} halves;
56
ogg_int64_t whole;
57
};
58
#endif
59
60
STIN ogg_int32_t MULT32(ogg_int32_t x, ogg_int32_t y) {
61
union magic magic;
62
magic.whole = (ogg_int64_t)x * y;
63
return magic.halves.hi;
64
}
65
66
STIN ogg_int32_t MULT31(ogg_int32_t x, ogg_int32_t y) {
67
return MULT32(x,y)<<1;
68
}
69
70
STIN ogg_int32_t MULT31_SHIFT15(ogg_int32_t x, ogg_int32_t y) {
71
union magic magic;
72
magic.whole = (ogg_int64_t)x * y;
73
return ((ogg_uint32_t)(magic.halves.lo)>>15) | ((magic.halves.hi)<<17);
74
}
75
76
#else
77
/* 32 bit multiply, more portable but less accurate */
78
79
/*
80
* Note: Precision is biased towards the first argument therefore ordering
81
* is important. Shift values were chosen for the best sound quality after
82
* many listening tests.
83
*/
84
85
/*
86
* For MULT32 and MULT31: The second argument is always a lookup table
87
* value already preshifted from 31 to 8 bits. We therefore take the
88
* opportunity to save on text space and use unsigned char for those
89
* tables in this case.
90
*/
91
92
STIN ogg_int32_t MULT32(ogg_int32_t x, ogg_int32_t y) {
93
return (x >> 9) * y; /* y preshifted >>23 */
94
}
95
96
STIN ogg_int32_t MULT31(ogg_int32_t x, ogg_int32_t y) {
97
return (x >> 8) * y; /* y preshifted >>23 */
98
}
99
100
STIN ogg_int32_t MULT31_SHIFT15(ogg_int32_t x, ogg_int32_t y) {
101
return (x >> 6) * y; /* y preshifted >>9 */
102
}
103
104
#endif
105
106
/*
107
* This should be used as a memory barrier, forcing all cached values in
108
* registers to wr writen back to memory. Might or might not be beneficial
109
* depending on the architecture and compiler.
110
*/
111
#define MB()
112
113
/*
114
* The XPROD functions are meant to optimize the cross products found all
115
* over the place in mdct.c by forcing memory operation ordering to avoid
116
* unnecessary register reloads as soon as memory is being written to.
117
* However this is only beneficial on CPUs with a sane number of general
118
* purpose registers which exclude the Intel x86. On Intel, better let the
119
* compiler actually reload registers directly from original memory by using
120
* macros.
121
*/
122
123
#ifdef __i386__
124
125
#define XPROD32(_a, _b, _t, _v, _x, _y) \
126
{ *(_x)=MULT32(_a,_t)+MULT32(_b,_v); \
127
*(_y)=MULT32(_b,_t)-MULT32(_a,_v); }
128
#define XPROD31(_a, _b, _t, _v, _x, _y) \
129
{ *(_x)=MULT31(_a,_t)+MULT31(_b,_v); \
130
*(_y)=MULT31(_b,_t)-MULT31(_a,_v); }
131
#define XNPROD31(_a, _b, _t, _v, _x, _y) \
132
{ *(_x)=MULT31(_a,_t)-MULT31(_b,_v); \
133
*(_y)=MULT31(_b,_t)+MULT31(_a,_v); }
134
135
#else
136
137
STIN void XPROD32(ogg_int32_t a, ogg_int32_t b,
138
ogg_int32_t t, ogg_int32_t v,
139
ogg_int32_t *x, ogg_int32_t *y)
140
{
141
*x = MULT32(a, t) + MULT32(b, v);
142
*y = MULT32(b, t) - MULT32(a, v);
143
}
144
145
STIN void XPROD31(ogg_int32_t a, ogg_int32_t b,
146
ogg_int32_t t, ogg_int32_t v,
147
ogg_int32_t *x, ogg_int32_t *y)
148
{
149
*x = MULT31(a, t) + MULT31(b, v);
150
*y = MULT31(b, t) - MULT31(a, v);
151
}
152
153
STIN void XNPROD31(ogg_int32_t a, ogg_int32_t b,
154
ogg_int32_t t, ogg_int32_t v,
155
ogg_int32_t *x, ogg_int32_t *y)
156
{
157
*x = MULT31(a, t) - MULT31(b, v);
158
*y = MULT31(b, t) + MULT31(a, v);
159
}
160
161
#endif
162
163
#endif
164
165
#ifndef _V_CLIP_MATH
166
#define _V_CLIP_MATH
167
168
STIN ogg_int32_t CLIP_TO_15(ogg_int32_t x) {
169
int ret=x;
170
ret-= ((x<=32767)-1)&(x-32767);
171
ret-= ((x>=-32768)-1)&(x+32768);
172
return(ret);
173
}
174
175
#endif
176
177
STIN ogg_int32_t VFLOAT_MULT(ogg_int32_t a,ogg_int32_t ap,
178
ogg_int32_t b,ogg_int32_t bp,
179
ogg_int32_t *p){
180
if(a && b){
181
#ifndef _LOW_ACCURACY_
182
*p=ap+bp+32;
183
return MULT32(a,b);
184
#else
185
*p=ap+bp+31;
186
return (a>>15)*(b>>16);
187
#endif
188
}else
189
return 0;
190
}
191
192
int _ilog(unsigned int);
193
194
STIN ogg_int32_t VFLOAT_MULTI(ogg_int32_t a,ogg_int32_t ap,
195
ogg_int32_t i,
196
ogg_int32_t *p){
197
198
int ip=_ilog(abs(i))-31;
199
return VFLOAT_MULT(a,ap,i<<-ip,ip,p);
200
}
201
202
STIN ogg_int32_t VFLOAT_ADD(ogg_int32_t a,ogg_int32_t ap,
203
ogg_int32_t b,ogg_int32_t bp,
204
ogg_int32_t *p){
205
206
if(!a){
207
*p=bp;
208
return b;
209
}else if(!b){
210
*p=ap;
211
return a;
212
}
213
214
/* yes, this can leak a bit. */
215
if(ap>bp){
216
int shift=ap-bp+1;
217
*p=ap+1;
218
a>>=1;
219
if(shift<32){
220
b=(b+(1<<(shift-1)))>>shift;
221
}else{
222
b=0;
223
}
224
}else{
225
int shift=bp-ap+1;
226
*p=bp+1;
227
b>>=1;
228
if(shift<32){
229
a=(a+(1<<(shift-1)))>>shift;
230
}else{
231
a=0;
232
}
233
}
234
235
a+=b;
236
if((a&0xc0000000)==0xc0000000 ||
237
(a&0xc0000000)==0){
238
a<<=1;
239
(*p)--;
240
}
241
return(a);
242
}
243
244
#endif
245
246
247
248
249
250