00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 # ifndef LIBMAD_FIXED_H
00023 # define LIBMAD_FIXED_H
00024
00025 # if SIZEOF_INT >= 4
00026 typedef signed int mad_fixed_t;
00027
00028 typedef signed int mad_fixed64hi_t;
00029 typedef unsigned int mad_fixed64lo_t;
00030 # else
00031 typedef signed long mad_fixed_t;
00032
00033 typedef signed long mad_fixed64hi_t;
00034 typedef unsigned long mad_fixed64lo_t;
00035 # endif
00036
00037 # if defined(_MSC_VER)
00038 # define mad_fixed64_t signed __int64
00039 # elif 1 || defined(__GNUC__)
00040 # define mad_fixed64_t signed long long
00041 # endif
00042
00043 # if defined(FPM_FLOAT)
00044 typedef double mad_sample_t;
00045 # else
00046 typedef mad_fixed_t mad_sample_t;
00047 # endif
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072 # define MAD_F_FRACBITS 28
00073
00074 # if MAD_F_FRACBITS == 28
00075 # define MAD_F(x) ((mad_fixed_t) (x##L))
00076 # else
00077 # if MAD_F_FRACBITS < 28
00078 # warning "MAD_F_FRACBITS < 28"
00079 # define MAD_F(x) ((mad_fixed_t) \
00080 (((x##L) + \
00081 (1L << (28 - MAD_F_FRACBITS - 1))) >> \
00082 (28 - MAD_F_FRACBITS)))
00083 # elif MAD_F_FRACBITS > 28
00084 # error "MAD_F_FRACBITS > 28 not currently supported"
00085 # define MAD_F(x) ((mad_fixed_t) \
00086 ((x##L) << (MAD_F_FRACBITS - 28)))
00087 # endif
00088 # endif
00089
00090 # define MAD_F_MIN ((mad_fixed_t) -0x80000000L)
00091 # define MAD_F_MAX ((mad_fixed_t) +0x7fffffffL)
00092
00093 # define MAD_F_ONE MAD_F(0x10000000)
00094
00095 # define mad_f_tofixed(x) ((mad_fixed_t) \
00096 ((x) * (double) (1L << MAD_F_FRACBITS) + 0.5))
00097 # define mad_f_todouble(x) ((double) \
00098 ((x) / (double) (1L << MAD_F_FRACBITS)))
00099
00100 # define mad_f_intpart(x) ((x) >> MAD_F_FRACBITS)
00101 # define mad_f_fracpart(x) ((x) & ((1L << MAD_F_FRACBITS) - 1))
00102
00103
00104 # define mad_f_fromint(x) ((x) << MAD_F_FRACBITS)
00105
00106 # define mad_f_add(x, y) ((x) + (y))
00107 # define mad_f_sub(x, y) ((x) - (y))
00108
00109 # if defined(FPM_FLOAT)
00110 # error "FPM_FLOAT not yet supported"
00111
00112 # undef MAD_F
00113 # define MAD_F(x) mad_f_todouble(x)
00114
00115 # define mad_f_mul(x, y) ((x) * (y))
00116 # define mad_f_scale64
00117
00118 # undef ASO_ZEROCHECK
00119
00120 # elif defined(FPM_64BIT)
00121
00122
00123
00124
00125
00126 # if defined(OPT_ACCURACY)
00127 # define mad_f_mul(x, y) \
00128 ((mad_fixed_t) \
00129 ((((mad_fixed64_t) (x) * (y)) + \
00130 (1L << (MAD_F_SCALEBITS - 1))) >> MAD_F_SCALEBITS))
00131 # else
00132 # define mad_f_mul(x, y) \
00133 ((mad_fixed_t) (((mad_fixed64_t) (x) * (y)) >> MAD_F_SCALEBITS))
00134 # endif
00135
00136 # define MAD_F_SCALEBITS MAD_F_FRACBITS
00137
00138
00139
00140 # elif defined(FPM_INTEL)
00141
00142 # if defined(_MSC_VER)
00143 # pragma warning(push)
00144 # pragma warning(disable: 4035)
00145 static __forceinline
00146 mad_fixed_t mad_f_mul_inline(mad_fixed_t x, mad_fixed_t y)
00147 {
00148 enum {
00149 fracbits = MAD_F_FRACBITS
00150 };
00151
00152 __asm {
00153 mov eax, x
00154 imul y
00155 shrd eax, edx, fracbits
00156 }
00157
00158
00159 }
00160 # pragma warning(pop)
00161
00162 # define mad_f_mul mad_f_mul_inline
00163 # define mad_f_scale64
00164 # else
00165
00166
00167
00168
00169 # define MAD_F_MLX(hi, lo, x, y) \
00170 asm ("imull %3" \
00171 : "=a" (lo), "=d" (hi) \
00172 : "%a" (x), "rm" (y) \
00173 : "cc")
00174
00175 # if defined(OPT_ACCURACY)
00176
00177
00178
00179 # define MAD_F_MLA(hi, lo, x, y) \
00180 ({ mad_fixed64hi_t __hi; \
00181 mad_fixed64lo_t __lo; \
00182 MAD_F_MLX(__hi, __lo, (x), (y)); \
00183 asm ("addl %2,%0\n\t" \
00184 "adcl %3,%1" \
00185 : "=rm" (lo), "=rm" (hi) \
00186 : "r" (__lo), "r" (__hi), "0" (lo), "1" (hi) \
00187 : "cc"); \
00188 })
00189 # endif
00190
00191 # if defined(OPT_ACCURACY)
00192
00193
00194
00195 # define mad_f_scale64(hi, lo) \
00196 ({ mad_fixed64hi_t __hi_; \
00197 mad_fixed64lo_t __lo_; \
00198 mad_fixed_t __result; \
00199 asm ("addl %4,%2\n\t" \
00200 "adcl %5,%3" \
00201 : "=rm" (__lo_), "=rm" (__hi_) \
00202 : "0" (lo), "1" (hi), \
00203 "ir" (1L << (MAD_F_SCALEBITS - 1)), "ir" (0) \
00204 : "cc"); \
00205 asm ("shrdl %3,%2,%1" \
00206 : "=rm" (__result) \
00207 : "0" (__lo_), "r" (__hi_), "I" (MAD_F_SCALEBITS) \
00208 : "cc"); \
00209 __result; \
00210 })
00211 # else
00212 # define mad_f_scale64(hi, lo) \
00213 ({ mad_fixed_t __result; \
00214 asm ("shrdl %3,%2,%1" \
00215 : "=rm" (__result) \
00216 : "0" (lo), "r" (hi), "I" (MAD_F_SCALEBITS) \
00217 : "cc"); \
00218 __result; \
00219 })
00220 # endif
00221
00222 # define MAD_F_SCALEBITS MAD_F_FRACBITS
00223 # endif
00224
00225
00226
00227 # elif defined(FPM_ARM)
00228
00229
00230
00231
00232
00233 # if 1
00234
00235
00236
00237
00238 # define mad_f_mul(x, y) \
00239 ({ mad_fixed64hi_t __hi; \
00240 mad_fixed64lo_t __lo; \
00241 mad_fixed_t __result; \
00242 asm ("smull %0, %1, %3, %4\n\t" \
00243 "movs %0, %0, lsr %5\n\t" \
00244 "adc %2, %0, %1, lsl %6" \
00245 : "=&r" (__lo), "=&r" (__hi), "=r" (__result) \
00246 : "%r" (x), "r" (y), \
00247 "M" (MAD_F_SCALEBITS), "M" (32 - MAD_F_SCALEBITS) \
00248 : "cc"); \
00249 __result; \
00250 })
00251 # endif
00252
00253 # define MAD_F_MLX(hi, lo, x, y) \
00254 asm ("smull %0, %1, %2, %3" \
00255 : "=&r" (lo), "=&r" (hi) \
00256 : "%r" (x), "r" (y))
00257
00258 # define MAD_F_MLA(hi, lo, x, y) \
00259 asm ("smlal %0, %1, %2, %3" \
00260 : "+r" (lo), "+r" (hi) \
00261 : "%r" (x), "r" (y))
00262
00263 # define MAD_F_MLN(hi, lo) \
00264 asm ("rsbs %0, %2, #0\n\t" \
00265 "rsc %1, %3, #0" \
00266 : "=r" (lo), "=r" (hi) \
00267 : "0" (lo), "1" (hi) \
00268 : "cc")
00269
00270 # define mad_f_scale64(hi, lo) \
00271 ({ mad_fixed_t __result; \
00272 asm ("movs %0, %1, lsr %3\n\t" \
00273 "adc %0, %0, %2, lsl %4" \
00274 : "=&r" (__result) \
00275 : "r" (lo), "r" (hi), \
00276 "M" (MAD_F_SCALEBITS), "M" (32 - MAD_F_SCALEBITS) \
00277 : "cc"); \
00278 __result; \
00279 })
00280
00281 # define MAD_F_SCALEBITS MAD_F_FRACBITS
00282
00283
00284
00285 # elif defined(FPM_MIPS)
00286
00287
00288
00289
00290
00291 # define MAD_F_MLX(hi, lo, x, y) \
00292 asm ("mult %2,%3" \
00293 : "=l" (lo), "=h" (hi) \
00294 : "%r" (x), "r" (y))
00295
00296 # if defined(HAVE_MADD_ASM)
00297 # define MAD_F_MLA(hi, lo, x, y) \
00298 asm ("madd %2,%3" \
00299 : "+l" (lo), "+h" (hi) \
00300 : "%r" (x), "r" (y))
00301 # elif defined(HAVE_MADD16_ASM)
00302
00303
00304
00305
00306 # define MAD_F_ML0(hi, lo, x, y) \
00307 asm ("mult %2,%3" \
00308 : "=l" (lo), "=h" (hi) \
00309 : "%r" ((x) >> 12), "r" ((y) >> 16))
00310 # define MAD_F_MLA(hi, lo, x, y) \
00311 asm ("madd16 %2,%3" \
00312 : "+l" (lo), "+h" (hi) \
00313 : "%r" ((x) >> 12), "r" ((y) >> 16))
00314 # define MAD_F_MLZ(hi, lo) ((mad_fixed_t) (lo))
00315 # endif
00316
00317 # if defined(OPT_SPEED)
00318 # define mad_f_scale64(hi, lo) \
00319 ((mad_fixed_t) ((hi) << (32 - MAD_F_SCALEBITS)))
00320 # define MAD_F_SCALEBITS MAD_F_FRACBITS
00321 # endif
00322
00323
00324
00325 # elif defined(FPM_SPARC)
00326
00327
00328
00329
00330
00331 # define MAD_F_MLX(hi, lo, x, y) \
00332 asm ("smul %2, %3, %0\n\t" \
00333 "rd %%y, %1" \
00334 : "=r" (lo), "=r" (hi) \
00335 : "%r" (x), "rI" (y))
00336
00337
00338
00339 # elif defined(FPM_PPC)
00340
00341
00342
00343
00344
00345 # define MAD_F_MLX(hi, lo, x, y) \
00346 do { \
00347 asm ("mullw %0,%1,%2" \
00348 : "=r" (lo) \
00349 : "%r" (x), "r" (y)); \
00350 asm ("mulhw %0,%1,%2" \
00351 : "=r" (hi) \
00352 : "%r" (x), "r" (y)); \
00353 } \
00354 while (0)
00355
00356 # if defined(OPT_ACCURACY)
00357
00358
00359
00360 # define MAD_F_MLA(hi, lo, x, y) \
00361 ({ mad_fixed64hi_t __hi; \
00362 mad_fixed64lo_t __lo; \
00363 MAD_F_MLX(__hi, __lo, (x), (y)); \
00364 asm ("addc %0,%2,%3\n\t" \
00365 "adde %1,%4,%5" \
00366 : "=r" (lo), "=r" (hi) \
00367 : "%r" (lo), "r" (__lo), \
00368 "%r" (hi), "r" (__hi) \
00369 : "xer"); \
00370 })
00371 # endif
00372
00373 # if defined(OPT_ACCURACY)
00374
00375
00376
00377 # define mad_f_scale64(hi, lo) \
00378 ({ mad_fixed_t __result, __round; \
00379 asm ("rotrwi %0,%1,%2" \
00380 : "=r" (__result) \
00381 : "r" (lo), "i" (MAD_F_SCALEBITS)); \
00382 asm ("extrwi %0,%1,1,0" \
00383 : "=r" (__round) \
00384 : "r" (__result)); \
00385 asm ("insrwi %0,%1,%2,0" \
00386 : "+r" (__result) \
00387 : "r" (hi), "i" (MAD_F_SCALEBITS)); \
00388 asm ("add %0,%1,%2" \
00389 : "=r" (__result) \
00390 : "%r" (__result), "r" (__round)); \
00391 __result; \
00392 })
00393 # else
00394 # define mad_f_scale64(hi, lo) \
00395 ({ mad_fixed_t __result; \
00396 asm ("rotrwi %0,%1,%2" \
00397 : "=r" (__result) \
00398 : "r" (lo), "i" (MAD_F_SCALEBITS)); \
00399 asm ("insrwi %0,%1,%2,0" \
00400 : "+r" (__result) \
00401 : "r" (hi), "i" (MAD_F_SCALEBITS)); \
00402 __result; \
00403 })
00404 # endif
00405
00406 # define MAD_F_SCALEBITS MAD_F_FRACBITS
00407
00408
00409
00410 # elif defined(FPM_DEFAULT)
00411
00412
00413
00414
00415
00416
00417
00418
00419
00420
00421 # if defined(OPT_SPEED)
00422 # define mad_f_mul(x, y) (((x) >> 12) * ((y) >> 16))
00423 # else
00424 # define mad_f_mul(x, y) ((((x) + (1L << 11)) >> 12) * \
00425 (((y) + (1L << 15)) >> 16))
00426 # endif
00427
00428
00429
00430 # else
00431 # error "no FPM selected"
00432 # endif
00433
00434
00435
00436 # if !defined(mad_f_mul)
00437 # define mad_f_mul(x, y) \
00438 ({ register mad_fixed64hi_t __hi; \
00439 register mad_fixed64lo_t __lo; \
00440 MAD_F_MLX(__hi, __lo, (x), (y)); \
00441 mad_f_scale64(__hi, __lo); \
00442 })
00443 # endif
00444
00445 # if !defined(MAD_F_MLA)
00446 # define MAD_F_ML0(hi, lo, x, y) ((lo) = mad_f_mul((x), (y)))
00447 # define MAD_F_MLA(hi, lo, x, y) ((lo) += mad_f_mul((x), (y)))
00448 # define MAD_F_MLN(hi, lo) ((lo) = -(lo))
00449 # define MAD_F_MLZ(hi, lo) ((void) (hi), (mad_fixed_t) (lo))
00450 # endif
00451
00452 # if !defined(MAD_F_ML0)
00453 # define MAD_F_ML0(hi, lo, x, y) MAD_F_MLX((hi), (lo), (x), (y))
00454 # endif
00455
00456 # if !defined(MAD_F_MLN)
00457 # define MAD_F_MLN(hi, lo) ((hi) = ((lo) = -(lo)) ? ~(hi) : -(hi))
00458 # endif
00459
00460 # if !defined(MAD_F_MLZ)
00461 # define MAD_F_MLZ(hi, lo) mad_f_scale64((hi), (lo))
00462 # endif
00463
00464 # if !defined(mad_f_scale64)
00465 # if defined(OPT_ACCURACY)
00466 # define mad_f_scale64(hi, lo) \
00467 ((((mad_fixed_t) \
00468 (((hi) << (32 - (MAD_F_SCALEBITS - 1))) | \
00469 ((lo) >> (MAD_F_SCALEBITS - 1)))) + 1) >> 1)
00470 # else
00471 # define mad_f_scale64(hi, lo) \
00472 ((mad_fixed_t) \
00473 (((hi) << (32 - MAD_F_SCALEBITS)) | \
00474 ((lo) >> MAD_F_SCALEBITS)))
00475 # endif
00476 # define MAD_F_SCALEBITS MAD_F_FRACBITS
00477 # endif
00478
00479
00480
00481 mad_fixed_t mad_f_abs(mad_fixed_t);
00482 mad_fixed_t mad_f_div(mad_fixed_t, mad_fixed_t);
00483
00484 # endif