dan sinclair | 0890380 | 2022-12-14 18:13:37 +0000 | [diff] [blame] | 1 | // Copyright 2022 The Tint Authors. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
dan sinclair | a2e9b49 | 2023-07-20 09:21:10 +0000 | [diff] [blame^] | 15 | #ifndef SRC_TINT_LANG_BASE_CONSTANT_SCALAR_H_ |
| 16 | #define SRC_TINT_LANG_BASE_CONSTANT_SCALAR_H_ |
dan sinclair | 0890380 | 2022-12-14 18:13:37 +0000 | [diff] [blame] | 17 | |
dan sinclair | 91b68cd | 2023-07-20 09:21:10 +0000 | [diff] [blame] | 18 | #include "src/tint/core/castable.h" |
| 19 | #include "src/tint/core/hash.h" |
dan sinclair | b14a778 | 2023-07-20 09:21:10 +0000 | [diff] [blame] | 20 | #include "src/tint/lang/base/builtin/number.h" |
dan sinclair | a2e9b49 | 2023-07-20 09:21:10 +0000 | [diff] [blame^] | 21 | #include "src/tint/lang/base/constant/manager.h" |
| 22 | #include "src/tint/lang/base/constant/value.h" |
dan sinclair | 0890380 | 2022-12-14 18:13:37 +0000 | [diff] [blame] | 23 | #include "src/tint/type/type.h" |
dan sinclair | 0890380 | 2022-12-14 18:13:37 +0000 | [diff] [blame] | 24 | |
dan sinclair | 8626c9e | 2022-12-14 19:22:19 +0000 | [diff] [blame] | 25 | namespace tint::constant { |
dan sinclair | 0890380 | 2022-12-14 18:13:37 +0000 | [diff] [blame] | 26 | |
Ben Clayton | 781f5bb | 2023-05-31 19:46:33 +0000 | [diff] [blame] | 27 | /// ScalarBase is the base class of all Scalar<T> specializations. |
| 28 | /// Used for querying whether a value is a scalar type. |
| 29 | class ScalarBase : public utils::Castable<ScalarBase, Value> { |
| 30 | public: |
| 31 | ~ScalarBase() override; |
| 32 | }; |
| 33 | |
dan sinclair | 0890380 | 2022-12-14 18:13:37 +0000 | [diff] [blame] | 34 | /// Scalar holds a single scalar or abstract-numeric value. |
dan sinclair | 0890380 | 2022-12-14 18:13:37 +0000 | [diff] [blame] | 35 | template <typename T> |
Ben Clayton | 781f5bb | 2023-05-31 19:46:33 +0000 | [diff] [blame] | 36 | class Scalar : public utils::Castable<Scalar<T>, ScalarBase> { |
dan sinclair | 0890380 | 2022-12-14 18:13:37 +0000 | [diff] [blame] | 37 | public: |
| 38 | static_assert(!std::is_same_v<UnwrapNumber<T>, T> || std::is_same_v<T, bool>, |
| 39 | "T must be a Number or bool"); |
| 40 | |
| 41 | /// Constructor |
| 42 | /// @param t the scalar type |
| 43 | /// @param v the scalar value |
| 44 | Scalar(const type::Type* t, T v) : type(t), value(v) { |
| 45 | if constexpr (IsFloatingPoint<T>) { |
dan sinclair | 8626c9e | 2022-12-14 19:22:19 +0000 | [diff] [blame] | 46 | TINT_ASSERT(Constant, std::isfinite(v.value)); |
dan sinclair | 0890380 | 2022-12-14 18:13:37 +0000 | [diff] [blame] | 47 | } |
| 48 | } |
| 49 | ~Scalar() override = default; |
| 50 | |
Ben Clayton | 574b4b1 | 2023-03-09 23:22:27 +0000 | [diff] [blame] | 51 | /// @copydoc Value::Type() |
dan sinclair | 0890380 | 2022-12-14 18:13:37 +0000 | [diff] [blame] | 52 | const type::Type* Type() const override { return type; } |
| 53 | |
Ben Clayton | 574b4b1 | 2023-03-09 23:22:27 +0000 | [diff] [blame] | 54 | /// @return nullptr, as Scalar does not hold any elements. |
| 55 | const Value* Index(size_t) const override { return nullptr; } |
dan sinclair | 0890380 | 2022-12-14 18:13:37 +0000 | [diff] [blame] | 56 | |
Ben Clayton | 574b4b1 | 2023-03-09 23:22:27 +0000 | [diff] [blame] | 57 | /// @copydoc Value::NumElements() |
| 58 | size_t NumElements() const override { return 1; } |
| 59 | |
| 60 | /// @copydoc Value::AllZero() |
dan sinclair | 0890380 | 2022-12-14 18:13:37 +0000 | [diff] [blame] | 61 | bool AllZero() const override { return IsPositiveZero(); } |
Ben Clayton | 574b4b1 | 2023-03-09 23:22:27 +0000 | [diff] [blame] | 62 | |
| 63 | /// @copydoc Value::AnyZero() |
dan sinclair | 0890380 | 2022-12-14 18:13:37 +0000 | [diff] [blame] | 64 | bool AnyZero() const override { return IsPositiveZero(); } |
Ben Clayton | 574b4b1 | 2023-03-09 23:22:27 +0000 | [diff] [blame] | 65 | |
Ben Clayton | 574b4b1 | 2023-03-09 23:22:27 +0000 | [diff] [blame] | 66 | /// @copydoc Value::Hash() |
dan sinclair | 0890380 | 2022-12-14 18:13:37 +0000 | [diff] [blame] | 67 | size_t Hash() const override { return utils::Hash(type, ValueOf()); } |
| 68 | |
dan sinclair | 529c3fd | 2023-01-06 02:57:36 +0000 | [diff] [blame] | 69 | /// Clones the constant into the provided context |
| 70 | /// @param ctx the clone context |
| 71 | /// @returns the cloned node |
Ben Clayton | a71bd22 | 2023-05-24 23:07:36 +0000 | [diff] [blame] | 72 | const Scalar* Clone(CloneContext& ctx) const override { |
dan sinclair | 529c3fd | 2023-01-06 02:57:36 +0000 | [diff] [blame] | 73 | auto* ty = type->Clone(ctx.type_ctx); |
Ben Clayton | 1e67e53 | 2023-05-24 23:07:36 +0000 | [diff] [blame] | 74 | return ctx.dst.Get<Scalar<T>>(ty, value); |
dan sinclair | 529c3fd | 2023-01-06 02:57:36 +0000 | [diff] [blame] | 75 | } |
| 76 | |
dan sinclair | 0890380 | 2022-12-14 18:13:37 +0000 | [diff] [blame] | 77 | /// @returns `value` if `T` is not a Number, otherwise ValueOf returns the inner value of the |
| 78 | /// Number. |
| 79 | inline auto ValueOf() const { |
| 80 | if constexpr (std::is_same_v<UnwrapNumber<T>, T>) { |
| 81 | return value; |
| 82 | } else { |
| 83 | return value.value; |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | /// @returns true if `value` is a positive zero. |
| 88 | inline bool IsPositiveZero() const { |
| 89 | using N = UnwrapNumber<T>; |
| 90 | return Number<N>(value) == Number<N>(0); // Considers sign bit |
| 91 | } |
| 92 | |
| 93 | /// The scalar type |
| 94 | type::Type const* const type; |
| 95 | /// The scalar value |
| 96 | const T value; |
dan sinclair | b53b8cf | 2022-12-15 16:25:31 +0000 | [diff] [blame] | 97 | |
| 98 | protected: |
Ben Clayton | 574b4b1 | 2023-03-09 23:22:27 +0000 | [diff] [blame] | 99 | /// @copydoc Value::InternalValue() |
dan sinclair | b53b8cf | 2022-12-15 16:25:31 +0000 | [diff] [blame] | 100 | std::variant<std::monostate, AInt, AFloat> InternalValue() const override { |
| 101 | if constexpr (IsFloatingPoint<UnwrapNumber<T>>) { |
| 102 | return static_cast<AFloat>(value); |
| 103 | } else { |
| 104 | return static_cast<AInt>(value); |
| 105 | } |
| 106 | } |
dan sinclair | 0890380 | 2022-12-14 18:13:37 +0000 | [diff] [blame] | 107 | }; |
| 108 | |
dan sinclair | 8626c9e | 2022-12-14 19:22:19 +0000 | [diff] [blame] | 109 | } // namespace tint::constant |
dan sinclair | 0890380 | 2022-12-14 18:13:37 +0000 | [diff] [blame] | 110 | |
dan sinclair | a2e9b49 | 2023-07-20 09:21:10 +0000 | [diff] [blame^] | 111 | #endif // SRC_TINT_LANG_BASE_CONSTANT_SCALAR_H_ |