CHERIoT RTOS
A compartmentalised RTOS for CHERIoT hardware
Loading...
Searching...
No Matches
anonymous_namespace{float_to_string_helpers.hh} Namespace Reference

This file provides routines to help convert from floating-point values to strings. More...

Classes

struct  FloatParts
 A floating-point value decomposed into components for printing. More...

Concepts

concept  SupportedFloatingPointType
 Set of supported floating-point types.

Functions

template<SupportedFloatingPointType T>
int normalize (T &value)
 Scale the floating-point value up or down in powers of ten so that the decimal point will end up in a sensible place.
constexpr uint32_t pow10 (int exponent)
template<SupportedFloatingPointType T>
FloatParts decompose_float (T value, int8_t decimalPlaces)

Variables

constexpr size_t NumberOfPowersOfTen = 9
 Number of powers of ten required to represent all values that fit in any supported floating-point type.
template<typename T>
constexpr size_t NecessaryNumberOfPowersOfTen
 Helper that computes the necessary value of NumberOfPowersOfTen for a given type.
template<typename T>
constexpr std::array< T, NumberOfPowersOfTenPositiveBinaryPowersOfTen
 Table of powers of ten.
template<typename T>
constexpr std::array< T, NumberOfPowersOfTenNegativeBinaryPowersOfTen
 Table of negative powers of ten.
template<typename T>
constexpr T PositiveExponentiationThreshold = 1e7
 Largest value that is printed without an exponent.
template<typename T>
constexpr T NegativeExponentiationThreshold = 1e-5
 Smallest value that is printed without an exponent.

Detailed Description

This file provides routines to help convert from floating-point values to strings.

This uses the algorithm described here:

https://blog.benoitblanchon.fr/lightweight-float-to-string/

This approach is optimised for code size, not performance or accuracy. It will give more rounding errors than common implementations but is a fraction of the code size.

Function Documentation

◆ decompose_float()

template<SupportedFloatingPointType T>
FloatParts anonymous_namespace{float_to_string_helpers.hh}::decompose_float ( T value,
int8_t decimalPlaces )

Definition at line 171 of file float_to_string_helpers.hh.

◆ normalize()

template<SupportedFloatingPointType T>
int anonymous_namespace{float_to_string_helpers.hh}::normalize ( T & value)
inline

Scale the floating-point value up or down in powers of ten so that the decimal point will end up in a sensible place.

The return value is the number of powers of ten (positive or negative) that the value was scaled by.

This uses binary exponentiation, see the blog post linked at the top of the file for more details.

Definition at line 107 of file float_to_string_helpers.hh.

References NecessaryNumberOfPowersOfTen, NegativeBinaryPowersOfTen, NegativeExponentiationThreshold, PositiveBinaryPowersOfTen, and PositiveExponentiationThreshold.

◆ pow10()

uint32_t anonymous_namespace{float_to_string_helpers.hh}::pow10 ( int exponent)
constexpr

Definition at line 165 of file float_to_string_helpers.hh.

Variable Documentation

◆ NecessaryNumberOfPowersOfTen

template<typename T>
size_t anonymous_namespace{float_to_string_helpers.hh}::NecessaryNumberOfPowersOfTen
constexpr
Initial value:
= []() {
return 32 - __builtin_clz(std::numeric_limits<T>::max_exponent10);
}()

Helper that computes the necessary value of NumberOfPowersOfTen for a given type.

Definition at line 40 of file float_to_string_helpers.hh.

Referenced by normalize().

◆ NegativeBinaryPowersOfTen

template<typename T>
std::array<T, NumberOfPowersOfTen> anonymous_namespace{float_to_string_helpers.hh}::NegativeBinaryPowersOfTen
constexpr
Initial value:
= {
static_cast<T>(1e-1),
static_cast<T>(1e-2),
static_cast<T>(1e-4),
static_cast<T>(1e-8),
static_cast<T>(1e-16),
static_cast<T>(1e-32),
static_cast<T>(1e-64),
static_cast<T>(1e-128),
static_cast<T>(1e-256)}

Table of negative powers of ten.

These are expensive to compute dynamically.

Definition at line 77 of file float_to_string_helpers.hh.

Referenced by normalize().

◆ NegativeExponentiationThreshold

template<typename T>
T anonymous_namespace{float_to_string_helpers.hh}::NegativeExponentiationThreshold = 1e-5
constexpr

Smallest value that is printed without an exponent.

Definition at line 94 of file float_to_string_helpers.hh.

Referenced by normalize().

◆ NumberOfPowersOfTen

size_t anonymous_namespace{float_to_string_helpers.hh}::NumberOfPowersOfTen = 9
constexpr

Number of powers of ten required to represent all values that fit in any supported floating-point type.

9 is sufficient for IEEE 754 64-bit floating-point values. Currently, CHERIoT does not provide a long double type.

Definition at line 33 of file float_to_string_helpers.hh.

◆ PositiveBinaryPowersOfTen

template<typename T>
std::array<T, NumberOfPowersOfTen> anonymous_namespace{float_to_string_helpers.hh}::PositiveBinaryPowersOfTen
constexpr
Initial value:
= {
static_cast<T>(1e1),
static_cast<T>(1e2),
static_cast<T>(1e4),
static_cast<T>(1e8),
static_cast<T>(1e16),
static_cast<T>(1e32),
static_cast<T>(1e64),
static_cast<T>(1e128),
static_cast<T>(1e256)}

Table of powers of ten.

These are expensive to compute dynamically.

Definition at line 61 of file float_to_string_helpers.hh.

Referenced by normalize().

◆ PositiveExponentiationThreshold

template<typename T>
T anonymous_namespace{float_to_string_helpers.hh}::PositiveExponentiationThreshold = 1e7
constexpr

Largest value that is printed without an exponent.

Definition at line 90 of file float_to_string_helpers.hh.

Referenced by normalize().