misc/libtremor/tremor/misc.h
changeset 7849 a12155461b34
parent 7697 767d3c4153a1
equal deleted inserted replaced
7848:775a72905708 7849:a12155461b34
     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-2003    *
     9  * THE OggVorbis 'TREMOR' SOURCE CODE IS (C) COPYRIGHT 1994-2002    *
    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_types.h"
    21 #include "os.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
       
    39 
    22 
    40 #include "asm_arm.h"
    23 #include "asm_arm.h"
       
    24 #include <stdlib.h> /* for abs() */
    41   
    25   
    42 #ifndef _V_WIDE_MATH
    26 #ifndef _V_WIDE_MATH
    43 #define _V_WIDE_MATH
    27 #define _V_WIDE_MATH
    44   
    28   
    45 #ifndef  _LOW_ACCURACY_
    29 #ifndef  _LOW_ACCURACY_
    46 /* 64 bit multiply */
    30 /* 64 bit multiply */
    47 
    31 
       
    32 #if !(defined WIN32 && defined WINCE)
    48 #include <sys/types.h>
    33 #include <sys/types.h>
       
    34 #endif
    49 
    35 
    50 #if BYTE_ORDER==LITTLE_ENDIAN
    36 #if BYTE_ORDER==LITTLE_ENDIAN
    51 union magic {
    37 union magic {
    52   struct {
    38   struct {
    53     ogg_int32_t lo;
    39     ogg_int32_t lo;
    56   ogg_int64_t whole;
    42   ogg_int64_t whole;
    57 };
    43 };
    58 #endif 
    44 #endif 
    59 
    45 
    60 #if BYTE_ORDER==BIG_ENDIAN
    46 #if BYTE_ORDER==BIG_ENDIAN
    61 union magic {
    47 /*union magic {
    62   struct {
    48   struct {
    63     ogg_int32_t hi;
    49     ogg_int32_t hi;
    64     ogg_int32_t lo;
    50     ogg_int32_t lo;
    65   } halves;
    51   } halves;
    66   ogg_int64_t whole;
    52   ogg_int64_t whole;
    67 };
    53 };*/
    68 #endif
    54 #endif
    69 
    55 
    70 static inline ogg_int32_t MULT32(ogg_int32_t x, ogg_int32_t y) {
    56 STIN ogg_int32_t MULT32(ogg_int32_t x, ogg_int32_t y) {
    71   union magic magic;
    57   union magic magic;
    72   magic.whole = (ogg_int64_t)x * y;
    58   magic.whole = (ogg_int64_t)x * y;
    73   return magic.halves.hi;
    59   return magic.halves.hi;
    74 }
    60 }
    75 
    61 
    76 static inline ogg_int32_t MULT31(ogg_int32_t x, ogg_int32_t y) {
    62 STIN ogg_int32_t MULT31(ogg_int32_t x, ogg_int32_t y) {
    77   return MULT32(x,y)<<1;
    63   return MULT32(x,y)<<1;
    78 }
    64 }
    79 
    65 
    80 static inline ogg_int32_t MULT31_SHIFT15(ogg_int32_t x, ogg_int32_t y) {
    66 STIN ogg_int32_t MULT31_SHIFT15(ogg_int32_t x, ogg_int32_t y) {
    81   union magic magic;
    67   union magic magic;
    82   magic.whole  = (ogg_int64_t)x * y;
    68   magic.whole  = (ogg_int64_t)x * y;
    83   return ((ogg_uint32_t)(magic.halves.lo)>>15) | ((magic.halves.hi)<<17);
    69   return ((ogg_uint32_t)(magic.halves.lo)>>15) | ((magic.halves.hi)<<17);
    84 }
    70 }
    85 
    71 
    97  * value already preshifted from 31 to 8 bits.  We therefore take the 
    83  * value already preshifted from 31 to 8 bits.  We therefore take the 
    98  * opportunity to save on text space and use unsigned char for those
    84  * opportunity to save on text space and use unsigned char for those
    99  * tables in this case.
    85  * tables in this case.
   100  */
    86  */
   101 
    87 
   102 static inline ogg_int32_t MULT32(ogg_int32_t x, ogg_int32_t y) {
    88 STIN ogg_int32_t MULT32(ogg_int32_t x, ogg_int32_t y) {
   103   return (x >> 9) * y;  /* y preshifted >>23 */
    89   return (x >> 9) * y;  /* y preshifted >>23 */
   104 }
    90 }
   105 
    91 
   106 static inline ogg_int32_t MULT31(ogg_int32_t x, ogg_int32_t y) {
    92 STIN ogg_int32_t MULT31(ogg_int32_t x, ogg_int32_t y) {
   107   return (x >> 8) * y;  /* y preshifted >>23 */
    93   return (x >> 8) * y;  /* y preshifted >>23 */
   108 }
    94 }
   109 
    95 
   110 static inline ogg_int32_t MULT31_SHIFT15(ogg_int32_t x, ogg_int32_t y) {
    96 STIN ogg_int32_t MULT31_SHIFT15(ogg_int32_t x, ogg_int32_t y) {
   111   return (x >> 6) * y;  /* y preshifted >>9 */
    97   return (x >> 6) * y;  /* y preshifted >>9 */
   112 }
    98 }
   113 
    99 
   114 #endif
   100 #endif
   115 
   101 
   142   { *(_x)=MULT31(_a,_t)-MULT31(_b,_v);		\
   128   { *(_x)=MULT31(_a,_t)-MULT31(_b,_v);		\
   143     *(_y)=MULT31(_b,_t)+MULT31(_a,_v); }
   129     *(_y)=MULT31(_b,_t)+MULT31(_a,_v); }
   144 
   130 
   145 #else
   131 #else
   146 
   132 
   147 static inline void XPROD32(ogg_int32_t  a, ogg_int32_t  b,
   133 STIN void XPROD32(ogg_int32_t  a, ogg_int32_t  b,
   148 			   ogg_int32_t  t, ogg_int32_t  v,
   134 			   ogg_int32_t  t, ogg_int32_t  v,
   149 			   ogg_int32_t *x, ogg_int32_t *y)
   135 			   ogg_int32_t *x, ogg_int32_t *y)
   150 {
   136 {
   151   *x = MULT32(a, t) + MULT32(b, v);
   137   *x = MULT32(a, t) + MULT32(b, v);
   152   *y = MULT32(b, t) - MULT32(a, v);
   138   *y = MULT32(b, t) - MULT32(a, v);
   153 }
   139 }
   154 
   140 
   155 static inline void XPROD31(ogg_int32_t  a, ogg_int32_t  b,
   141 STIN void XPROD31(ogg_int32_t  a, ogg_int32_t  b,
   156 			   ogg_int32_t  t, ogg_int32_t  v,
   142 			   ogg_int32_t  t, ogg_int32_t  v,
   157 			   ogg_int32_t *x, ogg_int32_t *y)
   143 			   ogg_int32_t *x, ogg_int32_t *y)
   158 {
   144 {
   159   *x = MULT31(a, t) + MULT31(b, v);
   145   *x = MULT31(a, t) + MULT31(b, v);
   160   *y = MULT31(b, t) - MULT31(a, v);
   146   *y = MULT31(b, t) - MULT31(a, v);
   161 }
   147 }
   162 
   148 
   163 static inline void XNPROD31(ogg_int32_t  a, ogg_int32_t  b,
   149 STIN void XNPROD31(ogg_int32_t  a, ogg_int32_t  b,
   164 			    ogg_int32_t  t, ogg_int32_t  v,
   150 			    ogg_int32_t  t, ogg_int32_t  v,
   165 			    ogg_int32_t *x, ogg_int32_t *y)
   151 			    ogg_int32_t *x, ogg_int32_t *y)
   166 {
   152 {
   167   *x = MULT31(a, t) - MULT31(b, v);
   153   *x = MULT31(a, t) - MULT31(b, v);
   168   *y = MULT31(b, t) + MULT31(a, v);
   154   *y = MULT31(b, t) + MULT31(a, v);
   173 #endif
   159 #endif
   174 
   160 
   175 #ifndef _V_CLIP_MATH
   161 #ifndef _V_CLIP_MATH
   176 #define _V_CLIP_MATH
   162 #define _V_CLIP_MATH
   177 
   163 
   178 static inline ogg_int32_t CLIP_TO_15(ogg_int32_t x) {
   164 STIN ogg_int32_t CLIP_TO_15(ogg_int32_t x) {
   179   int ret=x;
   165   int ret=x;
   180   ret-= ((x<=32767)-1)&(x-32767);
   166   ret-= ((x<=32767)-1)&(x-32767);
   181   ret-= ((x>=-32768)-1)&(x+32768);
   167   ret-= ((x>=-32768)-1)&(x+32768);
   182   return(ret);
   168   return(ret);
   183 }
   169 }
   184 
   170 
   185 #endif
   171 #endif
   186 
   172 
   187 #endif
   173 STIN ogg_int32_t VFLOAT_MULT(ogg_int32_t a,ogg_int32_t ap,
   188 
   174 				      ogg_int32_t b,ogg_int32_t bp,
   189 
   175 				      ogg_int32_t *p){
   190 
   176   if(a && b){
   191 
   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
       
   241 
       
   242 
       
   243 
       
   244