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-2003 *
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 <endian.h>
21 #include "ivorbiscodec.h"
22 #include "os_types.h"
23
24 /*#define _VDBG_GRAPHFILE "_0.m"*/
25
26
27 #ifdef _VDBG_GRAPHFILE
28 extern void *_VDBG_malloc(void *ptr,long bytes,char *file,long line);
29 extern void _VDBG_free(void *ptr,char *file,long line);
30
31 #undef _ogg_malloc
32 #undef _ogg_calloc
33 #undef _ogg_realloc
34 #undef _ogg_free
35
36 #define _ogg_malloc(x) _VDBG_malloc(NULL,(x),__FILE__,__LINE__)
37 #define _ogg_calloc(x,y) _VDBG_malloc(NULL,(x)*(y),__FILE__,__LINE__)
38 #define _ogg_realloc(x,y) _VDBG_malloc((x),(y),__FILE__,__LINE__)
39 #define _ogg_free(x) _VDBG_free((x),__FILE__,__LINE__)
40 #endif
41
42 #include "asm_arm.h"
43
44 #ifndef _V_WIDE_MATH
45 #define _V_WIDE_MATH
46
47 #ifndef _LOW_ACCURACY_
48 /* 64 bit multiply */
49
50 #include <sys/types.h>
51
52 #if BYTE_ORDER==LITTLE_ENDIAN
53 union magic {
54 struct {
55 ogg_int32_t lo;
56 ogg_int32_t hi;
57 } halves;
58 ogg_int64_t whole;
59 };
60 #endif
61
62 #if BYTE_ORDER==BIG_ENDIAN
63 union magic {
64 struct {
65 ogg_int32_t hi;
66 ogg_int32_t lo;
67 } halves;
68 ogg_int64_t whole;
69 };
70 #endif
71
MULT32(ogg_int32_t x,ogg_int32_t y)72 static inline ogg_int32_t MULT32(ogg_int32_t x, ogg_int32_t y) {
73 union magic magic;
74 magic.whole = (ogg_int64_t)x * y;
75 return magic.halves.hi;
76 }
77
MULT31(ogg_int32_t x,ogg_int32_t y)78 static inline ogg_int32_t MULT31(ogg_int32_t x, ogg_int32_t y) {
79 return MULT32(x,y)<<1;
80 }
81
MULT31_SHIFT15(ogg_int32_t x,ogg_int32_t y)82 static inline ogg_int32_t MULT31_SHIFT15(ogg_int32_t x, ogg_int32_t y) {
83 union magic magic;
84 magic.whole = (ogg_int64_t)x * y;
85 return ((ogg_uint32_t)(magic.halves.lo)>>15) | ((magic.halves.hi)<<17);
86 }
87
88 #else
89 /* 32 bit multiply, more portable but less accurate */
90
91 /*
92 * Note: Precision is biased towards the first argument therefore ordering
93 * is important. Shift values were chosen for the best sound quality after
94 * many listening tests.
95 */
96
97 /*
98 * For MULT32 and MULT31: The second argument is always a lookup table
99 * value already preshifted from 31 to 8 bits. We therefore take the
100 * opportunity to save on text space and use unsigned char for those
101 * tables in this case.
102 */
103
MULT32(ogg_int32_t x,ogg_int32_t y)104 static inline ogg_int32_t MULT32(ogg_int32_t x, ogg_int32_t y) {
105 return (x >> 9) * y; /* y preshifted >>23 */
106 }
107
MULT31(ogg_int32_t x,ogg_int32_t y)108 static inline ogg_int32_t MULT31(ogg_int32_t x, ogg_int32_t y) {
109 return (x >> 8) * y; /* y preshifted >>23 */
110 }
111
MULT31_SHIFT15(ogg_int32_t x,ogg_int32_t y)112 static inline ogg_int32_t MULT31_SHIFT15(ogg_int32_t x, ogg_int32_t y) {
113 return (x >> 6) * y; /* y preshifted >>9 */
114 }
115
116 #endif
117
118 /*
119 * This should be used as a memory barrier, forcing all cached values in
120 * registers to wr writen back to memory. Might or might not be beneficial
121 * depending on the architecture and compiler.
122 */
123 #define MB()
124
125 /*
126 * The XPROD functions are meant to optimize the cross products found all
127 * over the place in mdct.c by forcing memory operation ordering to avoid
128 * unnecessary register reloads as soon as memory is being written to.
129 * However this is only beneficial on CPUs with a sane number of general
130 * purpose registers which exclude the Intel x86. On Intel, better let the
131 * compiler actually reload registers directly from original memory by using
132 * macros.
133 */
134
135 #ifdef __i386__
136
137 #define XPROD32(_a, _b, _t, _v, _x, _y) \
138 { *(_x)=MULT32(_a,_t)+MULT32(_b,_v); \
139 *(_y)=MULT32(_b,_t)-MULT32(_a,_v); }
140 #define XPROD31(_a, _b, _t, _v, _x, _y) \
141 { *(_x)=MULT31(_a,_t)+MULT31(_b,_v); \
142 *(_y)=MULT31(_b,_t)-MULT31(_a,_v); }
143 #define XNPROD31(_a, _b, _t, _v, _x, _y) \
144 { *(_x)=MULT31(_a,_t)-MULT31(_b,_v); \
145 *(_y)=MULT31(_b,_t)+MULT31(_a,_v); }
146
147 #else
148
XPROD32(ogg_int32_t a,ogg_int32_t b,ogg_int32_t t,ogg_int32_t v,ogg_int32_t * x,ogg_int32_t * y)149 static inline void XPROD32(ogg_int32_t a, ogg_int32_t b,
150 ogg_int32_t t, ogg_int32_t v,
151 ogg_int32_t *x, ogg_int32_t *y)
152 {
153 *x = MULT32(a, t) + MULT32(b, v);
154 *y = MULT32(b, t) - MULT32(a, v);
155 }
156
XPROD31(ogg_int32_t a,ogg_int32_t b,ogg_int32_t t,ogg_int32_t v,ogg_int32_t * x,ogg_int32_t * y)157 static inline void XPROD31(ogg_int32_t a, ogg_int32_t b,
158 ogg_int32_t t, ogg_int32_t v,
159 ogg_int32_t *x, ogg_int32_t *y)
160 {
161 *x = MULT31(a, t) + MULT31(b, v);
162 *y = MULT31(b, t) - MULT31(a, v);
163 }
164
XNPROD31(ogg_int32_t a,ogg_int32_t b,ogg_int32_t t,ogg_int32_t v,ogg_int32_t * x,ogg_int32_t * y)165 static inline void XNPROD31(ogg_int32_t a, ogg_int32_t b,
166 ogg_int32_t t, ogg_int32_t v,
167 ogg_int32_t *x, ogg_int32_t *y)
168 {
169 *x = MULT31(a, t) - MULT31(b, v);
170 *y = MULT31(b, t) + MULT31(a, v);
171 }
172
173 #endif
174
175 #endif
176
177 #ifndef _V_CLIP_MATH
178 #define _V_CLIP_MATH
179
CLIP_TO_15(ogg_int32_t x)180 static inline ogg_int32_t CLIP_TO_15(ogg_int32_t x) {
181 int ret=x;
182 ret-= ((x<=32767)-1)&(x-32767);
183 ret-= ((x>=-32768)-1)&(x+32768);
184 return(ret);
185 }
186
187 #endif
188
189 #endif
190
191
192
193
194