mini-llvm 0.1.0
Loading...
Searching...
No Matches
SDiv.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 <limits>
8#include <optional>
9#include <type_traits>
10
11namespace mini_llvm::ops {
12
13struct SDiv {
14 template <typename T>
15 requires std::integral<T>
16 std::optional<T> operator()(T x, T y) const noexcept {
17 std::make_signed_t<T> sx = std::bit_cast<std::make_signed_t<T>>(x),
18 sy = std::bit_cast<std::make_signed_t<T>>(y);
19 if (sy == 0)
20 return std::nullopt;
21 if (sx == std::numeric_limits<std::make_signed_t<T>>::min() && sy == -1)
22 return std::nullopt;
23 return std::bit_cast<T>(static_cast<std::make_signed_t<T>>(sx / sy));
24 }
25
26 std::optional<bool> operator()(bool x, bool y) const noexcept {
27 if (y == false)
28 return std::nullopt;
29 return x;
30 }
31};
32
33} // namespace mini_llvm::ops
Definition Add.h:9
Definition SDiv.h:13
std::optional< bool > operator()(bool x, bool y) const noexcept
Definition SDiv.h:26
std::optional< T > operator()(T x, T y) const noexcept
Definition SDiv.h:16