native 0.0.1
Vectors, masks and wide register packs for C++26
Loading...
Searching...
No Matches
float_control.h
1// SPDX-FileCopyrightText: 2026 Edward Kmett <ekmett@gmail.com>
2// SPDX-License-Identifier: BSD-2-Clause OR Apache-2.0
3#pragma once
4#include "native/config.h"
5#include "native/detail/constexpr_float.h"
6
7namespace native::detail {
8 // A value snapshot: constant evaluation uses the established default controls.
9 // Runtime reads are ordered with surrounding explicit environment operations.
10 struct arm_float_control {
11 std::uint64_t bits{};
12 static constexpr arm_float_control current() noexcept {
13 if consteval { return {}; } else {
14 arm_float_control result;
15#if NATIVE_HOST_NEON
16 asm volatile("mrs %0, fpcr" : "=r"(result.bits) :: "memory");
17#endif
18 return result;
19 }
20 }
21 constexpr bool alternative() const noexcept { return bits&2; }
22 constexpr bool enhanced() const noexcept { return bits&(std::uint64_t{1}<<13); }
23 constexpr bool flush_half_inputs() const noexcept { return bits&(std::uint64_t{1}<<19); }
24 constexpr constexpr_float::rounding rounding_mode() const noexcept {
25 using enum constexpr_float::rounding;
26 switch((bits>>22)&3) {
27 case 1:return upward;
28 case 2:return downward;
29 case 3:return toward_zero;
30 default:return nearest_even;
31 }
32 }
33 template<class F> constexpr constexpr_float::policy policy() const noexcept {
34 namespace cf=constexpr_float;
35 bool ah=alternative(),half=std::same_as<F,cf::binary16>;
36 bool flush=half?flush_half_inputs():bool(bits&(std::uint64_t{1}<<24));
37 cf::policy result;
38 result.nan=(bits&(std::uint64_t{1}<<25))?cf::nan_propagation::default_nan:
39 (ah?cf::nan_propagation::first:cf::nan_propagation::signaling_first);
40 result.fma_order=ah?cf::fma_nan_order::multiplicands_first:cf::fma_nan_order::addend_first;
41 result.flush_inputs=half?flush:bool((bits&1) || (!ah && flush));
42 result.flush_outputs=flush && !ah;
43 result.flush_outputs_after_rounding=flush && ah;
44 result.default_nan_negative=ah;
45 result.invalid_product_overrides_quiet_addend=!ah;
46 return result;
47 }
48 template<class F> constexpr typename F::bits_type negate(typename F::bits_type value) const noexcept {
49 return alternative() && constexpr_float::is_nan<F>(value)?value:
50 typename F::bits_type(value^F::sign_mask);
51 }
52 };
53}