fixed.h
上传用户:tuheem
上传日期:2007-05-01
资源大小:21889k
文件大小:10k
- /*
- * libmad - MPEG audio decoder library
- */
- # ifndef LIBMAD_FIXED_H
- # define LIBMAD_FIXED_H
- # if SIZEOF_INT >= 4
- typedef signed int mad_fixed_t;
- typedef signed int mad_fixed64hi_t;
- typedef unsigned int mad_fixed64lo_t;
- # else
- typedef signed long mad_fixed_t;
- typedef signed long mad_fixed64hi_t;
- typedef unsigned long mad_fixed64lo_t;
- # endif
- # if defined(_MSC_VER)
- # define mad_fixed64_t signed __int64
- # elif 1 || defined(__GNUC__)
- # define mad_fixed64_t signed long long
- # endif
- # if defined(FPM_FLOAT)
- typedef double mad_sample_t;
- # else
- typedef mad_fixed_t mad_sample_t;
- # endif
- # define MAD_F_FRACBITS 28
- # if MAD_F_FRACBITS == 28
- # define MAD_F(x) ((mad_fixed_t) (x##L))
- # else
- # if MAD_F_FRACBITS < 28
- # warning "MAD_F_FRACBITS < 28"
- # define MAD_F(x) ((mad_fixed_t)
- (((x##L) +
- (1L << (28 - MAD_F_FRACBITS - 1))) >>
- (28 - MAD_F_FRACBITS)))
- # elif MAD_F_FRACBITS > 28
- # error "MAD_F_FRACBITS > 28 not currently supported"
- # define MAD_F(x) ((mad_fixed_t)
- ((x##L) << (MAD_F_FRACBITS - 28)))
- # endif
- # endif
- # define MAD_F_MIN ((mad_fixed_t) -0x80000000L)
- # define MAD_F_MAX ((mad_fixed_t) +0x7fffffffL)
- # define MAD_F_ONE MAD_F(0x10000000)
- # define mad_f_tofixed(x) ((mad_fixed_t)
- ((x) * (double) (1L << MAD_F_FRACBITS) + 0.5))
- # define mad_f_todouble(x) ((double)
- ((x) / (double) (1L << MAD_F_FRACBITS)))
- # define mad_f_intpart(x) ((x) >> MAD_F_FRACBITS)
- # define mad_f_fracpart(x) ((x) & ((1L << MAD_F_FRACBITS) - 1))
-
- # define mad_f_fromint(x) ((x) << MAD_F_FRACBITS)
- # define mad_f_add(x, y) ((x) + (y))
- # define mad_f_sub(x, y) ((x) - (y))
- # if defined(FPM_FLOAT)
- # error "FPM_FLOAT not yet supported"
- # undef MAD_F
- # define MAD_F(x) mad_f_todouble(x)
- # define mad_f_mul(x, y) ((x) * (y))
- # define mad_f_scale64
- # undef ASO_ZEROCHECK
- # elif defined(FPM_64BIT)
- # if defined(OPT_ACCURACY)
- # define mad_f_mul(x, y)
- ((mad_fixed_t)
- ((((mad_fixed64_t) (x) * (y)) +
- (1L << (MAD_F_SCALEBITS - 1))) >> MAD_F_SCALEBITS))
- # else
- # define mad_f_mul(x, y)
- ((mad_fixed_t) (((mad_fixed64_t) (x) * (y)) >> MAD_F_SCALEBITS))
- # endif
- # define MAD_F_SCALEBITS MAD_F_FRACBITS
- /* --- Intel --------------------------------------------------------------- */
- # elif defined(FPM_INTEL)
- # if defined(_MSC_VER)
- # pragma warning(push)
- # pragma warning(disable: 4035)
- static __forceinline
- mad_fixed_t mad_f_mul_inline(mad_fixed_t x, mad_fixed_t y)
- {
- enum {
- fracbits = MAD_F_FRACBITS
- };
- __asm {
- mov eax, x
- imul y
- shrd eax, edx, fracbits
- }
- }
- # pragma warning(pop)
- # define mad_f_mul mad_f_mul_inline
- # define mad_f_scale64
- # else
- # define MAD_F_MLX(hi, lo, x, y)
- asm ("imull %3"
- : "=a" (lo), "=d" (hi)
- : "%a" (x), "rm" (y)
- : "cc")
- # if defined(OPT_ACCURACY)
- # define MAD_F_MLA(hi, lo, x, y)
- ({ mad_fixed64hi_t __hi;
- mad_fixed64lo_t __lo;
- MAD_F_MLX(__hi, __lo, (x), (y));
- asm ("addl %2,%0nt"
- "adcl %3,%1"
- : "=rm" (lo), "=rm" (hi)
- : "r" (__lo), "r" (__hi), "0" (lo), "1" (hi)
- : "cc");
- })
- # endif
- # if defined(OPT_ACCURACY)
- # define mad_f_scale64(hi, lo)
- ({ mad_fixed64hi_t __hi_;
- mad_fixed64lo_t __lo_;
- mad_fixed_t __result;
- asm ("addl %4,%2nt"
- "adcl %5,%3"
- : "=rm" (__lo_), "=rm" (__hi_)
- : "0" (lo), "1" (hi),
- "ir" (1L << (MAD_F_SCALEBITS - 1)), "ir" (0)
- : "cc");
- asm ("shrdl %3,%2,%1"
- : "=rm" (__result)
- : "0" (__lo_), "r" (__hi_), "I" (MAD_F_SCALEBITS)
- : "cc");
- __result;
- })
- # else
- # define mad_f_scale64(hi, lo)
- ({ mad_fixed_t __result;
- asm ("shrdl %3,%2,%1"
- : "=rm" (__result)
- : "0" (lo), "r" (hi), "I" (MAD_F_SCALEBITS)
- : "cc");
- __result;
- })
- # endif
- # define MAD_F_SCALEBITS MAD_F_FRACBITS
- # endif
- /* --- ARM ----------------------------------------------------------------- */
- # elif defined(FPM_ARM)
- # if 1
- # define mad_f_mul(x, y)
- ({ mad_fixed64hi_t __hi;
- mad_fixed64lo_t __lo;
- mad_fixed_t __result;
- asm ("smull %0, %1, %3, %4nt"
- "movs %0, %0, lsr %5nt"
- "adc %2, %0, %1, lsl %6"
- : "=&r" (__lo), "=&r" (__hi), "=r" (__result)
- : "%r" (x), "r" (y),
- "M" (MAD_F_SCALEBITS), "M" (32 - MAD_F_SCALEBITS)
- : "cc");
- __result;
- })
- # endif
- # define MAD_F_MLX(hi, lo, x, y)
- asm ("smull %0, %1, %2, %3"
- : "=&r" (lo), "=&r" (hi)
- : "%r" (x), "r" (y))
- # define MAD_F_MLA(hi, lo, x, y)
- asm ("smlal %0, %1, %2, %3"
- : "+r" (lo), "+r" (hi)
- : "%r" (x), "r" (y))
- # define MAD_F_MLN(hi, lo)
- asm ("rsbs %0, %2, #0nt"
- "rsc %1, %3, #0"
- : "=r" (lo), "=r" (hi)
- : "0" (lo), "1" (hi)
- : "cc")
- # define mad_f_scale64(hi, lo)
- ({ mad_fixed_t __result;
- asm ("movs %0, %1, lsr %3nt"
- "adc %0, %0, %2, lsl %4"
- : "=r" (__result)
- : "r" (lo), "r" (hi),
- "M" (MAD_F_SCALEBITS), "M" (32 - MAD_F_SCALEBITS)
- : "cc");
- __result;
- })
- # define MAD_F_SCALEBITS MAD_F_FRACBITS
- /* --- MIPS ---------------------------------------------------------------- */
- # elif defined(FPM_MIPS)
- # define MAD_F_MLX(hi, lo, x, y)
- asm ("mult %2,%3"
- : "=l" (lo), "=h" (hi)
- : "%r" (x), "r" (y))
- # if defined(HAVE_MADD_ASM)
- # define MAD_F_MLA(hi, lo, x, y)
- asm ("madd %2,%3"
- : "+l" (lo), "+h" (hi)
- : "%r" (x), "r" (y))
- # elif defined(HAVE_MADD16_ASM)
- # define MAD_F_ML0(hi, lo, x, y)
- asm ("mult %2,%3"
- : "=l" (lo), "=h" (hi)
- : "%r" ((x) >> 12), "r" ((y) >> 16))
- # define MAD_F_MLA(hi, lo, x, y)
- asm ("madd16 %2,%3"
- : "+l" (lo), "+h" (hi)
- : "%r" ((x) >> 12), "r" ((y) >> 16))
- # define MAD_F_MLZ(hi, lo) ((mad_fixed_t) (lo))
- # endif
- # if defined(OPT_SPEED)
- # define mad_f_scale64(hi, lo)
- ((mad_fixed_t) ((hi) << (32 - MAD_F_SCALEBITS)))
- # define MAD_F_SCALEBITS MAD_F_FRACBITS
- # endif
- /* --- SPARC --------------------------------------------------------------- */
- # elif defined(FPM_SPARC)
- # define MAD_F_MLX(hi, lo, x, y)
- asm ("smul %2, %3, %0nt"
- "rd %%y, %1"
- : "=r" (lo), "=r" (hi)
- : "%r" (x), "rI" (y))
- /* --- PowerPC ------------------------------------------------------------- */
- # elif defined(FPM_PPC)
- # define MAD_F_MLX(hi, lo, x, y)
- asm ("mulhw %1, %2, %3nt"
- "mullw %0, %2, %3"
- : "=&r" (lo), "=&r" (hi)
- : "%r" (x), "r" (y))
- # define MAD_F_MLA(hi, lo, x, y)
- ({ mad_fixed64hi_t __hi;
- mad_fixed64lo_t __lo;
- MAD_F_MLX(__hi, __lo, (x), (y));
- asm ("addc %0, %2, %3nt"
- "adde %1, %4, %5"
- : "=r" (lo), "=r" (hi)
- : "%r" (__lo), "0" (lo), "%r" (__hi), "1" (hi));
- })
- # if defined(OPT_ACCURACY)
- # define mad_f_scale64(hi, lo)
- ({ mad_fixed_t __result;
- mad_fixed64hi_t __hi_;
- mad_fixed64lo_t __lo_;
- asm __volatile__ ("addc %0, %2, %4nt"
- "addze %1, %3"
- : "=r" (__lo_), "=r" (__hi_)
- : "r" (lo), "r" (hi), "r" (1 << (MAD_F_SCALEBITS - 1)));
- asm __volatile__ ("rlwinm %0, %2,32-%3,0,%3-1nt"
- "rlwimi %0, %1,32-%3,%3,31"
- : "=&r" (__result)
- : "r" (__lo_), "r" (__hi_), "I" (MAD_F_SCALEBITS));
- __result;
- })
- # else
- # define mad_f_scale64(hi, lo)
- ({ mad_fixed_t __result;
- asm ("rlwinm %0, %2,32-%3,0,%3-1nt"
- "rlwimi %0, %1,32-%3,%3,31"
- : "=r" (__result)
- : "r" (lo), "r" (hi), "I" (MAD_F_SCALEBITS));
- __result;
- })
- # endif
- # define MAD_F_SCALEBITS MAD_F_FRACBITS
- /* --- Default ------------------------------------------------------------- */
- # elif defined(FPM_DEFAULT)
- # if defined(OPT_SPEED)
- # define mad_f_mul(x, y) (((x) >> 12) * ((y) >> 16))
- # else
- # define mad_f_mul(x, y) ((((x) + (1L << 11)) >> 12) *
- (((y) + (1L << 15)) >> 16))
- # endif
- /* ------------------------------------------------------------------------- */
- # else
- # error "no FPM selected"
- # endif
- # if !defined(mad_f_mul)
- # define mad_f_mul(x, y)
- ({ mad_fixed64hi_t __hi;
- mad_fixed64lo_t __lo;
- MAD_F_MLX(__hi, __lo, (x), (y));
- mad_f_scale64(__hi, __lo);
- })
- # endif
- # if !defined(MAD_F_MLA)
- # define MAD_F_ML0(hi, lo, x, y) ((lo) = mad_f_mul((x), (y)))
- # define MAD_F_MLA(hi, lo, x, y) ((lo) += mad_f_mul((x), (y)))
- # define MAD_F_MLN(hi, lo) ((lo) = -(lo))
- # define MAD_F_MLZ(hi, lo) ((void) (hi), (mad_fixed_t) (lo))
- # endif
- # if !defined(MAD_F_ML0)
- # define MAD_F_ML0(hi, lo, x, y) MAD_F_MLX((hi), (lo), (x), (y))
- # endif
- # if !defined(MAD_F_MLN)
- # define MAD_F_MLN(hi, lo) ((hi) = ((lo) = -(lo)) ? ~(hi) : -(hi))
- # endif
- # if !defined(MAD_F_MLZ)
- # define MAD_F_MLZ(hi, lo) mad_f_scale64((hi), (lo))
- # endif
- # if !defined(mad_f_scale64)
- # if defined(OPT_ACCURACY)
- # define mad_f_scale64(hi, lo)
- ((((mad_fixed_t)
- (((hi) << (32 - (MAD_F_SCALEBITS - 1))) |
- ((lo) >> (MAD_F_SCALEBITS - 1)))) + 1) >> 1)
- # else
- # define mad_f_scale64(hi, lo)
- ((mad_fixed_t)
- (((hi) << (32 - MAD_F_SCALEBITS)) |
- ((lo) >> MAD_F_SCALEBITS)))
- # endif
- # define MAD_F_SCALEBITS MAD_F_FRACBITS
- # endif
- mad_fixed_t mad_f_abs(mad_fixed_t);
- # endif