/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 | 872M | { |
23 | | if constexpr (HostIsLittleEndian || sizeof(T) == 1) |
24 | 872M | 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 | 872M | } unsigned char AK::convert_between_host_and_little_endian<unsigned char>(unsigned char) Line | Count | Source | 22 | 9.48M | { | 23 | | if constexpr (HostIsLittleEndian || sizeof(T) == 1) | 24 | 9.48M | 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 | 9.48M | } |
unsigned short AK::convert_between_host_and_little_endian<unsigned short>(unsigned short) Line | Count | Source | 22 | 501M | { | 23 | | if constexpr (HostIsLittleEndian || sizeof(T) == 1) | 24 | 501M | 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 | 501M | } |
unsigned int AK::convert_between_host_and_little_endian<unsigned int>(unsigned int) Line | Count | Source | 22 | 280M | { | 23 | | if constexpr (HostIsLittleEndian || sizeof(T) == 1) | 24 | 280M | 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 | 280M | } |
int AK::convert_between_host_and_little_endian<int>(int) Line | Count | Source | 22 | 48.7M | { | 23 | | if constexpr (HostIsLittleEndian || sizeof(T) == 1) | 24 | 48.7M | 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 | 48.7M | } |
float AK::convert_between_host_and_little_endian<float>(float) Line | Count | Source | 22 | 21.6M | { | 23 | | if constexpr (HostIsLittleEndian || sizeof(T) == 1) | 24 | 21.6M | 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 | 21.6M | } |
double AK::convert_between_host_and_little_endian<double>(double) Line | Count | Source | 22 | 10.4M | { | 23 | | if constexpr (HostIsLittleEndian || sizeof(T) == 1) | 24 | 10.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 | 10.4M | } |
short AK::convert_between_host_and_little_endian<short>(short) Line | Count | Source | 22 | 362k | { | 23 | | if constexpr (HostIsLittleEndian || sizeof(T) == 1) | 24 | 362k | 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 | 362k | } |
unsigned long AK::convert_between_host_and_little_endian<unsigned long>(unsigned long) Line | Count | Source | 22 | 1.79k | { | 23 | | if constexpr (HostIsLittleEndian || sizeof(T) == 1) | 24 | 1.79k | 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 | 1.79k | } |
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 | 22.3k | { | 23 | | if constexpr (HostIsLittleEndian || sizeof(T) == 1) | 24 | 22.3k | 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 | 22.3k | } |
|
34 | | |
35 | | template<typename T> |
36 | | ALWAYS_INLINE constexpr T convert_between_host_and_big_endian(T value) |
37 | 112M | { |
38 | | if constexpr (sizeof(T) == 1 || !HostIsLittleEndian) |
39 | 7.26M | return value; |
40 | | else if constexpr (sizeof(T) == 8) |
41 | 2.19M | return static_cast<T>(__builtin_bswap64(static_cast<u64>(value))); |
42 | | else if constexpr (sizeof(T) == 4) |
43 | 18.6M | return static_cast<T>(__builtin_bswap32(static_cast<u32>(value))); |
44 | | else if constexpr (sizeof(T) == 2) |
45 | 84.2M | 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 | 112M | } unsigned int AK::convert_between_host_and_big_endian<unsigned int>(unsigned int) Line | Count | Source | 37 | 14.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 | 14.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 | 14.0M | } |
unsigned short AK::convert_between_host_and_big_endian<unsigned short>(unsigned short) Line | Count | Source | 37 | 82.7M | { | 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 | 82.7M | 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 | 82.7M | } |
int AK::convert_between_host_and_big_endian<int>(int) Line | Count | Source | 37 | 4.14M | { | 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 | 4.14M | 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 | 4.14M | } |
unsigned char AK::convert_between_host_and_big_endian<unsigned char>(unsigned char) Line | Count | Source | 37 | 7.26M | { | 38 | | if constexpr (sizeof(T) == 1 || !HostIsLittleEndian) | 39 | 7.26M | 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 | 7.26M | } |
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 | 13.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 | 13.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 | 13.8k | } |
Gfx::ICC::DeviceClass AK::convert_between_host_and_big_endian<Gfx::ICC::DeviceClass>(Gfx::ICC::DeviceClass) Line | Count | Source | 37 | 20.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 | 20.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 | 20.2k | } |
Gfx::ICC::ColorSpace AK::convert_between_host_and_big_endian<Gfx::ICC::ColorSpace>(Gfx::ICC::ColorSpace) Line | Count | Source | 37 | 13.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 | 13.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 | 13.2k | } |
Gfx::ICC::PrimaryPlatform AK::convert_between_host_and_big_endian<Gfx::ICC::PrimaryPlatform>(Gfx::ICC::PrimaryPlatform) Line | Count | Source | 37 | 14.3k | { | 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 | 14.3k | 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 | 14.3k | } |
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 | 10.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 | 10.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 | 10.5k | } |
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 | 9.98k | { | 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 | 9.98k | 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 | 9.98k | } |
unsigned long AK::convert_between_host_and_big_endian<unsigned long>(unsigned long) Line | Count | Source | 37 | 1.96M | { | 38 | | if constexpr (sizeof(T) == 1 || !HostIsLittleEndian) | 39 | | return value; | 40 | | else if constexpr (sizeof(T) == 8) | 41 | 1.96M | 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 | 1.96M | } |
Gfx::ICC::RenderingIntent AK::convert_between_host_and_big_endian<Gfx::ICC::RenderingIntent>(Gfx::ICC::RenderingIntent) Line | Count | Source | 37 | 11.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 | 11.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 | 11.5k | } |
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 | 10.6k | { | 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 | 10.6k | 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 | 10.6k | } |
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 | 24.1k | { | 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.1k | 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.1k | } |
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 | 30.3k | { | 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 | 30.3k | 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 | 30.3k | } |
Gfx::ICC::ChromaticityTagData::PhosphorOrColorantType AK::convert_between_host_and_big_endian<Gfx::ICC::ChromaticityTagData::PhosphorOrColorantType>(Gfx::ICC::ChromaticityTagData::PhosphorOrColorantType) Line | Count | Source | 37 | 77 | { | 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 | 77 | 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 | 77 | } |
Gfx::ICC::MeasurementTagData::StandardObserver AK::convert_between_host_and_big_endian<Gfx::ICC::MeasurementTagData::StandardObserver>(Gfx::ICC::MeasurementTagData::StandardObserver) Line | Count | Source | 37 | 158 | { | 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 | 158 | 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 | 158 | } |
Gfx::ICC::MeasurementTagData::MeasurementGeometry AK::convert_between_host_and_big_endian<Gfx::ICC::MeasurementTagData::MeasurementGeometry>(Gfx::ICC::MeasurementTagData::MeasurementGeometry) Line | Count | Source | 37 | 114 | { | 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 | 114 | 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 | 114 | } |
Gfx::ICC::MeasurementTagData::StandardIlluminant AK::convert_between_host_and_big_endian<Gfx::ICC::MeasurementTagData::StandardIlluminant>(Gfx::ICC::MeasurementTagData::StandardIlluminant) Line | Count | Source | 37 | 134 | { | 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 | 134 | 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 | 134 | } |
Gfx::ISOBMFF::BoxType AK::convert_between_host_and_big_endian<Gfx::ISOBMFF::BoxType>(Gfx::ISOBMFF::BoxType) Line | Count | Source | 37 | 25.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 | 25.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 | 25.8k | } |
Gfx::ISOBMFF::BrandIdentifier AK::convert_between_host_and_big_endian<Gfx::ISOBMFF::BrandIdentifier>(Gfx::ISOBMFF::BrandIdentifier) Line | Count | Source | 37 | 211k | { | 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 | 211k | 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 | 211k | } |
float AK::convert_between_host_and_big_endian<float>(float) Line | Count | Source | 37 | 36.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 | 36.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 | 36.2k | } |
double AK::convert_between_host_and_big_endian<double>(double) Line | Count | Source | 37 | 231k | { | 38 | | if constexpr (sizeof(T) == 1 || !HostIsLittleEndian) | 39 | | return value; | 40 | | else if constexpr (sizeof(T) == 8) | 41 | 231k | 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 | 231k | } |
short AK::convert_between_host_and_big_endian<short>(short) Line | Count | Source | 37 | 1.48M | { | 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.48M | 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.48M | } |
|
49 | | |
50 | | template<typename T> |
51 | | ALWAYS_INLINE T convert_between_host_and_network_endian(T value) |
52 | 1.58M | { |
53 | 1.58M | return convert_between_host_and_big_endian(value); |
54 | 1.58M | } |
55 | | |
56 | | template<typename T> |
57 | | class [[gnu::packed]] LittleEndian { |
58 | | public: |
59 | 2.04k | constexpr LittleEndian() = default; |
60 | | |
61 | | constexpr LittleEndian(T value) |
62 | 14.4k | : m_value(convert_between_host_and_little_endian(value)) |
63 | 14.4k | { |
64 | 14.4k | } AK::LittleEndian<unsigned short>::LittleEndian(unsigned short) Line | Count | Source | 62 | 8.27k | : m_value(convert_between_host_and_little_endian(value)) | 63 | 8.27k | { | 64 | 8.27k | } |
AK::LittleEndian<unsigned int>::LittleEndian(unsigned int) Line | Count | Source | 62 | 6.13k | : m_value(convert_between_host_and_little_endian(value)) | 63 | 6.13k | { | 64 | 6.13k | } |
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 | 866M | 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 | 6.81M | 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 | 501M | 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 | 278M | constexpr operator T() const { return convert_between_host_and_little_endian(m_value); } |
AK::LittleEndian<int>::operator int() const Line | Count | Source | 66 | 48.7M | constexpr operator T() const { return convert_between_host_and_little_endian(m_value); } |
AK::LittleEndian<float>::operator float() const Line | Count | Source | 66 | 21.3M | constexpr operator T() const { return convert_between_host_and_little_endian(m_value); } |
AK::LittleEndian<double>::operator double() const Line | Count | Source | 66 | 10.1M | constexpr operator T() const { return convert_between_host_and_little_endian(m_value); } |
AK::LittleEndian<short>::operator short() const Line | Count | Source | 66 | 107k | 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 | 1.79k | 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 | 22.3k | constexpr operator T() const { return convert_between_host_and_little_endian(m_value); } |
|
67 | | |
68 | | private: |
69 | | T m_value { 0 }; |
70 | | }; |
71 | | |
72 | | template<typename T> |
73 | | class [[gnu::packed]] BigEndian { |
74 | | public: |
75 | 562k | constexpr BigEndian() = default; AK::BigEndian<unsigned short>::BigEndian() Line | Count | Source | 75 | 2.81k | constexpr BigEndian() = default; |
AK::BigEndian<short>::BigEndian() Line | Count | Source | 75 | 230 | constexpr BigEndian() = default; |
AK::BigEndian<unsigned int>::BigEndian() Line | Count | Source | 75 | 559k | constexpr BigEndian() = default; |
|
76 | | |
77 | | constexpr BigEndian(T value) |
78 | 1.32M | : m_value(convert_between_host_and_big_endian(value)) |
79 | 1.32M | { |
80 | 1.32M | } AK::BigEndian<unsigned int>::BigEndian(unsigned int) Line | Count | Source | 78 | 80.0k | : m_value(convert_between_host_and_big_endian(value)) | 79 | 80.0k | { | 80 | 80.0k | } |
AK::BigEndian<unsigned short>::BigEndian(unsigned short) Line | Count | Source | 78 | 288k | : m_value(convert_between_host_and_big_endian(value)) | 79 | 288k | { | 80 | 288k | } |
AK::BigEndian<short>::BigEndian(short) Line | Count | Source | 78 | 953k | : m_value(convert_between_host_and_big_endian(value)) | 79 | 953k | { | 80 | 953k | } |
|
81 | | |
82 | 108M | constexpr operator T() const { return convert_between_host_and_big_endian(m_value); }AK::BigEndian<unsigned int>::operator unsigned int() const Line | Count | Source | 82 | 12.3M | constexpr operator T() const { return convert_between_host_and_big_endian(m_value); } |
AK::BigEndian<unsigned short>::operator unsigned short() const Line | Count | Source | 82 | 82.4M | constexpr operator T() const { return convert_between_host_and_big_endian(m_value); } |
AK::BigEndian<int>::operator int() const Line | Count | Source | 82 | 4.14M | constexpr operator T() const { return convert_between_host_and_big_endian(m_value); } |
AK::BigEndian<unsigned char>::operator unsigned char() const Line | Count | Source | 82 | 7.26M | 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 | 82 | 13.8k | 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 | 82 | 20.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 | 82 | 13.2k | 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 | 82 | 14.3k | 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 | 82 | 10.5k | 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 | 82 | 9.98k | constexpr operator T() const { return convert_between_host_and_big_endian(m_value); } |
AK::BigEndian<unsigned long>::operator unsigned long() const Line | Count | Source | 82 | 1.20M | 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 | 82 | 11.5k | 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 | 82 | 10.6k | 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 | 82 | 24.1k | 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 | 82 | 30.3k | 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 | 82 | 77 | 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 | 82 | 158 | 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 | 82 | 114 | 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 | 82 | 134 | 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 | 82 | 25.8k | 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 | 82 | 211k | constexpr operator T() const { return convert_between_host_and_big_endian(m_value); } |
AK::BigEndian<float>::operator float() const Line | Count | Source | 82 | 36.2k | constexpr operator T() const { return convert_between_host_and_big_endian(m_value); } |
AK::BigEndian<double>::operator double() const Line | Count | Source | 82 | 231k | constexpr operator T() const { return convert_between_host_and_big_endian(m_value); } |
AK::BigEndian<short>::operator short() const Line | Count | Source | 82 | 528k | constexpr operator T() const { return convert_between_host_and_big_endian(m_value); } |
|
83 | | |
84 | | private: |
85 | | T m_value { 0 }; |
86 | | }; |
87 | | |
88 | | template<typename T> |
89 | | using NetworkOrdered = BigEndian<T>; |
90 | | |
91 | | template<typename T> |
92 | | requires(HasFormatter<T>) struct Formatter<LittleEndian<T>> : Formatter<T> { |
93 | | }; |
94 | | |
95 | | template<typename T> |
96 | | requires(HasFormatter<T>) struct Formatter<BigEndian<T>> : Formatter<T> { |
97 | | }; |
98 | | |
99 | | template<typename T> |
100 | | struct Traits<LittleEndian<T>> : public DefaultTraits<LittleEndian<T>> { |
101 | 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() |
102 | | }; |
103 | | |
104 | | template<typename T> |
105 | | struct Traits<BigEndian<T>> : public DefaultTraits<BigEndian<T>> { |
106 | 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() |
107 | | }; |
108 | | |
109 | | constexpr u16 bitswap(u16 v) |
110 | 0 | { |
111 | 0 | v = ((v >> 1) & 0x5555) | ((v & 0x5555) << 1); // even & odd bits |
112 | 0 | v = ((v >> 2) & 0x3333) | ((v & 0x3333) << 2); // pairs |
113 | 0 | v = ((v >> 4) & 0x0F0F) | ((v & 0x0F0F) << 4); // nibbles |
114 | 0 | return ((v >> 8) & 0x00FF) | ((v & 0x00FF) << 8); // bytes |
115 | 0 | } |
116 | | |
117 | | } |
118 | | |
119 | | #if USING_AK_GLOBALLY |
120 | | using AK::BigEndian; |
121 | | using AK::bitswap; |
122 | | using AK::LittleEndian; |
123 | | using AK::NetworkOrdered; |
124 | | #endif |