-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
63 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
#ifndef APMATH_CONVERSION_H_ | ||
#define APMATH_CONVERSION_H_ | ||
|
||
#include <climits> | ||
#include <concepts> | ||
#include <cstddef> | ||
#include <cstdint> | ||
#include <initializer_list> | ||
#include <optional> | ||
#include <span> | ||
#include <string> | ||
#include <string_view> | ||
#include <utility> | ||
|
||
namespace APMath { | ||
|
||
class APInt; | ||
class APFloat; | ||
|
||
/// Convert value \p from to the value of type `To` with the same bit representation as \p from | ||
template <typename To, typename From> | ||
To bitcast(From const& from); // Undefined | ||
|
||
template <> | ||
APInt bitcast(APFloat const& from); | ||
|
||
template <> | ||
APFloat bitcast(APInt const& from); | ||
|
||
} // namespace APMath | ||
|
||
#endif // APMATH_CONVERSION_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
#include <APMath/Conversion.h> | ||
|
||
#include <cassert> | ||
#include <bit> | ||
|
||
#include <APMath/APInt.h> | ||
#include <APMath/APFloat.h> | ||
|
||
using namespace APMath; | ||
|
||
template <> | ||
APInt APMath::bitcast(APFloat const& from) { | ||
if (from.precision() == APFloatPrec::Single) { | ||
return APInt(std::bit_cast<uint32_t>(from.to<float>()), 32); | ||
} | ||
else { | ||
return APInt(std::bit_cast<uint64_t>(from.to<double>()), 64); | ||
} | ||
} | ||
|
||
template <> | ||
APFloat APMath::bitcast(APInt const& from) { | ||
assert((from.bitwidth() == 32 || from.bitwidth() == 64) && | ||
"Other sizes are not supported by APFloat"); | ||
if (from.bitwidth() == 32) { | ||
return APFloat(std::bit_cast<float>(from.to<uint32_t>()), APFloatPrec::Single); | ||
} | ||
else { | ||
return APFloat(std::bit_cast<double>(from.to<uint64_t>()), APFloatPrec::Double); | ||
} | ||
} |