|
| 1 | +//@HEADER |
| 2 | +// ************************************************************************ |
| 3 | +// |
| 4 | +// Kokkos v. 4.0 |
| 5 | +// Copyright (2022) National Technology & Engineering |
| 6 | +// Solutions of Sandia, LLC (NTESS). |
| 7 | +// |
| 8 | +// Under the terms of Contract DE-NA0003525 with NTESS, |
| 9 | +// the U.S. Government retains certain rights in this software. |
| 10 | +// |
| 11 | +// Part of Kokkos, under the Apache License v2.0 with LLVM Exceptions. |
| 12 | +// See https://kokkos.org/LICENSE for license information. |
| 13 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 14 | +// |
| 15 | +//@HEADER |
| 16 | + |
| 17 | +#pragma once |
| 18 | + |
| 19 | +#include "../__p0009_bits/utility.hpp" |
| 20 | +#include <type_traits> |
| 21 | + |
| 22 | +namespace MDSPAN_IMPL_STANDARD_NAMESPACE { |
| 23 | +namespace detail { |
| 24 | + |
| 25 | +// ============================================================ |
| 26 | +// constant_wrapper, cw, increment, is_constant_wrapper |
| 27 | +// ============================================================ |
| 28 | + |
| 29 | +#if defined(__cpp_lib_constant_wrapper) |
| 30 | + |
| 31 | +using std::constant_wrapper; |
| 32 | +using std::cw; |
| 33 | + |
| 34 | +template<class T> |
| 35 | +constexpr bool is_constant_wrapper = false; |
| 36 | + |
| 37 | +template<class T> |
| 38 | +constexpr bool is_constant_wrapper<const T> = is_constant_wrapper<T>; |
| 39 | + |
| 40 | +template<auto Value> |
| 41 | +constexpr bool is_constant_wrapper<constant_wrapper<Value>> = true; |
| 42 | + |
| 43 | +#else // back-port: constant_wrapper = detail::integral_constant |
| 44 | + |
| 45 | +template<auto Value, class T = decltype(Value)> |
| 46 | +using constant_wrapper = integral_constant<T, Value>; |
| 47 | + |
| 48 | +template<auto Value> |
| 49 | + constexpr auto cw = constant_wrapper<Value>{}; |
| 50 | + |
| 51 | +template<class T> |
| 52 | +constexpr bool is_constant_wrapper = false; |
| 53 | + |
| 54 | +template<class T> |
| 55 | +constexpr bool is_constant_wrapper<const T> = is_constant_wrapper<T>; |
| 56 | + |
| 57 | +// integral_constant is the underlying type of the back-port constant_wrapper |
| 58 | +// (alias templates can't be used in partial specialization patterns) |
| 59 | +template<class Type, Type Value> |
| 60 | +constexpr bool is_constant_wrapper<integral_constant<Type, Value>> = true; |
| 61 | + |
| 62 | +#endif // __cpp_lib_constant_wrapper |
| 63 | + |
| 64 | +// ============================================================ |
| 65 | +// increment function for constant wrapper |
| 66 | +// ============================================================ |
| 67 | + |
| 68 | +template<auto Value> |
| 69 | +MDSPAN_INLINE_FUNCTION |
| 70 | +constexpr auto |
| 71 | +increment([[maybe_unused]] constant_wrapper<Value> x) { |
| 72 | + using value_type = typename decltype(x)::value_type; |
| 73 | + return cw< value_type(Value) + value_type(1) >; |
| 74 | +} |
| 75 | + |
| 76 | + |
| 77 | +// ============================================================ |
| 78 | +// Generic divide / multiply (scalar fall-through) |
| 79 | +// ============================================================ |
| 80 | + |
| 81 | +template <class IndexT, class T0, class T1> |
| 82 | +MDSPAN_INLINE_FUNCTION |
| 83 | +constexpr auto divide(const T0 &v0, const T1 &v1) { |
| 84 | + return IndexT(v0) / IndexT(v1); |
| 85 | +} |
| 86 | + |
| 87 | +template <class IndexT, class T0, class T1> |
| 88 | +MDSPAN_INLINE_FUNCTION |
| 89 | +constexpr auto multiply(const T0 &v0, const T1 &v1) { |
| 90 | + return IndexT(v0) * IndexT(v1); |
| 91 | +} |
| 92 | + |
| 93 | +// ============================================================ |
| 94 | +// Compile-time-preserving overloads for std::integral_constant |
| 95 | +// (used when strided_slice template parameters are std::integral_constant) |
| 96 | +// ============================================================ |
| 97 | + |
| 98 | +template <class IndexT, class T0, T0 v0, class T1, T1 v1> |
| 99 | +MDSPAN_INLINE_FUNCTION |
| 100 | +constexpr auto divide(const std::integral_constant<T0, v0> &, |
| 101 | + const std::integral_constant<T1, v1> &) { |
| 102 | + // Short-circuit division by zero |
| 103 | + // (used for strided_slice with zero extent/stride) |
| 104 | + return integral_constant<IndexT, v0 == 0 ? 0 : v0 / v1>(); |
| 105 | +} |
| 106 | + |
| 107 | +template <class IndexT, class T0, T0 v0, class T1, T1 v1> |
| 108 | +MDSPAN_INLINE_FUNCTION |
| 109 | +constexpr auto multiply(const std::integral_constant<T0, v0> &, |
| 110 | + const std::integral_constant<T1, v1> &) { |
| 111 | + return integral_constant<IndexT, v0 * v1>(); |
| 112 | +} |
| 113 | + |
| 114 | +// ============================================================ |
| 115 | +// Compile-time-preserving overloads for constant_wrapper |
| 116 | +// ============================================================ |
| 117 | + |
| 118 | +#if defined(__cpp_lib_constant_wrapper) |
| 119 | + |
| 120 | +// std::constant_wrapper takes a single NTTP <auto V> |
| 121 | +template <class IndexT, auto v0, auto v1> |
| 122 | +MDSPAN_INLINE_FUNCTION |
| 123 | +constexpr auto divide(const constant_wrapper<v0> &, |
| 124 | + const constant_wrapper<v1> &) { |
| 125 | + constexpr IndexT result = |
| 126 | + IndexT(v0) == IndexT(0) ? IndexT(0) : IndexT(v0) / IndexT(v1); |
| 127 | + return cw<result>; |
| 128 | +} |
| 129 | + |
| 130 | +template <class IndexT, auto v0, auto v1> |
| 131 | +MDSPAN_INLINE_FUNCTION |
| 132 | +constexpr auto multiply(const constant_wrapper<v0> &, |
| 133 | + const constant_wrapper<v1> &) { |
| 134 | + constexpr IndexT result = IndexT(v0) * IndexT(v1); |
| 135 | + return cw<result>; |
| 136 | +} |
| 137 | + |
| 138 | +#else // back-port: constant_wrapper<v, T> = integral_constant<T, v> |
| 139 | + |
| 140 | +template <class IndexT, class T0, T0 v0, class T1, T1 v1> |
| 141 | +MDSPAN_INLINE_FUNCTION |
| 142 | +constexpr auto divide(const constant_wrapper<v0, T0> &, |
| 143 | + const constant_wrapper<v1, T1> &) { |
| 144 | + return integral_constant<IndexT, v0 == 0 ? 0 : v0 / v1>(); |
| 145 | +} |
| 146 | + |
| 147 | +template <class IndexT, class T0, T0 v0, class T1, T1 v1> |
| 148 | +MDSPAN_INLINE_FUNCTION |
| 149 | +constexpr auto multiply(const constant_wrapper<v0, T0> &, |
| 150 | + const constant_wrapper<v1, T1> &) { |
| 151 | + return integral_constant<IndexT, v0 * v1>(); |
| 152 | +} |
| 153 | + |
| 154 | +#endif // __cpp_lib_constant_wrapper |
| 155 | + |
| 156 | +} // namespace detail |
| 157 | +} // namespace MDSPAN_IMPL_STANDARD_NAMESPACE |
0 commit comments