Bug 1874684 - Part 31: Correctly reject invalid durations in some RoundDuration calls...
[gecko.git] / js / src / jit / ScalarTypeUtils.h
blob19e904d9b8bc86e8030dcae16436995eac544286
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 * vim: set ts=8 sts=2 et sw=2 tw=80:
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #ifndef jit_ScalarTypeUtils_h
8 #define jit_ScalarTypeUtils_h
10 #include "mozilla/CheckedInt.h"
12 #include <stdint.h>
14 #include "js/ScalarType.h"
16 namespace js {
17 namespace jit {
19 // Compute |index * Scalar::byteSize(type) + offsetAdjustment|. If this doesn't
20 // overflow and is non-negative, return true and store the result in *offset.
21 // If the computation overflows or the result is negative, false is returned and
22 // *offset is left unchanged.
23 [[nodiscard]] inline bool ArrayOffsetFitsInInt32(int32_t index,
24 Scalar::Type type,
25 int32_t offsetAdjustment,
26 int32_t* offset) {
27 mozilla::CheckedInt<int32_t> val = index;
28 val *= Scalar::byteSize(type);
29 val += offsetAdjustment;
30 if (!val.isValid() || val.value() < 0) {
31 return false;
34 *offset = val.value();
35 return true;
38 } // namespace jit
39 } // namespace js
41 #endif /* jit_ScalarTypeUtils_h */