Nuitka
The Python compiler
Loading...
Searching...
No Matches
HelpersFloats.c
1// Copyright 2025, Kay Hayen, mailto:kay.hayen@gmail.com find license text at end of file
2
3/* These helpers are used to work with float values.
4
5*/
6
7// This file is included from another C file, help IDEs to still parse it on
8// its own.
9#ifdef __IDE_ONLY__
10#include "nuitka/prelude.h"
11#endif
12
13PyObject *TO_FLOAT(PyObject *value) {
14 PyObject *result;
15
16#if PYTHON_VERSION < 0x300
17 if (PyString_CheckExact(value)) {
18 result = PyFloat_FromString(value, NULL);
19 }
20#else
21 if (PyUnicode_CheckExact(value)) {
22 result = PyFloat_FromString(value);
23 }
24#endif
25 else {
26 result = PyNumber_Float(value);
27 }
28
29 if (unlikely(result == NULL)) {
30 return NULL;
31 }
32
33 return result;
34}
35
36#if NUITKA_FLOAT_HAS_FREELIST
37
38static PyFloatObject *_Nuitka_AllocatePyFloatObject(PyThreadState *tstate) {
39#if PYTHON_VERSION >= 0x3e0
40 PyFloatObject *result_float = (PyFloatObject *)Nuitka_PyFreeList_Pop(&_Py_freelists_GET()->floats);
41
42 if (result_float == NULL) {
43 result_float = (PyFloatObject *)NuitkaObject_Malloc(sizeof(PyFloatObject));
44
45 Py_SET_TYPE(result_float, &PyFloat_Type);
46 }
47
48 Nuitka_Py_NewReference((PyObject *)result_float);
49#else
50 // This is the CPython name, spell-checker: ignore numfree
51#if PYTHON_VERSION < 0x3d0
52 struct _Py_float_state *state = &tstate->interp->float_state;
53 PyFloatObject **free_list = &state->free_list;
54 int *numfree = &state->numfree;
55#else
56 struct _Py_object_freelists *freelists = _Nuitka_object_freelists_GET(tstate);
57 struct _Py_float_freelist *state = &freelists->floats;
58 PyFloatObject **free_list = &state->items;
59 int *numfree = &state->numfree;
60#endif
61 PyFloatObject *result_float = *free_list;
62
63 if (result_float) {
64 (*numfree) -= 1;
65 *free_list = (PyFloatObject *)Py_TYPE(result_float);
66 } else {
67 result_float = (PyFloatObject *)NuitkaObject_Malloc(sizeof(PyFloatObject));
68 }
69
70 Py_SET_TYPE(result_float, &PyFloat_Type);
71 Nuitka_Py_NewReference((PyObject *)result_float);
72#endif
73 assert(result_float != NULL);
74 assert(PyFloat_CheckExact(result_float));
75
76 return result_float;
77}
78
79PyObject *MAKE_FLOAT_FROM_DOUBLE(double value) {
80 PyThreadState *tstate = PyThreadState_GET();
81
82 PyFloatObject *result = _Nuitka_AllocatePyFloatObject(tstate);
83
84 PyFloat_SET_DOUBLE(result, value);
85 return (PyObject *)result;
86}
87
88#endif
89
90// Part of "Nuitka", an optimizing Python compiler that is compatible and
91// integrates with CPython, but also works on its own.
92//
93// Licensed under the Apache License, Version 2.0 (the "License");
94// you may not use this file except in compliance with the License.
95// You may obtain a copy of the License at
96//
97// http://www.apache.org/licenses/LICENSE-2.0
98//
99// Unless required by applicable law or agreed to in writing, software
100// distributed under the License is distributed on an "AS IS" BASIS,
101// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
102// See the License for the specific language governing permissions and
103// limitations under the License.