libnum: generalize scalar product/division to all num types

This commit is contained in:
Lephenixnoir 2023-05-29 09:56:32 +02:00
parent f3bd29fb32
commit be4e4308cb
Signed by: Lephenixnoir
GPG Key ID: 1BBA026E13FC0495
1 changed files with 23 additions and 18 deletions

View File

@ -396,24 +396,6 @@ struct num32
int strToBuffer(char *str);
};
/* Arithmetic with integers */
inline constexpr num32 operator*(int n, num32 x) {
num32 r;
r.v = n * x.v;
return r;
}
inline constexpr num32 operator*(num32 x, int n) {
num32 r;
r.v = n * x.v;
return r;
}
inline constexpr num32 operator/(num32 x, int n) {
num32 r;
r.v = x.v / n;
return r;
}
/* num64: Signed 32:32 fixed-point type
* Size: 64 bits (8 bytes)
* Range: -2147483648.0 ... 2147483647.999999998
@ -616,6 +598,29 @@ inline constexpr T clamp(T const &val, T const &lower, T const &upper)
return max(lower, min(val, upper));
}
/* Arithmetic with integers */
template<typename T> requires(is_num<T>)
inline constexpr T operator*(int n, T const &x) {
T r;
r.v = n * x.v;
return r;
}
template<typename T> requires(is_num<T>)
inline constexpr T operator*(T const &x, int n) {
T r;
r.v = n * x.v;
return r;
}
template<typename T> requires(is_num<T>)
inline constexpr T operator/(T const &x, int n) {
T r;
r.v = x.v / n;
return r;
}
/* Other specific operations */
inline constexpr num32 num16::dmul(num16 const &x, num16 const &y)