daily update on 20230316 by nitload youmu

This commit is contained in:
Nitload
2023-03-17 01:52:35 +08:00
parent 2b3c6cea88
commit 167e33a3dd
95 changed files with 191669 additions and 85 deletions
+13
View File
@@ -8,5 +8,18 @@ namespace StgSharp.Math
{
public static unsafe partial class Calc
{
public static unsafe float Abs(float x)
{
uint* a = (uint*)&x;
*a &= 0b_0111_1111_1111_1111_1111_1111_1111_1111;
return *(float*)a;
}
}
}
-23
View File
@@ -1,23 +0,0 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace StgSharp.Math
{
public static unsafe partial class Calc
{
/// <summary>
/// Get the bits of a float number
/// </summary>
/// <param name="x">The given float number</param>
/// <returns></returns>
public static uint GetBits32(float x)
{
Union4 union = new Union4();
union.f = x;
return union.i;
}
}
}
+27 -24
View File
@@ -1,18 +1,9 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Text;
namespace StgSharp.Math
{
public static partial class Calc
{
/*本方法将小数部分统一提升至2^16处后进行线性近似
const uint sqmov = 0b_0100_0111_1000_0000_0000_0000_0000_0000;
const float sqmov1 = 0.0015947f;
const float sqmov2 = 156.7673435f;
const float sqmov3 = 261.2776027f;
/**/
public static unsafe float Sqrt(float x)
{
/*
@@ -21,10 +12,9 @@ namespace StgSharp.Math
* (补充:指数最低位被加到小数位中以抵消一个多余的2 ^ 1的影响)
* 小数部分则统一乘以2 ^ 16后由y = Sqrt(x)在1.5 * 2 ^ 16处的切线近似
* 为简化操作,提高计算速度,切线左界被移动到远点
* 在进一步简化操作后,计算精度达到0.5%,速度约为.Net库函数的1.8倍
* 在进一步简化操作后,计算精度达到0.5%,速度约为.Net库函数的3倍
*/
if (x < 0)
{
throw new ArgumentOutOfRangeException();
@@ -33,41 +23,54 @@ namespace StgSharp.Math
{
return 0;
}
/*
* 提取指数位并减去指数位中的127
* 由于平方根仅对正数有效
* 故不考虑正负号
*/
/*
* 提取指数位并减半
* 由于平方根仅对正数有效
* 故不考虑正负号
*/
if (x < 0)
{
throw new ArgumentOutOfRangeException();
}
if (x == 0)
{
return 0;
}
//提取指数位并减去指数位中的127
//由于平方根仅对正数有效
//故不考虑正负号
uint a = *(uint*)&x;
uint f = a;
f >>= 23;
f /= 2;
f += 64;
/*
* 提取小数位
* 由于平方根仅对正数有效
* 故不考虑正负号
*/
//提取小数位
//由于平方根仅对正数有效
//故不考虑正负号
a -= 0b_0011_1111_0000_0000_0000_0000_0000_0000;
uint e = a << 8;
e >>= 8;
/*线性近似*/
//线性近似
e *= 0b_1101_0001;
e >>= 24;
e += 0b_0000_0000_0001_0000_1010_1000_1110_0010;
/*还原指数*/
//还原指数
f <<= 23;
e += f;
float result = *(float*)&e;
result = (result + x / result) / 2;
return (result + x / result) / 2;
}
}
}