Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qv4math_p.h
Go to the documentation of this file.
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// Qt-Security score:significant
4#ifndef QMLJS_MATH_H
5#define QMLJS_MATH_H
6
7//
8// W A R N I N G
9// -------------
10//
11// This file is not part of the Qt API. It exists purely as an
12// implementation detail. This header file may change from version to
13// version without notice, or even be removed.
14//
15// We mean it.
16//
17
18#include <qglobal.h>
19
20#include <private/qv4staticvalue_p.h>
21#include <QtCore/qnumeric.h>
22#include <QtCore/private/qnumeric_p.h>
23#include <cmath>
24
25#if defined(Q_CC_GNU)
26#define QMLJS_READONLY __attribute((const))
27#else
28#define QMLJS_READONLY
29#endif
30
31QT_BEGIN_NAMESPACE
32
33namespace QV4 {
34
35static inline QMLJS_READONLY ReturnedValue add_int32(int a, int b)
36{
37 int result;
38 if (Q_UNLIKELY(qAddOverflow(a, b, &result)))
39 return StaticValue::fromDouble(static_cast<double>(a) + b).asReturnedValue();
40 return StaticValue::fromInt32(result).asReturnedValue();
41}
42
43static inline QMLJS_READONLY ReturnedValue sub_int32(int a, int b)
44{
45 int result;
46 if (Q_UNLIKELY(qSubOverflow(a, b, &result)))
47 return StaticValue::fromDouble(static_cast<double>(a) - b).asReturnedValue();
48 return StaticValue::fromInt32(result).asReturnedValue();
49}
50
51static inline QMLJS_READONLY ReturnedValue mul_int32(int a, int b)
52{
53 int result;
54 if (Q_UNLIKELY(qMulOverflow(a, b, &result)))
55 return StaticValue::fromDouble(static_cast<double>(a) * b).asReturnedValue();
56 // need to handle the case where one number is negative and the other 0 ==> -0
57 if (((a < 0) xor (b < 0)) && (result == 0))
58 return StaticValue::fromDouble(-0.0).asReturnedValue();
59 return StaticValue::fromInt32(result).asReturnedValue();
60}
61
62}
63
64QT_END_NAMESPACE
65
66#ifdef QMLJS_READONLY
67#undef QMLJS_READONLY
68#endif
69
70#endif // QMLJS_MATH_H
Definition qjsvalue.h:23
static QMLJS_READONLY ReturnedValue mul_int32(int a, int b)
Definition qv4math_p.h:51
static QMLJS_READONLY ReturnedValue add_int32(int a, int b)
Definition qv4math_p.h:35
static QMLJS_READONLY ReturnedValue sub_int32(int a, int b)
Definition qv4math_p.h:43
#define QMLJS_READONLY
Definition qv4math_p.h:28