/*
 * $Id: math64.h,v 1.1 2003/02/07 18:15:44 wheeler Exp $
 *
 * ANSI-C 準記 math ルーチン
 *
 * $Log: math64.h,v $
 * Revision 1.1  2003/02/07 18:15:44  wheeler
 * Fox's version of libultra and friends.
 *
 * Revision 1.10  1997/03/05  05:21:00  hayakawa
 * 変換が間違っていた。
 * ftrunc,froundを追加
 * intrinsicsバージョンを組み込んだ
 *
 * Revision 1.9  1996/11/26  10:17:33  hayakawa
 * 度 ←→ ラジアン変換を追加
 *
 * Revision 1.8  1996/11/21  06:32:56  hayakawa
 * マクロ追加
 *
 * Revision 1.7  1996/10/20  09:13:46  hayakawa
 * _LANGUAGE_C_PLUS_PLUS を __cplusplus に変更
 *
 * Revision 1.6  1996/10/01  05:30:52  hayakawa
 * 関数名をANSI-C準記に変更
 * 竹谷スペシャル 度バージョンの三角関数の追加
 *
 */

#ifndef __MATH64_H_
#define __MATH64_H_

#ifdef __cplusplus
extern "C" {
#endif

#include "gu.h" /* sinf,cosf */

/* 竹谷スペシャル 度バージョンの三角関数 */
#define DEG_fsin(x) fsin(DEG2RAD(x))
#define DEG_fcos(x) fcos(DEG2RAD(x))
#define DEG_ftan(x) ftan(DEG2RAD(x))
#define DEG_fasin(x) RAD2DEG(fasin(x))
#define DEG_facos(x) RAD2DEG(facos(x))
#define DEG_fatan(x) RAD2DEG(fatan(x))
#define DEG_fatan2(y, x) RAD2DEG(fatan2(y, x))
   

#define DEG2RAD(deg) ((deg) * (F_PI / 180.0f)) /* 度 → ラジアン */
#define RAD2DEG(rad) ((rad) * (180.0f / F_PI)) /* 度 ← ラジアン */

/* マクロの方は cosf(rad) が０だと落ちる */
#define Ftan(rad) (fsin(rad) / fcos(rad))
#if 0
#include "math64_intrinsics.h"
#define Ffloor(x) __ffloor(x)
#define Fceil(x) (-Ffloor(-(x)))
#define Ffabs(x) ((float)(((x) >= 0.0f) ? (x) : -(x)))
#define Frint(x) Ffloor((x)+0.5f)
#define Ftrunc(x)  ((float)(int)(x))
#define Fround(x)  __fround(x)
#else
#define Ffabs(x)  ffabs(x)
#include "math64_intrinsics.h"
#define Ffloor(x) __ffloor(x)
#define Fceil(x)  __fceil(x)
#define Frint(x)  __frint(x)
#define Ftrunc(x) __ftrunc(x)
#define Fround(x) __fround(x)
#define Floor(x)  __floor(x)
#define Ceil(x)   __ceil(x)
#define Rint(x)   __rint(x)
#define Trunc(x)  __trunc(x)
#define Round(x)  __round(x)
#define Iffloor(x) __iffloor(x)
#define Ifceil(x)  __ifceil(x)
#define Ifrint(x)  __ifrint(x)
#define Iftrunc(x) __iftrunc(x)
#define Ifround(x) __ifround(x)
#define Ifloor(x)  __ifloor(x)
#define Iceil(x)   __iceil(x)
#define Irint(x)   __irint(x)
#define Itrunc(x)  __itrunc(x)
#define Iround(x)  __iround(x)
#endif
float ftan(float rad);
float ffloor(float x);
float fceil(float x);
float ftrunc(float x);
float fround(float x);
#define ffabs(x) fabsf(x)
float frint(float x);
float fatan( float x );
float fatan2( float y, float x );
float fasin( float x );
float facos( float x );
#define fsin(x) sinf(x)
#define fcos(x) cosf(x)
#define fsqrt(x) sqrtf(x)

/* from </usr/include/math.h> */
/* Some useful constants */
#define M_E		2.7182818284590452354 /* 自然対数e */
#define M_LOG2E		1.4426950408889634074 /* Log2e */
#define M_LOG10E	0.43429448190325182765 /* Log10e */
#define M_LN2		0.69314718055994530942
#define M_LN10		2.30258509299404568402
#define M_PI		3.14159265358979323846 /* 円周率π */
#define M_PI_2		1.57079632679489661923 /* π／２ */
#define M_PI_4		0.78539816339744830962 /* π／４ */
#define M_1_PI		0.31830988618379067154 /* １／π */
#define M_2_PI		0.63661977236758134308 /* ２／π */
#define M_2_SQRTPI	1.12837916709551257390 /* ２／√π */
#define M_SQRT2		1.41421356237309504880 /* √２ */
#define M_SQRT1_2	0.70710678118654752440 /* √１／２ */

#define F_E		((float)M_E)
#define F_LOG2E		((float)M_LOG2E)
#define F_LOG10E	((float)M_LOG10E)
#define F_LN2		((float)M_LN2)
#define F_LN10		((float)M_LN10)
#define F_PI		((float)M_PI)
#define F_PI_2		((float)M_PI_2)
#define F_PI_4		((float)M_PI_4)
#define F_1_PI		((float)M_1_PI)
#define F_2_PI		((float)M_2_PI)
#define F_2_SQRTPI	((float)M_2_SQRTPI)
#define F_SQRT2		((float)M_SQRT2)
#define F_SQRT1_2	((float)M_SQRT1_2)

/*
 * 以下は /usr/include/math.h からのコピーです
 */
extern double	sqrt(double);
#pragma no side effects (sqrt)
extern float	sqrtf(float);
#pragma no side effects (sqrtf)
extern double	fabs(double);
#pragma no side effects (fabs)
/* version of fabsf implemented for completeness only */
extern float	fabsf(float);
#pragma no side effects (fabsf)

#ifdef __INLINE_INTRINSICS
/* The functions made intrinsic here can be activated by the driver
** passing -D__INLINE_INTRINSICS to cfe, but cfe should ensure that
** this has no effect unless the hardware architecture directly
** supports these basic operations.
*/
#if _MIPS_ISA != _MIPS_ISA_MIPS1
#pragma intrinsic (sqrt)
#pragma intrinsic (sqrtf)
#if (defined(_COMPILER_VERSION) && (_COMPILER_VERSION >= 400))
#pragma intrinsic (pow)
#pragma intrinsic (powf)
#endif
#endif
#pragma intrinsic (fabs)
#pragma intrinsic (fabsf)
/* マクロ版の Fabsf を関数に置き換える */
#undef  Fabsf
#define Fabsf(x) fabsf(x)
#endif
#ifdef __cplusplus
}
#endif

#endif
