Skip to content

Add Transmogrify concept with sus::mog<T>() #289

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

Merged
merged 17 commits into from
Jul 31, 2023

Conversation

danakj
Copy link
Collaborator

@danakj danakj commented Jul 24, 2023

Closes #221

mog<T>() is like into() except it:

  • does not deduce the type, you must specify it
  • supports all integer/float type conversions in an infallible way, yet
  • can be lossy, including truncation

It is much like static_cast<T>() except it does not introduce Undefined Behaviour when converting to/from floating point values.

This provides lossy conversions between integers and floats (and enums) for #221.

@danakj danakj force-pushed the num-conversions branch 12 times, most recently from 5050196 to 0ae1a74 Compare July 25, 2023 03:41
@danakj danakj changed the title Add AsBits concept with sus::as_bits<T>() Add ToBits concept with sus::to_bits<T>() Jul 28, 2023
@danakj danakj force-pushed the num-conversions branch from 16b025f to d9ce5ee Compare July 28, 2023 02:56
@danakj danakj marked this pull request as ready for review July 28, 2023 02:57
@danakj danakj force-pushed the num-conversions branch 2 times, most recently from 9425d0b to 9739b55 Compare July 28, 2023 05:48
danakj added 7 commits July 28, 2023 02:31
as_bits<T>() is like into() except it:
- does not deduce the type, you must specify it
- does not preserve the meaning, it preserves the bits instead
- can be lossy, including trucation

This provides bitwise conversions between integers for chromium#221.
This gives a cast-like syntax for fallably converting to a
type in a value-preserving way for chromium#221.
The conversion is owning to owning (cheap copy), not
reference to reference or owning to reference. So it's a
'to' transformation.
@danakj danakj force-pushed the num-conversions branch from 9739b55 to 11b03ff Compare July 28, 2023 06:32
/// Casting from a float to an integer will round the float towards zero, except:
/// * NaN will return 0.
/// * Values larger than the maximum integer value, including INFINITY, will
///   saturate to the maximum value of the integer type.
/// * Values smaller than the minimum integer value, including NEG_INFINITY,
///   will saturate to the minimum value of the integer type.
@danakj danakj force-pushed the num-conversions branch 3 times, most recently from 3e70ce5 to ef820ce Compare July 30, 2023 22:36
danakj added 5 commits July 30, 2023 19:26
Normalize the f32/f64 apis with ints they were missing:
- Constructor from f32/f64
- From impl for f32/f64
- Assignment from f32/f64

This ensures you can convert up, as operator== already lets
you convert up.
For converting types that may be out of bounds, use try_from(). If you
want a lossy conversion we have to_bits() (which is to be renamed).
From always succeeds. Use try_from() or to_bits() instead
for values that may not fit, with error handling possible
in the first, and value loss in the second.
std::byte is an enum class so remove those specializations
@danakj danakj force-pushed the num-conversions branch from ef820ce to 8d0cd7d Compare July 31, 2023 02:52
In lack of a better name, we defer to Calvin and Hobbes to show
us how to convert things to other types, through the Transmogrifier.

The sus::mog<T>(f) operation will convert an object `f` to an
object of type T, performing a copy as needed.

The mog operation is supported for subspace numeric types
both integers and floats, primitive integer and float types,
and enums and enum classes.

The operation can be applied to other types by providing a
specialization of the TransmogrifyImpl<To, From> struct with
a To mog_from(const From&) static method.
@danakj danakj force-pushed the num-conversions branch from acd25f7 to f2764ff Compare July 31, 2023 20:18
@danakj danakj changed the title Add ToBits concept with sus::to_bits<T>() Add Transmogrify concept with sus::mog<T>() Jul 31, 2023
@danakj danakj merged commit f3a3758 into chromium:main Jul 31, 2023
@danakj danakj deleted the num-conversions branch July 31, 2023 20:45
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

Successfully merging this pull request may close these issues.

Conversions between floats and integers
1 participant