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