Blame libcelt/float_cast.h

Packit 664db3
/*
Packit 664db3
** Copyright (C) 2001 Erik de Castro Lopo <erikd AT mega-nerd DOT com>
Packit 664db3
**
Packit 664db3
** Permission to use, copy, modify, distribute, and sell this file for any 
Packit 664db3
** purpose is hereby granted without fee, provided that the above copyright 
Packit 664db3
** and this permission notice appear in all copies.  No representations are
Packit 664db3
** made about the suitability of this software for any purpose.  It is 
Packit 664db3
** provided "as is" without express or implied warranty.
Packit 664db3
*/
Packit 664db3
Packit 664db3
/* Version 1.1 */
Packit 664db3
Packit 664db3
#ifndef FLOAT_CAST_H
Packit 664db3
#define FLOAT_CAST_H
Packit 664db3
Packit 664db3
/*============================================================================ 
Packit 664db3
**	On Intel Pentium processors (especially PIII and probably P4), converting
Packit 664db3
**	from float to int is very slow. To meet the C specs, the code produced by 
Packit 664db3
**	most C compilers targeting Pentium needs to change the FPU rounding mode 
Packit 664db3
**	before the float to int conversion is performed. 
Packit 664db3
**
Packit 664db3
**	Changing the FPU rounding mode causes the FPU pipeline to be flushed. It 
Packit 664db3
**	is this flushing of the pipeline which is so slow.
Packit 664db3
**
Packit 664db3
**	Fortunately the ISO C99 specifications define the functions lrint, lrintf,
Packit 664db3
**	llrint and llrintf which fix this problem as a side effect. 
Packit 664db3
**
Packit 664db3
**	On Unix-like systems, the configure process should have detected the 
Packit 664db3
**	presence of these functions. If they weren't found we have to replace them 
Packit 664db3
**	here with a standard C cast.
Packit 664db3
*/
Packit 664db3
Packit 664db3
/*	
Packit 664db3
**	The C99 prototypes for lrint and lrintf are as follows:
Packit 664db3
**	
Packit 664db3
**		long int lrintf (float x) ;
Packit 664db3
**		long int lrint  (double x) ;
Packit 664db3
*/
Packit 664db3
Packit 664db3
/*	The presence of the required functions are detected during the configure
Packit 664db3
**	process and the values HAVE_LRINT and HAVE_LRINTF are set accordingly in
Packit 664db3
**	the config.h file.
Packit 664db3
*/
Packit 664db3
Packit 664db3
#if (HAVE_LRINTF)
Packit 664db3
/*#if 0*/
Packit 664db3
Packit 664db3
	/*	These defines enable functionality introduced with the 1999 ISO C
Packit 664db3
	**	standard. They must be defined before the inclusion of math.h to
Packit 664db3
	**	engage them. If optimisation is enabled, these functions will be 
Packit 664db3
	**	inlined. With optimisation switched off, you have to link in the
Packit 664db3
	**	maths library using -lm.
Packit 664db3
	*/
Packit 664db3
Packit 664db3
	#define	_ISOC9X_SOURCE	1
Packit 664db3
	#define _ISOC99_SOURCE	1
Packit 664db3
Packit 664db3
	#define	__USE_ISOC9X	1
Packit 664db3
	#define	__USE_ISOC99	1
Packit 664db3
Packit 664db3
	#include	<math.h>
Packit 664db3
	#define float2int(x) lrintf(x)
Packit 664db3
Packit 664db3
#elif (defined(HAVE_LRINT))
Packit 664db3
Packit 664db3
#define	_ISOC9X_SOURCE	1
Packit 664db3
#define _ISOC99_SOURCE	1
Packit 664db3
Packit 664db3
#define	__USE_ISOC9X	1
Packit 664db3
#define	__USE_ISOC99	1
Packit 664db3
Packit 664db3
#include	<math.h>
Packit 664db3
#define float2int(x) lrint(x)
Packit 664db3
Packit 664db3
#elif (defined (WIN32) || defined (_WIN32))
Packit 664db3
Packit 664db3
	#include	<math.h>
Packit 664db3
Packit 664db3
	/*	Win32 doesn't seem to have these functions. 
Packit 664db3
	**	Therefore implement inline versions of these functions here.
Packit 664db3
	*/
Packit 664db3
	
Packit 664db3
	__inline long int 
Packit 664db3
	float2int (float flt)
Packit 664db3
	{	int intgr;
Packit 664db3
Packit 664db3
		_asm
Packit 664db3
		{	fld flt
Packit 664db3
			fistp intgr
Packit 664db3
			} ;
Packit 664db3
			
Packit 664db3
		return intgr ;
Packit 664db3
	}
Packit 664db3
Packit 664db3
#else
Packit 664db3
Packit 664db3
#ifdef __GNUC__ /* supported by gcc, but not by all other compilers*/
Packit 664db3
	#warning "Don't have the functions lrint() and lrintf ()."
Packit 664db3
	#warning "Replacing these functions with a standard C cast."
Packit 664db3
#endif /* __GNUC__ */
Packit 664db3
Packit 664db3
	#include	<math.h>
Packit 664db3
Packit 664db3
	#define	float2int(flt)		((int)(floor(.5+flt)))
Packit 664db3
Packit 664db3
#endif
Packit 664db3
Packit 664db3
Packit 664db3
#endif /* FLOAT_CAST_H */