microsoft/onnxruntime-extensions
Publicmirrored from https://github.com/microsoft/onnxruntime-extensionsAvailable
base/narrow.h
79lines · modeblame
c0555471Scott McKay3 years ago | 1 | // Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | // Licensed under the MIT License. | |
| 3 | | |
| 4 | #pragma once | |
| 5 | | |
| 6 | // ort_extensions::narrow() is like gsl::narrow() but it is also available when exceptions are disabled. | |
| 7 | | |
| 8 | #if !defined(OCOS_NO_EXCEPTIONS) | |
| 9 | | |
| 10 | #include "gsl/narrow" | |
| 11 | | |
| 12 | namespace ort_extensions { | |
| 13 | using gsl::narrow; | |
| 14 | } // namespace ort_extensions | |
| 15 | | |
| 16 | #else // !defined(OCOS_NO_EXCEPTIONS) | |
| 17 | | |
| 18 | #include <cstdio> // std::fprintf | |
| 19 | #include <exception> // std::terminate | |
| 20 | #include <type_traits> | |
| 21 | | |
| 22 | #include "gsl/util" // gsl::narrow_cast | |
| 23 | | |
| 24 | namespace ort_extensions { | |
| 25 | | |
| 26 | namespace detail { | |
| 27 | [[noreturn]] inline void OnNarrowingError() noexcept { | |
| 28 | std::fprintf(stderr, "%s", "narrowing error\n"); | |
| 29 | std::terminate(); | |
| 30 | } | |
| 31 | } // namespace detail | |
| 32 | | |
| 33 | // This implementation of ort_extensions::narrow was copied and adapted from: | |
| 34 | // https://github.com/microsoft/onnxruntime/blob/77b455b969db15d911cd6de5009f3a30fb42c531/include/onnxruntime/core/common/narrow.h | |
| 35 | // which was copied and adapted from | |
| 36 | // https://github.com/microsoft/GSL/blob/a3534567187d2edc428efd3f13466ff75fe5805c/include/gsl/narrow | |
| 37 | | |
| 38 | // narrow() : a checked version of narrow_cast() that terminates if the cast changed the value | |
| 39 | template <class T, class U, typename std::enable_if<std::is_arithmetic<T>::value>::type* = nullptr> | |
| 40 | // clang-format off | |
| 41 | GSL_SUPPRESS(type.1) // NO-FORMAT: attribute | |
| 42 | // clang-format on | |
| 43 | constexpr T narrow(U u) noexcept { | |
| 44 | constexpr const bool is_different_signedness = | |
| 45 | (std::is_signed<T>::value != std::is_signed<U>::value); | |
| 46 | | |
| 47 | // clang-format off | |
| 48 | GSL_SUPPRESS(es.103) // NO-FORMAT: attribute // don't overflow | |
| 49 | GSL_SUPPRESS(es.104) // NO-FORMAT: attribute // don't underflow | |
| 50 | GSL_SUPPRESS(p.2) // NO-FORMAT: attribute // don't rely on undefined behavior | |
| 51 | // clang-format on | |
| 52 | const T t = gsl::narrow_cast<T>(u); // While this is technically undefined behavior in some cases (i.e., if the source value is of floating-point type | |
| 53 | // and cannot fit into the destination integral type), the resultant behavior is benign on the platforms | |
| 54 | // that we target (i.e., no hardware trap representations are hit). | |
| 55 | | |
| 56 | if (static_cast<U>(t) != u || (is_different_signedness && ((t < T{}) != (u < U{})))) { | |
| 57 | detail::OnNarrowingError(); | |
| 58 | } | |
| 59 | | |
| 60 | return t; | |
| 61 | } | |
| 62 | | |
| 63 | template <class T, class U, typename std::enable_if<!std::is_arithmetic<T>::value>::type* = nullptr> | |
| 64 | // clang-format off | |
| 65 | GSL_SUPPRESS(type.1) // NO-FORMAT: attribute | |
| 66 | // clang-format on | |
| 67 | constexpr T narrow(U u) noexcept { | |
| 68 | const T t = gsl::narrow_cast<T>(u); | |
| 69 | | |
| 70 | if (static_cast<U>(t) != u) { | |
| 71 | detail::OnNarrowingError(); | |
| 72 | } | |
| 73 | | |
| 74 | return t; | |
| 75 | } | |
| 76 | | |
| 77 | } // namespace ort_extensions | |
| 78 | | |
| 79 | #endif // !defined(OCOS_NO_EXCEPTIONS) |