summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAli Mohammad Pur <ali.mpfard@gmail.com>2021-08-23 13:07:22 +0430
committerAndreas Kling <kling@serenityos.org>2021-08-26 22:00:17 +0200
commit355c2eef57089973f585b510506bbec88c460355 (patch)
tree0e0a7f3e3befef63f5d020da98ec64cb54fd43fa
parenta6fc80f069f4c59bbaaa3b48985847af66eec2aa (diff)
downloadserenity-355c2eef57089973f585b510506bbec88c460355.zip
AK: Make explode_byte depend on sizeof(FlatPtr) instead of ARCH(...)
The assumption that FlatPtr is 64-bit on every platform except i686 is not correct, and also makes the definition of explode_byte() less nice to look at.
-rw-r--r--AK/Types.h22
1 files changed, 8 insertions, 14 deletions
diff --git a/AK/Types.h b/AK/Types.h
index 38086047f2..6f99837508 100644
--- a/AK/Types.h
+++ b/AK/Types.h
@@ -67,22 +67,16 @@ using nullptr_t = decltype(nullptr);
static constexpr FlatPtr explode_byte(u8 b)
{
-#if ARCH(I386)
- return (u32)b << 24 | (u32)b << 16 | (u32)b << 8 | (u32)b;
-#else
- return (u64)b << 56 | (u64)b << 48 | (u64)b << 40 | (u64)b << 32 | (u64)b << 24 | (u64)b << 16 | (u64)b << 8 | (u64)b;
-#endif
+ FlatPtr value = b;
+ if constexpr (sizeof(FlatPtr) == 4)
+ return value << 24 | value << 16 | value << 8 | value;
+ else if (sizeof(FlatPtr) == 8)
+ return value << 56 | value << 48 | value << 40 | value << 32 | value << 24 | value << 16 | value << 8 | value;
}
-#if ARCH(I386)
-static_assert(explode_byte(0xff) == 0xffffffff);
-static_assert(explode_byte(0x80) == 0x80808080);
-static_assert(explode_byte(0x7f) == 0x7f7f7f7f);
-#else
-static_assert(explode_byte(0xff) == 0xffffffffffffffff);
-static_assert(explode_byte(0x80) == 0x8080808080808080);
-static_assert(explode_byte(0x7f) == 0x7f7f7f7f7f7f7f7f);
-#endif
+static_assert(explode_byte(0xff) == (FlatPtr)0xffffffffffffffffull);
+static_assert(explode_byte(0x80) == (FlatPtr)0x8080808080808080ull);
+static_assert(explode_byte(0x7f) == (FlatPtr)0x7f7f7f7f7f7f7f7full);
static_assert(explode_byte(0) == 0);
constexpr size_t align_up_to(const size_t value, const size_t alignment)