mini-llvm
0.1.0
Toggle main menu visibility
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
12
namespace
mini_llvm::ops
{
13
14
template
<
typename
To>
15
requires
std::integral<To>
16
struct
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
31
template
<>
32
struct
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
mini_llvm::ops
Definition
Add.h:9
mini_llvm::ops::FPToSI< bool >::operator()
std::optional< bool > operator()(From x) const noexcept
Definition
FPToSI.h:35
mini_llvm::ops::FPToSI
Definition
FPToSI.h:16
mini_llvm::ops::FPToSI::operator()
std::optional< To > operator()(From x) const noexcept
Definition
FPToSI.h:19
include
mini-llvm
common
ops
FPToSI.h
Generated by
1.17.0