mini-llvm 0.1.0
Loading...
Searching...
No Matches
FPToSI.h
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2
3#pragma once
4
5#include <bit>
6#include <cmath>
7#include <concepts>
8#include <limits>
9#include <optional>
10#include <type_traits>
11
12namespace mini_llvm::ops {
13
14template <typename To>
15 requires std::integral<To>
16struct FPToSI {
17 template <typename From>
18 requires std::floating_point<From>
19 std::optional<To> operator()(From x) const noexcept {
20 if (std::isnan(x))
21 return std::nullopt;
22 From t = std::trunc(x);
23 if (t > static_cast<From>(std::numeric_limits<std::make_signed_t<To>>::max()))
24 return std::nullopt;
25 if (t < static_cast<From>(std::numeric_limits<std::make_signed_t<To>>::min()))
26 return std::nullopt;
27 return std::bit_cast<To>(static_cast<std::make_signed_t<To>>(x));
28 }
29};
30
31template <>
32struct FPToSI<bool> {
33 template <typename From>
34 requires std::floating_point<From>
35 std::optional<bool> operator()(From x) const noexcept {
36 From t = std::trunc(x);
37 if (t == 0.0) return false;
38 if (t == -1.0) return true;
39 return std::nullopt;
40 }
41};
42
43} // namespace mini_llvm::ops
Definition Add.h:9
std::optional< bool > operator()(From x) const noexcept
Definition FPToSI.h:35
Definition FPToSI.h:16
std::optional< To > operator()(From x) const noexcept
Definition FPToSI.h:19