| 1 |
// Int primitive operations (tagged arbitrary-precision integers) |
| 2 |
// |
| 3 |
// These are registered in mypyc.primitives.int_ops. |
| 4 |
|
| 5 |
#include <Python.h> |
| 6 |
#include "CPy.h" |
| 7 |
|
| 8 |
#ifdef _MSC_VER |
| 9 |
#include <intrin.h> |
| 10 |
#endif |
| 11 |
|
| 12 |
#ifndef _WIN32 |
| 13 |
// On 64-bit Linux and macOS, ssize_t and long are both 64 bits, and |
| 14 |
// PyLong_FromLong is faster than PyLong_FromSsize_t, so use the faster one |
| 15 |
#define CPyLong_FromSsize_t PyLong_FromLong |
| 16 |
#else |
| 17 |
// On 64-bit Windows, ssize_t is 64 bits but long is 32 bits, so we |
| 18 |
// can't use the above trick |
| 19 |
#define CPyLong_FromSsize_t PyLong_FromSsize_t |
| 20 |
#endif |
| 21 |
|
| 22 |
#if defined(__GNUC__) || defined(__clang__) |
| 23 |
# if defined(__x86_64__) || defined(_M_X64) || defined(__aarch64__) || (defined(__SIZEOF_POINTER__) && __SIZEOF_POINTER__ == 8) |
| 24 |
# define CPY_CLZ(x) __builtin_clzll((unsigned long long)(x)) |
| 25 |
# define CPY_BITS 64 |
| 26 |
# else |
| 27 |
# define CPY_CLZ(x) __builtin_clz((unsigned int)(x)) |
| 28 |
# define CPY_BITS 32 |
| 29 |
# endif |
| 30 |
#endif |
| 31 |
|
| 32 |
|
| 33 |
CPyTagged CPyTagged_FromSsize_t(Py_ssize_t value) { |
| 34 |
// We use a Python object if the value shifted left by 1 is too |
| 35 |
// large for Py_ssize_t |
| 36 |
if (unlikely(CPyTagged_TooBig(value))) { |
| 37 |
PyObject *object = PyLong_FromSsize_t(value); |
| 38 |
return ((CPyTagged)object) | CPY_INT_TAG; |
| 39 |
} else { |
| 40 |
return value << 1; |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
CPyTagged CPyTagged_FromVoidPtr(void *ptr) { |
| 45 |
if ((uintptr_t)ptr > PY_SSIZE_T_MAX) { |
| 46 |
PyObject *object = PyLong_FromVoidPtr(ptr); |
| 47 |
return ((CPyTagged)object) | CPY_INT_TAG; |
| 48 |
} else { |
| 49 |
return CPyTagged_FromSsize_t((Py_ssize_t)ptr); |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
CPyTagged CPyTagged_FromInt64(int64_t value) { |
| 54 |
if (unlikely(CPyTagged_TooBigInt64(value))) { |
| 55 |
PyObject *object = PyLong_FromLongLong(value); |
| 56 |
return ((CPyTagged)object) | CPY_INT_TAG; |
| 57 |
} else { |
| 58 |
return value << 1; |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
PyObject *CPyTagged_AsObject(CPyTagged x) { |
| 63 |
PyObject *value; |
| 64 |
if (unlikely(CPyTagged_CheckLong(x))) { |
| 65 |
value = CPyTagged_LongAsObject(x); |
| 66 |
Py_INCREF(value); |
| 67 |
} else { |
| 68 |
value = CPyLong_FromSsize_t(CPyTagged_ShortAsSsize_t(x)); |
| 69 |
if (value == NULL) { |
| 70 |
CPyError_OutOfMemory(); |
| 71 |
} |
| 72 |
} |
| 73 |
return value; |
| 74 |
} |
| 75 |
|
| 76 |
PyObject *CPyTagged_StealAsObject(CPyTagged x) { |
| 77 |
PyObject *value; |
| 78 |
if (unlikely(CPyTagged_CheckLong(x))) { |
| 79 |
value = CPyTagged_LongAsObject(x); |
| 80 |
} else { |
| 81 |
value = CPyLong_FromSsize_t(CPyTagged_ShortAsSsize_t(x)); |
| 82 |
if (value == NULL) { |
| 83 |
CPyError_OutOfMemory(); |
| 84 |
} |
| 85 |
} |
| 86 |
return value; |
| 87 |
} |
| 88 |
|
| 89 |
Py_ssize_t CPyTagged_AsSsize_t(CPyTagged x) { |
| 90 |
if (likely(CPyTagged_CheckShort(x))) { |
| 91 |
return CPyTagged_ShortAsSsize_t(x); |
| 92 |
} else { |
| 93 |
return PyLong_AsSsize_t(CPyTagged_LongAsObject(x)); |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
CPy_NOINLINE |
| 98 |
void CPyTagged_IncRef(CPyTagged x) { |
| 99 |
if (unlikely(CPyTagged_CheckLong(x))) { |
| 100 |
Py_INCREF(CPyTagged_LongAsObject(x)); |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
CPy_NOINLINE |
| 105 |
void CPyTagged_DecRef(CPyTagged x) { |
| 106 |
if (unlikely(CPyTagged_CheckLong(x))) { |
| 107 |
Py_DECREF(CPyTagged_LongAsObject(x)); |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
CPy_NOINLINE |
| 112 |
void CPyTagged_XDecRef(CPyTagged x) { |
| 113 |
if (unlikely(CPyTagged_CheckLong(x))) { |
| 114 |
Py_XDECREF(CPyTagged_LongAsObject(x)); |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
// Tagged int negation slow path, where the result may be a long integer |
| 119 |
CPyTagged CPyTagged_Negate_(CPyTagged num) { |
| 120 |
PyObject *num_obj = CPyTagged_AsObject(num); |
| 121 |
PyObject *result = PyNumber_Negative(num_obj); |
| 122 |
if (result == NULL) { |
| 123 |
CPyError_OutOfMemory(); |
| 124 |
} |
| 125 |
Py_DECREF(num_obj); |
| 126 |
return CPyTagged_StealFromObject(result); |
| 127 |
} |
| 128 |
|
| 129 |
// Tagged int addition slow path, where the result may be a long integer |
| 130 |
CPyTagged CPyTagged_Add_(CPyTagged left, CPyTagged right) { |
| 131 |
PyObject *left_obj = CPyTagged_AsObject(left); |
| 132 |
PyObject *right_obj = CPyTagged_AsObject(right); |
| 133 |
PyObject *result = PyNumber_Add(left_obj, right_obj); |
| 134 |
if (result == NULL) { |
| 135 |
CPyError_OutOfMemory(); |
| 136 |
} |
| 137 |
Py_DECREF(left_obj); |
| 138 |
Py_DECREF(right_obj); |
| 139 |
return CPyTagged_StealFromObject(result); |
| 140 |
} |
| 141 |
|
| 142 |
// Tagged int subtraction slow path, where the result may be a long integer |
| 143 |
CPyTagged CPyTagged_Subtract_(CPyTagged left, CPyTagged right) { |
| 144 |
PyObject *left_obj = CPyTagged_AsObject(left); |
| 145 |
PyObject *right_obj = CPyTagged_AsObject(right); |
| 146 |
PyObject *result = PyNumber_Subtract(left_obj, right_obj); |
| 147 |
if (result == NULL) { |
| 148 |
CPyError_OutOfMemory(); |
| 149 |
} |
| 150 |
Py_DECREF(left_obj); |
| 151 |
Py_DECREF(right_obj); |
| 152 |
return CPyTagged_StealFromObject(result); |
| 153 |
} |
| 154 |
|
| 155 |
// Tagged int multiplication slow path, where the result may be a long integer |
| 156 |
CPyTagged CPyTagged_Multiply_(CPyTagged left, CPyTagged right) { |
| 157 |
PyObject *left_obj = CPyTagged_AsObject(left); |
| 158 |
PyObject *right_obj = CPyTagged_AsObject(right); |
| 159 |
PyObject *result = PyNumber_Multiply(left_obj, right_obj); |
| 160 |
if (result == NULL) { |
| 161 |
CPyError_OutOfMemory(); |
| 162 |
} |
| 163 |
Py_DECREF(left_obj); |
| 164 |
Py_DECREF(right_obj); |
| 165 |
return CPyTagged_StealFromObject(result); |
| 166 |
} |
| 167 |
|
| 168 |
// Tagged int // slow path, where the result may be a long integer (or raise) |
| 169 |
CPyTagged CPyTagged_FloorDivide_(CPyTagged left, CPyTagged right) { |
| 170 |
PyObject *left_obj = CPyTagged_AsObject(left); |
| 171 |
PyObject *right_obj = CPyTagged_AsObject(right); |
| 172 |
PyObject *result = PyNumber_FloorDivide(left_obj, right_obj); |
| 173 |
Py_DECREF(left_obj); |
| 174 |
Py_DECREF(right_obj); |
| 175 |
// Handle exceptions honestly because it could be ZeroDivisionError |
| 176 |
if (result == NULL) { |
| 177 |
return CPY_INT_TAG; |
| 178 |
} else { |
| 179 |
return CPyTagged_StealFromObject(result); |
| 180 |
} |
| 181 |
} |
| 182 |
|
| 183 |
// Tagged int % slow path, where the result may be a long integer (or raise) |
| 184 |
CPyTagged CPyTagged_Remainder_(CPyTagged left, CPyTagged right) { |
| 185 |
PyObject *left_obj = CPyTagged_AsObject(left); |
| 186 |
PyObject *right_obj = CPyTagged_AsObject(right); |
| 187 |
PyObject *result = PyNumber_Remainder(left_obj, right_obj); |
| 188 |
Py_DECREF(left_obj); |
| 189 |
Py_DECREF(right_obj); |
| 190 |
// Handle exceptions honestly because it could be ZeroDivisionError |
| 191 |
if (result == NULL) { |
| 192 |
return CPY_INT_TAG; |
| 193 |
} else { |
| 194 |
return CPyTagged_StealFromObject(result); |
| 195 |
} |
| 196 |
} |
| 197 |
|
| 198 |
bool CPyTagged_IsEq_(CPyTagged left, CPyTagged right) { |
| 199 |
if (CPyTagged_CheckShort(right)) { |
| 200 |
return false; |
| 201 |
} else { |
| 202 |
PyObject *left_obj = CPyTagged_AsObject(left); |
| 203 |
PyObject *right_obj = CPyTagged_AsObject(right); |
| 204 |
int result = PyObject_RichCompareBool(left_obj, right_obj, Py_EQ); |
| 205 |
Py_DECREF(left_obj); |
| 206 |
Py_DECREF(right_obj); |
| 207 |
if (result == -1) { |
| 208 |
CPyError_OutOfMemory(); |
| 209 |
} |
| 210 |
return result; |
| 211 |
} |
| 212 |
} |
| 213 |
|
| 214 |
bool CPyTagged_IsLt_(CPyTagged left, CPyTagged right) { |
| 215 |
PyObject *left_obj = CPyTagged_AsObject(left); |
| 216 |
PyObject *right_obj = CPyTagged_AsObject(right); |
| 217 |
int result = PyObject_RichCompareBool(left_obj, right_obj, Py_LT); |
| 218 |
Py_DECREF(left_obj); |
| 219 |
Py_DECREF(right_obj); |
| 220 |
if (result == -1) { |
| 221 |
CPyError_OutOfMemory(); |
| 222 |
} |
| 223 |
return result; |
| 224 |
} |
| 225 |
|
| 226 |
PyObject *CPyLong_FromStrWithBase(PyObject *o, CPyTagged base) { |
| 227 |
Py_ssize_t base_size_t = CPyTagged_AsSsize_t(base); |
| 228 |
return PyLong_FromUnicodeObject(o, base_size_t); |
| 229 |
} |
| 230 |
|
| 231 |
PyObject *CPyLong_FromStr(PyObject *o) { |
| 232 |
CPyTagged base = CPyTagged_FromSsize_t(10); |
| 233 |
return CPyLong_FromStrWithBase(o, base); |
| 234 |
} |
| 235 |
|
| 236 |
CPyTagged CPyTagged_FromFloat(double f) { |
| 237 |
if (f < ((double)CPY_TAGGED_MAX + 1.0) && f > (CPY_TAGGED_MIN - 1.0)) { |
| 238 |
return (Py_ssize_t)f << 1; |
| 239 |
} |
| 240 |
PyObject *o = PyLong_FromDouble(f); |
| 241 |
if (o == NULL) |
| 242 |
return CPY_INT_TAG; |
| 243 |
return CPyTagged_StealFromObject(o); |
| 244 |
} |
| 245 |
|
| 246 |
PyObject *CPyBool_Str(bool b) { |
| 247 |
return PyObject_Str(b ? Py_True : Py_False); |
| 248 |
} |
| 249 |
|
| 250 |
// Bitwise op '&', '|' or '^' using the generic (slow) API |
| 251 |
static CPyTagged GenericBitwiseOp(CPyTagged a, CPyTagged b, char op) { |
| 252 |
PyObject *aobj = CPyTagged_AsObject(a); |
| 253 |
PyObject *bobj = CPyTagged_AsObject(b); |
| 254 |
PyObject *r; |
| 255 |
if (op == '&') { |
| 256 |
r = PyNumber_And(aobj, bobj); |
| 257 |
} else if (op == '|') { |
| 258 |
r = PyNumber_Or(aobj, bobj); |
| 259 |
} else { |
| 260 |
r = PyNumber_Xor(aobj, bobj); |
| 261 |
} |
| 262 |
if (unlikely(r == NULL)) { |
| 263 |
CPyError_OutOfMemory(); |
| 264 |
} |
| 265 |
Py_DECREF(aobj); |
| 266 |
Py_DECREF(bobj); |
| 267 |
return CPyTagged_StealFromObject(r); |
| 268 |
} |
| 269 |
|
| 270 |
// Return pointer to digits of a PyLong object. If it's a short |
| 271 |
// integer, place digits in the buffer buf instead to avoid memory |
| 272 |
// allocation (it's assumed to be big enough). Return the number of |
| 273 |
// digits in *size. *size is negative if the integer is negative. |
| 274 |
static digit *GetIntDigits(CPyTagged n, Py_ssize_t *size, digit *buf) { |
| 275 |
if (CPyTagged_CheckShort(n)) { |
| 276 |
Py_ssize_t val = CPyTagged_ShortAsSsize_t(n); |
| 277 |
bool neg = val < 0; |
| 278 |
int len = 1; |
| 279 |
if (neg) { |
| 280 |
val = -val; |
| 281 |
} |
| 282 |
buf[0] = val & PyLong_MASK; |
| 283 |
if (val > (Py_ssize_t)PyLong_MASK) { |
| 284 |
val >>= PyLong_SHIFT; |
| 285 |
buf[1] = val & PyLong_MASK; |
| 286 |
if (val > (Py_ssize_t)PyLong_MASK) { |
| 287 |
buf[2] = val >> PyLong_SHIFT; |
| 288 |
len = 3; |
| 289 |
} else { |
| 290 |
len = 2; |
| 291 |
} |
| 292 |
} |
| 293 |
*size = neg ? -len : len; |
| 294 |
return buf; |
| 295 |
} else { |
| 296 |
PyLongObject *obj = (PyLongObject *)CPyTagged_LongAsObject(n); |
| 297 |
*size = CPY_LONG_SIZE_SIGNED(obj); |
| 298 |
return &CPY_LONG_DIGIT(obj, 0); |
| 299 |
} |
| 300 |
} |
| 301 |
|
| 302 |
// Shared implementation of bitwise '&', '|' and '^' (specified by op) for at least |
| 303 |
// one long operand. This is somewhat optimized for performance. |
| 304 |
CPyTagged CPyTagged_BitwiseLongOp_(CPyTagged a, CPyTagged b, char op) { |
| 305 |
// Directly access the digits, as there is no fast C API function for this. |
| 306 |
digit abuf[3]; |
| 307 |
digit bbuf[3]; |
| 308 |
Py_ssize_t asize; |
| 309 |
Py_ssize_t bsize; |
| 310 |
digit *adigits = GetIntDigits(a, &asize, abuf); |
| 311 |
digit *bdigits = GetIntDigits(b, &bsize, bbuf); |
| 312 |
|
| 313 |
if (unlikely(asize < 0 || bsize < 0)) { |
| 314 |
// Negative operand. This is slower, but bitwise ops on them are pretty rare. |
| 315 |
return GenericBitwiseOp(a, b, op); |
| 316 |
} |
| 317 |
// Optimized implementation for two non-negative integers. |
| 318 |
// Swap a and b as needed to ensure a is no longer than b. |
| 319 |
if (asize > bsize) { |
| 320 |
digit *tmp = adigits; |
| 321 |
adigits = bdigits; |
| 322 |
bdigits = tmp; |
| 323 |
Py_ssize_t tmp_size = asize; |
| 324 |
asize = bsize; |
| 325 |
bsize = tmp_size; |
| 326 |
} |
| 327 |
void *digits = NULL; |
| 328 |
PyLongWriter *writer = PyLongWriter_Create(0, op == '&' ? asize : bsize, &digits); |
| 329 |
if (unlikely(writer == NULL)) { |
| 330 |
CPyError_OutOfMemory(); |
| 331 |
} |
| 332 |
Py_ssize_t i; |
| 333 |
if (op == '&') { |
| 334 |
for (i = 0; i < asize; i++) { |
| 335 |
((digit *)digits)[i] = adigits[i] & bdigits[i]; |
| 336 |
} |
| 337 |
} else { |
| 338 |
if (op == '|') { |
| 339 |
for (i = 0; i < asize; i++) { |
| 340 |
((digit *)digits)[i] = adigits[i] | bdigits[i]; |
| 341 |
} |
| 342 |
} else { |
| 343 |
for (i = 0; i < asize; i++) { |
| 344 |
((digit *)digits)[i] = adigits[i] ^ bdigits[i]; |
| 345 |
} |
| 346 |
} |
| 347 |
for (; i < bsize; i++) { |
| 348 |
((digit *)digits)[i] = bdigits[i]; |
| 349 |
} |
| 350 |
} |
| 351 |
return CPyTagged_StealFromObject(PyLongWriter_Finish(writer)); |
| 352 |
} |
| 353 |
|
| 354 |
// Bitwise '~' slow path |
| 355 |
CPyTagged CPyTagged_Invert_(CPyTagged num) { |
| 356 |
PyObject *obj = CPyTagged_AsObject(num); |
| 357 |
PyObject *result = PyNumber_Invert(obj); |
| 358 |
if (unlikely(result == NULL)) { |
| 359 |
CPyError_OutOfMemory(); |
| 360 |
} |
| 361 |
Py_DECREF(obj); |
| 362 |
return CPyTagged_StealFromObject(result); |
| 363 |
} |
| 364 |
|
| 365 |
// Bitwise '>>' slow path |
| 366 |
CPyTagged CPyTagged_Rshift_(CPyTagged left, CPyTagged right) { |
| 367 |
// Long integer or negative shift -- use generic op |
| 368 |
PyObject *lobj = CPyTagged_AsObject(left); |
| 369 |
PyObject *robj = CPyTagged_AsObject(right); |
| 370 |
PyObject *result = PyNumber_Rshift(lobj, robj); |
| 371 |
Py_DECREF(lobj); |
| 372 |
Py_DECREF(robj); |
| 373 |
if (result == NULL) { |
| 374 |
// Propagate error (could be negative shift count) |
| 375 |
return CPY_INT_TAG; |
| 376 |
} |
| 377 |
return CPyTagged_StealFromObject(result); |
| 378 |
} |
| 379 |
|
| 380 |
// Bitwise '<<' slow path |
| 381 |
CPyTagged CPyTagged_Lshift_(CPyTagged left, CPyTagged right) { |
| 382 |
// Long integer or out of range shift -- use generic op |
| 383 |
PyObject *lobj = CPyTagged_AsObject(left); |
| 384 |
PyObject *robj = CPyTagged_AsObject(right); |
| 385 |
PyObject *result = PyNumber_Lshift(lobj, robj); |
| 386 |
Py_DECREF(lobj); |
| 387 |
Py_DECREF(robj); |
| 388 |
if (result == NULL) { |
| 389 |
// Propagate error (could be negative shift count) |
| 390 |
return CPY_INT_TAG; |
| 391 |
} |
| 392 |
return CPyTagged_StealFromObject(result); |
| 393 |
} |
| 394 |
|
| 395 |
// i64 unboxing slow path |
| 396 |
int64_t CPyLong_AsInt64_(PyObject *o) { |
| 397 |
int overflow; |
| 398 |
int64_t result = PyLong_AsLongLongAndOverflow(o, &overflow); |
| 399 |
if (result == -1) { |
| 400 |
if (PyErr_Occurred()) { |
| 401 |
return CPY_LL_INT_ERROR; |
| 402 |
} else if (overflow) { |
| 403 |
PyErr_SetString(PyExc_ValueError, "int too large to convert to i64"); |
| 404 |
return CPY_LL_INT_ERROR; |
| 405 |
} |
| 406 |
} |
| 407 |
return result; |
| 408 |
} |
| 409 |
|
| 410 |
int64_t CPyInt64_Divide(int64_t x, int64_t y) { |
| 411 |
if (y == 0) { |
| 412 |
PyErr_SetString(PyExc_ZeroDivisionError, "integer division or modulo by zero"); |
| 413 |
return CPY_LL_INT_ERROR; |
| 414 |
} |
| 415 |
if (y == -1 && x == INT64_MIN) { |
| 416 |
PyErr_SetString(PyExc_OverflowError, "integer division overflow"); |
| 417 |
return CPY_LL_INT_ERROR; |
| 418 |
} |
| 419 |
int64_t d = x / y; |
| 420 |
// Adjust for Python semantics |
| 421 |
if (((x < 0) != (y < 0)) && d * y != x) { |
| 422 |
d--; |
| 423 |
} |
| 424 |
return d; |
| 425 |
} |
| 426 |
|
| 427 |
int64_t CPyInt64_Remainder(int64_t x, int64_t y) { |
| 428 |
if (y == 0) { |
| 429 |
PyErr_SetString(PyExc_ZeroDivisionError, "integer division or modulo by zero"); |
| 430 |
return CPY_LL_INT_ERROR; |
| 431 |
} |
| 432 |
// Edge case: avoid core dump |
| 433 |
if (y == -1 && x == INT64_MIN) { |
| 434 |
return 0; |
| 435 |
} |
| 436 |
int64_t d = x % y; |
| 437 |
// Adjust for Python semantics |
| 438 |
if (((x < 0) != (y < 0)) && d != 0) { |
| 439 |
d += y; |
| 440 |
} |
| 441 |
return d; |
| 442 |
} |
| 443 |
|
| 444 |
// i32 unboxing slow path |
| 445 |
int32_t CPyLong_AsInt32_(PyObject *o) { |
| 446 |
int overflow; |
| 447 |
long result = PyLong_AsLongAndOverflow(o, &overflow); |
| 448 |
if (result > 0x7fffffffLL || result < -0x80000000LL) { |
| 449 |
overflow = 1; |
| 450 |
result = -1; |
| 451 |
} |
| 452 |
if (result == -1) { |
| 453 |
if (PyErr_Occurred()) { |
| 454 |
return CPY_LL_INT_ERROR; |
| 455 |
} else if (overflow) { |
| 456 |
PyErr_SetString(PyExc_ValueError, "int too large to convert to i32"); |
| 457 |
return CPY_LL_INT_ERROR; |
| 458 |
} |
| 459 |
} |
| 460 |
return result; |
| 461 |
} |
| 462 |
|
| 463 |
int32_t CPyInt32_Divide(int32_t x, int32_t y) { |
| 464 |
if (y == 0) { |
| 465 |
PyErr_SetString(PyExc_ZeroDivisionError, "integer division or modulo by zero"); |
| 466 |
return CPY_LL_INT_ERROR; |
| 467 |
} |
| 468 |
if (y == -1 && x == INT32_MIN) { |
| 469 |
PyErr_SetString(PyExc_OverflowError, "integer division overflow"); |
| 470 |
return CPY_LL_INT_ERROR; |
| 471 |
} |
| 472 |
int32_t d = x / y; |
| 473 |
// Adjust for Python semantics |
| 474 |
if (((x < 0) != (y < 0)) && d * y != x) { |
| 475 |
d--; |
| 476 |
} |
| 477 |
return d; |
| 478 |
} |
| 479 |
|
| 480 |
int32_t CPyInt32_Remainder(int32_t x, int32_t y) { |
| 481 |
if (y == 0) { |
| 482 |
PyErr_SetString(PyExc_ZeroDivisionError, "integer division or modulo by zero"); |
| 483 |
return CPY_LL_INT_ERROR; |
| 484 |
} |
| 485 |
// Edge case: avoid core dump |
| 486 |
if (y == -1 && x == INT32_MIN) { |
| 487 |
return 0; |
| 488 |
} |
| 489 |
int32_t d = x % y; |
| 490 |
// Adjust for Python semantics |
| 491 |
if (((x < 0) != (y < 0)) && d != 0) { |
| 492 |
d += y; |
| 493 |
} |
| 494 |
return d; |
| 495 |
} |
| 496 |
|
| 497 |
void CPyInt32_Overflow() { |
| 498 |
PyErr_SetString(PyExc_ValueError, "int too large to convert to i32"); |
| 499 |
} |
| 500 |
|
| 501 |
// i16 unboxing slow path |
| 502 |
int16_t CPyLong_AsInt16_(PyObject *o) { |
| 503 |
int overflow; |
| 504 |
long result = PyLong_AsLongAndOverflow(o, &overflow); |
| 505 |
if (result > 0x7fff || result < -0x8000) { |
| 506 |
overflow = 1; |
| 507 |
result = -1; |
| 508 |
} |
| 509 |
if (result == -1) { |
| 510 |
if (PyErr_Occurred()) { |
| 511 |
return CPY_LL_INT_ERROR; |
| 512 |
} else if (overflow) { |
| 513 |
PyErr_SetString(PyExc_ValueError, "int too large to convert to i16"); |
| 514 |
return CPY_LL_INT_ERROR; |
| 515 |
} |
| 516 |
} |
| 517 |
return result; |
| 518 |
} |
| 519 |
|
| 520 |
int16_t CPyInt16_Divide(int16_t x, int16_t y) { |
| 521 |
if (y == 0) { |
| 522 |
PyErr_SetString(PyExc_ZeroDivisionError, "integer division or modulo by zero"); |
| 523 |
return CPY_LL_INT_ERROR; |
| 524 |
} |
| 525 |
if (y == -1 && x == INT16_MIN) { |
| 526 |
PyErr_SetString(PyExc_OverflowError, "integer division overflow"); |
| 527 |
return CPY_LL_INT_ERROR; |
| 528 |
} |
| 529 |
int16_t d = x / y; |
| 530 |
// Adjust for Python semantics |
| 531 |
if (((x < 0) != (y < 0)) && d * y != x) { |
| 532 |
d--; |
| 533 |
} |
| 534 |
return d; |
| 535 |
} |
| 536 |
|
| 537 |
int16_t CPyInt16_Remainder(int16_t x, int16_t y) { |
| 538 |
if (y == 0) { |
| 539 |
PyErr_SetString(PyExc_ZeroDivisionError, "integer division or modulo by zero"); |
| 540 |
return CPY_LL_INT_ERROR; |
| 541 |
} |
| 542 |
// Edge case: avoid core dump |
| 543 |
if (y == -1 && x == INT16_MIN) { |
| 544 |
return 0; |
| 545 |
} |
| 546 |
int16_t d = x % y; |
| 547 |
// Adjust for Python semantics |
| 548 |
if (((x < 0) != (y < 0)) && d != 0) { |
| 549 |
d += y; |
| 550 |
} |
| 551 |
return d; |
| 552 |
} |
| 553 |
|
| 554 |
void CPyInt16_Overflow() { |
| 555 |
PyErr_SetString(PyExc_ValueError, "int too large to convert to i16"); |
| 556 |
} |
| 557 |
|
| 558 |
// u8 unboxing slow path |
| 559 |
uint8_t CPyLong_AsUInt8_(PyObject *o) { |
| 560 |
int overflow; |
| 561 |
long result = PyLong_AsLongAndOverflow(o, &overflow); |
| 562 |
if (result < 0 || result >= 256) { |
| 563 |
overflow = 1; |
| 564 |
result = -1; |
| 565 |
} |
| 566 |
if (result == -1) { |
| 567 |
if (PyErr_Occurred()) { |
| 568 |
return CPY_LL_UINT_ERROR; |
| 569 |
} else if (overflow) { |
| 570 |
PyErr_SetString(PyExc_ValueError, "int too large or small to convert to u8"); |
| 571 |
return CPY_LL_UINT_ERROR; |
| 572 |
} |
| 573 |
} |
| 574 |
return result; |
| 575 |
} |
| 576 |
|
| 577 |
void CPyUInt8_Overflow() { |
| 578 |
PyErr_SetString(PyExc_ValueError, "int too large or small to convert to u8"); |
| 579 |
} |
| 580 |
|
| 581 |
double CPyTagged_TrueDivide(CPyTagged x, CPyTagged y) { |
| 582 |
if (unlikely(y == 0)) { |
| 583 |
PyErr_SetString(PyExc_ZeroDivisionError, "division by zero"); |
| 584 |
return CPY_FLOAT_ERROR; |
| 585 |
} |
| 586 |
if (likely(!CPyTagged_CheckLong(x) && !CPyTagged_CheckLong(y))) { |
| 587 |
return (double)((Py_ssize_t)x >> 1) / (double)((Py_ssize_t)y >> 1); |
| 588 |
} else { |
| 589 |
PyObject *xo = CPyTagged_AsObject(x); |
| 590 |
PyObject *yo = CPyTagged_AsObject(y); |
| 591 |
PyObject *result = PyNumber_TrueDivide(xo, yo); |
| 592 |
if (result == NULL) { |
| 593 |
Py_DECREF(xo); |
| 594 |
Py_DECREF(yo); |
| 595 |
return CPY_FLOAT_ERROR; |
| 596 |
} |
| 597 |
double value = PyFloat_AsDouble(result); |
| 598 |
Py_DECREF(xo); |
| 599 |
Py_DECREF(yo); |
| 600 |
Py_DECREF(result); |
| 601 |
return value; |
| 602 |
} |
| 603 |
return 1.0; |
| 604 |
} |
| 605 |
|
| 606 |
static PyObject *CPyLong_ToBytes(PyObject *v, Py_ssize_t length, int little_endian, int signed_flag) { |
| 607 |
// This is a wrapper for PyLong_AsByteArray and PyBytes_FromStringAndSize |
| 608 |
PyObject *result = PyBytes_FromStringAndSize(NULL, length); |
| 609 |
if (!result) { |
| 610 |
return NULL; |
| 611 |
} |
| 612 |
unsigned char *bytes = (unsigned char *)PyBytes_AS_STRING(result); |
| 613 |
#if PY_VERSION_HEX >= 0x030D0000 // 3.13.0 |
| 614 |
int res = _PyLong_AsByteArray((PyLongObject *)v, bytes, length, little_endian, signed_flag, 1); |
| 615 |
#else |
| 616 |
int res = _PyLong_AsByteArray((PyLongObject *)v, bytes, length, little_endian, signed_flag); |
| 617 |
#endif |
| 618 |
if (res < 0) { |
| 619 |
Py_DECREF(result); |
| 620 |
return NULL; |
| 621 |
} |
| 622 |
return result; |
| 623 |
} |
| 624 |
|
| 625 |
// int.to_bytes(length, byteorder, signed=False) |
| 626 |
PyObject *CPyTagged_ToBytes(CPyTagged self, Py_ssize_t length, PyObject *byteorder, int signed_flag) { |
| 627 |
PyObject *pyint = CPyTagged_AsObject(self); |
| 628 |
if (!PyUnicode_Check(byteorder)) { |
| 629 |
Py_DECREF(pyint); |
| 630 |
PyErr_SetString(PyExc_TypeError, "byteorder must be str"); |
| 631 |
return NULL; |
| 632 |
} |
| 633 |
const char *order = PyUnicode_AsUTF8(byteorder); |
| 634 |
if (!order) { |
| 635 |
Py_DECREF(pyint); |
| 636 |
return NULL; |
| 637 |
} |
| 638 |
int little_endian; |
| 639 |
if (strcmp(order, "big") == 0) { |
| 640 |
little_endian = 0; |
| 641 |
} else if (strcmp(order, "little") == 0) { |
| 642 |
little_endian = 1; |
| 643 |
} else { |
| 644 |
PyErr_SetString(PyExc_ValueError, "byteorder must be either 'little' or 'big'"); |
| 645 |
return NULL; |
| 646 |
} |
| 647 |
PyObject *result = CPyLong_ToBytes(pyint, length, little_endian, signed_flag); |
| 648 |
Py_DECREF(pyint); |
| 649 |
return result; |
| 650 |
} |
| 651 |
|
| 652 |
// int.to_bytes(length, byteorder="little", signed=False) |
| 653 |
PyObject *CPyTagged_ToLittleEndianBytes(CPyTagged self, Py_ssize_t length, int signed_flag) { |
| 654 |
PyObject *pyint = CPyTagged_AsObject(self); |
| 655 |
PyObject *result = CPyLong_ToBytes(pyint, length, 1, signed_flag); |
| 656 |
Py_DECREF(pyint); |
| 657 |
return result; |
| 658 |
} |
| 659 |
|
| 660 |
// int.to_bytes(length, "big", signed=False) |
| 661 |
PyObject *CPyTagged_ToBigEndianBytes(CPyTagged self, Py_ssize_t length, int signed_flag) { |
| 662 |
PyObject *pyint = CPyTagged_AsObject(self); |
| 663 |
PyObject *result = CPyLong_ToBytes(pyint, length, 0, signed_flag); |
| 664 |
Py_DECREF(pyint); |
| 665 |
return result; |
| 666 |
} |
| 667 |
|
| 668 |
// int.bit_length() |
| 669 |
CPyTagged CPyTagged_BitLength(CPyTagged self) { |
| 670 |
// Handle zero |
| 671 |
if (self == 0) { |
| 672 |
return 0; |
| 673 |
} |
| 674 |
|
| 675 |
// Fast path for small (tagged) ints |
| 676 |
if (CPyTagged_CheckShort(self)) { |
| 677 |
Py_ssize_t val = CPyTagged_ShortAsSsize_t(self); |
| 678 |
Py_ssize_t absval = val < 0 ? -val : val; |
| 679 |
int bits = 0; |
| 680 |
if (absval) { |
| 681 |
#if defined(_MSC_VER) |
| 682 |
#if defined(_WIN64) |
| 683 |
unsigned long idx; |
| 684 |
if (_BitScanReverse64(&idx, (unsigned __int64)absval)) { |
| 685 |
bits = (int)(idx + 1); |
| 686 |
} |
| 687 |
#else |
| 688 |
unsigned long idx; |
| 689 |
if (_BitScanReverse(&idx, (unsigned long)absval)) { |
| 690 |
bits = (int)(idx + 1); |
| 691 |
} |
| 692 |
#endif |
| 693 |
#elif defined(__GNUC__) || defined(__clang__) |
| 694 |
bits = (int)(CPY_BITS - CPY_CLZ(absval)); |
| 695 |
#else |
| 696 |
// Fallback to loop if no builtin |
| 697 |
while (absval) { |
| 698 |
absval >>= 1; |
| 699 |
bits++; |
| 700 |
} |
| 701 |
#endif |
| 702 |
} |
| 703 |
return bits << 1; |
| 704 |
} |
| 705 |
|
| 706 |
// Slow path for big ints |
| 707 |
PyObject *pyint = CPyTagged_AsObject(self); |
| 708 |
int bits = _PyLong_NumBits(pyint); |
| 709 |
Py_DECREF(pyint); |
| 710 |
if (bits < 0) { |
| 711 |
// _PyLong_NumBits sets an error on failure |
| 712 |
return CPY_INT_TAG; |
| 713 |
} |
| 714 |
return bits << 1; |
| 715 |
} |