mirror of
https://github.com/SerenityOS/serenity
synced 2024-09-06 17:06:31 +00:00
![Jesse Buhagiar](/assets/img/avatar_default.png)
We now use the compiler's buitin version of bitcast if it's available instead of just resorting to using the builtin `memcpy`.
28 lines
436 B
C++
28 lines
436 B
C++
/*
|
|
* Copyright (c) 2021, the SerenityOS developers.
|
|
*
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
namespace AK {
|
|
|
|
template<typename T, typename U>
|
|
inline T bit_cast(const U& a)
|
|
{
|
|
#if (__has_builtin(__builtin_bit_cast))
|
|
return __builtin_bit_cast(T, a);
|
|
#else
|
|
static_assert(sizeof(T) == sizeof(U));
|
|
|
|
T result;
|
|
__builtin_memcpy(&result, &a, sizeof(T));
|
|
return result;
|
|
#endif
|
|
}
|
|
|
|
}
|
|
|
|
using AK::bit_cast;
|