1
0
mirror of https://github.com/SerenityOS/serenity synced 2024-07-09 06:40:45 +00:00
serenity/AK/BitCast.h

32 lines
515 B
C
Raw Normal View History

2021-03-22 18:25:06 +00:00
/*
* Copyright (c) 2021, the SerenityOS developers.
*
* SPDX-License-Identifier: BSD-2-Clause
2021-03-22 18:25:06 +00:00
*/
#pragma once
#include <AK/Platform.h>
2021-03-22 18:25:06 +00:00
namespace AK {
template<typename T, typename U>
2022-10-16 22:06:11 +00:00
[[nodiscard]] constexpr inline T bit_cast(U const& a)
2021-03-22 18:25:06 +00:00
{
#if (__has_builtin(__builtin_bit_cast))
return __builtin_bit_cast(T, a);
#else
2021-03-22 18:25:06 +00:00
static_assert(sizeof(T) == sizeof(U));
T result;
__builtin_memcpy(&result, &a, sizeof(T));
return result;
#endif
2021-03-22 18:25:06 +00:00
}
}
#if USING_AK_GLOBALLY
2021-03-22 18:25:06 +00:00
using AK::bit_cast;
#endif