Daily update on 20230204 by nitload youmu, pow func added
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
|
||||
//
|
||||
//
|
||||
// some of the code is rewritten from the AMD code:
|
||||
// https://github.com/amd/aocl-libm-ose/blob/a43e092b03213eb0e129a1cc129900e6e4475778/src/ref/asinf.c
|
||||
|
||||
|
||||
@@ -4,7 +4,22 @@ using System.Text;
|
||||
|
||||
namespace StgSharp
|
||||
{
|
||||
|
||||
|
||||
public static unsafe partial class Math
|
||||
{
|
||||
|
||||
/// <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;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -24,10 +24,6 @@ namespace StgSharp
|
||||
0.53062825106217f, 0.536493370514568f, 0.542324290825362f, 0.548121408509688f, 0.553885113226438f, 0.559615787935423f, 0.56531380905006f, 0.570979546585738f, 0.576613364303994f, 0.582215619852664f,
|
||||
0.587786664902119f, 0.593326845277734f, 0.598836501088704f, 0.60431596685333f, 0.609765571620894f, 0.615185639090233f, 0.62057648772511f, 0.625938430866495f, 0.631271776841858f, 0.636576829071551f,
|
||||
0.641853886172395f, 0.647103242058538f, 0.65232518603969f, 0.657520002916794f, 0.662687973075237f, 0.667829372575655f, 0.672944473242426f, 0.678033542749897f, 0.683096844706444f, 0.688134638736401f,
|
||||
|
||||
|
||||
|
||||
|
||||
};
|
||||
|
||||
/// <summary>
|
||||
@@ -41,7 +37,7 @@ namespace StgSharp
|
||||
{
|
||||
if (x<0)
|
||||
{
|
||||
throw new ArgumentException();
|
||||
throw new ArgumentOutOfRangeException();
|
||||
}
|
||||
uint input = *(uint*)&x;
|
||||
|
||||
|
||||
@@ -0,0 +1,90 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace StgSharp
|
||||
{
|
||||
public static unsafe partial class Math
|
||||
{
|
||||
/// <summary>
|
||||
/// calculate the value of x^y
|
||||
/// </summary>
|
||||
/// <param name="x"></param>
|
||||
/// <param name="y"></param>
|
||||
/// <returns>Returns the value of x^y</returns>
|
||||
public static unsafe float Pow(float x, float y)
|
||||
{
|
||||
/*
|
||||
* x^y = E ^ ( log( x ) * y )
|
||||
* Now define z = | log(x) * y | = m * n
|
||||
* and m is an integer, and 0.5 < |n| < 1
|
||||
* then E ^ z = ( E ^ n ) ^ m
|
||||
*
|
||||
*
|
||||
* As a float number can be expressed in (1+e)*(2^f)
|
||||
* n = (1+e)/2
|
||||
* m = 2 ^ (f + 1)
|
||||
*
|
||||
*/
|
||||
float z, t;
|
||||
if (x == 1 || y == 0)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
if (x==1&&y==0)
|
||||
{
|
||||
return 1.0f / 0;
|
||||
}
|
||||
z = StgSharp.Math.Log(x) * y;
|
||||
|
||||
bool sign = false;
|
||||
if (z < 0)
|
||||
{
|
||||
sign = true;
|
||||
z = -z;
|
||||
}
|
||||
|
||||
if (z<0.5f)
|
||||
{
|
||||
t = 1.0f + z * (
|
||||
1.0f + z * (
|
||||
0.5f + z * (
|
||||
0.1666666667f + z * (
|
||||
0.0416666666667f + z *
|
||||
8.333333333e-3f))));
|
||||
}
|
||||
else
|
||||
{
|
||||
uint zBit = *(uint*)&z;
|
||||
uint zBitAbs = zBit & 0b_0111_1111_1111_1111_1111_1111_1111_1111;
|
||||
uint uintm = (zBitAbs >> 23);
|
||||
int m = (*(int*)&uintm) - 126;
|
||||
|
||||
|
||||
uint nBit = (zBitAbs & 0b_0000_0000_0111_1111_1111_1111_1111_1111)
|
||||
+ 0b_0011_1111_0000_0000_0000_0000_0000_0000;
|
||||
float n = *(float*)&nBit - 1;
|
||||
|
||||
t = 2.718281828f + n * (
|
||||
2.718281828f + n * (
|
||||
1.359140914f + n * (
|
||||
0.4530469714f + n * (
|
||||
0.1132617429f + n *
|
||||
0.02265234857f))));
|
||||
|
||||
for (int i = 0; i < m; i++)
|
||||
{
|
||||
t = t * t;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
if (sign)
|
||||
{
|
||||
t = 1 / t;
|
||||
}
|
||||
|
||||
return t;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -24,7 +24,16 @@ namespace StgSharp
|
||||
* 在进一步简化操作后,计算精度达到0.5%,速度约为.Net库函数的1.8倍
|
||||
*/
|
||||
|
||||
/*
|
||||
|
||||
if (x < 0)
|
||||
{
|
||||
throw new ArgumentOutOfRangeException();
|
||||
}
|
||||
if (x == 0)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
/*
|
||||
* 提取指数位并减去指数位中的127
|
||||
* 由于平方根仅对正数有效
|
||||
* 故不考虑正负号
|
||||
|
||||
Reference in New Issue
Block a user