misc/libtremor/tremor/misc.h
changeset 7697 767d3c4153a1
parent 6045 9a7cc0f29430
child 7849 a12155461b34
equal deleted inserted replaced
7696:78a00bc68913 7697:767d3c4153a1
     4  *                                                                  *
     4  *                                                                  *
     5  * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS     *
     5  * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS     *
     6  * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE *
     6  * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE *
     7  * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING.       *
     7  * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING.       *
     8  *                                                                  *
     8  *                                                                  *
     9  * THE OggVorbis 'TREMOR' SOURCE CODE IS (C) COPYRIGHT 1994-2002    *
     9  * THE OggVorbis 'TREMOR' SOURCE CODE IS (C) COPYRIGHT 1994-2003    *
    10  * BY THE Xiph.Org FOUNDATION http://www.xiph.org/                  *
    10  * BY THE Xiph.Org FOUNDATION http://www.xiph.org/                  *
    11  *                                                                  *
    11  *                                                                  *
    12  ********************************************************************
    12  ********************************************************************
    13 
    13 
    14  function: miscellaneous math and prototypes
    14  function: miscellaneous math and prototypes
    16  ********************************************************************/
    16  ********************************************************************/
    17 
    17 
    18 #ifndef _V_RANDOM_H_
    18 #ifndef _V_RANDOM_H_
    19 #define _V_RANDOM_H_
    19 #define _V_RANDOM_H_
    20 #include "ivorbiscodec.h"
    20 #include "ivorbiscodec.h"
    21 #include "os.h"
    21 #include "os_types.h"
       
    22 
       
    23 /*#define _VDBG_GRAPHFILE "_0.m"*/
       
    24 
       
    25 #ifdef _VDBG_GRAPHFILE
       
    26 extern void *_VDBG_malloc(void *ptr,long bytes,char *file,long line); 
       
    27 extern void _VDBG_free(void *ptr,char *file,long line); 
       
    28 
       
    29 #undef _ogg_malloc
       
    30 #undef _ogg_calloc
       
    31 #undef _ogg_realloc
       
    32 #undef _ogg_free
       
    33 
       
    34 #define _ogg_malloc(x) _VDBG_malloc(NULL,(x),__FILE__,__LINE__)
       
    35 #define _ogg_calloc(x,y) _VDBG_malloc(NULL,(x)*(y),__FILE__,__LINE__)
       
    36 #define _ogg_realloc(x,y) _VDBG_malloc((x),(y),__FILE__,__LINE__)
       
    37 #define _ogg_free(x) _VDBG_free((x),__FILE__,__LINE__)
       
    38 #endif
    22 
    39 
    23 #include "asm_arm.h"
    40 #include "asm_arm.h"
    24 #include <stdlib.h> /* for abs() */
       
    25   
    41   
    26 #ifndef _V_WIDE_MATH
    42 #ifndef _V_WIDE_MATH
    27 #define _V_WIDE_MATH
    43 #define _V_WIDE_MATH
    28   
    44   
    29 #ifndef  _LOW_ACCURACY_
    45 #ifndef  _LOW_ACCURACY_
    30 /* 64 bit multiply */
    46 /* 64 bit multiply */
    31 
    47 
    32 #if !(defined WIN32 && defined WINCE)
       
    33 #include <sys/types.h>
    48 #include <sys/types.h>
    34 #endif
       
    35 
    49 
    36 #if BYTE_ORDER==LITTLE_ENDIAN
    50 #if BYTE_ORDER==LITTLE_ENDIAN
    37 union magic {
    51 union magic {
    38   struct {
    52   struct {
    39     ogg_int32_t lo;
    53     ogg_int32_t lo;
    42   ogg_int64_t whole;
    56   ogg_int64_t whole;
    43 };
    57 };
    44 #endif 
    58 #endif 
    45 
    59 
    46 #if BYTE_ORDER==BIG_ENDIAN
    60 #if BYTE_ORDER==BIG_ENDIAN
    47 /*union magic {
    61 union magic {
    48   struct {
    62   struct {
    49     ogg_int32_t hi;
    63     ogg_int32_t hi;
    50     ogg_int32_t lo;
    64     ogg_int32_t lo;
    51   } halves;
    65   } halves;
    52   ogg_int64_t whole;
    66   ogg_int64_t whole;
    53 };*/
    67 };
    54 #endif
    68 #endif
    55 
    69 
    56 STIN ogg_int32_t MULT32(ogg_int32_t x, ogg_int32_t y) {
    70 static inline ogg_int32_t MULT32(ogg_int32_t x, ogg_int32_t y) {
    57   union magic magic;
    71   union magic magic;
    58   magic.whole = (ogg_int64_t)x * y;
    72   magic.whole = (ogg_int64_t)x * y;
    59   return magic.halves.hi;
    73   return magic.halves.hi;
    60 }
    74 }
    61 
    75 
    62 STIN ogg_int32_t MULT31(ogg_int32_t x, ogg_int32_t y) {
    76 static inline ogg_int32_t MULT31(ogg_int32_t x, ogg_int32_t y) {
    63   return MULT32(x,y)<<1;
    77   return MULT32(x,y)<<1;
    64 }
    78 }
    65 
    79 
    66 STIN ogg_int32_t MULT31_SHIFT15(ogg_int32_t x, ogg_int32_t y) {
    80 static inline ogg_int32_t MULT31_SHIFT15(ogg_int32_t x, ogg_int32_t y) {
    67   union magic magic;
    81   union magic magic;
    68   magic.whole  = (ogg_int64_t)x * y;
    82   magic.whole  = (ogg_int64_t)x * y;
    69   return ((ogg_uint32_t)(magic.halves.lo)>>15) | ((magic.halves.hi)<<17);
    83   return ((ogg_uint32_t)(magic.halves.lo)>>15) | ((magic.halves.hi)<<17);
    70 }
    84 }
    71 
    85 
    83  * value already preshifted from 31 to 8 bits.  We therefore take the 
    97  * value already preshifted from 31 to 8 bits.  We therefore take the 
    84  * opportunity to save on text space and use unsigned char for those
    98  * opportunity to save on text space and use unsigned char for those
    85  * tables in this case.
    99  * tables in this case.
    86  */
   100  */
    87 
   101 
    88 STIN ogg_int32_t MULT32(ogg_int32_t x, ogg_int32_t y) {
   102 static inline ogg_int32_t MULT32(ogg_int32_t x, ogg_int32_t y) {
    89   return (x >> 9) * y;  /* y preshifted >>23 */
   103   return (x >> 9) * y;  /* y preshifted >>23 */
    90 }
   104 }
    91 
   105 
    92 STIN ogg_int32_t MULT31(ogg_int32_t x, ogg_int32_t y) {
   106 static inline ogg_int32_t MULT31(ogg_int32_t x, ogg_int32_t y) {
    93   return (x >> 8) * y;  /* y preshifted >>23 */
   107   return (x >> 8) * y;  /* y preshifted >>23 */
    94 }
   108 }
    95 
   109 
    96 STIN ogg_int32_t MULT31_SHIFT15(ogg_int32_t x, ogg_int32_t y) {
   110 static inline ogg_int32_t MULT31_SHIFT15(ogg_int32_t x, ogg_int32_t y) {
    97   return (x >> 6) * y;  /* y preshifted >>9 */
   111   return (x >> 6) * y;  /* y preshifted >>9 */
    98 }
   112 }
    99 
   113 
   100 #endif
   114 #endif
   101 
   115 
   128   { *(_x)=MULT31(_a,_t)-MULT31(_b,_v);		\
   142   { *(_x)=MULT31(_a,_t)-MULT31(_b,_v);		\
   129     *(_y)=MULT31(_b,_t)+MULT31(_a,_v); }
   143     *(_y)=MULT31(_b,_t)+MULT31(_a,_v); }
   130 
   144 
   131 #else
   145 #else
   132 
   146 
   133 STIN void XPROD32(ogg_int32_t  a, ogg_int32_t  b,
   147 static inline void XPROD32(ogg_int32_t  a, ogg_int32_t  b,
   134 			   ogg_int32_t  t, ogg_int32_t  v,
   148 			   ogg_int32_t  t, ogg_int32_t  v,
   135 			   ogg_int32_t *x, ogg_int32_t *y)
   149 			   ogg_int32_t *x, ogg_int32_t *y)
   136 {
   150 {
   137   *x = MULT32(a, t) + MULT32(b, v);
   151   *x = MULT32(a, t) + MULT32(b, v);
   138   *y = MULT32(b, t) - MULT32(a, v);
   152   *y = MULT32(b, t) - MULT32(a, v);
   139 }
   153 }
   140 
   154 
   141 STIN void XPROD31(ogg_int32_t  a, ogg_int32_t  b,
   155 static inline void XPROD31(ogg_int32_t  a, ogg_int32_t  b,
   142 			   ogg_int32_t  t, ogg_int32_t  v,
   156 			   ogg_int32_t  t, ogg_int32_t  v,
   143 			   ogg_int32_t *x, ogg_int32_t *y)
   157 			   ogg_int32_t *x, ogg_int32_t *y)
   144 {
   158 {
   145   *x = MULT31(a, t) + MULT31(b, v);
   159   *x = MULT31(a, t) + MULT31(b, v);
   146   *y = MULT31(b, t) - MULT31(a, v);
   160   *y = MULT31(b, t) - MULT31(a, v);
   147 }
   161 }
   148 
   162 
   149 STIN void XNPROD31(ogg_int32_t  a, ogg_int32_t  b,
   163 static inline void XNPROD31(ogg_int32_t  a, ogg_int32_t  b,
   150 			    ogg_int32_t  t, ogg_int32_t  v,
   164 			    ogg_int32_t  t, ogg_int32_t  v,
   151 			    ogg_int32_t *x, ogg_int32_t *y)
   165 			    ogg_int32_t *x, ogg_int32_t *y)
   152 {
   166 {
   153   *x = MULT31(a, t) - MULT31(b, v);
   167   *x = MULT31(a, t) - MULT31(b, v);
   154   *y = MULT31(b, t) + MULT31(a, v);
   168   *y = MULT31(b, t) + MULT31(a, v);
   159 #endif
   173 #endif
   160 
   174 
   161 #ifndef _V_CLIP_MATH
   175 #ifndef _V_CLIP_MATH
   162 #define _V_CLIP_MATH
   176 #define _V_CLIP_MATH
   163 
   177 
   164 STIN ogg_int32_t CLIP_TO_15(ogg_int32_t x) {
   178 static inline ogg_int32_t CLIP_TO_15(ogg_int32_t x) {
   165   int ret=x;
   179   int ret=x;
   166   ret-= ((x<=32767)-1)&(x-32767);
   180   ret-= ((x<=32767)-1)&(x-32767);
   167   ret-= ((x>=-32768)-1)&(x+32768);
   181   ret-= ((x>=-32768)-1)&(x+32768);
   168   return(ret);
   182   return(ret);
   169 }
   183 }
   170 
   184 
   171 #endif
   185 #endif
   172 
   186 
   173 STIN ogg_int32_t VFLOAT_MULT(ogg_int32_t a,ogg_int32_t ap,
       
   174 				      ogg_int32_t b,ogg_int32_t bp,
       
   175 				      ogg_int32_t *p){
       
   176   if(a && b){
       
   177 #ifndef _LOW_ACCURACY_
       
   178     *p=ap+bp+32;
       
   179     return MULT32(a,b);
       
   180 #else
       
   181     *p=ap+bp+31;
       
   182     return (a>>15)*(b>>16); 
       
   183 #endif
       
   184   }else
       
   185     return 0;
       
   186 }
       
   187 
       
   188 int _ilog(unsigned int);
       
   189 
       
   190 STIN ogg_int32_t VFLOAT_MULTI(ogg_int32_t a,ogg_int32_t ap,
       
   191 				      ogg_int32_t i,
       
   192 				      ogg_int32_t *p){
       
   193 
       
   194   int ip=_ilog(abs(i))-31;
       
   195   return VFLOAT_MULT(a,ap,i<<-ip,ip,p);
       
   196 }
       
   197 
       
   198 STIN ogg_int32_t VFLOAT_ADD(ogg_int32_t a,ogg_int32_t ap,
       
   199 				      ogg_int32_t b,ogg_int32_t bp,
       
   200 				      ogg_int32_t *p){
       
   201 
       
   202   if(!a){
       
   203     *p=bp;
       
   204     return b;
       
   205   }else if(!b){
       
   206     *p=ap;
       
   207     return a;
       
   208   }
       
   209 
       
   210   /* yes, this can leak a bit. */
       
   211   if(ap>bp){
       
   212     int shift=ap-bp+1;
       
   213     *p=ap+1;
       
   214     a>>=1;
       
   215     if(shift<32){
       
   216       b=(b+(1<<(shift-1)))>>shift;
       
   217     }else{
       
   218       b=0;
       
   219     }
       
   220   }else{
       
   221     int shift=bp-ap+1;
       
   222     *p=bp+1;
       
   223     b>>=1;
       
   224     if(shift<32){
       
   225       a=(a+(1<<(shift-1)))>>shift;
       
   226     }else{
       
   227       a=0;
       
   228     }
       
   229   }
       
   230 
       
   231   a+=b;
       
   232   if((a&0xc0000000)==0xc0000000 || 
       
   233      (a&0xc0000000)==0){
       
   234     a<<=1;
       
   235     (*p)--;
       
   236   }
       
   237   return(a);
       
   238 }
       
   239 
       
   240 #endif
   187 #endif
   241 
   188 
   242 
   189 
   243 
   190 
   244 
   191