mapleFU commented on code in PR #185:
URL: https://github.com/apache/iceberg-cpp/pull/185#discussion_r2296087426


##########
src/iceberg/expression/literal.cc:
##########
@@ -19,13 +19,107 @@
 
 #include "iceberg/expression/literal.h"
 
+#include <algorithm>
+#include <bit>
+#include <chrono>
 #include <cmath>
 #include <concepts>
+#include <cstring>
+#include <iomanip>
+#include <sstream>
 
 #include "iceberg/exception.h"
+#include "iceberg/util/macros.h"
 
 namespace iceberg {
 
+namespace {
+/// \brief Write a value in little-endian format to the buffer.
+template <typename T>
+void WriteLittleEndian(std::vector<uint8_t>& buffer, T value) {
+  static_assert(std::is_trivially_copyable_v<T>, "Type must be trivially 
copyable");
+
+  if constexpr (std::endian::native == std::endian::little) {
+    const auto* bytes = reinterpret_cast<const uint8_t*>(&value);
+    buffer.insert(buffer.end(), bytes, bytes + sizeof(T));
+  } else {
+    if constexpr (sizeof(T) > 1) {
+      T le_value = std::byteswap(value);
+      const auto* bytes = reinterpret_cast<const uint8_t*>(&le_value);
+      buffer.insert(buffer.end(), bytes, bytes + sizeof(T));
+    } else {
+      // For single byte types, no byteswap needed
+      buffer.push_back(static_cast<uint8_t>(value));
+    }
+  }
+}
+
+/// \brief Read a value in little-endian format from the data.
+template <typename T>
+Result<T> ReadLittleEndian(std::span<const uint8_t> data) {
+  static_assert(std::is_trivially_copyable_v<T>, "Type must be trivially 
copyable");
+
+  if (data.size() < sizeof(T)) {
+    return InvalidArgument("Insufficient data to read {} bytes, got {}", 
sizeof(T),
+                           data.size());
+  }
+
+  T value;
+  std::memcpy(&value, data.data(), sizeof(T));
+
+  if constexpr (std::endian::native != std::endian::little && sizeof(T) > 1) {
+    value = std::byteswap(value);
+  }
+  return value;
+}
+
+/// \brief Write a 16-byte value in big-endian format (for UUID and Decimal).
+void WriteBigEndian16(std::vector<uint8_t>& buffer,

Review Comment:
   In C, the abi can pass 16bit argument in registers under linux platform, so 
generally small value would be better when passing by value



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to