diff --git a/Descent3/mission_download.cpp b/Descent3/mission_download.cpp index f494abe96..00263779a 100644 --- a/Descent3/mission_download.cpp +++ b/Descent3/mission_download.cpp @@ -399,7 +399,7 @@ bool msn_DownloadWithStatus(const char *url, const std::filesystem::path &filena } httplib::Result (D3::HttpClient::*hcg)(const std::string &, const httplib::ContentReceiver &, - const httplib::Progress &) = &D3::HttpClient::Get; + const D3::HttpClient::Progress &) = &D3::HttpClient::Get; std::fstream in(qualfile, std::ios::binary | std::ios::trunc | std::ios::out); auto async_task = std::async( std::launch::async, hcg, &http_client, download_uri, diff --git a/lib/vecmat_external.h b/lib/vecmat_external.h index 1f6ce5d29..6c28384ac 100644 --- a/lib/vecmat_external.h +++ b/lib/vecmat_external.h @@ -49,47 +49,39 @@ #include #include -static constexpr inline bool VM_ISPOW2(uintmax_t x) { return (x && (!(x & (x-1)))); } - -template -static constexpr inline int VM_BIT_SCAN_REVERSE_CONST(const T n) noexcept { - if (n == 0) return -1; - if (VM_ISPOW2(n)) return n; - T a = n, b = 0, j = std::numeric_limits::digits, k = 0; - do { - j >>= 1; - k = (T)1 << j; - if (a >= k) { - a >>= j; - b += j; - } - } while (j > 0); - return int(b); -} - -template -static constexpr inline T VM_BIT_CEIL(T x) noexcept { return T(1) << (VM_BIT_SCAN_REVERSE_CONST((T)x-1) + 1); }; - -template -static constexpr inline T VM_BIT_FLOOR(T x) noexcept { return x == 0 || VM_ISPOW2(x) ? x : ((VM_BIT_FLOOR(x >> 1)) << 1); } - -#ifdef _MSVC_LANG -#define VM_ALIGN(n) __declspec(align(n)) -#else -#define VM_ALIGN(n) __attribute__((aligned(n))) -#endif +#define BITOP_RUP01__(x) ( (x) | ( (x) >> 1)) +#define BITOP_RUP02__(x) (BITOP_RUP01__(x) | (BITOP_RUP01__(x) >> 2)) +#define BITOP_RUP04__(x) (BITOP_RUP02__(x) | (BITOP_RUP02__(x) >> 4)) +#define BITOP_RUP08__(x) (BITOP_RUP04__(x) | (BITOP_RUP04__(x) >> 8)) +#define BITOP_RUP16__(x) (BITOP_RUP08__(x) | (BITOP_RUP08__(x) >> 16)) +#define BITOP_RUP32__(x) (BITOP_RUP16__(x) | (BITOP_RUP16__(x) >> 32)) +#define BITOP_RUP64__(x) (BITOP_RUP32__(x) | (BITOP_RUP32__(x) >> 64)) + +constexpr static inline uintmax_t VM_BIT_CEIL (uintmax_t x) { +switch(std::numeric_limits::digits) +{ + case 8: return (BITOP_RUP04__(uint8_t (x) - 1) + 1); + case 16: return (BITOP_RUP08__(uint16_t(x) - 1) + 1); + default: + case 32: return (BITOP_RUP16__(uint32_t(x) - 1) + 1); + case 64: return (BITOP_RUP32__(uint64_t(x) - 1) + 1); +} +} + +constexpr static inline bool VM_ISPOW2 (uintmax_t x) { return x && (!(x & (x-1))); } +constexpr static inline uintmax_t VM_BIT_FLOOR(uintmax_t x) { return ((x == 0) || VM_ISPOW2(x) ? x : ((VM_BIT_FLOOR(x >> 1)) << 1)); } enum class align { none = 0 << 0, - scalar = 1 << 1, - vector = 2 << 1, - matrix = 3 << 1, - adaptive = 4 << 1, + scalar = 1 << 0, + linear = 1 << 1, + matrix = 1 << 2, + adaptive = 1 << 3, }; -template -struct alignas(N==N_POW2 && A != align::scalar || A == align::vector ? alignof(T) * N_POW2 : alignof(T)) vec : std::array { +template(alignof(T), sizeof(T))> +struct alignas((((N == N_POW2) && (A != align::scalar)) || ((A == align::linear) || (A == align::matrix))) ? N_POW2 * T_S : T_S) vec : std::array { template operator vec() { return *reinterpret_cast*>(this); } @@ -287,8 +279,8 @@ constexpr static inline scalar dot(const vec &a, const vec template constexpr static inline vec cross3(const vec &a, const vec &b) { - return vec{a.y()*b.z(), a.z()*b.x(), a.x()*b.y()} - - vec{b.y()*a.z(), b.z()*a.x(), b.x()*a.y()}; + return vec{a.y()*b.z(), a.z()*b.x(), a.x()*b.y()} + - vec{b.y()*a.z(), b.z()*a.x(), b.x()*a.y()}; } constexpr inline scalar mag() const { return (scalar)sqrt(dot((*this),(*this))); } @@ -300,17 +292,17 @@ static constexpr inline scalar distance(const vec &a, const vec; using vector_array = vector; -using aligned_vector = vec; +using aligned_vector = vec; using aligned_vector_array = aligned_vector; using vector4 = vec; using vector4_array = vector4; -using aligned_vector4 = vec; +using aligned_vector4 = vec; using aligned_vector4_array = aligned_vector4; using angvec = vec; using angvec_array = angvec; -using aligned_angvec = vec; +using aligned_angvec = vec; using aligned_angvec_array = aligned_angvec; // Set an angvec to {0,0,0} @@ -339,9 +331,31 @@ constexpr static inline const matrix ne() { return matrix{ vector{}, vector{}, vector{} }; } +constexpr inline void set_col(size_t i, const vector v) +{ + a2d[0][i % 3] = v[0]; + a2d[1][i % 3] = v[1]; + a2d[2][i % 3] = v[2]; +} +constexpr inline vector col(size_t i) const +{ + return vector{ a2d[0][i % 3], a2d[1][i % 3], a2d[2][i % 3] }; +} +constexpr inline matrix transposed() const +{ + return matrix{ col(0), col(1), col(2) }; +} +constexpr inline scalar det() const +{ + scalar dst = scalar(0); + for(size_t i = 0; i < 3; i++) + dst += a2d[0][i] * (a2d[1][(i+1) % 3] * a2d[2][(i+2) % 3] + - a2d[1][(i+2) % 3] * a2d[2][(i+1) % 3]); + return dst; +} }; -constexpr matrix IDENTITY_MATRIX = matrix::id(); +constexpr matrix IDENTITY_MATRIX = { vector::id(0), vector::id(1), vector::id(2) }; struct alignas(alignof(vector4) * 4) matrix4 { constexpr static const size_t RIGHT_HAND = 0; @@ -366,6 +380,21 @@ constexpr static inline const matrix4 ne() { return matrix4{ vector4{}, vector4{}, vector4{}, vector4{} }; } +constexpr inline void set_col(size_t i, const vector4 v) +{ + a2d[0][i % 4] = v[0]; + a2d[1][i % 4] = v[1]; + a2d[2][i % 4] = v[2]; + a2d[3][i % 4] = v[3]; +} +constexpr inline vector4 col(size_t i) const +{ + return vector4{ a2d[0][i % 4], a2d[1][i % 4], a2d[2][i % 4], a2d[2][i % 4] }; +} +constexpr inline matrix4 transposed() const +{ + return matrix4{ col(0), col(1), col(2), col(3) }; +} }; // Adds 2 matrices @@ -436,19 +465,7 @@ static inline matrix operator/=(matrix &src, float n) { return (src = src / n); // Matrix transpose static inline matrix operator~(matrix m) { - float t; - - t = m.uvec.x(); - m.uvec.x() = m.rvec.y(); - m.rvec.y() = t; - t = m.fvec.x(); - m.fvec.x() = m.rvec.z(); - m.rvec.z() = t; - t = m.fvec.y(); - m.fvec.y() = m.uvec.z(); - m.uvec.z() = t; - - return m; + return m.transposed(); } // Apply a matrix to a vector diff --git a/netcon/inetfile/httpclient.cpp b/netcon/inetfile/httpclient.cpp index d15bc9063..d6810d235 100644 --- a/netcon/inetfile/httpclient.cpp +++ b/netcon/inetfile/httpclient.cpp @@ -30,7 +30,7 @@ httplib::Result HttpClient::Get(const std::string &URIPath) { return m_client->Get(URIPath); } -httplib::Result HttpClient::Get(const std::string &URIPath, const httplib::Progress &progress) { +httplib::Result HttpClient::Get(const std::string &URIPath, const HttpClient::Progress &progress) { return m_client->Get(URIPath, progress); } @@ -39,7 +39,7 @@ httplib::Result HttpClient::Get(const std::string &URIPath, const httplib::Conte } httplib::Result HttpClient::Get(const std::string &URIPath, const httplib::ContentReceiver &content_receiver, - const httplib::Progress &progress) { + const HttpClient::Progress &progress) { return m_client->Get(URIPath, content_receiver, progress); } diff --git a/netcon/inetfile/httpclient.h b/netcon/inetfile/httpclient.h index d3e2499ce..72b8105e7 100644 --- a/netcon/inetfile/httpclient.h +++ b/netcon/inetfile/httpclient.h @@ -32,6 +32,7 @@ namespace D3 { class HttpClient { public: + using Progress = std::function; /** * Constructor for HttpClient * @param URL request URL, should be in form of http://example.com. Don't add "/" on the end as it's part of URIPath. @@ -46,12 +47,12 @@ class HttpClient { * @param URIPath requested path (for example, "/some_dir/my_file.txt") * @return standard HTTP code. 200 means is OK. */ - httplib::Result Get(const std::string &URIPath, const httplib::Progress &progress); + httplib::Result Get(const std::string &URIPath, const HttpClient::Progress &progress); httplib::Result Get(const std::string &URIPath, const httplib::ContentReceiver &content_receiver); httplib::Result Get(const std::string &URIPath, const httplib::ContentReceiver &content_receiver, - const httplib::Progress &progress); + const HttpClient::Progress &progress); void SetProxy(const std::string &proxy_host, uint16_t port); diff --git a/vecmat/vector.cpp b/vecmat/vector.cpp index c0a3d047a..2bcfd9895 100644 --- a/vecmat/vector.cpp +++ b/vecmat/vector.cpp @@ -273,17 +273,9 @@ void vm_MatrixMulTMatrix(matrix *dest, const matrix *src0, const matrix *src1) { ASSERT((dest != src0) && (dest != src1)); - dest->rvec.x() = src0->rvec.x() * src1->rvec.x() + src0->uvec.x() * src1->uvec.x() + src0->fvec.x() * src1->fvec.x(); - dest->uvec.x() = src0->rvec.x() * src1->rvec.y() + src0->uvec.x() * src1->uvec.y() + src0->fvec.x() * src1->fvec.y(); - dest->fvec.x() = src0->rvec.x() * src1->rvec.z() + src0->uvec.x() * src1->uvec.z() + src0->fvec.x() * src1->fvec.z(); - - dest->rvec.y() = src0->rvec.y() * src1->rvec.x() + src0->uvec.y() * src1->uvec.x() + src0->fvec.y() * src1->fvec.x(); - dest->uvec.y() = src0->rvec.y() * src1->rvec.y() + src0->uvec.y() * src1->uvec.y() + src0->fvec.y() * src1->fvec.y(); - dest->fvec.y() = src0->rvec.y() * src1->rvec.z() + src0->uvec.y() * src1->uvec.z() + src0->fvec.y() * src1->fvec.z(); - - dest->rvec.z() = src0->rvec.z() * src1->rvec.x() + src0->uvec.z() * src1->uvec.x() + src0->fvec.z() * src1->fvec.x(); - dest->uvec.z() = src0->rvec.z() * src1->rvec.y() + src0->uvec.z() * src1->uvec.y() + src0->fvec.z() * src1->fvec.y(); - dest->fvec.z() = src0->rvec.z() * src1->rvec.z() + src0->uvec.z() * src1->uvec.z() + src0->fvec.z() * src1->fvec.z(); + dest->set_col(0, src0->rvec.x() * src1->rvec + src0->uvec.x() * src1->uvec + src0->fvec.x() * src1->fvec); + dest->set_col(1, src0->rvec.y() * src1->rvec + src0->uvec.y() * src1->uvec + src0->fvec.y() * src1->fvec); + dest->set_col(2, src0->rvec.z() * src1->rvec + src0->uvec.z() * src1->uvec + src0->fvec.z() * src1->fvec); } matrix operator*(const matrix &src0, const matrix &src1) { @@ -612,9 +604,7 @@ angvec *vm_ExtractAnglesFromMatrix(angvec *a, const matrix *m) { // returns the value of a determinant scalar calc_det_value(const matrix *det) { - return det->rvec.x() * det->uvec.y() * det->fvec.z() - det->rvec.x() * det->uvec.z() * det->fvec.y() - - det->rvec.y() * det->uvec.x() * det->fvec.z() + det->rvec.y() * det->uvec.z() * det->fvec.x() + - det->rvec.z() * det->uvec.x() * det->fvec.y() - det->rvec.z() * det->uvec.y() * det->fvec.x(); + return det->det(); } // computes the delta angle between two vectors. @@ -762,9 +752,7 @@ scalar vm_GetCentroidFast(vector *centroid, const vector *src, int nv) { // creates a completely random, non-normalized vector with a range of values from -1023 to +1024 values) void vm_MakeRandomVector(vector *vec) { - vec->x() = ps_rand() - D3_RAND_MAX / 2; - vec->y() = ps_rand() - D3_RAND_MAX / 2; - vec->z() = ps_rand() - D3_RAND_MAX / 2; + vec->fill(ps_rand() - D3_RAND_MAX / 2); } // Given a set of points, computes the minimum bounding sphere of those points