|
| 1 | +//===-- amdgpu floating point env manipulation functions --------*- C++ -*-===// |
| 2 | +// |
| 3 | +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | +// See https://llvm.org/LICENSE.txt for license information. |
| 5 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | +// |
| 7 | +//===----------------------------------------------------------------------===// |
| 8 | + |
| 9 | +#ifndef LLVM_LIBC_SRC___SUPPORT_FPUTIL_AMDGPU_FENVIMPL_H |
| 10 | +#define LLVM_LIBC_SRC___SUPPORT_FPUTIL_AMDGPU_FENVIMPL_H |
| 11 | + |
| 12 | +#include "src/__support/GPU/utils.h" |
| 13 | +#include "src/__support/macros/attributes.h" |
| 14 | +#include "src/__support/macros/properties/architectures.h" |
| 15 | + |
| 16 | +#if !defined(LIBC_TARGET_ARCH_IS_AMDGPU) |
| 17 | +#error "Invalid include" |
| 18 | +#endif |
| 19 | + |
| 20 | +#include "hdr/types/fenv_t.h" |
| 21 | + |
| 22 | +#include <stdint.h> |
| 23 | + |
| 24 | +namespace LIBC_NAMESPACE { |
| 25 | +namespace fputil { |
| 26 | + |
| 27 | +namespace internal { |
| 28 | +// Retuns the current status of the AMDGPU floating point environment. In |
| 29 | +// practice this is simply a 64-bit concatenation of the mode register and the |
| 30 | +// trap status register. |
| 31 | +// |
| 32 | +// The mode register controls the floating point behaviour of the device. It |
| 33 | +// can be read or written to by the kernel during runtime It is laid out as a |
| 34 | +// bit field with the following offsets and sizes listed for the relevant |
| 35 | +// entries. |
| 36 | +// |
| 37 | +// ┌─────┬─────────────┬─────┬─────────┬──────────┬─────────────┬────────────┐ |
| 38 | +// │ ... │ EXCP[20:12] │ ... │ IEEE[9] │ CLAMP[8] │ DENORM[7:4] │ ROUND[3:0] │ |
| 39 | +// └─────┴─────────────┴─────┴─────────┴──────────┴─────────────┴────────────┘ |
| 40 | +// |
| 41 | +// The rounding mode and denormal modes both control f64/f16 and f32 precision |
| 42 | +// operations separately with two bits. The accepted values for the rounding |
| 43 | +// mode are nearest, upward, downward, and toward given 0, 1, 2, and 3 |
| 44 | +// respectively. |
| 45 | +// |
| 46 | +// The CLAMP bit indicates that DirectX 10 handling of NaNs is enabled in the |
| 47 | +// vector ALU. When set this will clamp NaN values to zero and pass them |
| 48 | +// otherwise. A hardware bug causes this bit to prevent floating exceptions |
| 49 | +// from being recorded if this bit is set on all generations before GFX12. |
| 50 | +// |
| 51 | +// The IEEE bit controls whether or not floating point operations supporting |
| 52 | +// exception gathering are IEEE 754-2008 compliant. |
| 53 | +// |
| 54 | +// The EXCP field indicates which exceptions will cause the instruction to |
| 55 | +// take a trap if traps are enabled, see the status register. The bit layout |
| 56 | +// is identical to that in the trap status register. We are only concerned |
| 57 | +// with the first six bits and ignore the other three. |
| 58 | +// |
| 59 | +// The trap status register contains information about the status of the |
| 60 | +// exceptions. These bits are accumulated regarless of trap handling statuss |
| 61 | +// and are sticky until cleared. |
| 62 | +// |
| 63 | +// 5 4 3 2 1 0 |
| 64 | +// ┌─────────┬───────────┬──────────┬────────────────┬──────────┬─────────┐ |
| 65 | +// │ Inexact │ Underflow │ Overflow │ Divide by zero │ Denormal │ Invalid │ |
| 66 | +// └─────────┴───────────┴──────────┴────────────────┴──────────┴─────────┘ |
| 67 | +// |
| 68 | +// These exceptions indicate that at least one lane in the current wavefront |
| 69 | +// signalled an floating point exception. There is no way to increase the |
| 70 | +// granularity. |
| 71 | +// |
| 72 | +// The returned value has the following layout. |
| 73 | +// |
| 74 | +// ┌────────────────────┬─────────────────────┐ |
| 75 | +// │ Trap Status[38:32] │ Mode Register[31:0] │ |
| 76 | +// └────────────────────┴─────────────────────┘ |
| 77 | +LIBC_INLINE uint64_t get_fpenv() { return __builtin_amdgcn_get_fpenv(); } |
| 78 | + |
| 79 | +// Set the floating point environment using the same layout as above. |
| 80 | +LIBC_INLINE void set_fpenv(uint64_t env) { __builtin_amdgcn_set_fpenv(env); } |
| 81 | + |
| 82 | +// The six bits used to encode the standard floating point exceptions in the |
| 83 | +// trap status register. |
| 84 | +enum ExceptionFlags : uint32_t { |
| 85 | + EXCP_INVALID_F = 0x1, |
| 86 | + EXCP_DENORMAL_F = 0x2, |
| 87 | + EXCP_DIV_BY_ZERO_F = 0x4, |
| 88 | + EXCP_OVERFLOW_F = 0x8, |
| 89 | + EXCP_UNDERFLOW_F = 0x10, |
| 90 | + EXCP_INEXACT_F = 0x20, |
| 91 | +}; |
| 92 | + |
| 93 | +// The values used by the AMDGPU backend to handle the `llvm.get.rounding` |
| 94 | +// intrinsic function. See the values in the documentation for more information. |
| 95 | +// https://llvm.org/docs/AMDGPUUsage.html#amdgpu-rounding-mode-enumeration-values-table |
| 96 | +enum RoundingFlags : uint32_t { |
| 97 | + ROUND_TOWARD_ZERO = 0x0, |
| 98 | + ROUND_TO_NEAREST = 0x1, |
| 99 | + ROUND_UPWARD = 0x2, |
| 100 | + ROUND_DOWNWARD = 0x3, |
| 101 | +}; |
| 102 | + |
| 103 | +// Exception flags are individual bits in the corresponding hardware register. |
| 104 | +// This converts between the exported C standard values and the hardware values. |
| 105 | +LIBC_INLINE uint32_t get_status_value_for_except(uint32_t excepts) { |
| 106 | + return (excepts & FE_INVALID ? EXCP_INVALID_F : 0) | |
| 107 | +#ifdef __FE_DENORM |
| 108 | + (excepts & __FE_DENORM ? EXCP_DENORMAL_F : 0) | |
| 109 | +#endif // __FE_DENORM |
| 110 | + (excepts & FE_DIVBYZERO ? EXCP_DIV_BY_ZERO_F : 0) | |
| 111 | + (excepts & FE_OVERFLOW ? EXCP_OVERFLOW_F : 0) | |
| 112 | + (excepts & FE_UNDERFLOW ? EXCP_UNDERFLOW_F : 0) | |
| 113 | + (excepts & FE_INEXACT ? EXCP_INEXACT_F : 0); |
| 114 | +} |
| 115 | + |
| 116 | +LIBC_INLINE uint32_t get_except_value_for_status(uint32_t status) { |
| 117 | + return (status & EXCP_INVALID_F ? FE_INVALID : 0) | |
| 118 | +#ifdef __FE_DENORM |
| 119 | + (status & EXCP_DENORMAL_F ? __FE_DENORM : 0) | |
| 120 | +#endif // __FE_DENORM |
| 121 | + (status & EXCP_DIV_BY_ZERO_F ? FE_DIVBYZERO : 0) | |
| 122 | + (status & EXCP_OVERFLOW_F ? FE_OVERFLOW : 0) | |
| 123 | + (status & EXCP_UNDERFLOW_F ? FE_UNDERFLOW : 0) | |
| 124 | + (status & EXCP_INEXACT_F ? FE_INEXACT : 0); |
| 125 | +} |
| 126 | + |
| 127 | +// Set the hardware rounding mode using the llvm.set.rounding intrinsic |
| 128 | +// function. |
| 129 | +// FIXME: This requires `noinline` to flush the hardware register in time. |
| 130 | +[[gnu::noinline]] LIBC_INLINE void set_rounding_mode(uint32_t mode) { |
| 131 | + __builtin_set_flt_rounds(mode); |
| 132 | +} |
| 133 | + |
| 134 | +// Get the hardware rounding mode using the llvm.get.rounding intrinsic |
| 135 | +// function. |
| 136 | +// FIXME: This requires `noinline` to flush the hardware register in time. |
| 137 | +[[gnu::noinline]] LIBC_INLINE uint32_t get_rounding_mode() { |
| 138 | + return __builtin_flt_rounds(); |
| 139 | +} |
| 140 | + |
| 141 | +} // namespace internal |
| 142 | + |
| 143 | +// TODO: Not implemented yet. |
| 144 | +LIBC_INLINE int clear_except(int) { return 0; } |
| 145 | + |
| 146 | +// TODO: Not implemented yet. |
| 147 | +LIBC_INLINE int test_except(int) { return 0; } |
| 148 | + |
| 149 | +// TODO: Not implemented yet. |
| 150 | +LIBC_INLINE int get_except() { return 0; } |
| 151 | + |
| 152 | +// TODO: Not implemented yet. |
| 153 | +LIBC_INLINE int set_except(int) { return 0; } |
| 154 | + |
| 155 | +// TODO: Not implemented yet. |
| 156 | +LIBC_INLINE int enable_except(int) { return 0; } |
| 157 | + |
| 158 | +// TODO: Not implemented yet. |
| 159 | +LIBC_INLINE int disable_except(int) { return 0; } |
| 160 | + |
| 161 | +// TODO: Not implemented yet. |
| 162 | +LIBC_INLINE int raise_except(int) { return 0; } |
| 163 | + |
| 164 | +// Get the currently set rounding mode from the environment. The AMDGPU backend |
| 165 | +// supports an extension for separate f64 / f32 rounding control. If the |
| 166 | +// provided value is outside of the standard region we handle it without |
| 167 | +// modification. |
| 168 | +LIBC_INLINE int get_round() { |
| 169 | + uint32_t mode = internal::get_rounding_mode(); |
| 170 | + switch (mode) { |
| 171 | + case internal::ROUND_TO_NEAREST: |
| 172 | + return FE_TONEAREST; |
| 173 | + case internal::ROUND_UPWARD: |
| 174 | + return FE_UPWARD; |
| 175 | + case internal::ROUND_DOWNWARD: |
| 176 | + return FE_DOWNWARD; |
| 177 | + case internal::ROUND_TOWARD_ZERO: |
| 178 | + return FE_TOWARDZERO; |
| 179 | + default: |
| 180 | + return mode; |
| 181 | + } |
| 182 | + __builtin_unreachable(); |
| 183 | +} |
| 184 | + |
| 185 | +// Set the rounding mode for the environment. If the provided mode is above the |
| 186 | +// expected range we assume it is an extended value to control f32 / f64 |
| 187 | +// separately. |
| 188 | +LIBC_INLINE int set_round(int rounding_mode) { |
| 189 | + switch (rounding_mode) { |
| 190 | + case FE_TONEAREST: |
| 191 | + internal::set_rounding_mode(internal::ROUND_TO_NEAREST); |
| 192 | + break; |
| 193 | + case FE_UPWARD: |
| 194 | + internal::set_rounding_mode(internal::ROUND_UPWARD); |
| 195 | + break; |
| 196 | + case FE_DOWNWARD: |
| 197 | + internal::set_rounding_mode(internal::ROUND_DOWNWARD); |
| 198 | + break; |
| 199 | + case FE_TOWARDZERO: |
| 200 | + internal::set_rounding_mode(internal::ROUND_TOWARD_ZERO); |
| 201 | + break; |
| 202 | + default: |
| 203 | + internal::set_rounding_mode(rounding_mode); |
| 204 | + break; |
| 205 | + } |
| 206 | + return 0; |
| 207 | +} |
| 208 | + |
| 209 | +LIBC_INLINE int get_env(fenv_t *env) { |
| 210 | + if (!env) |
| 211 | + return 1; |
| 212 | + |
| 213 | + env->__fpc = internal::get_fpenv(); |
| 214 | + return 0; |
| 215 | +} |
| 216 | + |
| 217 | +LIBC_INLINE int set_env(const fenv_t *env) { |
| 218 | + if (!env) |
| 219 | + return 1; |
| 220 | + |
| 221 | + internal::set_fpenv(env->__fpc); |
| 222 | + return 0; |
| 223 | +} |
| 224 | + |
| 225 | +} // namespace fputil |
| 226 | +} // namespace LIBC_NAMESPACE |
| 227 | + |
| 228 | +#endif // LLVM_LIBC_SRC___SUPPORT_FPUTIL_AMDGPU_FENVIMPL_H |
0 commit comments