Skip to content

Commit 46878a0

Browse files
committed
lets see the actual size
1 parent 633df7d commit 46878a0

File tree

1 file changed

+50
-4
lines changed

1 file changed

+50
-4
lines changed

quaddtype/numpy_quaddtype/src/umath/binary_ops.cpp

Lines changed: 50 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -344,11 +344,38 @@ quad_ldexp_strided_loop_unaligned(PyArrayMethod_Context *context, char *const da
344344
QuadBackendType backend = descr->backend;
345345
size_t elem_size = (backend == BACKEND_SLEEF) ? sizeof(Sleef_quad) : sizeof(long double);
346346

347+
// Get the actual integer descriptor to determine its size
348+
PyArray_Descr *int_descr = context->descriptors[1];
349+
int int_size = int_descr->elsize;
350+
347351
quad_value in1, out;
348352
int in2;
349353
while (N--) {
350354
memcpy(&in1, in1_ptr, elem_size);
351-
memcpy(&in2, in2_ptr, sizeof(int));
355+
356+
// Read the integer value correctly based on its actual size
357+
// to handle endianness properly
358+
if (int_size == sizeof(npy_int32)) {
359+
npy_int32 temp_int;
360+
memcpy(&temp_int, in2_ptr, sizeof(npy_int32));
361+
in2 = (int)temp_int;
362+
} else if (int_size == sizeof(npy_int64)) {
363+
npy_int64 temp_int;
364+
memcpy(&temp_int, in2_ptr, sizeof(npy_int64));
365+
in2 = (int)temp_int;
366+
} else if (int_size == sizeof(npy_int16)) {
367+
npy_int16 temp_int;
368+
memcpy(&temp_int, in2_ptr, sizeof(npy_int16));
369+
in2 = (int)temp_int;
370+
} else if (int_size == sizeof(npy_int8)) {
371+
npy_int8 temp_int;
372+
memcpy(&temp_int, in2_ptr, sizeof(npy_int8));
373+
in2 = (int)temp_int;
374+
} else {
375+
// Fallback for other sizes
376+
memcpy(&in2, in2_ptr, sizeof(int));
377+
}
378+
352379
if (backend == BACKEND_SLEEF) {
353380
out.sleef_value = sleef_op(&in1.sleef_value, &in2);
354381
} else {
@@ -381,13 +408,32 @@ quad_ldexp_strided_loop_aligned(PyArrayMethod_Context *context, char *const data
381408
QuadPrecDTypeObject *descr = (QuadPrecDTypeObject *)context->descriptors[0];
382409
QuadBackendType backend = descr->backend;
383410

411+
// Get the actual integer descriptor to determine its size
412+
PyArray_Descr *int_descr = context->descriptors[1];
413+
int int_size = int_descr->elsize;
414+
384415
while (N--) {
385-
int *exp = (int *)in2_ptr;
416+
int exp_value;
417+
418+
// Read the integer value correctly based on its actual size
419+
// to handle endianness properly
420+
if (int_size == sizeof(npy_int32)) {
421+
exp_value = (int)(*(npy_int32 *)in2_ptr);
422+
} else if (int_size == sizeof(npy_int64)) {
423+
exp_value = (int)(*(npy_int64 *)in2_ptr);
424+
} else if (int_size == sizeof(npy_int16)) {
425+
exp_value = (int)(*(npy_int16 *)in2_ptr);
426+
} else if (int_size == sizeof(npy_int8)) {
427+
exp_value = (int)(*(npy_int8 *)in2_ptr);
428+
} else {
429+
// Fallback: cast directly (may not work on big-endian)
430+
exp_value = *(int *)in2_ptr;
431+
}
386432

387433
if (backend == BACKEND_SLEEF) {
388-
*(Sleef_quad *)out_ptr = sleef_op((Sleef_quad *)in1_ptr, exp);
434+
*(Sleef_quad *)out_ptr = sleef_op((Sleef_quad *)in1_ptr, &exp_value);
389435
} else {
390-
*(long double *)out_ptr = longdouble_op((long double *)in1_ptr, exp);
436+
*(long double *)out_ptr = longdouble_op((long double *)in1_ptr, &exp_value);
391437
}
392438

393439
in1_ptr += in1_stride;

0 commit comments

Comments
 (0)