Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bit_cast/padding_cast #147

Open
gpeterhoff opened this issue May 31, 2023 · 9 comments
Open

bit_cast/padding_cast #147

gpeterhoff opened this issue May 31, 2023 · 9 comments

Comments

@gpeterhoff
Copy link

*Problem
In many cases a bit_cast is needed. However, it cannot be assumed that std::bit_cast is available. Therefore it would make sense to "rebuild" this in boost; my implementation:
cast.hpp.txt

*Functionality

  • if std::bit_cast or __builtin_bit_cast are available they are used -> constexpr
  • otherwise reinterpret_cast is used, but with the restrictions for std::bit_cast -> not constexpr

*padding_cast

  • allows resizing

thx
Gero

@pdimov
Copy link
Member

pdimov commented May 31, 2023

The current implementation is in boost/core/bit.hpp.

@gpeterhoff
Copy link
Author

But the implementation for bit_cast is not constexpr if std::bit_cast/__builtin_bit_cast are available. I'm sure you can easily adapt that - then I would be happy already :-)
But I need my padding_cast.

thx
Gero

@Lastique
Copy link
Member

It is not obvious as to what the behavior should be when the size of the source and target types are different. And such a behavior would likely be endian-dependent. I'm feeling skeptical about adding padding_cast, unless there is a strong case for one particular implementation and not any other.

@pdimov
Copy link
Member

pdimov commented May 31, 2023

I don't think padding_cast, however specified, belongs here.

I'll make use of __builtin_bit_cast for constexpr reasons though, because I've already done some constexpr work on the rest of bit.hpp.

@pdimov
Copy link
Member

pdimov commented May 31, 2023

What is the intended use of padding_cast?

@gpeterhoff
Copy link
Author

I need this for some math/FP functions, especially for the type boost::float80_t. The memory size of this type (sizeof) can be 16, 12 or 10 bytes depending on the platform/compiler/etc (of course only 10 bytes are relevant). To cast boost::float80_t e.g. to __int128 and back padding_cast is helpful.

@gpeterhoff
Copy link
Author

Hello Peter,
I have seen that you have customized bit_cast (bit.hpp). But this is incomplete:

constexpr case
You only check if __builtin_bit_cast is available. But it cannot be assumed that all compilers support this. Therefore you have to check additionally for std::bit_cast (__cpp_lib_bit_cast >= 201806L).

non constexpr case
You only check the size (BOOST_STATIC_ASSERT(sizeof(To)==sizeof(From)), but bit_cast is subject to further restrictions: https://en.cppreference.com/w/cpp/numeric/bit_cast

thx & regards
Gero

@pdimov
Copy link
Member

pdimov commented Jun 5, 2023

At the moment all the compilers that have a constexpr std::bit_cast implement it via the intrinsic.

Checking the further restrictions requires type traits, but Core can't use TypeTraits (or <type_traits> because we still support C++03.)

@gpeterhoff
Copy link
Author

You can use boost::type_traits ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants