1313#include " src/__support/CPP/limits.h" // numeric_limits
1414#include " src/__support/macros/attributes.h" // LIBC_INLINE
1515#include " src/__support/macros/config.h"
16- #include " src/__support/uint128.h" // UInt128
17- #include < stdint.h> // For uint64_t
16+ #include " src/__support/uint128.h" // UInt128
17+ #include < stdint.h> // For uint64_t
1818
1919namespace LIBC_NAMESPACE_DECL {
2020namespace internal {
@@ -34,25 +34,23 @@ LIBC_INLINE uint64_t folded_multiply(uint64_t x, uint64_t y) {
3434// Therefore, we use a union to read the value.
3535template <typename T> LIBC_INLINE T read_little_endian (const void *ptr) {
3636 const uint8_t *bytes = static_cast <const uint8_t *>(ptr);
37- union {
38- T value;
39- uint8_t buffer[sizeof (T)];
40- } data;
37+ uint8_t buffer[sizeof (T)];
4138#if __BYTE_ORDER__ != __ORDER_LITTLE_ENDIAN__
42- // Compiler should able to optimize this as a load followed by a byte swap.
43- // On aarch64 (-mbig-endian), this compiles to the following for int:
39+ // Compiler should able to optimize this as a load followed by a byte
40+ // swap. On aarch64 (-mbig-endian), this compiles to the following for
41+ // int:
4442 // ldr w0, [x0]
4543 // rev w0, w0
4644 // ret
4745 for (size_t i = 0 ; i < sizeof (T); ++i) {
48- data. buffer [i] = bytes[sizeof (T) - i - 1 ];
46+ buffer[i] = bytes[sizeof (T) - i - 1 ];
4947 }
5048#else
5149 for (size_t i = 0 ; i < sizeof (T); ++i) {
52- data. buffer [i] = bytes[i];
50+ buffer[i] = bytes[i];
5351 }
5452#endif
55- return data. value ;
53+ return cpp::bit_cast<T>(buffer) ;
5654}
5755
5856// Specialized read functions for small values. size must be <= 8.
0 commit comments