/*
|
/*
|
* mad - MPEG audio decoder
|
* mad - MPEG audio decoder
|
* Copyright (C) 2000-2001 Robert Leslie
|
* Copyright (C) 2000-2001 Robert Leslie
|
*
|
*
|
* This program is free software; you can redistribute it and/or modify
|
* This program is free software; you can redistribute it and/or modify
|
* it under the terms of the GNU General Public License as published by
|
* it under the terms of the GNU General Public License as published by
|
* the Free Software Foundation; either version 2 of the License, or
|
* the Free Software Foundation; either version 2 of the License, or
|
* (at your option) any later version.
|
* (at your option) any later version.
|
*
|
*
|
* This program is distributed in the hope that it will be useful,
|
* This program is distributed in the hope that it will be useful,
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
* GNU General Public License for more details.
|
* GNU General Public License for more details.
|
*
|
*
|
* You should have received a copy of the GNU General Public License
|
* You should have received a copy of the GNU General Public License
|
* along with this program; if not, write to the Free Software
|
* along with this program; if not, write to the Free Software
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
*
|
*
|
* $Id: fixed.h,v 1.3 2001-11-06 17:01:28 simons Exp $
|
* $Id: fixed.h,v 1.3 2001-11-06 17:01:28 simons Exp $
|
*/
|
*/
|
|
|
# ifndef LIBMAD_FIXED_H
|
# ifndef LIBMAD_FIXED_H
|
# define LIBMAD_FIXED_H
|
# define LIBMAD_FIXED_H
|
|
|
# if SIZEOF_INT >= 4
|
# if SIZEOF_INT >= 4
|
typedef signed int mad_fixed_t;
|
typedef signed int mad_fixed_t;
|
|
|
typedef signed int mad_fixed64hi_t;
|
typedef signed int mad_fixed64hi_t;
|
typedef unsigned int mad_fixed64lo_t;
|
typedef unsigned int mad_fixed64lo_t;
|
# else
|
# else
|
typedef signed long mad_fixed_t;
|
typedef signed long mad_fixed_t;
|
|
|
typedef signed long mad_fixed64hi_t;
|
typedef signed long mad_fixed64hi_t;
|
typedef unsigned long mad_fixed64lo_t;
|
typedef unsigned long mad_fixed64lo_t;
|
# endif
|
# endif
|
|
|
/*
|
/*
|
* Fixed-point format: 0xABBBBBBB
|
* Fixed-point format: 0xABBBBBBB
|
* A == whole part (sign + 3 bits)
|
* A == whole part (sign + 3 bits)
|
* B == fractional part (28 bits)
|
* B == fractional part (28 bits)
|
*
|
*
|
* Values are signed two's complement, so the effective range is:
|
* Values are signed two's complement, so the effective range is:
|
* 0x80000000 to 0x7fffffff
|
* 0x80000000 to 0x7fffffff
|
* -8.0 to +7.9999999962747097015380859375
|
* -8.0 to +7.9999999962747097015380859375
|
*
|
*
|
* The smallest representable value is:
|
* The smallest representable value is:
|
* 0x00000001 == 0.0000000037252902984619140625 (i.e. about 3.725e-9)
|
* 0x00000001 == 0.0000000037252902984619140625 (i.e. about 3.725e-9)
|
*
|
*
|
* 28 bits of fractional accuracy represent about
|
* 28 bits of fractional accuracy represent about
|
* 8.6 digits of decimal accuracy.
|
* 8.6 digits of decimal accuracy.
|
*
|
*
|
* Fixed-point numbers can be added or subtracted as normal
|
* Fixed-point numbers can be added or subtracted as normal
|
* integers, but multiplication requires shifting the 64-bit result
|
* integers, but multiplication requires shifting the 64-bit result
|
* from 56 fractional bits back to 28 (and rounding.)
|
* from 56 fractional bits back to 28 (and rounding.)
|
*
|
*
|
* Changing the definition of MAD_F_FRACBITS is only partially
|
* Changing the definition of MAD_F_FRACBITS is only partially
|
* supported, and must be done with care.
|
* supported, and must be done with care.
|
*/
|
*/
|
|
|
# define MAD_F_FRACBITS 28
|
# define MAD_F_FRACBITS 28
|
|
|
# if MAD_F_FRACBITS == 28
|
# if MAD_F_FRACBITS == 28
|
# define MAD_F(x) ((mad_fixed_t) (x##L))
|
# define MAD_F(x) ((mad_fixed_t) (x##L))
|
# else
|
# else
|
# if MAD_F_FRACBITS < 28
|
# if MAD_F_FRACBITS < 28
|
# warning "MAD_F_FRACBITS < 28"
|
# warning "MAD_F_FRACBITS < 28"
|
# define MAD_F(x) ((mad_fixed_t) \
|
# define MAD_F(x) ((mad_fixed_t) \
|
(((x##L) + \
|
(((x##L) + \
|
(1L << (28 - MAD_F_FRACBITS - 1))) >> \
|
(1L << (28 - MAD_F_FRACBITS - 1))) >> \
|
(28 - MAD_F_FRACBITS)))
|
(28 - MAD_F_FRACBITS)))
|
# elif MAD_F_FRACBITS > 28
|
# elif MAD_F_FRACBITS > 28
|
# error "MAD_F_FRACBITS > 28 not currently supported"
|
# error "MAD_F_FRACBITS > 28 not currently supported"
|
# define MAD_F(x) ((mad_fixed_t) \
|
# define MAD_F(x) ((mad_fixed_t) \
|
((x##L) << (MAD_F_FRACBITS - 28)))
|
((x##L) << (MAD_F_FRACBITS - 28)))
|
# endif
|
# endif
|
# endif
|
# endif
|
|
|
# define MAD_F_MIN ((mad_fixed_t) -0x80000000L)
|
# define MAD_F_MIN ((mad_fixed_t) -0x80000000L)
|
# define MAD_F_MAX ((mad_fixed_t) +0x7fffffffL)
|
# define MAD_F_MAX ((mad_fixed_t) +0x7fffffffL)
|
|
|
# define MAD_F_ONE MAD_F(0x10000000)
|
# define MAD_F_ONE MAD_F(0x10000000)
|
|
|
#ifndef EMBED
|
#ifndef EMBED
|
# define mad_f_tofixed(x) ((mad_fixed_t) \
|
# define mad_f_tofixed(x) ((mad_fixed_t) \
|
((x) * (double) (1L << MAD_F_FRACBITS) + 0.5))
|
((x) * (double) (1L << MAD_F_FRACBITS) + 0.5))
|
# define mad_f_todouble(x) ((double) \
|
# define mad_f_todouble(x) ((double) \
|
((x) / (double) (1L << MAD_F_FRACBITS)))
|
((x) / (double) (1L << MAD_F_FRACBITS)))
|
#endif
|
#endif
|
|
|
# define mad_f_intpart(x) ((x) >> 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_fracpart(x) ((x) & ((1L << MAD_F_FRACBITS) - 1))
|
/* (x should be positive) */
|
/* (x should be positive) */
|
|
|
# define mad_f_fromint(x) ((x) << MAD_F_FRACBITS)
|
# define mad_f_fromint(x) ((x) << MAD_F_FRACBITS)
|
|
|
# define mad_f_add(x, y) ((x) + (y))
|
# define mad_f_add(x, y) ((x) + (y))
|
# define mad_f_sub(x, y) ((x) - (y))
|
# define mad_f_sub(x, y) ((x) - (y))
|
|
|
# if defined(FPM_64BIT)
|
# if defined(FPM_64BIT)
|
|
|
/*
|
/*
|
* This version should be the most accurate if 64-bit (long long) types are
|
* This version should be the most accurate if 64-bit (long long) types are
|
* supported by the compiler, although it may not be the most efficient.
|
* supported by the compiler, although it may not be the most efficient.
|
*/
|
*/
|
# if defined(OPT_ACCURACY)
|
# if defined(OPT_ACCURACY)
|
# define mad_f_mul(x, y) \
|
# define mad_f_mul(x, y) \
|
((mad_fixed_t) \
|
((mad_fixed_t) \
|
((((signed long long) (x) * (y)) + \
|
((((signed long long) (x) * (y)) + \
|
(1L << (MAD_F_SCALEBITS - 1))) >> MAD_F_SCALEBITS))
|
(1L << (MAD_F_SCALEBITS - 1))) >> MAD_F_SCALEBITS))
|
# else
|
# else
|
# define mad_f_mul(x, y) \
|
# define mad_f_mul(x, y) \
|
((mad_fixed_t) (((signed long long) (x) * (y)) >> MAD_F_SCALEBITS))
|
((mad_fixed_t) (((signed long long) (x) * (y)) >> MAD_F_SCALEBITS))
|
# endif
|
# endif
|
|
|
# define MAD_F_SCALEBITS MAD_F_FRACBITS
|
# define MAD_F_SCALEBITS MAD_F_FRACBITS
|
|
|
/* --- Intel --------------------------------------------------------------- */
|
/* --- Intel --------------------------------------------------------------- */
|
# elif defined(FPM_INTEL)
|
# elif defined(FPM_INTEL)
|
|
|
/*
|
/*
|
* This Intel version is fast and accurate; the disposition of the least
|
* This Intel version is fast and accurate; the disposition of the least
|
* significant bit depends on OPT_ACCURACY via mad_f_scale64().
|
* significant bit depends on OPT_ACCURACY via mad_f_scale64().
|
*/
|
*/
|
# define MAD_F_MLX(hi, lo, x, y) \
|
# define MAD_F_MLX(hi, lo, x, y) \
|
asm ("imull %3" \
|
asm ("imull %3" \
|
: "=a" (lo), "=d" (hi) \
|
: "=a" (lo), "=d" (hi) \
|
: "%a" (x), "rm" (y) \
|
: "%a" (x), "rm" (y) \
|
: "cc")
|
: "cc")
|
|
|
# if defined(OPT_ACCURACY)
|
# if defined(OPT_ACCURACY)
|
/*
|
/*
|
* This gives best accuracy but is not very fast.
|
* This gives best accuracy but is not very fast.
|
*/
|
*/
|
# define MAD_F_MLA(hi, lo, x, y) \
|
# define MAD_F_MLA(hi, lo, x, y) \
|
({ mad_fixed64hi_t __hi; \
|
({ mad_fixed64hi_t __hi; \
|
mad_fixed64lo_t __lo; \
|
mad_fixed64lo_t __lo; \
|
MAD_F_MLX(__hi, __lo, (x), (y)); \
|
MAD_F_MLX(__hi, __lo, (x), (y)); \
|
asm ("addl %2,%0\n\t" \
|
asm ("addl %2,%0\n\t" \
|
"adcl %3,%1" \
|
"adcl %3,%1" \
|
: "=rm" (lo), "=rm" (hi) \
|
: "=rm" (lo), "=rm" (hi) \
|
: "r" (__lo), "r" (__hi), "0" (lo), "1" (hi) \
|
: "r" (__lo), "r" (__hi), "0" (lo), "1" (hi) \
|
: "cc"); \
|
: "cc"); \
|
})
|
})
|
# endif /* OPT_ACCURACY */
|
# endif /* OPT_ACCURACY */
|
|
|
# if defined(OPT_ACCURACY)
|
# if defined(OPT_ACCURACY)
|
/*
|
/*
|
* Surprisingly, this is faster than SHRD followed by ADC.
|
* Surprisingly, this is faster than SHRD followed by ADC.
|
*/
|
*/
|
# define mad_f_scale64(hi, lo) \
|
# define mad_f_scale64(hi, lo) \
|
({ mad_fixed64hi_t __hi_; \
|
({ mad_fixed64hi_t __hi_; \
|
mad_fixed64lo_t __lo_; \
|
mad_fixed64lo_t __lo_; \
|
mad_fixed_t __result; \
|
mad_fixed_t __result; \
|
asm ("addl %4,%2\n\t" \
|
asm ("addl %4,%2\n\t" \
|
"adcl %5,%3" \
|
"adcl %5,%3" \
|
: "=rm" (__lo_), "=rm" (__hi_) \
|
: "=rm" (__lo_), "=rm" (__hi_) \
|
: "0" (lo), "1" (hi), \
|
: "0" (lo), "1" (hi), \
|
"ir" (1L << (MAD_F_SCALEBITS - 1)), "ir" (0) \
|
"ir" (1L << (MAD_F_SCALEBITS - 1)), "ir" (0) \
|
: "cc"); \
|
: "cc"); \
|
asm ("shrdl %3,%2,%1" \
|
asm ("shrdl %3,%2,%1" \
|
: "=rm" (__result) \
|
: "=rm" (__result) \
|
: "0" (__lo_), "r" (__hi_), "I" (MAD_F_SCALEBITS) \
|
: "0" (__lo_), "r" (__hi_), "I" (MAD_F_SCALEBITS) \
|
: "cc"); \
|
: "cc"); \
|
__result; \
|
__result; \
|
})
|
})
|
# else
|
# else
|
# define mad_f_scale64(hi, lo) \
|
# define mad_f_scale64(hi, lo) \
|
({ mad_fixed_t __result; \
|
({ mad_fixed_t __result; \
|
asm ("shrdl %3,%2,%1" \
|
asm ("shrdl %3,%2,%1" \
|
: "=rm" (__result) \
|
: "=rm" (__result) \
|
: "0" (lo), "r" (hi), "I" (MAD_F_SCALEBITS) \
|
: "0" (lo), "r" (hi), "I" (MAD_F_SCALEBITS) \
|
: "cc"); \
|
: "cc"); \
|
__result; \
|
__result; \
|
})
|
})
|
# endif /* OPT_ACCURACY */
|
# endif /* OPT_ACCURACY */
|
|
|
# define MAD_F_SCALEBITS MAD_F_FRACBITS
|
# define MAD_F_SCALEBITS MAD_F_FRACBITS
|
|
|
/* --- ARM ----------------------------------------------------------------- */
|
/* --- ARM ----------------------------------------------------------------- */
|
|
|
# elif defined(FPM_ARM)
|
# elif defined(FPM_ARM)
|
|
|
/*
|
/*
|
* This ARM V4 version is as accurate as FPM_64BIT but much faster. The
|
* This ARM V4 version is as accurate as FPM_64BIT but much faster. The
|
* least significant bit is properly rounded at no CPU cycle cost!
|
* least significant bit is properly rounded at no CPU cycle cost!
|
*/
|
*/
|
# if 1
|
# if 1
|
/*
|
/*
|
* There's a bug somewhere, possibly in the compiler, that sometimes makes
|
* There's a bug somewhere, possibly in the compiler, that sometimes makes
|
* this necessary instead of the default implementation via MAD_F_MLX and
|
* this necessary instead of the default implementation via MAD_F_MLX and
|
* mad_f_scale64. It may be related to the use (or lack) of
|
* mad_f_scale64. It may be related to the use (or lack) of
|
* -finline-functions and/or -fstrength-reduce.
|
* -finline-functions and/or -fstrength-reduce.
|
*
|
*
|
* This is also apparently faster than MAD_F_MLX/mad_f_scale64.
|
* This is also apparently faster than MAD_F_MLX/mad_f_scale64.
|
*/
|
*/
|
# define mad_f_mul(x, y) \
|
# define mad_f_mul(x, y) \
|
({ mad_fixed64hi_t __hi; \
|
({ mad_fixed64hi_t __hi; \
|
mad_fixed64lo_t __lo; \
|
mad_fixed64lo_t __lo; \
|
mad_fixed_t __result; \
|
mad_fixed_t __result; \
|
asm ("smull %0, %1, %3, %4\n\t" \
|
asm ("smull %0, %1, %3, %4\n\t" \
|
"movs %0, %0, lsr %5\n\t" \
|
"movs %0, %0, lsr %5\n\t" \
|
"adc %2, %0, %1, lsl %6" \
|
"adc %2, %0, %1, lsl %6" \
|
: "=&r" (__lo), "=&r" (__hi), "=r" (__result) \
|
: "=&r" (__lo), "=&r" (__hi), "=r" (__result) \
|
: "%r" (x), "r" (y), \
|
: "%r" (x), "r" (y), \
|
"M" (MAD_F_SCALEBITS), "M" (32 - MAD_F_SCALEBITS) \
|
"M" (MAD_F_SCALEBITS), "M" (32 - MAD_F_SCALEBITS) \
|
: "cc"); \
|
: "cc"); \
|
__result; \
|
__result; \
|
})
|
})
|
# endif
|
# endif
|
|
|
# define MAD_F_MLX(hi, lo, x, y) \
|
# define MAD_F_MLX(hi, lo, x, y) \
|
asm ("smull %0, %1, %2, %3" \
|
asm ("smull %0, %1, %2, %3" \
|
: "=&r" (lo), "=&r" (hi) \
|
: "=&r" (lo), "=&r" (hi) \
|
: "%r" (x), "r" (y))
|
: "%r" (x), "r" (y))
|
|
|
# define MAD_F_MLA(hi, lo, x, y) \
|
# define MAD_F_MLA(hi, lo, x, y) \
|
asm ("smlal %0, %1, %2, %3" \
|
asm ("smlal %0, %1, %2, %3" \
|
: "+r" (lo), "+r" (hi) \
|
: "+r" (lo), "+r" (hi) \
|
: "%r" (x), "r" (y))
|
: "%r" (x), "r" (y))
|
|
|
# define mad_f_scale64(hi, lo) \
|
# define mad_f_scale64(hi, lo) \
|
({ mad_fixed_t __result; \
|
({ mad_fixed_t __result; \
|
asm ("movs %0, %1, lsr %3\n\t" \
|
asm ("movs %0, %1, lsr %3\n\t" \
|
"adc %0, %0, %2, lsl %4" \
|
"adc %0, %0, %2, lsl %4" \
|
: "=r" (__result) \
|
: "=r" (__result) \
|
: "r" (lo), "r" (hi), \
|
: "r" (lo), "r" (hi), \
|
"M" (MAD_F_SCALEBITS), "M" (32 - MAD_F_SCALEBITS) \
|
"M" (MAD_F_SCALEBITS), "M" (32 - MAD_F_SCALEBITS) \
|
: "cc"); \
|
: "cc"); \
|
__result; \
|
__result; \
|
})
|
})
|
|
|
# define MAD_F_SCALEBITS MAD_F_FRACBITS
|
# define MAD_F_SCALEBITS MAD_F_FRACBITS
|
|
|
/* --- MIPS ---------------------------------------------------------------- */
|
/* --- MIPS ---------------------------------------------------------------- */
|
|
|
# elif defined(FPM_MIPS)
|
# elif defined(FPM_MIPS)
|
|
|
/*
|
/*
|
* This MIPS version is fast and accurate; the disposition of the least
|
* This MIPS version is fast and accurate; the disposition of the least
|
* significant bit depends on OPT_ACCURACY via mad_f_scale64().
|
* significant bit depends on OPT_ACCURACY via mad_f_scale64().
|
*/
|
*/
|
# define MAD_F_MLX(hi, lo, x, y) \
|
# define MAD_F_MLX(hi, lo, x, y) \
|
asm ("mult %2,%3" \
|
asm ("mult %2,%3" \
|
: "=l" (lo), "=h" (hi) \
|
: "=l" (lo), "=h" (hi) \
|
: "%r" (x), "r" (y))
|
: "%r" (x), "r" (y))
|
|
|
# if defined(HAVE_MADD_ASM)
|
# if defined(HAVE_MADD_ASM)
|
# define MAD_F_MLA(hi, lo, x, y) \
|
# define MAD_F_MLA(hi, lo, x, y) \
|
asm ("madd %2,%3" \
|
asm ("madd %2,%3" \
|
: "+l" (lo), "+h" (hi) \
|
: "+l" (lo), "+h" (hi) \
|
: "%r" (x), "r" (y))
|
: "%r" (x), "r" (y))
|
# elif defined(HAVE_MADD16_ASM)
|
# elif defined(HAVE_MADD16_ASM)
|
/*
|
/*
|
* This loses significant accuracy due to the 16-bit integer limit in the
|
* This loses significant accuracy due to the 16-bit integer limit in the
|
* multiply/accumulate instruction.
|
* multiply/accumulate instruction.
|
*/
|
*/
|
# define MAD_F_ML0(hi, lo, x, y) \
|
# define MAD_F_ML0(hi, lo, x, y) \
|
asm ("mult %2,%3" \
|
asm ("mult %2,%3" \
|
: "=l" (lo), "=h" (hi) \
|
: "=l" (lo), "=h" (hi) \
|
: "%r" ((x) >> 12), "r" ((y) >> 16))
|
: "%r" ((x) >> 12), "r" ((y) >> 16))
|
# define MAD_F_MLA(hi, lo, x, y) \
|
# define MAD_F_MLA(hi, lo, x, y) \
|
asm ("madd16 %2,%3" \
|
asm ("madd16 %2,%3" \
|
: "+l" (lo), "+h" (hi) \
|
: "+l" (lo), "+h" (hi) \
|
: "%r" ((x) >> 12), "r" ((y) >> 16))
|
: "%r" ((x) >> 12), "r" ((y) >> 16))
|
# define MAD_F_MLZ(hi, lo) ((mad_fixed_t) (lo))
|
# define MAD_F_MLZ(hi, lo) ((mad_fixed_t) (lo))
|
# endif
|
# endif
|
|
|
# if defined(OPT_SPEED)
|
# if defined(OPT_SPEED)
|
# define mad_f_scale64(hi, lo) \
|
# define mad_f_scale64(hi, lo) \
|
((mad_fixed_t) ((hi) << (32 - MAD_F_SCALEBITS)))
|
((mad_fixed_t) ((hi) << (32 - MAD_F_SCALEBITS)))
|
# define MAD_F_SCALEBITS MAD_F_FRACBITS
|
# define MAD_F_SCALEBITS MAD_F_FRACBITS
|
# endif
|
# endif
|
|
|
/* --- SPARC --------------------------------------------------------------- */
|
/* --- SPARC --------------------------------------------------------------- */
|
|
|
# elif defined(FPM_SPARC)
|
# elif defined(FPM_SPARC)
|
|
|
/*
|
/*
|
* This SPARC V8 version is fast and accurate; the disposition of the least
|
* This SPARC V8 version is fast and accurate; the disposition of the least
|
* significant bit depends on OPT_ACCURACY via mad_f_scale64().
|
* significant bit depends on OPT_ACCURACY via mad_f_scale64().
|
*/
|
*/
|
# define MAD_F_MLX(hi, lo, x, y) \
|
# define MAD_F_MLX(hi, lo, x, y) \
|
asm ("smul %2, %3, %0\n\t" \
|
asm ("smul %2, %3, %0\n\t" \
|
"rd %%y, %1" \
|
"rd %%y, %1" \
|
: "=r" (lo), "=r" (hi) \
|
: "=r" (lo), "=r" (hi) \
|
: "%r" (x), "rI" (y))
|
: "%r" (x), "rI" (y))
|
|
|
/* --- PowerPC ------------------------------------------------------------- */
|
/* --- PowerPC ------------------------------------------------------------- */
|
|
|
# elif defined(FPM_PPC)
|
# elif defined(FPM_PPC)
|
|
|
/*
|
/*
|
* This PowerPC version is tuned for the 4xx embedded processors. It is
|
* This PowerPC version is tuned for the 4xx embedded processors. It is
|
* effectively a tuned version of FPM_64BIT. It is a little faster and just
|
* effectively a tuned version of FPM_64BIT. It is a little faster and just
|
* as accurate. The disposition of the least significant bit depends on
|
* as accurate. The disposition of the least significant bit depends on
|
* OPT_ACCURACY via mad_f_scale64().
|
* OPT_ACCURACY via mad_f_scale64().
|
*/
|
*/
|
# define MAD_F_MLX(hi, lo, x, y) \
|
# define MAD_F_MLX(hi, lo, x, y) \
|
asm ("mulhw %1, %2, %3\n\t" \
|
asm ("mulhw %1, %2, %3\n\t" \
|
"mullw %0, %2, %3" \
|
"mullw %0, %2, %3" \
|
: "=&r" (lo), "=&r" (hi) \
|
: "=&r" (lo), "=&r" (hi) \
|
: "%r" (x), "r" (y))
|
: "%r" (x), "r" (y))
|
|
|
# define MAD_F_MLA(hi, lo, x, y) \
|
# define MAD_F_MLA(hi, lo, x, y) \
|
({ mad_fixed64hi_t __hi; \
|
({ mad_fixed64hi_t __hi; \
|
mad_fixed64lo_t __lo; \
|
mad_fixed64lo_t __lo; \
|
MAD_F_MLX(__hi, __lo, (x), (y)); \
|
MAD_F_MLX(__hi, __lo, (x), (y)); \
|
asm ("addc %0, %2, %3\n\t" \
|
asm ("addc %0, %2, %3\n\t" \
|
"adde %1, %4, %5" \
|
"adde %1, %4, %5" \
|
: "=r" (lo), "=r" (hi) \
|
: "=r" (lo), "=r" (hi) \
|
: "%r" (__lo), "0" (lo), "%r" (__hi), "1" (hi)); \
|
: "%r" (__lo), "0" (lo), "%r" (__hi), "1" (hi)); \
|
})
|
})
|
|
|
# if defined(OPT_ACCURACY)
|
# if defined(OPT_ACCURACY)
|
/*
|
/*
|
* This is accurate and ~2 - 2.5 times slower than the unrounded version.
|
* This is accurate and ~2 - 2.5 times slower than the unrounded version.
|
*
|
*
|
* The __volatile__ improves the generated code by another 5% (fewer spills
|
* The __volatile__ improves the generated code by another 5% (fewer spills
|
* to memory); eventually they should be removed.
|
* to memory); eventually they should be removed.
|
*/
|
*/
|
# define mad_f_scale64(hi, lo) \
|
# define mad_f_scale64(hi, lo) \
|
({ mad_fixed_t __result; \
|
({ mad_fixed_t __result; \
|
mad_fixed64hi_t __hi_; \
|
mad_fixed64hi_t __hi_; \
|
mad_fixed64lo_t __lo_; \
|
mad_fixed64lo_t __lo_; \
|
asm __volatile__ ("addc %0, %2, %4\n\t" \
|
asm __volatile__ ("addc %0, %2, %4\n\t" \
|
"addze %1, %3" \
|
"addze %1, %3" \
|
: "=r" (__lo_), "=r" (__hi_) \
|
: "=r" (__lo_), "=r" (__hi_) \
|
: "r" (lo), "r" (hi), "r" (1 << (MAD_F_SCALEBITS - 1))); \
|
: "r" (lo), "r" (hi), "r" (1 << (MAD_F_SCALEBITS - 1))); \
|
asm __volatile__ ("rlwinm %0, %2,32-%3,0,%3-1\n\t" \
|
asm __volatile__ ("rlwinm %0, %2,32-%3,0,%3-1\n\t" \
|
"rlwimi %0, %1,32-%3,%3,31" \
|
"rlwimi %0, %1,32-%3,%3,31" \
|
: "=&r" (__result) \
|
: "=&r" (__result) \
|
: "r" (__lo_), "r" (__hi_), "I" (MAD_F_SCALEBITS)); \
|
: "r" (__lo_), "r" (__hi_), "I" (MAD_F_SCALEBITS)); \
|
__result; \
|
__result; \
|
})
|
})
|
# else
|
# else
|
# define mad_f_scale64(hi, lo) \
|
# define mad_f_scale64(hi, lo) \
|
({ mad_fixed_t __result; \
|
({ mad_fixed_t __result; \
|
asm ("rlwinm %0, %2,32-%3,0,%3-1\n\t" \
|
asm ("rlwinm %0, %2,32-%3,0,%3-1\n\t" \
|
"rlwimi %0, %1,32-%3,%3,31" \
|
"rlwimi %0, %1,32-%3,%3,31" \
|
: "=r" (__result) \
|
: "=r" (__result) \
|
: "r" (lo), "r" (hi), "I" (MAD_F_SCALEBITS)); \
|
: "r" (lo), "r" (hi), "I" (MAD_F_SCALEBITS)); \
|
__result; \
|
__result; \
|
})
|
})
|
# endif /* OPT_ACCURACY */
|
# endif /* OPT_ACCURACY */
|
|
|
# define MAD_F_SCALEBITS MAD_F_FRACBITS
|
# define MAD_F_SCALEBITS MAD_F_FRACBITS
|
|
|
/* ------ OR32 ------------------------------------------------------------- */
|
/* ------ OR32 ------------------------------------------------------------- */
|
|
|
# elif defined(FPM_OR32)
|
# elif defined(FPM_OR32)
|
|
|
/* We assume here that we always call macros in following sequence:
|
/* We assume here that we always call macros in following sequence:
|
MAD_F_ML0
|
MAD_F_ML0
|
MAD_F_MLA
|
MAD_F_MLA
|
...
|
...
|
MAD_F_MLA
|
MAD_F_MLA
|
MAD_F_MLX
|
MAD_F_MLX
|
*/
|
*/
|
|
|
# define MAD_F_MLX(hi, lo, x, y) \
|
# define MAD_F_MLX(hi, lo, x, y) \
|
asm volatile ("l.mac %0,%1" : : "%r" (x), "r" (y))
|
asm volatile ("l.mac %0,%1" : : "%r" (x), "r" (y))
|
|
|
# define MAD_F_MLA(hi, lo, x, y) MAX_F_MLX(hi, lo, x, y)
|
# define MAD_F_MLA(hi, lo, x, y) MAX_F_MLX(hi, lo, x, y)
|
|
|
|
|
# define MAX_F_ML0(hi, lo, x, y) MAX_F_MLX(hi, lo, x, y)
|
# define MAX_F_ML0(hi, lo, x, y) MAX_F_MLX(hi, lo, x, y)
|
|
|
# define MAX_F_MLZ(hi, lo, x, y) \
|
# define MAX_F_MLZ(hi, lo, x, y) \
|
asm volatile ("l.macrc %0" : "=r" (lo))
|
asm volatile ("l.macrc %0" : "=r" (lo))
|
|
|
# define MAD_F_SCALEBITS MAD_F_FRACBITS
|
# define MAD_F_SCALEBITS MAD_F_FRACBITS
|
|
|
/* --- Default ------------------------------------------------------------- */
|
/* --- Default ------------------------------------------------------------- */
|
|
|
# elif defined(FPM_DEFAULT)
|
# elif defined(FPM_DEFAULT)
|
|
|
/*
|
/*
|
* This version is the most portable but it loses significant accuracy.
|
* This version is the most portable but it loses significant accuracy.
|
* Furthermore, accuracy is biased against the second argument, so care
|
* Furthermore, accuracy is biased against the second argument, so care
|
* should be taken when ordering operands.
|
* should be taken when ordering operands.
|
*
|
*
|
* The scale factors are constant as this is not used with SSO.
|
* The scale factors are constant as this is not used with SSO.
|
*
|
*
|
* Pre-rounding is required to stay within the limits of compliance.
|
* Pre-rounding is required to stay within the limits of compliance.
|
*/
|
*/
|
# define mad_f_mul(x, y) ( (((x) + (1L << 11)) >> 12) * \
|
# define mad_f_mul(x, y) ( (((x) + (1L << 11)) >> 12) * \
|
(((y) + (1L << 15)) >> 16) )
|
(((y) + (1L << 15)) >> 16) )
|
/*# define mad_f_mul(x, y) ((x)+(y))*/
|
/*# define mad_f_mul(x, y) ((x)+(y))*/
|
/* --- Default 16 ------------------------------------------------------------- */
|
/* --- Default 16 ------------------------------------------------------------- */
|
|
|
# elif defined(FPM_DEFAULT16)
|
# elif defined(FPM_DEFAULT16)
|
|
|
/*
|
/*
|
* This version is the most portable but it loses significant accuracy.
|
* This version is the most portable but it loses significant accuracy.
|
* Furthermore, accuracy is biased against the second argument, so care
|
* Furthermore, accuracy is biased against the second argument, so care
|
* should be taken when ordering operands.
|
* should be taken when ordering operands.
|
*
|
*
|
* The scale factors are constant as this is not used with SSO.
|
* The scale factors are constant as this is not used with SSO.
|
*
|
*
|
* Pre-rounding is required to stay within the limits of compliance.
|
* Pre-rounding is required to stay within the limits of compliance.
|
*/
|
*/
|
# define mad_f_mul(x, y) (( (((x) + (1L << 15)) >> 16) * \
|
# define mad_f_mul(x, y) (( (((x) + (1L << 15)) >> 16) * \
|
(((y) + (1L << 15)) >> 16) ) << 4)
|
(((y) + (1L << 15)) >> 16) ) << 4)
|
/* ------------------------------------------------------------------------- */
|
/* ------------------------------------------------------------------------- */
|
|
|
# else
|
# else
|
# error "no FPM selected"
|
# error "no FPM selected"
|
# endif
|
# endif
|
|
|
/* default implementations */
|
/* default implementations */
|
|
|
# if !defined(mad_f_mul)
|
# if !defined(mad_f_mul)
|
# define mad_f_mul(x, y) \
|
# define mad_f_mul(x, y) \
|
({ mad_fixed64hi_t __hi; \
|
({ mad_fixed64hi_t __hi; \
|
mad_fixed64lo_t __lo; \
|
mad_fixed64lo_t __lo; \
|
MAD_F_MLX(__hi, __lo, (x), (y)); \
|
MAD_F_MLX(__hi, __lo, (x), (y)); \
|
mad_f_scale64(__hi, __lo); \
|
mad_f_scale64(__hi, __lo); \
|
})
|
})
|
# endif
|
# endif
|
|
|
# if !defined(MAD_F_MLA)
|
# if !defined(MAD_F_MLA)
|
# define MAD_F_ML0(hi, lo, x, y) ((lo) = mad_f_mul((x), (y)))
|
# 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_MLA(hi, lo, x, y) ((lo) += mad_f_mul((x), (y)))
|
# define MAD_F_MLZ(hi, lo) ((void) (hi), (mad_fixed_t) (lo))
|
# define MAD_F_MLZ(hi, lo) ((void) (hi), (mad_fixed_t) (lo))
|
# endif
|
# endif
|
|
|
# if !defined(MAD_F_ML0)
|
# if !defined(MAD_F_ML0)
|
# define MAD_F_ML0(hi, lo, x, y) MAD_F_MLX((hi), (lo), (x), (y))
|
# define MAD_F_ML0(hi, lo, x, y) MAD_F_MLX((hi), (lo), (x), (y))
|
# endif
|
# endif
|
|
|
# if !defined(MAD_F_MLZ)
|
# if !defined(MAD_F_MLZ)
|
# define MAD_F_MLZ(hi, lo) mad_f_scale64((hi), (lo))
|
# define MAD_F_MLZ(hi, lo) mad_f_scale64((hi), (lo))
|
# endif
|
# endif
|
|
|
# if !defined(mad_f_scale64)
|
# if !defined(mad_f_scale64)
|
# if defined(OPT_ACCURACY)
|
# if defined(OPT_ACCURACY)
|
# define mad_f_scale64(hi, lo) \
|
# define mad_f_scale64(hi, lo) \
|
((((mad_fixed_t) \
|
((((mad_fixed_t) \
|
(((hi) << (32 - (MAD_F_SCALEBITS - 1))) | \
|
(((hi) << (32 - (MAD_F_SCALEBITS - 1))) | \
|
((lo) >> (MAD_F_SCALEBITS - 1)))) + 1) >> 1)
|
((lo) >> (MAD_F_SCALEBITS - 1)))) + 1) >> 1)
|
# else
|
# else
|
# define mad_f_scale64(hi, lo) \
|
# define mad_f_scale64(hi, lo) \
|
((mad_fixed_t) \
|
((mad_fixed_t) \
|
(((hi) << (32 - MAD_F_SCALEBITS)) | \
|
(((hi) << (32 - MAD_F_SCALEBITS)) | \
|
((lo) >> MAD_F_SCALEBITS)))
|
((lo) >> MAD_F_SCALEBITS)))
|
# endif
|
# endif
|
# define MAD_F_SCALEBITS MAD_F_FRACBITS
|
# define MAD_F_SCALEBITS MAD_F_FRACBITS
|
# endif
|
# endif
|
|
|
/* miscellaneous C routines */
|
/* miscellaneous C routines */
|
|
|
mad_fixed_t mad_f_abs(mad_fixed_t);
|
mad_fixed_t mad_f_abs(mad_fixed_t);
|
|
|
# endif
|
# endif
|
|
|