michael@0: /******************************************************************** michael@0: * * michael@0: * THIS FILE IS PART OF THE OggVorbis 'TREMOR' CODEC SOURCE CODE. * michael@0: * * michael@0: * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS * michael@0: * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE * michael@0: * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. * michael@0: * * michael@0: * THE OggVorbis 'TREMOR' SOURCE CODE IS (C) COPYRIGHT 1994-2002 * michael@0: * BY THE Xiph.Org FOUNDATION http://www.xiph.org/ * michael@0: * * michael@0: ******************************************************************** michael@0: michael@0: function: miscellaneous math and prototypes michael@0: michael@0: ********************************************************************/ michael@0: michael@0: #ifndef _V_RANDOM_H_ michael@0: #define _V_RANDOM_H_ michael@0: #include "ivorbiscodec.h" michael@0: #include "os.h" michael@0: michael@0: #ifdef _LOW_ACCURACY_ michael@0: # define X(n) (((((n)>>22)+1)>>1) - ((((n)>>22)+1)>>9)) michael@0: # define LOOKUP_T const unsigned char michael@0: #else michael@0: # define X(n) (n) michael@0: # define LOOKUP_T const ogg_int32_t michael@0: #endif michael@0: michael@0: #include "asm_arm.h" michael@0: #include /* for abs() */ michael@0: michael@0: #ifndef _V_WIDE_MATH michael@0: #define _V_WIDE_MATH michael@0: michael@0: #ifndef _LOW_ACCURACY_ michael@0: /* 64 bit multiply */ michael@0: michael@0: #if !(defined WIN32 && defined WINCE) michael@0: #include michael@0: #endif michael@0: michael@0: #if BYTE_ORDER==LITTLE_ENDIAN michael@0: union magic { michael@0: struct { michael@0: ogg_int32_t lo; michael@0: ogg_int32_t hi; michael@0: } halves; michael@0: ogg_int64_t whole; michael@0: }; michael@0: #elif BYTE_ORDER==BIG_ENDIAN michael@0: union magic { michael@0: struct { michael@0: ogg_int32_t hi; michael@0: ogg_int32_t lo; michael@0: } halves; michael@0: ogg_int64_t whole; michael@0: }; michael@0: #endif michael@0: michael@0: STIN ogg_int32_t MULT32(ogg_int32_t x, ogg_int32_t y) { michael@0: union magic magic; michael@0: magic.whole = (ogg_int64_t)x * y; michael@0: return magic.halves.hi; michael@0: } michael@0: michael@0: STIN ogg_int32_t MULT31(ogg_int32_t x, ogg_int32_t y) { michael@0: return MULT32(x,y)<<1; michael@0: } michael@0: michael@0: STIN ogg_int32_t MULT31_SHIFT15(ogg_int32_t x, ogg_int32_t y) { michael@0: union magic magic; michael@0: magic.whole = (ogg_int64_t)x * y; michael@0: return ((ogg_uint32_t)(magic.halves.lo)>>15) | ((magic.halves.hi)<<17); michael@0: } michael@0: michael@0: #else michael@0: /* 32 bit multiply, more portable but less accurate */ michael@0: michael@0: /* michael@0: * Note: Precision is biased towards the first argument therefore ordering michael@0: * is important. Shift values were chosen for the best sound quality after michael@0: * many listening tests. michael@0: */ michael@0: michael@0: /* michael@0: * For MULT32 and MULT31: The second argument is always a lookup table michael@0: * value already preshifted from 31 to 8 bits. We therefore take the michael@0: * opportunity to save on text space and use unsigned char for those michael@0: * tables in this case. michael@0: */ michael@0: michael@0: STIN ogg_int32_t MULT32(ogg_int32_t x, ogg_int32_t y) { michael@0: return (x >> 9) * y; /* y preshifted >>23 */ michael@0: } michael@0: michael@0: STIN ogg_int32_t MULT31(ogg_int32_t x, ogg_int32_t y) { michael@0: return (x >> 8) * y; /* y preshifted >>23 */ michael@0: } michael@0: michael@0: STIN ogg_int32_t MULT31_SHIFT15(ogg_int32_t x, ogg_int32_t y) { michael@0: return (x >> 6) * y; /* y preshifted >>9 */ michael@0: } michael@0: michael@0: #endif michael@0: michael@0: /* michael@0: * This should be used as a memory barrier, forcing all cached values in michael@0: * registers to wr writen back to memory. Might or might not be beneficial michael@0: * depending on the architecture and compiler. michael@0: */ michael@0: #define MB() michael@0: michael@0: /* michael@0: * The XPROD functions are meant to optimize the cross products found all michael@0: * over the place in mdct.c by forcing memory operation ordering to avoid michael@0: * unnecessary register reloads as soon as memory is being written to. michael@0: * However this is only beneficial on CPUs with a sane number of general michael@0: * purpose registers which exclude the Intel x86. On Intel, better let the michael@0: * compiler actually reload registers directly from original memory by using michael@0: * macros. michael@0: */ michael@0: michael@0: #ifdef __i386__ michael@0: michael@0: #define XPROD32(_a, _b, _t, _v, _x, _y) \ michael@0: { *(_x)=MULT32(_a,_t)+MULT32(_b,_v); \ michael@0: *(_y)=MULT32(_b,_t)-MULT32(_a,_v); } michael@0: #define XPROD31(_a, _b, _t, _v, _x, _y) \ michael@0: { *(_x)=MULT31(_a,_t)+MULT31(_b,_v); \ michael@0: *(_y)=MULT31(_b,_t)-MULT31(_a,_v); } michael@0: #define XNPROD31(_a, _b, _t, _v, _x, _y) \ michael@0: { *(_x)=MULT31(_a,_t)-MULT31(_b,_v); \ michael@0: *(_y)=MULT31(_b,_t)+MULT31(_a,_v); } michael@0: michael@0: #else michael@0: michael@0: STIN void XPROD32(ogg_int32_t a, ogg_int32_t b, michael@0: ogg_int32_t t, ogg_int32_t v, michael@0: ogg_int32_t *x, ogg_int32_t *y) michael@0: { michael@0: *x = MULT32(a, t) + MULT32(b, v); michael@0: *y = MULT32(b, t) - MULT32(a, v); michael@0: } michael@0: michael@0: STIN void XPROD31(ogg_int32_t a, ogg_int32_t b, michael@0: ogg_int32_t t, ogg_int32_t v, michael@0: ogg_int32_t *x, ogg_int32_t *y) michael@0: { michael@0: *x = MULT31(a, t) + MULT31(b, v); michael@0: *y = MULT31(b, t) - MULT31(a, v); michael@0: } michael@0: michael@0: STIN void XNPROD31(ogg_int32_t a, ogg_int32_t b, michael@0: ogg_int32_t t, ogg_int32_t v, michael@0: ogg_int32_t *x, ogg_int32_t *y) michael@0: { michael@0: *x = MULT31(a, t) - MULT31(b, v); michael@0: *y = MULT31(b, t) + MULT31(a, v); michael@0: } michael@0: michael@0: #endif michael@0: michael@0: #endif michael@0: michael@0: #ifndef _V_CLIP_MATH michael@0: #define _V_CLIP_MATH michael@0: michael@0: STIN ogg_int32_t CLIP_TO_15(ogg_int32_t x) { michael@0: int ret=x; michael@0: ret-= ((x<=32767)-1)&(x-32767); michael@0: ret-= ((x>=-32768)-1)&(x+32768); michael@0: return(ret); michael@0: } michael@0: michael@0: #endif michael@0: michael@0: STIN ogg_int32_t VFLOAT_MULT(ogg_int32_t a,ogg_int32_t ap, michael@0: ogg_int32_t b,ogg_int32_t bp, michael@0: ogg_int32_t *p){ michael@0: if(a && b){ michael@0: #ifndef _LOW_ACCURACY_ michael@0: *p=ap+bp+32; michael@0: return MULT32(a,b); michael@0: #else michael@0: *p=ap+bp+31; michael@0: return (a>>15)*(b>>16); michael@0: #endif michael@0: }else michael@0: return 0; michael@0: } michael@0: michael@0: int _ilog(unsigned int); michael@0: michael@0: STIN ogg_int32_t VFLOAT_MULTI(ogg_int32_t a,ogg_int32_t ap, michael@0: ogg_int32_t i, michael@0: ogg_int32_t *p){ michael@0: michael@0: int ip=_ilog(abs(i))-31; michael@0: return VFLOAT_MULT(a,ap,i<<-ip,ip,p); michael@0: } michael@0: michael@0: STIN ogg_int32_t VFLOAT_ADD(ogg_int32_t a,ogg_int32_t ap, michael@0: ogg_int32_t b,ogg_int32_t bp, michael@0: ogg_int32_t *p){ michael@0: michael@0: if(!a){ michael@0: *p=bp; michael@0: return b; michael@0: }else if(!b){ michael@0: *p=ap; michael@0: return a; michael@0: } michael@0: michael@0: /* yes, this can leak a bit. */ michael@0: if(ap>bp){ michael@0: int shift=ap-bp+1; michael@0: *p=ap+1; michael@0: a>>=1; michael@0: if(shift<32){ michael@0: b=(b+(1<<(shift-1)))>>shift; michael@0: }else{ michael@0: b=0; michael@0: } michael@0: }else{ michael@0: int shift=bp-ap+1; michael@0: *p=bp+1; michael@0: b>>=1; michael@0: if(shift<32){ michael@0: a=(a+(1<<(shift-1)))>>shift; michael@0: }else{ michael@0: a=0; michael@0: } michael@0: } michael@0: michael@0: a+=b; michael@0: if((a&0xc0000000)==0xc0000000 || michael@0: (a&0xc0000000)==0){ michael@0: a<<=1; michael@0: (*p)--; michael@0: } michael@0: return(a); michael@0: } michael@0: michael@0: #endif michael@0: michael@0: michael@0: michael@0: