Nuitka
The Python compiler
Loading...
Searching...
No Matches
HelpersOperationBinaryDualAdd.c
1// Copyright 2025, Kay Hayen, mailto:kay.hayen@gmail.com find license text at end of file
2
3/* WARNING, this code is GENERATED. Modify the template HelperOperationBinaryDual.c.j2 instead! */
4
5/* This file is included from another C file, help IDEs to still parse it on its own. */
6#ifdef __IDE_ONLY__
7#include "nuitka/prelude.h"
8#endif
9
10/* C helpers for type specialized "+" (ADD) operations */
11
12/* Code referring to "NILONG" corresponds to Nuitka int/long/C long value and "NILONG" to Nuitka int/long/C long value.
13 */
14bool BINARY_OPERATION_ADD_NILONG_NILONG_NILONG(nuitka_ilong *result, nuitka_ilong *operand1, nuitka_ilong *operand2) {
15 CHECK_NILONG_OBJECT(operand1);
16 CHECK_NILONG_OBJECT(operand2);
17
18 bool left_c_usable = IS_NILONG_C_VALUE_VALID(operand1);
19 bool right_c_usable = IS_NILONG_C_VALUE_VALID(operand2);
20
21 if (left_c_usable && right_c_usable) {
22 // Not every code path will make use of all possible results.
23#if defined(_MSC_VER)
24#pragma warning(push)
25#pragma warning(disable : 4101)
26#endif
27 NUITKA_MAY_BE_UNUSED bool cbool_result;
28 NUITKA_MAY_BE_UNUSED PyObject *obj_result;
29 NUITKA_MAY_BE_UNUSED long clong_result;
30 NUITKA_MAY_BE_UNUSED double cfloat_result;
31#if defined(_MSC_VER)
32#pragma warning(pop)
33#endif
34
35 CHECK_NILONG_OBJECT(operand1);
36 CHECK_NILONG_OBJECT(operand2);
37
38 const long a = GET_NILONG_C_VALUE(operand1);
39 const long b = GET_NILONG_C_VALUE(operand2);
40
41 const long x = (long)((unsigned long)a + b);
42 bool no_overflow = ((x ^ a) >= 0 || (x ^ b) >= 0);
43 if (likely(no_overflow)) {
44 clong_result = x;
45 goto exit_result_ok_clong;
46 }
47
48 ENFORCE_NILONG_OBJECT_VALUE(operand1);
49 obj_result = BINARY_OPERATION_ADD_OBJECT_LONG_CLONG(operand1->python_value, operand2->c_value);
50
51 if (unlikely(result == NULL)) {
52 return false;
53 }
54
55 SET_NILONG_OBJECT_VALUE(result, obj_result);
56 return true;
57
58 exit_result_ok_clong:
59 SET_NILONG_C_VALUE(result, clong_result);
60 return true;
61
62 } else if (left_c_usable == false && right_c_usable) {
63 PyObject *python_result = BINARY_OPERATION_ADD_OBJECT_LONG_CLONG(operand1->python_value, operand2->c_value);
64
65 if (unlikely(python_result == NULL)) {
66 return false;
67 }
68
69 SET_NILONG_OBJECT_VALUE(result, python_result);
70 return true;
71 } else if (left_c_usable && right_c_usable == false) {
72 PyObject *python_result = BINARY_OPERATION_ADD_OBJECT_LONG_CLONG(operand2->python_value, operand1->c_value);
73
74 if (unlikely(python_result == NULL)) {
75 return false;
76 }
77
78 SET_NILONG_OBJECT_VALUE(result, python_result);
79
80 return true;
81 } else {
82 PyObject *python_result = BINARY_OPERATION_ADD_OBJECT_LONG_LONG(operand1->python_value, operand1->python_value);
83
84 if (unlikely(python_result == NULL)) {
85 return false;
86 }
87
88 SET_NILONG_OBJECT_VALUE(result, python_result);
89
90 return true;
91 }
92}
93
94/* Code referring to "NILONG" corresponds to Nuitka int/long/C long value and "DIGIT" to C platform digit value for long
95 * Python objects. */
96bool BINARY_OPERATION_ADD_NILONG_NILONG_DIGIT(nuitka_ilong *result, nuitka_ilong *operand1, long operand2) {
97 CHECK_NILONG_OBJECT(operand1);
98 assert(Py_ABS(operand2) < (1 << PyLong_SHIFT));
99
100 bool left_c_usable = IS_NILONG_C_VALUE_VALID(operand1);
101 bool right_c_usable = true;
102
103 if (left_c_usable && right_c_usable) {
104 // Not every code path will make use of all possible results.
105#if defined(_MSC_VER)
106#pragma warning(push)
107#pragma warning(disable : 4101)
108#endif
109 NUITKA_MAY_BE_UNUSED bool cbool_result;
110 NUITKA_MAY_BE_UNUSED PyObject *obj_result;
111 NUITKA_MAY_BE_UNUSED long clong_result;
112 NUITKA_MAY_BE_UNUSED double cfloat_result;
113#if defined(_MSC_VER)
114#pragma warning(pop)
115#endif
116
117 CHECK_NILONG_OBJECT(operand1);
118 assert(Py_ABS(operand2) < (1 << PyLong_SHIFT));
119
120 const long a = GET_NILONG_C_VALUE(operand1);
121 const long b = (long)(operand2);
122
123 const long x = (long)((unsigned long)a + b);
124 bool no_overflow = ((x ^ a) >= 0 || (x ^ b) >= 0);
125 if (likely(no_overflow)) {
126 clong_result = x;
127 goto exit_result_ok_clong;
128 }
129
130 ENFORCE_NILONG_OBJECT_VALUE(operand1);
131 obj_result = BINARY_OPERATION_ADD_OBJECT_LONG_DIGIT(operand1->python_value, operand2);
132
133 if (unlikely(result == NULL)) {
134 return false;
135 }
136
137 SET_NILONG_OBJECT_VALUE(result, obj_result);
138 return true;
139
140 exit_result_ok_clong:
141 SET_NILONG_C_VALUE(result, clong_result);
142 return true;
143
144 } else if (left_c_usable == false && right_c_usable) {
145 PyObject *python_result = BINARY_OPERATION_ADD_OBJECT_LONG_DIGIT(operand1->python_value, operand2);
146
147 if (unlikely(python_result == NULL)) {
148 return false;
149 }
150
151 SET_NILONG_OBJECT_VALUE(result, python_result);
152 return true;
153 } else {
154 NUITKA_CANNOT_GET_HERE("cannot happen with types NILONG DIGIT");
155 return false;
156 }
157}
158
159// Part of "Nuitka", an optimizing Python compiler that is compatible and
160// integrates with CPython, but also works on its own.
161//
162// Licensed under the Apache License, Version 2.0 (the "License");
163// you may not use this file except in compliance with the License.
164// You may obtain a copy of the License at
165//
166// http://www.apache.org/licenses/LICENSE-2.0
167//
168// Unless required by applicable law or agreed to in writing, software
169// distributed under the License is distributed on an "AS IS" BASIS,
170// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
171// See the License for the specific language governing permissions and
172// limitations under the License.
Definition ints.h:27