Uni20
Asynchronous tensor and runtime library.
Loading...
Searching...
No Matches
scalar_precision.hpp
Go to the documentation of this file.
1#pragma once
2
9#include "types.hpp"
10
11#include <uni20/config.hpp>
12
13#include <array>
14#include <optional>
15#include <span>
16#include <stdexcept>
17#include <string_view>
18#include <utility>
19
20namespace uni20
21{
22
25{
26 fp32,
27 fp64,
28 fp80,
29 fp128
30};
31
33inline constexpr bool has_float80 = UNI20_HAS_FLOAT80 != 0;
34
36inline constexpr bool has_float128 = UNI20_HAS_FLOAT128 != 0;
37
41[[nodiscard]] constexpr std::string_view scalar_precision_name(ScalarPrecision precision)
42{
43 switch (precision)
44 {
46 return "fp32";
48 return "fp64";
50 return "fp80";
52 return "fp128";
53 }
54 return "unknown";
55}
56
60[[nodiscard]] constexpr std::optional<ScalarPrecision> parse_scalar_precision(std::string_view name)
61{
62 if (name == "fp32") return ScalarPrecision::fp32;
63 if (name == "fp64") return ScalarPrecision::fp64;
64 if (name == "fp80") return ScalarPrecision::fp80;
65 if (name == "fp128") return ScalarPrecision::fp128;
66 return std::nullopt;
67}
68
72[[nodiscard]] constexpr bool scalar_precision_is_available(ScalarPrecision precision)
73{
74 switch (precision)
75 {
78 return true;
80 return has_float80;
82 return has_float128;
83 }
84 return false;
85}
86
87namespace detail
88{
89inline constexpr std::array configured_scalar_precisions{
92#if UNI20_HAS_FLOAT80
94#endif
95#if UNI20_HAS_FLOAT128
97#endif
98};
99} // namespace detail
100
103[[nodiscard]] constexpr std::span<ScalarPrecision const> configured_scalar_precisions()
104{
105 return detail::configured_scalar_precisions;
106}
107
110[[nodiscard]] constexpr std::string_view configured_scalar_precision_choices()
111{
112 return "fp32|fp64"
113#if UNI20_HAS_FLOAT80
114 "|fp80"
115#endif
116#if UNI20_HAS_FLOAT128
117 "|fp128"
118#endif
119 ;
120}
121
131template <class Function> decltype(auto) visit_scalar_precision(ScalarPrecision precision, Function&& function)
132{
133 switch (precision)
134 {
136 return std::forward<Function>(function).template operator()<float32>();
138 return std::forward<Function>(function).template operator()<float64>();
140#if UNI20_HAS_FLOAT80
141 return std::forward<Function>(function).template operator()<float80>();
142#else
143 throw std::invalid_argument("fp80 is not available in this Uni20 build");
144#endif
146#if UNI20_HAS_FLOAT128
147 return std::forward<Function>(function).template operator()<float128>();
148#else
149 throw std::invalid_argument("fp128 is not available in this Uni20 build");
150#endif
151 }
152 throw std::invalid_argument("invalid Uni20 scalar precision");
153}
154
155} // namespace uni20
Definition async/async.hpp:26
constexpr bool scalar_precision_is_available(ScalarPrecision precision)
Report whether a runtime precision has a concrete scalar type in this build.
Definition scalar_precision.hpp:72
float float32
Definition types.hpp:22
constexpr bool has_float80
Whether this build provides native x87 extended precision as uni20::float80.
Definition scalar_precision.hpp:33
double float64
Definition types.hpp:23
constexpr bool has_float128
Whether this build provides a concrete uni20::float128 type.
Definition scalar_precision.hpp:36
ScalarPrecision
Runtime identifier for the real precision underlying a scalar type.
Definition scalar_precision.hpp:25
constexpr std::string_view configured_scalar_precision_choices()
Return a compact choice list suitable for command-line help.
Definition scalar_precision.hpp:110
constexpr std::string_view scalar_precision_name(ScalarPrecision precision)
Return the canonical command-line and diagnostic spelling of a precision.
Definition scalar_precision.hpp:41
decltype(auto) visit_scalar_precision(ScalarPrecision precision, Function &&function)
Invoke a templated callable with the concrete real scalar for a runtime precision.
Definition scalar_precision.hpp:131
constexpr std::optional< ScalarPrecision > parse_scalar_precision(std::string_view name)
Parse a canonical scalar precision name independently of build availability.
Definition scalar_precision.hpp:60
constexpr std::span< ScalarPrecision const > configured_scalar_precisions()
Return the real scalar precisions configured in this build.
Definition scalar_precision.hpp:103