From cda8c6164c8de550fcfcdd0579b7d7d3c3a677b5 Mon Sep 17 00:00:00 2001 From: Jeonghwan Lee Date: Thu, 16 Apr 2026 12:51:17 +0900 Subject: [PATCH 1/3] fix: add MinGW compatibility for aligned memory allocation Replace std::aligned_alloc / operator new[](align_val_t) with platform-abstracted helpers that use _aligned_malloc/_aligned_free on Windows and the standard functions elsewhere. MinGW's C++ stdlib does not provide std::aligned_alloc, and aligned operator new[]/delete[] support is inconsistent across MinGW runtime versions. Co-Authored-By: Claude Opus 4.6 (1M context) --- src/CKKSTypes.cpp | 56 +++++++++++++++++++++++++++++++---------------- 1 file changed, 37 insertions(+), 19 deletions(-) diff --git a/src/CKKSTypes.cpp b/src/CKKSTypes.cpp index c59a4f3..7b263a0 100644 --- a/src/CKKSTypes.cpp +++ b/src/CKKSTypes.cpp @@ -17,8 +17,30 @@ #include "CKKSTypes.hpp" #include +#if defined(_WIN32) +#include +#endif + namespace deb { +#if DEB_ALINAS_LEN != 0 +inline void *deb_aligned_alloc(size_t alignment, size_t size) { +#if defined(_WIN32) + return _aligned_malloc(size, alignment); +#else + return std::aligned_alloc(alignment, size); +#endif +} + +inline void deb_aligned_free(void *ptr) { +#if defined(_WIN32) + _aligned_free(ptr); +#else + std::free(ptr); +#endif +} +#endif + //// --------------------------------------------------------------------- //// Implementation of Message //// --------------------------------------------------------------------- @@ -72,11 +94,9 @@ PolyUnitT::PolyUnitT(const Preset preset, const Size level, const bool alloc) data_ptr_ = std::shared_ptr(new U[degree_], std::default_delete()); #else - auto *buf = static_cast(::operator new[]( - sizeof(U) * degree_, std::align_val_t(DEB_ALINAS_LEN))); - data_ptr_ = std::shared_ptr(buf, [](U *p) { - ::operator delete[](p, std::align_val_t(DEB_ALINAS_LEN)); - }); + auto *buf = static_cast( + deb_aligned_alloc(DEB_ALINAS_LEN, sizeof(U) * degree_)); + data_ptr_ = std::shared_ptr(buf, [](U *p) { deb_aligned_free(p); }); #endif } @@ -92,11 +112,9 @@ PolyUnitT::PolyUnitT(u64 prime, Size degree, const bool alloc) data_ptr_ = std::shared_ptr(new U[degree_], std::default_delete()); #else - auto *buf = static_cast(::operator new[]( - sizeof(U) * degree_, std::align_val_t(DEB_ALINAS_LEN))); - data_ptr_ = std::shared_ptr(buf, [](U *p) { - ::operator delete[](p, std::align_val_t(DEB_ALINAS_LEN)); - }); + auto *buf = static_cast( + deb_aligned_alloc(DEB_ALINAS_LEN, sizeof(U) * degree_)); + data_ptr_ = std::shared_ptr(buf, [](U *p) { deb_aligned_free(p); }); #endif } @@ -150,8 +168,8 @@ PolynomialT::PolynomialT(const Preset preset, const bool full_level) { std::default_delete()); #else auto *buf = static_cast( - std::aligned_alloc(DEB_ALINAS_LEN, sizeof(U) * num_poly * degree)); - dealloc_ptr_ = std::shared_ptr(buf, [](U *p) { std::free(p); }); + deb_aligned_alloc(DEB_ALINAS_LEN, sizeof(U) * num_poly * degree)); + dealloc_ptr_ = std::shared_ptr(buf, [](U *p) { deb_aligned_free(p); }); #endif for (Size l = 0; l < num_poly; ++l) { polyunits_.emplace_back(preset, l, false); @@ -167,8 +185,8 @@ PolynomialT::PolynomialT(const Preset preset, const Size custom_size) { std::default_delete()); #else auto *buf = static_cast( - std::aligned_alloc(DEB_ALINAS_LEN, sizeof(U) * custom_size * degree)); - dealloc_ptr_ = std::shared_ptr(buf, [](U *p) { std::free(p); }); + deb_aligned_alloc(DEB_ALINAS_LEN, sizeof(U) * custom_size * degree)); + dealloc_ptr_ = std::shared_ptr(buf, [](U *p) { deb_aligned_free(p); }); #endif for (Size l = 0; l < custom_size; ++l) { polyunits_.emplace_back(preset, l, false); @@ -198,11 +216,11 @@ PolynomialT::deepCopy(std::optional num_polyunit) const { new U[num_polyunit_val * polyunits_[0].degree()], std::default_delete()); #else - auto *buf = static_cast(::operator new[]( - sizeof(U) * num_polyunit_val * polyunits_[0].degree(), - std::align_val_t(DEB_ALINAS_LEN))); - copy.dealloc_ptr_ = std::shared_ptr(buf, [buf](U *p) { - ::operator delete[](buf, std::align_val_t(DEB_ALINAS_LEN)); + auto *buf = static_cast(deb_aligned_alloc( + DEB_ALINAS_LEN, + sizeof(U) * num_polyunit_val * polyunits_[0].degree())); + copy.dealloc_ptr_ = std::shared_ptr(buf, [](U *p) { + deb_aligned_free(p); }); #endif for (Size i = 0; i < num_polyunit_val; ++i) { From bc7f9622ac662e9ffe526c04b64c9eef319ebde9 Mon Sep 17 00:00:00 2001 From: Jeonghwan Lee Date: Thu, 16 Apr 2026 13:27:21 +0900 Subject: [PATCH 2/3] style: apply clang-format to CKKSTypes.cpp Co-Authored-By: Claude Opus 4.6 (1M context) --- src/CKKSTypes.cpp | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/src/CKKSTypes.cpp b/src/CKKSTypes.cpp index 7b263a0..d105f7a 100644 --- a/src/CKKSTypes.cpp +++ b/src/CKKSTypes.cpp @@ -216,12 +216,11 @@ PolynomialT::deepCopy(std::optional num_polyunit) const { new U[num_polyunit_val * polyunits_[0].degree()], std::default_delete()); #else - auto *buf = static_cast(deb_aligned_alloc( - DEB_ALINAS_LEN, - sizeof(U) * num_polyunit_val * polyunits_[0].degree())); - copy.dealloc_ptr_ = std::shared_ptr(buf, [](U *p) { - deb_aligned_free(p); - }); + auto *buf = static_cast( + deb_aligned_alloc(DEB_ALINAS_LEN, sizeof(U) * num_polyunit_val * + polyunits_[0].degree())); + copy.dealloc_ptr_ = + std::shared_ptr(buf, [](U *p) { deb_aligned_free(p); }); #endif for (Size i = 0; i < num_polyunit_val; ++i) { copy.polyunits_.emplace_back(polyunits_[i].prime(), From 0fc2582f83f22aac3c89ed375716d335bfdd9b78 Mon Sep 17 00:00:00 2001 From: Jeonghwan Lee Date: Thu, 16 Apr 2026 13:45:33 +0900 Subject: [PATCH 3/3] fix: narrow Windows guard to __MINGW32__ only MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit MSVC already supports std::aligned_alloc — the _aligned_malloc fallback is only needed for MinGW where the C++ stdlib lacks it. Co-Authored-By: Claude Opus 4.6 (1M context) --- src/CKKSTypes.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/CKKSTypes.cpp b/src/CKKSTypes.cpp index d105f7a..b5d0763 100644 --- a/src/CKKSTypes.cpp +++ b/src/CKKSTypes.cpp @@ -17,7 +17,7 @@ #include "CKKSTypes.hpp" #include -#if defined(_WIN32) +#if defined(__MINGW32__) #include #endif @@ -25,7 +25,7 @@ namespace deb { #if DEB_ALINAS_LEN != 0 inline void *deb_aligned_alloc(size_t alignment, size_t size) { -#if defined(_WIN32) +#if defined(__MINGW32__) return _aligned_malloc(size, alignment); #else return std::aligned_alloc(alignment, size); @@ -33,7 +33,7 @@ inline void *deb_aligned_alloc(size_t alignment, size_t size) { } inline void deb_aligned_free(void *ptr) { -#if defined(_WIN32) +#if defined(__MINGW32__) _aligned_free(ptr); #else std::free(ptr);