使用新的iFloat替代原先的ifloat_t

This commit is contained in:
2025-06-28 18:32:37 +08:00
parent 4db0f654ed
commit 8388f448f3
9 changed files with 1089 additions and 142 deletions
+456
View File
@@ -0,0 +1,456 @@
#pragma once
#include "dvc_config.h"
DV_CORE_BEGIN_NAMESPACE
class ifloat32
{
public:
static constexpr int32_t kShift = 16;
static constexpr float32_t kFloatScale = static_cast<float32_t>(1<<kShift); //65536.0
static constexpr float32_t kFloatInvScale = 1.f / kFloatScale;
static constexpr int32_t kZero_s1516 = 0;
static constexpr int32_t kOne_s1516 = 1 << kShift;
static constexpr int32_t kTwo_s1516 = 2 << kShift;
static constexpr int32_t kThree_s1516 = 3 << kShift;
static constexpr int32_t kFour_s1516 = 4 << kShift;
static constexpr int32_t kHalf_s1516 = kOne_s1516 >> 1;
static constexpr int32_t kPI_s1516 = 0x3243f;//(int32_t)(M_PI * kFloatScale);
static constexpr int32_t kPi2_s1516 = 0x6487e;
static constexpr int32_t kPiHalf_s1516 = 0x1921f;
static constexpr int32_t kE_s1516 = 0x2b7e1;
static constexpr int32_t kNegOne_s1516 = -1 << kShift;
static constexpr int32_t kMax_s1516 = 0x7fffff80; //(int32_t)(nextafter(INT32_MAX/kFloatScale, 0.f)*kFloatScale);
static constexpr int32_t kMax_int32 = kMax_s1516 >> kShift;
static constexpr float32_t kMax_float = (float32_t)kMax_s1516 * kFloatInvScale;
static constexpr int32_t kMin_s1516 = 0x80000000; // (int32_t)(nextafter(-INT32_MAX / ifloat32::kFloatScale, -FLT_MAX) * ifloat32::kFloatScale)
static constexpr int32_t kMin_int32 = kMin_s1516 >> kShift;
static constexpr float32_t kMin_float = (float32_t)kMin_s1516 * kFloatInvScale;
public:
ifloat32() : s1516(0) {}
ifloat32(const ifloat32& other) : s1516(other.s1516) {}
//Converts a float to a fixed-point value.
ifloat32(float32_t f) {
s1516 = static_cast<int32_t>(f * kFloatScale);
}
//Converts a double to a fixed-point value.
ifloat32(float64_t f) {
s1516 = static_cast<int32_t>(f * kFloatScale);
}
//Converts an integer to a fixed-point value.
ifloat32(int32_t n) {
s1516 = static_cast<int32_t>(n << kShift);
}
public:
//Converts into a float.
float32_t to_float() const {
return static_cast<float32_t>(s1516 * kFloatInvScale);
}
//Converts a fixed-point value into an integer by rounding it down to nearest integer. example: 3.94 -> 3, -2.1 -> -3
int32_t to_int32() const {
return static_cast<int32_t>(s1516 >> kShift);
}
public:
inline ifloat32& operator = (const ifloat32& other) {
s1516 = other.s1516;
return *this;
}
inline bool operator == (const ifloat32& other) const {
return (s1516 == other.s1516);
}
inline bool operator != (const ifloat32& other) const {
return (s1516 != other.s1516);
}
inline bool operator < (const ifloat32& other) const {
return (s1516 < other.s1516);
}
inline bool operator > (const ifloat32& other) const {
return (s1516 > other.s1516);
}
public:
inline ifloat32& operator += (const ifloat32 other) {
s1516 += other.s1516;
return *this;
}
inline ifloat32& operator -= (const ifloat32 other) {
s1516 -= other.s1516;
return *this;
}
public:
//Adds the two FP numbers together.
inline friend ifloat32 operator + (const ifloat32& l, const ifloat32& r) {
int64_t t = static_cast<int64_t>(l.s1516) + static_cast<int64_t>(r.s1516);
ifloat32 ret;
ret.s1516 = static_cast<int32_t>(t);
return ret;
}
//Subtracts the two FP numbers from each other.
inline friend ifloat32 operator - (const ifloat32& l, const ifloat32& r) {
int64_t t = static_cast<int64_t>(l.s1516) - static_cast<int64_t>(r.s1516);
ifloat32 ret;
ret.s1516 = t;
return ret;
}
inline friend ifloat32 operator * (const ifloat32& a, const float32_t& b) {
int64_t t = static_cast<int64_t>(a.s1516) * static_cast<int64_t>(b * kFloatScale);
t = t >> kShift;
ifloat32 ret;
ret.s1516 = t;
return ret;
}
inline friend ifloat32 operator * (const ifloat32& a, const int32_t& b) {
ifloat32 ret;
ret.s1516 = a.s1516 * b;
return ret;
}
//Multiplies two FP values together.
inline friend ifloat32 operator * (const ifloat32& a, const ifloat32& b) {
int64_t t = static_cast<int64_t>(a.s1516) * static_cast<int64_t>(b.s1516);
t = t >> kShift;
ifloat32 ret;
ret.s1516 = static_cast<int32_t>(t);
return ret;
}
inline friend ifloat32 operator / (const ifloat32& a, const ifloat32& b) {
// pre-multiply by the base
int64_t t = static_cast<int64_t>(a.s1516) << kShift;
t /= b.s1516;
ifloat32 ret;
ret.s1516 = static_cast<int32_t>(t);
return ret;
}
public:
int32_t s1516;
};
class ifloat64
{
public:
static constexpr int32_t kShift = 32;
static constexpr float32_t kFloatScale = static_cast<float32_t>(INT64_C(1) << kShift); //4294967296.0
static constexpr float32_t kFloatInvScale = 1.0f / kFloatScale;
static constexpr int64_t kFractionMask = (INT64_C(1) << kShift) - 1; // Space before INT64_C(1) needed because of hacky C++ code generator
//static_cast<int64_t>(nextafter(INT64_MAX / iFloat64::kFloatScale, 0.0) * iFloat64::kFloatScale);
static constexpr int64_t kMax_s3132 = INT64_C(0x7ffffffffffffc00);
static constexpr int32_t kMax_int32 = static_cast<int32_t>(kMax_s3132 >> kShift);
static constexpr float32_t kMax_float = 2.14748352e+09f; //nextafter(static_cast<float32_t>(kS3132_Max * kFloatInvScale), 0.0f);
//(int32_t)(nextafter(-INT64_MAX / iFloat64::kFloatScale, -DBL_MAX) * iFloat64::kFloatScale);
static constexpr int64_t kMin_s3132 = INT64_C(0x8000000000000000);
static constexpr int32_t kMin_int32 = static_cast<int32_t>(kMin_s3132 >> kShift);
static constexpr float32_t kMin_float = static_cast<float64_t>(kMin_s3132 * kFloatInvScale);
// special value
static DV_CORE_API const ifloat64 kMax;
static DV_CORE_API const ifloat64 kMin;
static DV_CORE_API const ifloat64 kZero;
static DV_CORE_API const ifloat64 kOne;
static DV_CORE_API const ifloat64 kTwo;
static DV_CORE_API const ifloat64 kThree;
static DV_CORE_API const ifloat64 kFour;
static DV_CORE_API const ifloat64 kHalf;
static DV_CORE_API const ifloat64 kPi;
static DV_CORE_API const ifloat64 kPi2;
static DV_CORE_API const ifloat64 kPiHalf;
static DV_CORE_API const ifloat64 kE;
static DV_CORE_API const ifloat64 kNegOne;
public:
ifloat64() : s3132(0) {}
ifloat64(const ifloat64& other) : s3132(other.s3132) {}
//Converts a float to a fixed-point value.
ifloat64(float32_t f) {
s3132 = static_cast<int64_t>(std::round(f * kFloatScale));
}
//Converts a double to a fixed-point value.
ifloat64(float64_t f) {
s3132 = static_cast<int64_t>(std::round(f * kFloatScale));
}
//Converts an integer to a fixed-point value.
ifloat64(int32_t n) {
s3132 = static_cast<int64_t>(n) << kShift;
}
private:
ifloat64(int64_t _s3132) {
s3132 = _s3132;
}
public:
//Converts into a float.
float32_t to_float() const {
return static_cast<float32_t>(s3132 * kFloatInvScale);
}
//Converts a fixed-point value into an integer by rounding it down to nearest integer. example: 3.94 -> 3, -2.1 -> -3
int32_t to_int32() const {
return static_cast<int32_t>(s3132 >> kShift);
}
public:
inline const ifloat64& operator + () const {
return *this;
}
inline ifloat64 operator - () const {
return ifloat64(-s3132);
}
inline ifloat64& operator = (const ifloat64& other) {
s3132 = other.s3132;
return *this;
}
inline bool operator == (const ifloat64& other) const {
return (s3132 == other.s3132);
}
inline bool operator != (const ifloat64& other) const {
return (s3132 != other.s3132);
}
inline bool operator < (const ifloat64& other) const {
return (s3132 < other.s3132);
}
inline bool operator > (const ifloat64& other) const {
return (s3132 > other.s3132);
}
public:
inline ifloat64& operator += (const ifloat64 other) {
s3132 += other.s3132;
return *this;
}
inline ifloat64& operator -= (const ifloat64 other) {
s3132 -= other.s3132;
return *this;
}
public:
//Adds the two FP numbers together.
inline friend ifloat64 operator + (const ifloat64& a, const ifloat64& b) {
ifloat64 ret;
ret.s3132 = a.s3132 + b.s3132;
return ret;
}
//Subtracts the two FP numbers from each other.
inline friend ifloat64 operator - (const ifloat64& a, const ifloat64& b) {
ifloat64 ret;
ret.s3132 = a.s3132 - b.s3132;
return ret;
}
inline friend ifloat64 operator * (const ifloat64& a, const int32_t& b) {
ifloat64 ret;
ret.s3132 = a.s3132 * b;
return ret;
}
inline friend ifloat64 operator * (const ifloat64& a, const float32_t& b) {
ifloat64 ret;
ret.s3132 = _safeMulti(a.s3132, static_cast<int64_t>(b * kFloatScale));
return ret;
}
//Multiplies two FP values together.
inline friend ifloat64 operator * (const ifloat64& a, const ifloat64& b) {
ifloat64 ret;
ret.s3132 = _safeMulti(a.s3132, b.s3132);
return ret;
}
inline friend ifloat64 operator / (const ifloat64& a, const ifloat64& b) {
// From http://www.hackersdelight.org/hdcodetxt/divlu.c.txt
int64_t sign_dif = a.s3132 ^ b.s3132;
static const uint64_t B = INT64_C(0x100000000); // Number base (32 bits)
uint64_t abs_a = (uint64_t)((a.s3132 < 0) ? -a.s3132 : a.s3132);
uint64_t u1 = abs_a >> 32;
uint64_t u0 = abs_a << 32;
uint64_t v = (uint64_t)((b.s3132 < 0) ? -b.s3132 : b.s3132);
// Overflow?
if (u1 >= v)
{
//invalid number
ifloat64 ret;
ret.s3132 = 0x7fffffffffffffff;
return ret;
}
// Shift amount for norm
int32_t s = _nlz(v); // 0 <= s <= 63
v = v << s; // Normalize the divisor
uint64_t vn1 = v >> 32; // Break the divisor into two 32-bit digits
uint64_t vn0 = v & INT64_C(0xffffffff);
uint64_t un32 = (u1 << s) | (u0 >> (64 - s)) & (uint64_t)((int64_t)-s >> 63);
uint64_t un10 = u0 << s; // Shift dividend left
uint64_t un1 = un10 >> 32; // Break the right half of dividend into two digits
uint64_t un0 = un10 & INT64_C(0xffffffff);
// Compute the first quotient digit, q1
uint64_t q1 = un32 / vn1;
uint64_t rhat = un32 - q1 * vn1;
do
{
if ((q1 >= B) || ((q1 * vn0) > (B * rhat + un1)))
{
q1 = q1 - 1;
rhat = rhat + vn1;
}
else break;
} while (rhat < B);
uint64_t un21 = un32 * B + un1 - q1 * v; // Multiply and subtract
// Compute the second quotient digit, q0
uint64_t q0 = un21 / vn1;
rhat = un21 - q0 * vn1;
do
{
if ((q0 >= B) || ((q0 * vn0) > (B * rhat + un0)))
{
q0 = q0 - 1;
rhat = rhat + vn1;
}
else break;
} while (rhat < B);
// Calculate the remainder
// uint64_t r = (un21 * b + un0 - q0 * v) >> s;
// rem = (int64_t)r;
int64_t t = q1 * B + q0;
ifloat64 ret;
ret.s3132 = (sign_dif < 0) ? -(int64_t)t : (int64_t)t;
return ret;
}
public:
static int64_t _safeMulti(const int64_t a, const int64_t b) {
int64_t sign_diff = a ^ b;
uint64_t abs_a = a < 0 ? -a : a;
uint64_t abs_b = b < 0 ? -b : b;
uint64_t ai = abs_a >> kShift;
uint64_t af = (abs_a & kFractionMask);
uint64_t bi = abs_b >> kShift;
uint64_t bf = (abs_b & kFractionMask);
// (Ai+Af)*(Bi+Bf)=Af*Bf + Ai*(Bi+Bf) + Af*Bi
int64_t t = ((af * bf) >> kShift) + ai * abs_b + af * bi;
return (sign_diff < 0) ? -t : t;
}
static int32_t _nlz(uint64_t x)
{
int32_t n = 0;
if (x <= INT64_C(0x00000000FFFFFFFF)) { n = n + 32; x = x << 32; }
if (x <= INT64_C(0x0000FFFFFFFFFFFF)) { n = n + 16; x = x << 16; }
if (x <= INT64_C(0x00FFFFFFFFFFFFFF)) { n = n + 8; x = x << 8; }
if (x <= INT64_C(0x0FFFFFFFFFFFFFFF)) { n = n + 4; x = x << 4; }
if (x <= INT64_C(0x3FFFFFFFFFFFFFFF)) { n = n + 2; x = x << 2; }
if (x <= INT64_C(0x7FFFFFFFFFFFFFFF)) { n = n + 1; }
if (x == 0) return 64;
return n;
}
public:
int64_t s3132;
};
typedef ifloat32 iFloat32;
typedef ifloat64 iFloat64;
typedef ifloat64 iFloat;
struct iFloat2
{
iFloat x, y;
iFloat2() : x(0), y(0) {}
iFloat2(const iFloat2& other) : x(other.x), y(other.y) {}
iFloat2(float32_t _x, float32_t _y) : x(_x), y(_y) {}
};
struct iFloat3
{
iFloat x, y, z;
inline void add(const iFloat3& other) {
x += other.x;
y += other.y;
z += other.z;
}
inline void sub(const iFloat3& other) {
x -= other.x;
y -= other.y;
z -= other.z;
}
inline void mul(const iFloat& multiplier) {
x = x * multiplier;
y = y * multiplier;
z = z * multiplier;
}
inline iFloat operator [] (const size_t i) const {
return *(&x + i);
}
inline iFloat& operator [] (const size_t i) {
return *(&x + i);
}
iFloat3() : x(0), y(0), z(0) {}
iFloat3(const iFloat3& other) : x(other.x), y(other.y), z(other.z) {}
iFloat3(float32_t _x, float32_t _y, float32_t _z) : x(_x), y(_y), z(_z) {}
iFloat3(iFloat _x, iFloat _y, iFloat _z) : x(_x), y(_y), z(_z) {}
};
DV_CORE_END_NAMESPACE
-98
View File
@@ -1,98 +0,0 @@
#pragma once
#include "dvc_config.h"
DV_CORE_BEGIN_NAMESPACE
typedef int64_t ifloat_t;
//typedef int64_t ifloat2_t[2];
//typedef int64_t ifloat3_t[3];
#define IFLOAT_SHIFT (16)
#define IFLOAT_SCALE ((float64_t)(1<<IFLOAT_SHIFT))
//-------------------------------------------------------------------------------------
inline ifloat_t ifloat_init(float32_t a)
{
return (ifloat_t)((float64_t)a * IFLOAT_SCALE + 0.5);
}
//-------------------------------------------------------------------------------------
inline ifloat_t ifloat_init(int32_t a)
{
return (int64_t)(a)<< IFLOAT_SHIFT;
}
//-------------------------------------------------------------------------------------
inline float32_t ifloat_get_float(ifloat_t a)
{
return (float32_t)((float64_t)a/ IFLOAT_SCALE);
}
//-------------------------------------------------------------------------------------
inline ifloat_t ifloat_multiply(ifloat_t a, ifloat_t b)
{
return (ifloat_t)((a * b) >> IFLOAT_SHIFT);
}
//-------------------------------------------------------------------------------------
//inline void ifloat3_add(ifloat3_t& a, const ifloat3_t& b)
//{
// a[0] += b[0]; a[1] += b[1]; a[2] += b[2];
//}
//-------------------------------------------------------------------------------------
//inline void ifloat3_sub(ifloat3_t& a, const ifloat3_t& b)
//{
// a[0] -= b[0]; a[1] -= b[1]; a[2] -= b[2];
//}
//-------------------------------------------------------------------------------------
//inline void ifloat3_cpy(ifloat3_t& a, const ifloat3_t& b)
//{
// a[0] = b[0]; a[1] = b[1]; a[2] = b[2];
//}
//-------------------------------------------------------------------------------------
struct ifloat2_t
{
ifloat_t x, y;
ifloat2_t() : x(0ll), y(0ll) {}
ifloat2_t(const ifloat2_t& other) : x(other.x), y(other.y) {}
ifloat2_t(float32_t _x, float32_t _y) : x(ifloat_init(_x)), y(ifloat_init(_y)) {}
};
struct ifloat3_t
{
ifloat_t x, y, z;
inline void add(const ifloat3_t& other) {
x += other.x;
y += other.y;
z += other.z;
}
inline void sub(const ifloat3_t& other) {
x -= other.x;
y -= other.y;
z -= other.z;
}
inline ifloat_t operator [] (const size_t i) const {
return *(&x + i);
}
inline ifloat_t& operator [] (const size_t i) {
return *(&x + i);
}
ifloat3_t() : x(0ll), y(0ll), z(0ll) {}
ifloat3_t(const ifloat3_t& other) : x(other.x), y(other.y), z(other.z) {}
ifloat3_t(float32_t _x, float32_t _y, float32_t _z) : x(ifloat_init(_x)), y(ifloat_init(_y)), z(ifloat_init(_z)) {}
ifloat3_t(ifloat_t _x, ifloat_t _y, ifloat_t _z) : x(_x), y(_y), z(_z) {}
};
DV_CORE_END_NAMESPACE