增加iFloat64::from_raw函数接口
This commit is contained in:
@@ -62,6 +62,11 @@ public:
|
|||||||
return static_cast<int32_t>(s1516 >> kShift);
|
return static_cast<int32_t>(s1516 >> kShift);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static ifloat32 from_raw(int32_t raw) {
|
||||||
|
ifloat32 ret;
|
||||||
|
ret.s1516 = raw;
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
public:
|
public:
|
||||||
inline ifloat32& operator = (const ifloat32& other) {
|
inline ifloat32& operator = (const ifloat32& other) {
|
||||||
s1516 = other.s1516;
|
s1516 = other.s1516;
|
||||||
@@ -99,56 +104,34 @@ public:
|
|||||||
//Adds the two FP numbers together.
|
//Adds the two FP numbers together.
|
||||||
inline friend ifloat32 operator + (const ifloat32& l, const ifloat32& r) {
|
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);
|
int64_t t = static_cast<int64_t>(l.s1516) + static_cast<int64_t>(r.s1516);
|
||||||
|
return from_raw(t);
|
||||||
ifloat32 ret;
|
|
||||||
ret.s1516 = static_cast<int32_t>(t);
|
|
||||||
return ret;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//Subtracts the two FP numbers from each other.
|
//Subtracts the two FP numbers from each other.
|
||||||
inline friend ifloat32 operator - (const ifloat32& l, const ifloat32& r) {
|
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);
|
int64_t t = static_cast<int64_t>(l.s1516) - static_cast<int64_t>(r.s1516);
|
||||||
|
return from_raw(t);
|
||||||
ifloat32 ret;
|
|
||||||
ret.s1516 = t;
|
|
||||||
return ret;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
inline friend ifloat32 operator * (const ifloat32& a, const float32_t& b) {
|
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);
|
int64_t t = static_cast<int64_t>(a.s1516) * static_cast<int64_t>(b * kFloatScale);
|
||||||
|
return from_raw(t >> kShift);
|
||||||
t = t >> kShift;
|
|
||||||
|
|
||||||
ifloat32 ret;
|
|
||||||
ret.s1516 = t;
|
|
||||||
return ret;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
inline friend ifloat32 operator * (const ifloat32& a, const int32_t& b) {
|
inline friend ifloat32 operator * (const ifloat32& a, const int32_t& b) {
|
||||||
ifloat32 ret;
|
return from_raw(a.s1516 * b);
|
||||||
ret.s1516 = a.s1516 * b;
|
|
||||||
return ret;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//Multiplies two FP values together.
|
//Multiplies two FP values together.
|
||||||
inline friend ifloat32 operator * (const ifloat32& a, const ifloat32& b) {
|
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);
|
int64_t t = static_cast<int64_t>(a.s1516) * static_cast<int64_t>(b.s1516);
|
||||||
|
return from_raw(static_cast<int32_t>(t >> kShift));
|
||||||
t = t >> kShift;
|
|
||||||
|
|
||||||
ifloat32 ret;
|
|
||||||
ret.s1516 = static_cast<int32_t>(t);
|
|
||||||
return ret;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
inline friend ifloat32 operator / (const ifloat32& a, const ifloat32& b) {
|
inline friend ifloat32 operator / (const ifloat32& a, const ifloat32& b) {
|
||||||
// pre-multiply by the base
|
// pre-multiply by the base
|
||||||
int64_t t = static_cast<int64_t>(a.s1516) << kShift;
|
int64_t t = static_cast<int64_t>(a.s1516) << kShift;
|
||||||
t /= b.s1516;
|
return from_raw(static_cast<int32_t>(t/ b.s1516));
|
||||||
|
|
||||||
ifloat32 ret;
|
|
||||||
ret.s1516 = static_cast<int32_t>(t);
|
|
||||||
return ret;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public:
|
public:
|
||||||
@@ -297,7 +280,7 @@ public:
|
|||||||
ret.s3132 = _safeMulti(a.s3132, b.s3132);
|
ret.s3132 = _safeMulti(a.s3132, b.s3132);
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline friend ifloat64 operator / (const ifloat64& a, const ifloat64& b) {
|
inline friend ifloat64 operator / (const ifloat64& a, const ifloat64& b) {
|
||||||
// From http://www.hackersdelight.org/hdcodetxt/divlu.c.txt
|
// From http://www.hackersdelight.org/hdcodetxt/divlu.c.txt
|
||||||
|
|
||||||
@@ -369,7 +352,7 @@ public:
|
|||||||
ret.s3132 = (sign_dif < 0) ? -(int64_t)t : (int64_t)t;
|
ret.s3132 = (sign_dif < 0) ? -(int64_t)t : (int64_t)t;
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
public:
|
public:
|
||||||
static int64_t _safeMulti(const int64_t a, const int64_t b) {
|
static int64_t _safeMulti(const int64_t a, const int64_t b) {
|
||||||
int64_t sign_diff = a ^ b;
|
int64_t sign_diff = a ^ b;
|
||||||
@@ -440,6 +423,12 @@ struct iFloat3
|
|||||||
z = z * multiplier;
|
z = z * multiplier;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
inline void mul(const int32_t& multiplier) {
|
||||||
|
x = x * multiplier;
|
||||||
|
y = y * multiplier;
|
||||||
|
z = z * multiplier;
|
||||||
|
}
|
||||||
|
|
||||||
inline iFloat operator [] (const size_t i) const {
|
inline iFloat operator [] (const size_t i) const {
|
||||||
return *(&x + i);
|
return *(&x + i);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user