| 1 | // Copyright (C) 2016 The Qt Company Ltd. |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
| 3 | #ifndef QMLJS_MATH_H |
| 4 | #define QMLJS_MATH_H |
| 5 | |
| 6 | // |
| 7 | // W A R N I N G |
| 8 | // ------------- |
| 9 | // |
| 10 | // This file is not part of the Qt API. It exists purely as an |
| 11 | // implementation detail. This header file may change from version to |
| 12 | // version without notice, or even be removed. |
| 13 | // |
| 14 | // We mean it. |
| 15 | // |
| 16 | |
| 17 | #include <qglobal.h> |
| 18 | |
| 19 | #include <private/qv4staticvalue_p.h> |
| 20 | #include <QtCore/qnumeric.h> |
| 21 | #include <QtCore/private/qnumeric_p.h> |
| 22 | #include <cmath> |
| 23 | |
| 24 | #if defined(Q_CC_GNU) |
| 25 | #define QMLJS_READONLY __attribute((const)) |
| 26 | #else |
| 27 | #define QMLJS_READONLY |
| 28 | #endif |
| 29 | |
| 30 | QT_BEGIN_NAMESPACE |
| 31 | |
| 32 | namespace QV4 { |
| 33 | |
| 34 | static inline QMLJS_READONLY ReturnedValue add_int32(int a, int b) |
| 35 | { |
| 36 | int result; |
| 37 | if (Q_UNLIKELY(qAddOverflow(a, b, &result))) |
| 38 | return StaticValue::fromDouble(d: static_cast<double>(a) + b).asReturnedValue(); |
| 39 | return StaticValue::fromInt32(i: result).asReturnedValue(); |
| 40 | } |
| 41 | |
| 42 | static inline QMLJS_READONLY ReturnedValue sub_int32(int a, int b) |
| 43 | { |
| 44 | int result; |
| 45 | if (Q_UNLIKELY(qSubOverflow(a, b, &result))) |
| 46 | return StaticValue::fromDouble(d: static_cast<double>(a) - b).asReturnedValue(); |
| 47 | return StaticValue::fromInt32(i: result).asReturnedValue(); |
| 48 | } |
| 49 | |
| 50 | static inline QMLJS_READONLY ReturnedValue mul_int32(int a, int b) |
| 51 | { |
| 52 | int result; |
| 53 | if (Q_UNLIKELY(qMulOverflow(a, b, &result))) |
| 54 | return StaticValue::fromDouble(d: static_cast<double>(a) * b).asReturnedValue(); |
| 55 | // need to handle the case where one number is negative and the other 0 ==> -0 |
| 56 | if (((a < 0) xor (b < 0)) && (result == 0)) |
| 57 | return StaticValue::fromDouble(d: -0.0).asReturnedValue(); |
| 58 | return StaticValue::fromInt32(i: result).asReturnedValue(); |
| 59 | } |
| 60 | |
| 61 | } |
| 62 | |
| 63 | QT_END_NAMESPACE |
| 64 | |
| 65 | #ifdef QMLJS_READONLY |
| 66 | #undef QMLJS_READONLY |
| 67 | #endif |
| 68 | |
| 69 | #endif // QMLJS_MATH_H |
| 70 | |