ScalarTypeUtils.h (1207B)
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/. */ 6 7 #ifndef jit_ScalarTypeUtils_h 8 #define jit_ScalarTypeUtils_h 9 10 #include "mozilla/CheckedInt.h" 11 12 #include <stdint.h> 13 14 #include "js/ScalarType.h" 15 16 namespace js { 17 namespace jit { 18 19 // Compute |index * Scalar::byteSize(type)|. If this doesn't overflow and is 20 // 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* offset) { 26 mozilla::CheckedInt<int32_t> val = index; 27 val *= Scalar::byteSize(type); 28 if (!val.isValid() || val.value() < 0) { 29 return false; 30 } 31 32 *offset = val.value(); 33 return true; 34 } 35 36 } // namespace jit 37 } // namespace js 38 39 #endif /* jit_ScalarTypeUtils_h */