快速平方根倒数算法C程序(包括单精度与双精度)

程序摘自:A Brief History of InvSqrt (By Matthew Robertson)

/**********************************************************************************************
Function: fast_invSqrt32
Description: 快速平方根倒数
Input: 单精度浮点数number
Output: 无
Input_Output: 无
Return: 快速平方根倒数的值
Author: Marc Pony(marc_pony@163.com)
***********************************************************************************************/
float fast_invSqrt32(float number)
{
	long i;
	float x2, y;
	const float threehalfs = 1.5F;

	x2 = number * 0.5F;
	y = number;
	i = *(long*) &y;
	i = 0x5f375a86 - (i >> 1);
	y = *(float*) &i;
	y = y * (threehalfs - (x2 * y * y));
	y = y * (threehalfs - (x2 * y * y));

	return y;
}


/**********************************************************************************************
Function: fast_sqrt32
Description: 快速平方根
Input: 单精度浮点数number
Output: 无
Input_Output: 无
Return: 快速平方根的值
Author: Marc Pony(marc_pony@163.com)
***********************************************************************************************/
float fast_sqrt32(float number)
{
	return 1.0F / fast_invSqrt32(number);
}


/**********************************************************************************************
Function: fast_invSqrt64
Description: 快速平方根倒数
Input: 双精度浮点数number
Output: 无
Input_Output: 无
Return: 快速平方根倒数的值
Author: Marc Pony(marc_pony@163.com)
***********************************************************************************************/
double fast_invSqrt64(double number)
{
	long long i;
	double x2, y;
	const double threehalfs = 1.5;

	x2 = number * 0.5;
	y = number;
	i = *(long long*)& y;
	i = 0x5fe6eb50c7b537a9 - (i >> 1);
	y = *(double*) &i;
	y = y * (threehalfs - (x2 * y * y));
	y = y * (threehalfs - (x2 * y * y));

	return y;
}


/**********************************************************************************************
Function: fast_sqrt64
Description: 快速平方根
Input: 双精度浮点数number
Output: 无
Input_Output: 无
Return: 快速平方根的值
Author: Marc Pony(marc_pony@163.com)
***********************************************************************************************/
double fast_sqrt64(double number)
{
	return 1.0 / fast_invSqrt64(number);
}
  • 5
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 8
    评论
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值