mini-llvm 0.1.0
Loading...
Searching...
No Matches
Trunc.h
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2
3#pragma once
4
5#include <bit>
6#include <concepts>
7#include <cstdlib>
8#include <type_traits>
9
10namespace mini_llvm::ops {
11
12template <typename To>
13 requires std::integral<To>
14struct Trunc {
15 template <typename From>
16 requires std::integral<From>
17 To operator()(From) const noexcept {
18 abort();
19 }
20
21 template <typename From>
22 requires (std::integral<From> && !std::same_as<From, bool> && sizeof(To) <= sizeof(From))
23 To operator()(From x) const noexcept {
24 return
25 std::bit_cast<To>(
26 static_cast<std::make_unsigned_t<To>>(
27 std::bit_cast<std::make_unsigned_t<From>>(x)));
28 }
29};
30
31template <>
32struct Trunc<bool> {
33 template <typename From>
34 requires std::integral<From>
35 bool operator()(From x) const noexcept {
36 return static_cast<bool>(x & 1);
37 }
38
39 bool operator()(bool x) const noexcept {
40 return x;
41 }
42};
43
44} // namespace mini_llvm::ops
Definition Add.h:9
bool operator()(From x) const noexcept
Definition Trunc.h:35
bool operator()(bool x) const noexcept
Definition Trunc.h:39
Definition Trunc.h:14
To operator()(From) const noexcept
Definition Trunc.h:17