1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
|
/*
FPTruncateMode.h
Copyright 2001-11 Tim Goetze <tim@quitte.de>
http://quitte.de/dsp/
Sets the FP rounding mode to 'truncate' in the constructor
and loads the previous FP conrol word in the destructor.
By directly using the machine instruction to convert float to int
we avoid the performance hit that loading the control word twice for
every (int) cast causes on i386.
On other architectures this is a no-op.
*/
/*
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 the Free Software Foundation; either version 3
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
02111-1307, USA or point your web browser to http://www.gnu.org.
*/
#ifndef _DSP_FP_TRUNCATE_MODE_H_
#define _DSP_FP_TRUNCATE_MODE_H_
#ifdef __i386__
#define fstcw(i) \
__asm__ __volatile__ ("fstcw %0" : "=m" (i))
#define fldcw(i) \
__asm__ __volatile__ ("fldcw %0" : : "m" (i))
/* gcc chokes on __volatile__ sometimes. */
#define fistp(f,i) \
__asm__ ("fistpl %0" : "=m" (i) : "t" (f) : "st")
#else /* ! __i386__ */
#define fstcw(i)
#define fldcw(i)
#define fistp(f,i) \
i = (int) f
#endif
namespace DSP {
static inline int
fast_trunc (float f)
{
int i;
fistp (f, i);
return i;
}
class FPTruncateMode
{
public:
int cw0, cw1; /* fp control word */
FPTruncateMode()
{
fstcw (cw0);
cw1 = cw0 | 0xC00;
fldcw (cw1);
}
~FPTruncateMode()
{
fldcw (cw0);
}
};
} /* namespace DSP */
#endif /* _DSP_FP_TRUNCATE_MODE_H_ */
|