/src/serenity/AK/Endian.h
Line | Count | Source |
1 | | /* |
2 | | * Copyright (c) 2020, the SerenityOS developers. |
3 | | * |
4 | | * SPDX-License-Identifier: BSD-2-Clause |
5 | | */ |
6 | | |
7 | | #pragma once |
8 | | |
9 | | #include <AK/Format.h> |
10 | | #include <AK/Forward.h> |
11 | | #include <AK/Platform.h> |
12 | | |
13 | | namespace AK { |
14 | | #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ |
15 | | inline constexpr static bool HostIsLittleEndian = true; |
16 | | #else |
17 | | inline constexpr static bool HostIsLittleEndian = false; |
18 | | #endif |
19 | | |
20 | | template<typename T> |
21 | | ALWAYS_INLINE constexpr T convert_between_host_and_little_endian(T value) |
22 | 550M | { |
23 | | if constexpr (HostIsLittleEndian || sizeof(T) == 1) |
24 | 550M | return value; |
25 | | else if constexpr (sizeof(T) == 8) |
26 | | return static_cast<T>(__builtin_bswap64(static_cast<u64>(value))); |
27 | | else if constexpr (sizeof(T) == 4) |
28 | | return static_cast<T>(__builtin_bswap32(static_cast<u32>(value))); |
29 | | else if constexpr (sizeof(T) == 2) |
30 | | return static_cast<T>(__builtin_bswap16(static_cast<u16>(value))); |
31 | | else |
32 | | static_assert(DependentFalse<T>, "Cannot byte-swap values larger than 64-bits"); |
33 | 550M | } unsigned char AK::convert_between_host_and_little_endian<unsigned char>(unsigned char) Line | Count | Source | 22 | 13.4M | { | 23 | | if constexpr (HostIsLittleEndian || sizeof(T) == 1) | 24 | 13.4M | return value; | 25 | | else if constexpr (sizeof(T) == 8) | 26 | | return static_cast<T>(__builtin_bswap64(static_cast<u64>(value))); | 27 | | else if constexpr (sizeof(T) == 4) | 28 | | return static_cast<T>(__builtin_bswap32(static_cast<u32>(value))); | 29 | | else if constexpr (sizeof(T) == 2) | 30 | | return static_cast<T>(__builtin_bswap16(static_cast<u16>(value))); | 31 | | else | 32 | | static_assert(DependentFalse<T>, "Cannot byte-swap values larger than 64-bits"); | 33 | 13.4M | } |
unsigned short AK::convert_between_host_and_little_endian<unsigned short>(unsigned short) Line | Count | Source | 22 | 343M | { | 23 | | if constexpr (HostIsLittleEndian || sizeof(T) == 1) | 24 | 343M | return value; | 25 | | else if constexpr (sizeof(T) == 8) | 26 | | return static_cast<T>(__builtin_bswap64(static_cast<u64>(value))); | 27 | | else if constexpr (sizeof(T) == 4) | 28 | | return static_cast<T>(__builtin_bswap32(static_cast<u32>(value))); | 29 | | else if constexpr (sizeof(T) == 2) | 30 | | return static_cast<T>(__builtin_bswap16(static_cast<u16>(value))); | 31 | | else | 32 | | static_assert(DependentFalse<T>, "Cannot byte-swap values larger than 64-bits"); | 33 | 343M | } |
unsigned int AK::convert_between_host_and_little_endian<unsigned int>(unsigned int) Line | Count | Source | 22 | 149M | { | 23 | | if constexpr (HostIsLittleEndian || sizeof(T) == 1) | 24 | 149M | return value; | 25 | | else if constexpr (sizeof(T) == 8) | 26 | | return static_cast<T>(__builtin_bswap64(static_cast<u64>(value))); | 27 | | else if constexpr (sizeof(T) == 4) | 28 | | return static_cast<T>(__builtin_bswap32(static_cast<u32>(value))); | 29 | | else if constexpr (sizeof(T) == 2) | 30 | | return static_cast<T>(__builtin_bswap16(static_cast<u16>(value))); | 31 | | else | 32 | | static_assert(DependentFalse<T>, "Cannot byte-swap values larger than 64-bits"); | 33 | 149M | } |
int AK::convert_between_host_and_little_endian<int>(int) Line | Count | Source | 22 | 29.1M | { | 23 | | if constexpr (HostIsLittleEndian || sizeof(T) == 1) | 24 | 29.1M | return value; | 25 | | else if constexpr (sizeof(T) == 8) | 26 | | return static_cast<T>(__builtin_bswap64(static_cast<u64>(value))); | 27 | | else if constexpr (sizeof(T) == 4) | 28 | | return static_cast<T>(__builtin_bswap32(static_cast<u32>(value))); | 29 | | else if constexpr (sizeof(T) == 2) | 30 | | return static_cast<T>(__builtin_bswap16(static_cast<u16>(value))); | 31 | | else | 32 | | static_assert(DependentFalse<T>, "Cannot byte-swap values larger than 64-bits"); | 33 | 29.1M | } |
float AK::convert_between_host_and_little_endian<float>(float) Line | Count | Source | 22 | 8.57M | { | 23 | | if constexpr (HostIsLittleEndian || sizeof(T) == 1) | 24 | 8.57M | return value; | 25 | | else if constexpr (sizeof(T) == 8) | 26 | | return static_cast<T>(__builtin_bswap64(static_cast<u64>(value))); | 27 | | else if constexpr (sizeof(T) == 4) | 28 | | return static_cast<T>(__builtin_bswap32(static_cast<u32>(value))); | 29 | | else if constexpr (sizeof(T) == 2) | 30 | | return static_cast<T>(__builtin_bswap16(static_cast<u16>(value))); | 31 | | else | 32 | | static_assert(DependentFalse<T>, "Cannot byte-swap values larger than 64-bits"); | 33 | 8.57M | } |
double AK::convert_between_host_and_little_endian<double>(double) Line | Count | Source | 22 | 5.74M | { | 23 | | if constexpr (HostIsLittleEndian || sizeof(T) == 1) | 24 | 5.74M | return value; | 25 | | else if constexpr (sizeof(T) == 8) | 26 | | return static_cast<T>(__builtin_bswap64(static_cast<u64>(value))); | 27 | | else if constexpr (sizeof(T) == 4) | 28 | | return static_cast<T>(__builtin_bswap32(static_cast<u32>(value))); | 29 | | else if constexpr (sizeof(T) == 2) | 30 | | return static_cast<T>(__builtin_bswap16(static_cast<u16>(value))); | 31 | | else | 32 | | static_assert(DependentFalse<T>, "Cannot byte-swap values larger than 64-bits"); | 33 | 5.74M | } |
short AK::convert_between_host_and_little_endian<short>(short) Line | Count | Source | 22 | 423k | { | 23 | | if constexpr (HostIsLittleEndian || sizeof(T) == 1) | 24 | 423k | return value; | 25 | | else if constexpr (sizeof(T) == 8) | 26 | | return static_cast<T>(__builtin_bswap64(static_cast<u64>(value))); | 27 | | else if constexpr (sizeof(T) == 4) | 28 | | return static_cast<T>(__builtin_bswap32(static_cast<u32>(value))); | 29 | | else if constexpr (sizeof(T) == 2) | 30 | | return static_cast<T>(__builtin_bswap16(static_cast<u16>(value))); | 31 | | else | 32 | | static_assert(DependentFalse<T>, "Cannot byte-swap values larger than 64-bits"); | 33 | 423k | } |
unsigned long AK::convert_between_host_and_little_endian<unsigned long>(unsigned long) Line | Count | Source | 22 | 2.27k | { | 23 | | if constexpr (HostIsLittleEndian || sizeof(T) == 1) | 24 | 2.27k | return value; | 25 | | else if constexpr (sizeof(T) == 8) | 26 | | return static_cast<T>(__builtin_bswap64(static_cast<u64>(value))); | 27 | | else if constexpr (sizeof(T) == 4) | 28 | | return static_cast<T>(__builtin_bswap32(static_cast<u32>(value))); | 29 | | else if constexpr (sizeof(T) == 2) | 30 | | return static_cast<T>(__builtin_bswap16(static_cast<u16>(value))); | 31 | | else | 32 | | static_assert(DependentFalse<T>, "Cannot byte-swap values larger than 64-bits"); | 33 | 2.27k | } |
Unexecuted instantiation: long AK::convert_between_host_and_little_endian<long>(long) signed char AK::convert_between_host_and_little_endian<signed char>(signed char) Line | Count | Source | 22 | 24.0k | { | 23 | | if constexpr (HostIsLittleEndian || sizeof(T) == 1) | 24 | 24.0k | return value; | 25 | | else if constexpr (sizeof(T) == 8) | 26 | | return static_cast<T>(__builtin_bswap64(static_cast<u64>(value))); | 27 | | else if constexpr (sizeof(T) == 4) | 28 | | return static_cast<T>(__builtin_bswap32(static_cast<u32>(value))); | 29 | | else if constexpr (sizeof(T) == 2) | 30 | | return static_cast<T>(__builtin_bswap16(static_cast<u16>(value))); | 31 | | else | 32 | | static_assert(DependentFalse<T>, "Cannot byte-swap values larger than 64-bits"); | 33 | 24.0k | } |
|
34 | | |
35 | | template<typename T> |
36 | | ALWAYS_INLINE constexpr T convert_between_host_and_big_endian(T value) |
37 | 2.82G | { |
38 | | if constexpr (sizeof(T) == 1 || !HostIsLittleEndian) |
39 | 12.7M | return value; |
40 | | else if constexpr (sizeof(T) == 8) |
41 | 3.01M | return static_cast<T>(__builtin_bswap64(static_cast<u64>(value))); |
42 | | else if constexpr (sizeof(T) == 4) |
43 | 25.3M | return static_cast<T>(__builtin_bswap32(static_cast<u32>(value))); |
44 | | else if constexpr (sizeof(T) == 2) |
45 | 2.78G | return static_cast<T>(__builtin_bswap16(static_cast<u16>(value))); |
46 | | else |
47 | | static_assert(DependentFalse<T>, "Cannot byte-swap values larger than 64-bits"); |
48 | 2.82G | } unsigned int AK::convert_between_host_and_big_endian<unsigned int>(unsigned int) Line | Count | Source | 37 | 18.0M | { | 38 | | if constexpr (sizeof(T) == 1 || !HostIsLittleEndian) | 39 | | return value; | 40 | | else if constexpr (sizeof(T) == 8) | 41 | | return static_cast<T>(__builtin_bswap64(static_cast<u64>(value))); | 42 | | else if constexpr (sizeof(T) == 4) | 43 | 18.0M | return static_cast<T>(__builtin_bswap32(static_cast<u32>(value))); | 44 | | else if constexpr (sizeof(T) == 2) | 45 | | return static_cast<T>(__builtin_bswap16(static_cast<u16>(value))); | 46 | | else | 47 | | static_assert(DependentFalse<T>, "Cannot byte-swap values larger than 64-bits"); | 48 | 18.0M | } |
unsigned short AK::convert_between_host_and_big_endian<unsigned short>(unsigned short) Line | Count | Source | 37 | 2.77G | { | 38 | | if constexpr (sizeof(T) == 1 || !HostIsLittleEndian) | 39 | | return value; | 40 | | else if constexpr (sizeof(T) == 8) | 41 | | return static_cast<T>(__builtin_bswap64(static_cast<u64>(value))); | 42 | | else if constexpr (sizeof(T) == 4) | 43 | | return static_cast<T>(__builtin_bswap32(static_cast<u32>(value))); | 44 | | else if constexpr (sizeof(T) == 2) | 45 | 2.77G | return static_cast<T>(__builtin_bswap16(static_cast<u16>(value))); | 46 | | else | 47 | | static_assert(DependentFalse<T>, "Cannot byte-swap values larger than 64-bits"); | 48 | 2.77G | } |
int AK::convert_between_host_and_big_endian<int>(int) Line | Count | Source | 37 | 5.00M | { | 38 | | if constexpr (sizeof(T) == 1 || !HostIsLittleEndian) | 39 | | return value; | 40 | | else if constexpr (sizeof(T) == 8) | 41 | | return static_cast<T>(__builtin_bswap64(static_cast<u64>(value))); | 42 | | else if constexpr (sizeof(T) == 4) | 43 | 5.00M | return static_cast<T>(__builtin_bswap32(static_cast<u32>(value))); | 44 | | else if constexpr (sizeof(T) == 2) | 45 | | return static_cast<T>(__builtin_bswap16(static_cast<u16>(value))); | 46 | | else | 47 | | static_assert(DependentFalse<T>, "Cannot byte-swap values larger than 64-bits"); | 48 | 5.00M | } |
unsigned char AK::convert_between_host_and_big_endian<unsigned char>(unsigned char) Line | Count | Source | 37 | 12.7M | { | 38 | | if constexpr (sizeof(T) == 1 || !HostIsLittleEndian) | 39 | 12.7M | return value; | 40 | | else if constexpr (sizeof(T) == 8) | 41 | | return static_cast<T>(__builtin_bswap64(static_cast<u64>(value))); | 42 | | else if constexpr (sizeof(T) == 4) | 43 | | return static_cast<T>(__builtin_bswap32(static_cast<u32>(value))); | 44 | | else if constexpr (sizeof(T) == 2) | 45 | | return static_cast<T>(__builtin_bswap16(static_cast<u16>(value))); | 46 | | else | 47 | | static_assert(DependentFalse<T>, "Cannot byte-swap values larger than 64-bits"); | 48 | 12.7M | } |
Gfx::ICC::DistinctFourCC<(Gfx::ICC::FourCCType)0> AK::convert_between_host_and_big_endian<Gfx::ICC::DistinctFourCC<(Gfx::ICC::FourCCType)0> >(Gfx::ICC::DistinctFourCC<(Gfx::ICC::FourCCType)0>) Line | Count | Source | 37 | 16.5k | { | 38 | | if constexpr (sizeof(T) == 1 || !HostIsLittleEndian) | 39 | | return value; | 40 | | else if constexpr (sizeof(T) == 8) | 41 | | return static_cast<T>(__builtin_bswap64(static_cast<u64>(value))); | 42 | | else if constexpr (sizeof(T) == 4) | 43 | 16.5k | return static_cast<T>(__builtin_bswap32(static_cast<u32>(value))); | 44 | | else if constexpr (sizeof(T) == 2) | 45 | | return static_cast<T>(__builtin_bswap16(static_cast<u16>(value))); | 46 | | else | 47 | | static_assert(DependentFalse<T>, "Cannot byte-swap values larger than 64-bits"); | 48 | 16.5k | } |
Gfx::ICC::DeviceClass AK::convert_between_host_and_big_endian<Gfx::ICC::DeviceClass>(Gfx::ICC::DeviceClass) Line | Count | Source | 37 | 24.2k | { | 38 | | if constexpr (sizeof(T) == 1 || !HostIsLittleEndian) | 39 | | return value; | 40 | | else if constexpr (sizeof(T) == 8) | 41 | | return static_cast<T>(__builtin_bswap64(static_cast<u64>(value))); | 42 | | else if constexpr (sizeof(T) == 4) | 43 | 24.2k | return static_cast<T>(__builtin_bswap32(static_cast<u32>(value))); | 44 | | else if constexpr (sizeof(T) == 2) | 45 | | return static_cast<T>(__builtin_bswap16(static_cast<u16>(value))); | 46 | | else | 47 | | static_assert(DependentFalse<T>, "Cannot byte-swap values larger than 64-bits"); | 48 | 24.2k | } |
Gfx::ICC::ColorSpace AK::convert_between_host_and_big_endian<Gfx::ICC::ColorSpace>(Gfx::ICC::ColorSpace) Line | Count | Source | 37 | 15.9k | { | 38 | | if constexpr (sizeof(T) == 1 || !HostIsLittleEndian) | 39 | | return value; | 40 | | else if constexpr (sizeof(T) == 8) | 41 | | return static_cast<T>(__builtin_bswap64(static_cast<u64>(value))); | 42 | | else if constexpr (sizeof(T) == 4) | 43 | 15.9k | return static_cast<T>(__builtin_bswap32(static_cast<u32>(value))); | 44 | | else if constexpr (sizeof(T) == 2) | 45 | | return static_cast<T>(__builtin_bswap16(static_cast<u16>(value))); | 46 | | else | 47 | | static_assert(DependentFalse<T>, "Cannot byte-swap values larger than 64-bits"); | 48 | 15.9k | } |
Gfx::ICC::PrimaryPlatform AK::convert_between_host_and_big_endian<Gfx::ICC::PrimaryPlatform>(Gfx::ICC::PrimaryPlatform) Line | Count | Source | 37 | 17.4k | { | 38 | | if constexpr (sizeof(T) == 1 || !HostIsLittleEndian) | 39 | | return value; | 40 | | else if constexpr (sizeof(T) == 8) | 41 | | return static_cast<T>(__builtin_bswap64(static_cast<u64>(value))); | 42 | | else if constexpr (sizeof(T) == 4) | 43 | 17.4k | return static_cast<T>(__builtin_bswap32(static_cast<u32>(value))); | 44 | | else if constexpr (sizeof(T) == 2) | 45 | | return static_cast<T>(__builtin_bswap16(static_cast<u16>(value))); | 46 | | else | 47 | | static_assert(DependentFalse<T>, "Cannot byte-swap values larger than 64-bits"); | 48 | 17.4k | } |
Gfx::ICC::DistinctFourCC<(Gfx::ICC::FourCCType)1> AK::convert_between_host_and_big_endian<Gfx::ICC::DistinctFourCC<(Gfx::ICC::FourCCType)1> >(Gfx::ICC::DistinctFourCC<(Gfx::ICC::FourCCType)1>) Line | Count | Source | 37 | 12.8k | { | 38 | | if constexpr (sizeof(T) == 1 || !HostIsLittleEndian) | 39 | | return value; | 40 | | else if constexpr (sizeof(T) == 8) | 41 | | return static_cast<T>(__builtin_bswap64(static_cast<u64>(value))); | 42 | | else if constexpr (sizeof(T) == 4) | 43 | 12.8k | return static_cast<T>(__builtin_bswap32(static_cast<u32>(value))); | 44 | | else if constexpr (sizeof(T) == 2) | 45 | | return static_cast<T>(__builtin_bswap16(static_cast<u16>(value))); | 46 | | else | 47 | | static_assert(DependentFalse<T>, "Cannot byte-swap values larger than 64-bits"); | 48 | 12.8k | } |
Gfx::ICC::DistinctFourCC<(Gfx::ICC::FourCCType)2> AK::convert_between_host_and_big_endian<Gfx::ICC::DistinctFourCC<(Gfx::ICC::FourCCType)2> >(Gfx::ICC::DistinctFourCC<(Gfx::ICC::FourCCType)2>) Line | Count | Source | 37 | 12.0k | { | 38 | | if constexpr (sizeof(T) == 1 || !HostIsLittleEndian) | 39 | | return value; | 40 | | else if constexpr (sizeof(T) == 8) | 41 | | return static_cast<T>(__builtin_bswap64(static_cast<u64>(value))); | 42 | | else if constexpr (sizeof(T) == 4) | 43 | 12.0k | return static_cast<T>(__builtin_bswap32(static_cast<u32>(value))); | 44 | | else if constexpr (sizeof(T) == 2) | 45 | | return static_cast<T>(__builtin_bswap16(static_cast<u16>(value))); | 46 | | else | 47 | | static_assert(DependentFalse<T>, "Cannot byte-swap values larger than 64-bits"); | 48 | 12.0k | } |
unsigned long AK::convert_between_host_and_big_endian<unsigned long>(unsigned long) Line | Count | Source | 37 | 2.64M | { | 38 | | if constexpr (sizeof(T) == 1 || !HostIsLittleEndian) | 39 | | return value; | 40 | | else if constexpr (sizeof(T) == 8) | 41 | 2.64M | return static_cast<T>(__builtin_bswap64(static_cast<u64>(value))); | 42 | | else if constexpr (sizeof(T) == 4) | 43 | | return static_cast<T>(__builtin_bswap32(static_cast<u32>(value))); | 44 | | else if constexpr (sizeof(T) == 2) | 45 | | return static_cast<T>(__builtin_bswap16(static_cast<u16>(value))); | 46 | | else | 47 | | static_assert(DependentFalse<T>, "Cannot byte-swap values larger than 64-bits"); | 48 | 2.64M | } |
Gfx::ICC::RenderingIntent AK::convert_between_host_and_big_endian<Gfx::ICC::RenderingIntent>(Gfx::ICC::RenderingIntent) Line | Count | Source | 37 | 13.9k | { | 38 | | if constexpr (sizeof(T) == 1 || !HostIsLittleEndian) | 39 | | return value; | 40 | | else if constexpr (sizeof(T) == 8) | 41 | | return static_cast<T>(__builtin_bswap64(static_cast<u64>(value))); | 42 | | else if constexpr (sizeof(T) == 4) | 43 | 13.9k | return static_cast<T>(__builtin_bswap32(static_cast<u32>(value))); | 44 | | else if constexpr (sizeof(T) == 2) | 45 | | return static_cast<T>(__builtin_bswap16(static_cast<u16>(value))); | 46 | | else | 47 | | static_assert(DependentFalse<T>, "Cannot byte-swap values larger than 64-bits"); | 48 | 13.9k | } |
Gfx::ICC::DistinctFourCC<(Gfx::ICC::FourCCType)3> AK::convert_between_host_and_big_endian<Gfx::ICC::DistinctFourCC<(Gfx::ICC::FourCCType)3> >(Gfx::ICC::DistinctFourCC<(Gfx::ICC::FourCCType)3>) Line | Count | Source | 37 | 12.8k | { | 38 | | if constexpr (sizeof(T) == 1 || !HostIsLittleEndian) | 39 | | return value; | 40 | | else if constexpr (sizeof(T) == 8) | 41 | | return static_cast<T>(__builtin_bswap64(static_cast<u64>(value))); | 42 | | else if constexpr (sizeof(T) == 4) | 43 | 12.8k | return static_cast<T>(__builtin_bswap32(static_cast<u32>(value))); | 44 | | else if constexpr (sizeof(T) == 2) | 45 | | return static_cast<T>(__builtin_bswap16(static_cast<u16>(value))); | 46 | | else | 47 | | static_assert(DependentFalse<T>, "Cannot byte-swap values larger than 64-bits"); | 48 | 12.8k | } |
Gfx::ICC::DistinctFourCC<(Gfx::ICC::FourCCType)4> AK::convert_between_host_and_big_endian<Gfx::ICC::DistinctFourCC<(Gfx::ICC::FourCCType)4> >(Gfx::ICC::DistinctFourCC<(Gfx::ICC::FourCCType)4>) Line | Count | Source | 37 | 28.0k | { | 38 | | if constexpr (sizeof(T) == 1 || !HostIsLittleEndian) | 39 | | return value; | 40 | | else if constexpr (sizeof(T) == 8) | 41 | | return static_cast<T>(__builtin_bswap64(static_cast<u64>(value))); | 42 | | else if constexpr (sizeof(T) == 4) | 43 | 28.0k | return static_cast<T>(__builtin_bswap32(static_cast<u32>(value))); | 44 | | else if constexpr (sizeof(T) == 2) | 45 | | return static_cast<T>(__builtin_bswap16(static_cast<u16>(value))); | 46 | | else | 47 | | static_assert(DependentFalse<T>, "Cannot byte-swap values larger than 64-bits"); | 48 | 28.0k | } |
Gfx::ICC::DistinctFourCC<(Gfx::ICC::FourCCType)5> AK::convert_between_host_and_big_endian<Gfx::ICC::DistinctFourCC<(Gfx::ICC::FourCCType)5> >(Gfx::ICC::DistinctFourCC<(Gfx::ICC::FourCCType)5>) Line | Count | Source | 37 | 37.4k | { | 38 | | if constexpr (sizeof(T) == 1 || !HostIsLittleEndian) | 39 | | return value; | 40 | | else if constexpr (sizeof(T) == 8) | 41 | | return static_cast<T>(__builtin_bswap64(static_cast<u64>(value))); | 42 | | else if constexpr (sizeof(T) == 4) | 43 | 37.4k | return static_cast<T>(__builtin_bswap32(static_cast<u32>(value))); | 44 | | else if constexpr (sizeof(T) == 2) | 45 | | return static_cast<T>(__builtin_bswap16(static_cast<u16>(value))); | 46 | | else | 47 | | static_assert(DependentFalse<T>, "Cannot byte-swap values larger than 64-bits"); | 48 | 37.4k | } |
Gfx::ICC::ChromaticityTagData::PhosphorOrColorantType AK::convert_between_host_and_big_endian<Gfx::ICC::ChromaticityTagData::PhosphorOrColorantType>(Gfx::ICC::ChromaticityTagData::PhosphorOrColorantType) Line | Count | Source | 37 | 152 | { | 38 | | if constexpr (sizeof(T) == 1 || !HostIsLittleEndian) | 39 | | return value; | 40 | | else if constexpr (sizeof(T) == 8) | 41 | | return static_cast<T>(__builtin_bswap64(static_cast<u64>(value))); | 42 | | else if constexpr (sizeof(T) == 4) | 43 | | return static_cast<T>(__builtin_bswap32(static_cast<u32>(value))); | 44 | | else if constexpr (sizeof(T) == 2) | 45 | 152 | return static_cast<T>(__builtin_bswap16(static_cast<u16>(value))); | 46 | | else | 47 | | static_assert(DependentFalse<T>, "Cannot byte-swap values larger than 64-bits"); | 48 | 152 | } |
Gfx::ICC::MeasurementTagData::StandardObserver AK::convert_between_host_and_big_endian<Gfx::ICC::MeasurementTagData::StandardObserver>(Gfx::ICC::MeasurementTagData::StandardObserver) Line | Count | Source | 37 | 233 | { | 38 | | if constexpr (sizeof(T) == 1 || !HostIsLittleEndian) | 39 | | return value; | 40 | | else if constexpr (sizeof(T) == 8) | 41 | | return static_cast<T>(__builtin_bswap64(static_cast<u64>(value))); | 42 | | else if constexpr (sizeof(T) == 4) | 43 | 233 | return static_cast<T>(__builtin_bswap32(static_cast<u32>(value))); | 44 | | else if constexpr (sizeof(T) == 2) | 45 | | return static_cast<T>(__builtin_bswap16(static_cast<u16>(value))); | 46 | | else | 47 | | static_assert(DependentFalse<T>, "Cannot byte-swap values larger than 64-bits"); | 48 | 233 | } |
Gfx::ICC::MeasurementTagData::MeasurementGeometry AK::convert_between_host_and_big_endian<Gfx::ICC::MeasurementTagData::MeasurementGeometry>(Gfx::ICC::MeasurementTagData::MeasurementGeometry) Line | Count | Source | 37 | 197 | { | 38 | | if constexpr (sizeof(T) == 1 || !HostIsLittleEndian) | 39 | | return value; | 40 | | else if constexpr (sizeof(T) == 8) | 41 | | return static_cast<T>(__builtin_bswap64(static_cast<u64>(value))); | 42 | | else if constexpr (sizeof(T) == 4) | 43 | 197 | return static_cast<T>(__builtin_bswap32(static_cast<u32>(value))); | 44 | | else if constexpr (sizeof(T) == 2) | 45 | | return static_cast<T>(__builtin_bswap16(static_cast<u16>(value))); | 46 | | else | 47 | | static_assert(DependentFalse<T>, "Cannot byte-swap values larger than 64-bits"); | 48 | 197 | } |
Gfx::ICC::MeasurementTagData::StandardIlluminant AK::convert_between_host_and_big_endian<Gfx::ICC::MeasurementTagData::StandardIlluminant>(Gfx::ICC::MeasurementTagData::StandardIlluminant) Line | Count | Source | 37 | 328 | { | 38 | | if constexpr (sizeof(T) == 1 || !HostIsLittleEndian) | 39 | | return value; | 40 | | else if constexpr (sizeof(T) == 8) | 41 | | return static_cast<T>(__builtin_bswap64(static_cast<u64>(value))); | 42 | | else if constexpr (sizeof(T) == 4) | 43 | 328 | return static_cast<T>(__builtin_bswap32(static_cast<u32>(value))); | 44 | | else if constexpr (sizeof(T) == 2) | 45 | | return static_cast<T>(__builtin_bswap16(static_cast<u16>(value))); | 46 | | else | 47 | | static_assert(DependentFalse<T>, "Cannot byte-swap values larger than 64-bits"); | 48 | 328 | } |
Gfx::ISOBMFF::BoxType AK::convert_between_host_and_big_endian<Gfx::ISOBMFF::BoxType>(Gfx::ISOBMFF::BoxType) Line | Count | Source | 37 | 40.9k | { | 38 | | if constexpr (sizeof(T) == 1 || !HostIsLittleEndian) | 39 | | return value; | 40 | | else if constexpr (sizeof(T) == 8) | 41 | | return static_cast<T>(__builtin_bswap64(static_cast<u64>(value))); | 42 | | else if constexpr (sizeof(T) == 4) | 43 | 40.9k | return static_cast<T>(__builtin_bswap32(static_cast<u32>(value))); | 44 | | else if constexpr (sizeof(T) == 2) | 45 | | return static_cast<T>(__builtin_bswap16(static_cast<u16>(value))); | 46 | | else | 47 | | static_assert(DependentFalse<T>, "Cannot byte-swap values larger than 64-bits"); | 48 | 40.9k | } |
Gfx::ISOBMFF::BrandIdentifier AK::convert_between_host_and_big_endian<Gfx::ISOBMFF::BrandIdentifier>(Gfx::ISOBMFF::BrandIdentifier) Line | Count | Source | 37 | 925k | { | 38 | | if constexpr (sizeof(T) == 1 || !HostIsLittleEndian) | 39 | | return value; | 40 | | else if constexpr (sizeof(T) == 8) | 41 | | return static_cast<T>(__builtin_bswap64(static_cast<u64>(value))); | 42 | | else if constexpr (sizeof(T) == 4) | 43 | 925k | return static_cast<T>(__builtin_bswap32(static_cast<u32>(value))); | 44 | | else if constexpr (sizeof(T) == 2) | 45 | | return static_cast<T>(__builtin_bswap16(static_cast<u16>(value))); | 46 | | else | 47 | | static_assert(DependentFalse<T>, "Cannot byte-swap values larger than 64-bits"); | 48 | 925k | } |
float AK::convert_between_host_and_big_endian<float>(float) Line | Count | Source | 37 | 1.10M | { | 38 | | if constexpr (sizeof(T) == 1 || !HostIsLittleEndian) | 39 | | return value; | 40 | | else if constexpr (sizeof(T) == 8) | 41 | | return static_cast<T>(__builtin_bswap64(static_cast<u64>(value))); | 42 | | else if constexpr (sizeof(T) == 4) | 43 | 1.10M | return static_cast<T>(__builtin_bswap32(static_cast<u32>(value))); | 44 | | else if constexpr (sizeof(T) == 2) | 45 | | return static_cast<T>(__builtin_bswap16(static_cast<u16>(value))); | 46 | | else | 47 | | static_assert(DependentFalse<T>, "Cannot byte-swap values larger than 64-bits"); | 48 | 1.10M | } |
double AK::convert_between_host_and_big_endian<double>(double) Line | Count | Source | 37 | 365k | { | 38 | | if constexpr (sizeof(T) == 1 || !HostIsLittleEndian) | 39 | | return value; | 40 | | else if constexpr (sizeof(T) == 8) | 41 | 365k | return static_cast<T>(__builtin_bswap64(static_cast<u64>(value))); | 42 | | else if constexpr (sizeof(T) == 4) | 43 | | return static_cast<T>(__builtin_bswap32(static_cast<u32>(value))); | 44 | | else if constexpr (sizeof(T) == 2) | 45 | | return static_cast<T>(__builtin_bswap16(static_cast<u16>(value))); | 46 | | else | 47 | | static_assert(DependentFalse<T>, "Cannot byte-swap values larger than 64-bits"); | 48 | 365k | } |
short AK::convert_between_host_and_big_endian<short>(short) Line | Count | Source | 37 | 1.57M | { | 38 | | if constexpr (sizeof(T) == 1 || !HostIsLittleEndian) | 39 | | return value; | 40 | | else if constexpr (sizeof(T) == 8) | 41 | | return static_cast<T>(__builtin_bswap64(static_cast<u64>(value))); | 42 | | else if constexpr (sizeof(T) == 4) | 43 | | return static_cast<T>(__builtin_bswap32(static_cast<u32>(value))); | 44 | | else if constexpr (sizeof(T) == 2) | 45 | 1.57M | return static_cast<T>(__builtin_bswap16(static_cast<u16>(value))); | 46 | | else | 47 | | static_assert(DependentFalse<T>, "Cannot byte-swap values larger than 64-bits"); | 48 | 1.57M | } |
|
49 | | |
50 | | template<typename T> |
51 | | ALWAYS_INLINE T convert_between_host_and_network_endian(T value) |
52 | 0 | { |
53 | 0 | return convert_between_host_and_big_endian(value); |
54 | 0 | } |
55 | | |
56 | | template<typename T> |
57 | | class [[gnu::packed]] LittleEndian { |
58 | | public: |
59 | 2.27k | constexpr LittleEndian() = default; |
60 | | |
61 | | constexpr LittleEndian(T value) |
62 | 15.9k | : m_value(convert_between_host_and_little_endian(value)) |
63 | 15.9k | { |
64 | 15.9k | } AK::LittleEndian<unsigned short>::LittleEndian(unsigned short) Line | Count | Source | 62 | 9.14k | : m_value(convert_between_host_and_little_endian(value)) | 63 | 9.14k | { | 64 | 9.14k | } |
AK::LittleEndian<unsigned int>::LittleEndian(unsigned int) Line | Count | Source | 62 | 6.83k | : m_value(convert_between_host_and_little_endian(value)) | 63 | 6.83k | { | 64 | 6.83k | } |
Unexecuted instantiation: AK::LittleEndian<int>::LittleEndian(int) Unexecuted instantiation: AK::LittleEndian<long>::LittleEndian(long) Unexecuted instantiation: AK::LittleEndian<signed char>::LittleEndian(signed char) Unexecuted instantiation: AK::LittleEndian<short>::LittleEndian(short) |
65 | | |
66 | 543M | constexpr operator T() const { return convert_between_host_and_little_endian(m_value); }AK::LittleEndian<unsigned char>::operator unsigned char() const Line | Count | Source | 66 | 10.6M | constexpr operator T() const { return convert_between_host_and_little_endian(m_value); } |
AK::LittleEndian<unsigned short>::operator unsigned short() const Line | Count | Source | 66 | 343M | constexpr operator T() const { return convert_between_host_and_little_endian(m_value); } |
AK::LittleEndian<unsigned int>::operator unsigned int() const Line | Count | Source | 66 | 147M | constexpr operator T() const { return convert_between_host_and_little_endian(m_value); } |
AK::LittleEndian<int>::operator int() const Line | Count | Source | 66 | 29.1M | constexpr operator T() const { return convert_between_host_and_little_endian(m_value); } |
AK::LittleEndian<float>::operator float() const Line | Count | Source | 66 | 8.31M | constexpr operator T() const { return convert_between_host_and_little_endian(m_value); } |
AK::LittleEndian<double>::operator double() const Line | Count | Source | 66 | 5.45M | constexpr operator T() const { return convert_between_host_and_little_endian(m_value); } |
AK::LittleEndian<short>::operator short() const Line | Count | Source | 66 | 170k | constexpr operator T() const { return convert_between_host_and_little_endian(m_value); } |
AK::LittleEndian<unsigned long>::operator unsigned long() const Line | Count | Source | 66 | 2.27k | constexpr operator T() const { return convert_between_host_and_little_endian(m_value); } |
Unexecuted instantiation: AK::LittleEndian<long>::operator long() const AK::LittleEndian<signed char>::operator signed char() const Line | Count | Source | 66 | 24.0k | constexpr operator T() const { return convert_between_host_and_little_endian(m_value); } |
|
67 | | |
68 | | constexpr ReadonlyBytes bytes() const { return { this, sizeof(*this) }; } |
69 | | |
70 | | private: |
71 | | T m_value { 0 }; |
72 | | }; |
73 | | |
74 | | template<typename T> |
75 | | class [[gnu::packed]] BigEndian { |
76 | | public: |
77 | 1.76M | constexpr BigEndian() = default; AK::BigEndian<unsigned short>::BigEndian() Line | Count | Source | 77 | 5.89k | constexpr BigEndian() = default; |
AK::BigEndian<short>::BigEndian() Line | Count | Source | 77 | 1.47k | constexpr BigEndian() = default; |
AK::BigEndian<unsigned int>::BigEndian() Line | Count | Source | 77 | 1.75M | constexpr BigEndian() = default; |
|
78 | | |
79 | | constexpr BigEndian(T value) |
80 | 1.42M | : m_value(convert_between_host_and_big_endian(value)) |
81 | 1.42M | { |
82 | 1.42M | } AK::BigEndian<unsigned int>::BigEndian(unsigned int) Line | Count | Source | 80 | 102k | : m_value(convert_between_host_and_big_endian(value)) | 81 | 102k | { | 82 | 102k | } |
AK::BigEndian<unsigned short>::BigEndian(unsigned short) Line | Count | Source | 80 | 338k | : m_value(convert_between_host_and_big_endian(value)) | 81 | 338k | { | 82 | 338k | } |
AK::BigEndian<short>::BigEndian(short) Line | Count | Source | 80 | 981k | : m_value(convert_between_host_and_big_endian(value)) | 81 | 981k | { | 82 | 981k | } |
|
83 | | |
84 | 2.81G | constexpr operator T() const { return convert_between_host_and_big_endian(m_value); }AK::BigEndian<unsigned int>::operator unsigned int() const Line | Count | Source | 84 | 17.9M | constexpr operator T() const { return convert_between_host_and_big_endian(m_value); } |
AK::BigEndian<unsigned short>::operator unsigned short() const Line | Count | Source | 84 | 2.77G | constexpr operator T() const { return convert_between_host_and_big_endian(m_value); } |
AK::BigEndian<int>::operator int() const Line | Count | Source | 84 | 5.00M | constexpr operator T() const { return convert_between_host_and_big_endian(m_value); } |
AK::BigEndian<unsigned char>::operator unsigned char() const Line | Count | Source | 84 | 12.7M | constexpr operator T() const { return convert_between_host_and_big_endian(m_value); } |
AK::BigEndian<Gfx::ICC::DistinctFourCC<(Gfx::ICC::FourCCType)0> >::operator Gfx::ICC::DistinctFourCC<(Gfx::ICC::FourCCType)0>() const Line | Count | Source | 84 | 16.5k | constexpr operator T() const { return convert_between_host_and_big_endian(m_value); } |
AK::BigEndian<Gfx::ICC::DeviceClass>::operator Gfx::ICC::DeviceClass() const Line | Count | Source | 84 | 24.2k | constexpr operator T() const { return convert_between_host_and_big_endian(m_value); } |
AK::BigEndian<Gfx::ICC::ColorSpace>::operator Gfx::ICC::ColorSpace() const Line | Count | Source | 84 | 15.9k | constexpr operator T() const { return convert_between_host_and_big_endian(m_value); } |
AK::BigEndian<Gfx::ICC::PrimaryPlatform>::operator Gfx::ICC::PrimaryPlatform() const Line | Count | Source | 84 | 17.4k | constexpr operator T() const { return convert_between_host_and_big_endian(m_value); } |
AK::BigEndian<Gfx::ICC::DistinctFourCC<(Gfx::ICC::FourCCType)1> >::operator Gfx::ICC::DistinctFourCC<(Gfx::ICC::FourCCType)1>() const Line | Count | Source | 84 | 12.8k | constexpr operator T() const { return convert_between_host_and_big_endian(m_value); } |
AK::BigEndian<Gfx::ICC::DistinctFourCC<(Gfx::ICC::FourCCType)2> >::operator Gfx::ICC::DistinctFourCC<(Gfx::ICC::FourCCType)2>() const Line | Count | Source | 84 | 12.0k | constexpr operator T() const { return convert_between_host_and_big_endian(m_value); } |
AK::BigEndian<unsigned long>::operator unsigned long() const Line | Count | Source | 84 | 1.31M | constexpr operator T() const { return convert_between_host_and_big_endian(m_value); } |
AK::BigEndian<Gfx::ICC::RenderingIntent>::operator Gfx::ICC::RenderingIntent() const Line | Count | Source | 84 | 13.9k | constexpr operator T() const { return convert_between_host_and_big_endian(m_value); } |
AK::BigEndian<Gfx::ICC::DistinctFourCC<(Gfx::ICC::FourCCType)3> >::operator Gfx::ICC::DistinctFourCC<(Gfx::ICC::FourCCType)3>() const Line | Count | Source | 84 | 12.8k | constexpr operator T() const { return convert_between_host_and_big_endian(m_value); } |
AK::BigEndian<Gfx::ICC::DistinctFourCC<(Gfx::ICC::FourCCType)4> >::operator Gfx::ICC::DistinctFourCC<(Gfx::ICC::FourCCType)4>() const Line | Count | Source | 84 | 28.0k | constexpr operator T() const { return convert_between_host_and_big_endian(m_value); } |
AK::BigEndian<Gfx::ICC::DistinctFourCC<(Gfx::ICC::FourCCType)5> >::operator Gfx::ICC::DistinctFourCC<(Gfx::ICC::FourCCType)5>() const Line | Count | Source | 84 | 37.4k | constexpr operator T() const { return convert_between_host_and_big_endian(m_value); } |
AK::BigEndian<Gfx::ICC::ChromaticityTagData::PhosphorOrColorantType>::operator Gfx::ICC::ChromaticityTagData::PhosphorOrColorantType() const Line | Count | Source | 84 | 152 | constexpr operator T() const { return convert_between_host_and_big_endian(m_value); } |
AK::BigEndian<Gfx::ICC::MeasurementTagData::StandardObserver>::operator Gfx::ICC::MeasurementTagData::StandardObserver() const Line | Count | Source | 84 | 233 | constexpr operator T() const { return convert_between_host_and_big_endian(m_value); } |
AK::BigEndian<Gfx::ICC::MeasurementTagData::MeasurementGeometry>::operator Gfx::ICC::MeasurementTagData::MeasurementGeometry() const Line | Count | Source | 84 | 197 | constexpr operator T() const { return convert_between_host_and_big_endian(m_value); } |
AK::BigEndian<Gfx::ICC::MeasurementTagData::StandardIlluminant>::operator Gfx::ICC::MeasurementTagData::StandardIlluminant() const Line | Count | Source | 84 | 328 | constexpr operator T() const { return convert_between_host_and_big_endian(m_value); } |
AK::BigEndian<Gfx::ISOBMFF::BoxType>::operator Gfx::ISOBMFF::BoxType() const Line | Count | Source | 84 | 40.9k | constexpr operator T() const { return convert_between_host_and_big_endian(m_value); } |
AK::BigEndian<Gfx::ISOBMFF::BrandIdentifier>::operator Gfx::ISOBMFF::BrandIdentifier() const Line | Count | Source | 84 | 925k | constexpr operator T() const { return convert_between_host_and_big_endian(m_value); } |
AK::BigEndian<float>::operator float() const Line | Count | Source | 84 | 1.10M | constexpr operator T() const { return convert_between_host_and_big_endian(m_value); } |
AK::BigEndian<double>::operator double() const Line | Count | Source | 84 | 365k | constexpr operator T() const { return convert_between_host_and_big_endian(m_value); } |
AK::BigEndian<short>::operator short() const Line | Count | Source | 84 | 597k | constexpr operator T() const { return convert_between_host_and_big_endian(m_value); } |
|
85 | | |
86 | | constexpr ReadonlyBytes bytes() const { return { this, sizeof(*this) }; } |
87 | | |
88 | | private: |
89 | | T m_value { 0 }; |
90 | | }; |
91 | | |
92 | | template<typename T> |
93 | | using NetworkOrdered = BigEndian<T>; |
94 | | |
95 | | template<typename T> |
96 | | requires(HasFormatter<T>) struct Formatter<LittleEndian<T>> : Formatter<T> { |
97 | | }; |
98 | | |
99 | | template<typename T> |
100 | | requires(HasFormatter<T>) struct Formatter<BigEndian<T>> : Formatter<T> { |
101 | | }; |
102 | | |
103 | | template<typename T> |
104 | | struct Traits<LittleEndian<T>> : public DefaultTraits<LittleEndian<T>> { |
105 | 0 | static constexpr bool is_trivially_serializable() { return Traits<T>::is_trivially_serializable(); }Unexecuted instantiation: AK::Traits<AK::LittleEndian<unsigned char> >::is_trivially_serializable() Unexecuted instantiation: AK::Traits<AK::LittleEndian<unsigned short> >::is_trivially_serializable() Unexecuted instantiation: AK::Traits<AK::LittleEndian<unsigned int> >::is_trivially_serializable() Unexecuted instantiation: AK::Traits<AK::LittleEndian<int> >::is_trivially_serializable() Unexecuted instantiation: AK::Traits<AK::LittleEndian<float> >::is_trivially_serializable() Unexecuted instantiation: AK::Traits<AK::LittleEndian<double> >::is_trivially_serializable() Unexecuted instantiation: AK::Traits<AK::LittleEndian<short> >::is_trivially_serializable() Unexecuted instantiation: AK::Traits<AK::LittleEndian<unsigned long> >::is_trivially_serializable() Unexecuted instantiation: AK::Traits<AK::LittleEndian<AK::Detail::UFixedBigInt<128ul, AK::Detail::StaticStorage<false, 128ul> > > >::is_trivially_serializable() Unexecuted instantiation: AK::Traits<AK::LittleEndian<long> >::is_trivially_serializable() Unexecuted instantiation: AK::Traits<AK::LittleEndian<signed char> >::is_trivially_serializable() |
106 | | }; |
107 | | |
108 | | template<typename T> |
109 | | struct Traits<BigEndian<T>> : public DefaultTraits<BigEndian<T>> { |
110 | 0 | static constexpr bool is_trivially_serializable() { return Traits<T>::is_trivially_serializable(); }Unexecuted instantiation: AK::Traits<AK::BigEndian<unsigned int> >::is_trivially_serializable() Unexecuted instantiation: AK::Traits<AK::BigEndian<unsigned short> >::is_trivially_serializable() Unexecuted instantiation: AK::Traits<AK::BigEndian<int> >::is_trivially_serializable() Unexecuted instantiation: AK::Traits<AK::BigEndian<unsigned char> >::is_trivially_serializable() Unexecuted instantiation: AK::Traits<AK::BigEndian<Gfx::ISOBMFF::BoxType> >::is_trivially_serializable() Unexecuted instantiation: AK::Traits<AK::BigEndian<unsigned long> >::is_trivially_serializable() Unexecuted instantiation: AK::Traits<AK::BigEndian<Gfx::ISOBMFF::BrandIdentifier> >::is_trivially_serializable() Unexecuted instantiation: AK::Traits<AK::BigEndian<float> >::is_trivially_serializable() Unexecuted instantiation: AK::Traits<AK::BigEndian<double> >::is_trivially_serializable() Unexecuted instantiation: AK::Traits<AK::BigEndian<short> >::is_trivially_serializable() Unexecuted instantiation: AK::Traits<AK::BigEndian<AK::Detail::UFixedBigInt<256ul, AK::Detail::StaticStorage<false, 256ul> > > >::is_trivially_serializable() Unexecuted instantiation: AK::Traits<AK::BigEndian<AK::Detail::UFixedBigInt<384ul, AK::Detail::StaticStorage<false, 384ul> > > >::is_trivially_serializable() |
111 | | }; |
112 | | |
113 | | constexpr u16 bitswap(u16 v) |
114 | 0 | { |
115 | 0 | v = ((v >> 1) & 0x5555) | ((v & 0x5555) << 1); // even & odd bits |
116 | 0 | v = ((v >> 2) & 0x3333) | ((v & 0x3333) << 2); // pairs |
117 | 0 | v = ((v >> 4) & 0x0F0F) | ((v & 0x0F0F) << 4); // nibbles |
118 | 0 | return ((v >> 8) & 0x00FF) | ((v & 0x00FF) << 8); // bytes |
119 | 0 | } |
120 | | |
121 | | } |
122 | | |
123 | | #if USING_AK_GLOBALLY |
124 | | using AK::BigEndian; |
125 | | using AK::bitswap; |
126 | | using AK::LittleEndian; |
127 | | using AK::NetworkOrdered; |
128 | | #endif |