HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
Namespaces | Functions
cast.hpp File Reference

Functions for casting values between types savely. More...

#include "type_traits.hpp"
#include "terminate.hpp"
#include "../macros.hpp"
#include "assert.hpp"
#include "compare.hpp"
#include "concepts.hpp"
#include "exception.hpp"
#include <type_traits>
#include <typeinfo>
#include <concepts>
#include <limits>
#include <span>
#include <numeric>
#include <bit>
#include <cmath>

Go to the source code of this file.

Namespaces

namespace  hi
 The HikoGUI namespace.
 
namespace  hi::v1
 The HikoGUI API version 1.
 

Functions

 hi_export_module (hikogui.utility.cast)
 
 hi_warning_push ()
 
 hi_warning_ignore_msvc (26472)
 
 hi_warning_ignore_msvc (26467)
 
 hi_warning_ignore_msvc (26496)
 
 hi_warning_ignore_msvc (26466)
 
 hi_warning_ignore_msvc (26474)
 
 hi_warning_ignore_msvc (4702)
 
template<typename T >
constexpr T hi::v1::copy (T value) noexcept
 
template<typename Out , typename In >
constexpr Out hi::v1::up_cast (In *rhs) noexcept
 Cast a pointer to a class to its base class or itself.
 
template<typename Out >
constexpr Out hi::v1::up_cast (nullptr_t) noexcept
 Cast a nullptr to a class.
 
template<typename Out , typename In >
constexpr Out hi::v1::up_cast (In &rhs) noexcept
 Cast a reference to a class to its base class or itself.
 
template<typename Out , typename In >
constexpr Out hi::v1::down_cast (In *rhs) noexcept
 Cast a pointer to a class to its derived class or itself.
 
template<typename Out >
requires std::is_pointer_v<Out>
constexpr Out hi::v1::down_cast (nullptr_t) noexcept
 Cast a pointer to a class to its derived class or itself.
 
template<typename Out , typename In >
constexpr Out hi::v1::down_cast (In &rhs) noexcept
 Cast a reference to a class to its derived class or itself.
 
template<typename Out , std::same_as< Out > In>
constexpr Out hi::v1::wide_cast (In const &rhs) noexcept
 Cast to a type which can hold all values from the input type.
 
template<std::floating_point Out, std::floating_point In>
requires (not std::same_as<In, Out>)
constexpr Out hi::v1::wide_cast (In const &rhs) noexcept
 Cast a floating point number to a floating point type that is wider.
 
template<std::integral Out, std::integral In>
requires (not std::same_as<In, Out>)
constexpr Out hi::v1::wide_cast (In rhs) noexcept
 Cast a integer to an integer type which is wider.
 
template<std::floating_point Out, std::integral In>
constexpr Out hi::v1::wide_cast (In rhs) noexcept
 Cast a integer to an float type which is wider.
 
template<std::integral Out, arithmetic In>
constexpr Out hi::v1::saturate_cast (In rhs) noexcept
 Cast a numeric value to an integer saturating on overflow.
 
template<typename Out , std::same_as< Out > In>
constexpr bool hi::v1::can_narrow_cast (In const &rhs) noexcept
 Check if a value can be casted to a narrow type.
 
template<std::floating_point Out, std::floating_point In>
requires (not std::same_as<In, Out>)
constexpr bool hi::v1::can_narrow_cast (In const &rhs) noexcept
 Check if a value can be casted to a narrow type.
 
template<typename Out , std::same_as< Out > In>
constexpr Out hi::v1::narrow_cast (In const &rhs) noexcept
 Cast numeric values without loss of precision.
 
template<std::floating_point Out, std::floating_point In>
requires (not std::same_as<In, Out>)
constexpr Out hi::v1::narrow_cast (In const &rhs) noexcept
 Cast numeric values without loss of precision.
 
template<std::integral Out, std::floating_point In>
constexpr bool hi::v1::can_round_cast (In rhs) noexcept
 
template<std::integral Out, std::floating_point In>
constexpr bool hi::v1::can_floor_cast (In rhs) noexcept
 
template<std::integral Out, std::floating_point In>
constexpr bool hi::v1::can_ceil_cast (In rhs) noexcept
 
template<std::integral Out, std::floating_point In>
constexpr Out hi::v1::round_cast (In rhs) noexcept
 
template<std::integral Out, std::floating_point In>
constexpr Out hi::v1::floor_cast (In rhs) noexcept
 
template<std::integral Out, std::floating_point In>
constexpr Out hi::v1::ceil_cast (In rhs) noexcept
 
template<std::integral In>
constexpr std::make_unsigned_t< In > hi::v1::to_unsigned (In rhs) noexcept
 Cast an integral to an unsigned integral of the same size.
 
template<std::integral In>
constexpr std::make_signed_t< In > hi::v1::to_signed (In rhs) noexcept
 Cast an integral to an signed integral of the same size.
 
template<std::integral Out, std::integral In>
constexpr Out hi::v1::truncate (In rhs) noexcept
 Cast between integral types truncating or zero-extending the result.
 
template<std::integral Out, std::integral In>
constexpr Out hi::v1::char_cast (In rhs) noexcept
 Cast a character.
 
template<std::integral Out>
constexpr Out hi::v1::char_cast (std::byte rhs) noexcept
 Cast a character.
 
template<std::unsigned_integral OutType, std::unsigned_integral InType>
constexpr OutType hi::v1::low_bit_cast (InType value) noexcept
 Return the low half of the input value.
 
template<std::unsigned_integral OutType, std::unsigned_integral InType>
constexpr OutType hi::v1::high_bit_cast (InType value) noexcept
 Return the upper half of the input value.
 
template<std::unsigned_integral OutType, std::unsigned_integral InType>
constexpr OutType hi::v1::merge_bit_cast (InType hi, InType lo) noexcept
 Return the upper half of the input value.
 
template<typename T >
requires (requires(T&& x) { static_cast<bool>(std::forward<T>(x)); })
constexpr bool hi::v1::to_bool (T &&rhs) noexcept
 
template<typename T >
constexpr T hi::v1::to_mask (bool v) noexcept
 Create a mask from a boolean value.
 
template<typename T >
requires std::is_pointer_v<T>
hi::v1::to_ptr (std::intptr_t value) noexcept
 
template<typename T >
std::intptr_t hi::v1::to_int (T *ptr) noexcept
 
template<typename T , byte_like Byte>
copy_cv_t< T, Byte > & hi::v1::implicit_cast (std::span< Byte > bytes)
 
template<typename T , byte_like Byte>
std::span< copy_cv_t< T, Byte > > hi::v1::implicit_cast (std::span< Byte > bytes, size_t n)
 
template<typename T , byte_like Byte>
copy_cv_t< T, Byte > & hi::v1::implicit_cast (size_t &offset, std::span< Byte > bytes)
 
template<typename T , byte_like Byte>
std::span< copy_cv_t< T, Byte > > hi::v1::implicit_cast (size_t &offset, std::span< Byte > bytes, size_t n)
 
 hi_warning_pop ()
 

Detailed Description

Functions for casting values between types savely.

Function Documentation

◆ hi_warning_ignore_msvc()

hi_warning_ignore_msvc ( 26472 )

Get a bit from an array of unsigned integers. The integers are arranged in little-endian order.

Parameters
lhsThe array of integers from which to take the bit.
indexThe index of the bit
Returns
The value of the bit, either 0 or 1, with the same type as the integers of the array.

Set a bit from an array of unsigned integers. The integers are arranged in little-endian order.

Parameters
rThe array of integers on which to set the bit.
indexThe index of the bit
valueThe value of the bit, either 0 or 1.

Shift logical left with carry chain.

Parameters
lhsThe original value
rhsThe count by how much to shift lhs left.
carryThe carry data to or with the lower bits.
Returns
(result, carry); the carry which can be used to pass into the next iteration.

Shift logical right with carry chain.

Parameters
lhsThe original value
rhsThe count by how much to shift lhs right.
carryThe carry data to or with the lower bits.
Returns
(result, carry); the carry which can be used to pass into the next iteration.

Shift arithmetic right with carry chain.

Parameters
lhsThe original value
rhsThe count by how much to shift lhs right.
Returns
(result, carry); the carry which can be used to pass into the next iteration.

Add two numbers with carry chain.

Parameters
lhsThe left hand side
rhsThe right hand side
carryFrom the previous add in the chain
Returns
(result, carry) pair

Multiply with carry. The carry is a high-word of the multiplication result and has the same size as the inputs. The accumulator is used when doing long-multiplication from the previous row. This function does not overflow even if all the arguments are at max.

Parameters
lhsThe left hand side.
rhsThe right hand side.
carryThe carry-input; carry-output from the previous mul_carry().
accumulatorThe column value during a long multiply.
Returns
low, high of the result.

Wide divide. Can be used to divide a wide unsigned integer by a unsigned integer, as long as the result fits in an unsigned integer.

Parameters
lhs_loThe low side of a wide left-hand-side
lhs_hiThe high side of a wide left-hand-side
rhsThe right hand side
Returns
The result.

Bit scan reverse.

Parameters
lhsThe array of unsigned integers to find the highest set bit off.
nThe number of unsigned integers in the array.
Returns
index of leading one, or -1 when rhs is zero.

Invert unsigned integers using a carry-chain Technically this is not an carry chain.

Parameters
rThe result of the inversion.
rhsThe right hand side operand.
nThe number of digits of r and rhs.

shift logical right using a carry-chain

Parameters
rThe result of the logical-shift-right.
lhsThe left hand side operand of the lsr.
rhsThe right hand side operand of the lsr, the number of bits to shift.
nThe number of digits of r, lhs and rhs.

shift logical right using a carry-chain

Parameters
rThe result of the logical-shift-right.
lhsThe left hand side operand of the lsr.
rhsThe right hand side operand of the lsr, the number of bits to shift.
nThe number of digits of r, lhs and rhs.

shift arithmetic right using a carry-chain This sign-extends the left most bit.

Parameters
rThe result of the logical-shift-right.
lhsThe left hand side operand of the lsr.
rhsThe right hand side operand of the lsr, the number of bits to shift.
nThe number of digits of r, lhs and rhs.

and-operation unsigned integers using a carry-chain

Parameters
rThe result of the and-operation.
lhsThe left hand side operand of the and-operation.
rhsThe right hand side operand of the and-operation.
nThe number of digits of r, lhs and rhs.

or-operation unsigned integers using a carry-chain

Parameters
rThe result of the and-operation.
lhsThe left hand side operand of the or-operation.
rhsThe right hand side operand of the or-operation.
nThe number of digits of r, lhs and rhs.

xor-operation unsigned integers using a carry-chain

Parameters
rThe result of the and-operation.
lhsThe left hand side operand of the xor-operation.
rhsThe right hand side operand of the xor-operation.
nThe number of digits of r, lhs and rhs.

Negate unsigned integers using a carry-chain This is a two's compliment negate.

Parameters
rThe result of the addition.
rhsThe left hand side operand of the addition.
nThe number of digits of r, lhs and rhs.

Add unsigned integers using a carry-chain

Parameters
rThe result of the addition.
lhsThe left hand side operand of the addition.
rhsThe right hand side operand of the addition.
nThe number of digits of r, lhs and rhs.

Subtract unsigned integers using a carry-chain

Parameters
rThe result of the addition.
lhsThe left hand side operand of the addition.
rhsThe right hand side operand of the addition.
nThe number of digits of r, lhs and rhs.

Multiply unsigned integers using a carry-chain

Note
r May not alias with lhs or rhs.
Parameters
rThe result of the multiplication.
lhsThe left hand side operand.
rhsThe right hand side operand.
nThe number of digits of r, lhs and rhs.

Divide unsigned integers using a carry-chain This function does a bit-wise division.

Note
quotient and remainder may not alias with lhs or rhs or with each other.
Parameters
quotientThe result of the division.
remainderThe remainder of the division.
lhsThe left hand side operand.
rhsThe right hand side operand.
nThe number of digits of quotient, remainder, lhs and rhs.

signed divide unsigned integers using a carry-chain This function does a bit-wise division. This function will allocate memory when one or both operands are negative.

Note
quotient and remainder may not alias with lhs or rhs or with each other.
Parameters
quotientThe result of the division.
remainderThe remainder of the division. The remainder has same sign as lhs.
lhsThe left hand side operand.
rhsThe right hand side operand.
nThe number of digits of quotient, remainder, lhs and rhs.