Skip to content

Commit 1ef4d7e

Browse files
committed
combine string int cast macros to simplify int cast setup
1 parent eebba59 commit 1ef4d7e

File tree

1 file changed

+57
-79
lines changed

1 file changed

+57
-79
lines changed

stringdtype/stringdtype/src/casts.c

Lines changed: 57 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -583,8 +583,8 @@ uint_to_string(unsigned long long in, char *out)
583583
return pylong_to_string(pylong_val, out);
584584
}
585585

586-
#define STRING_TO_INT(typename, typekind, shortname, numpy_tag, printf_code, \
587-
npy_longtype) \
586+
#define STRING_INT_CASTS(typename, typekind, shortname, numpy_tag, \
587+
printf_code, npy_longtype, longtype) \
588588
static NPY_CASTING string_to_##typename##_resolve_descriptors( \
589589
PyObject *NPY_UNUSED(self), \
590590
PyArray_DTypeMeta *NPY_UNUSED(dtypes[2]), \
@@ -645,38 +645,37 @@ uint_to_string(unsigned long long in, char *out)
645645
{NPY_METH_strided_loop, &string_to_##typename}, \
646646
{0, NULL}}; \
647647
\
648-
static char *s2##shortname##_name = "cast_StringDType_to_" #typename;
649-
650-
#define INT_TO_STRING(typename, typekind, shortname, longtype) \
651-
static int typename##_to_string( \
652-
PyArrayMethod_Context *NPY_UNUSED(context), char *const data[], \
653-
npy_intp const dimensions[], npy_intp const strides[], \
654-
NpyAuxData *NPY_UNUSED(auxdata)) \
655-
{ \
656-
npy_intp N = dimensions[0]; \
657-
npy_##typename *in = (npy_##typename *)data[0]; \
658-
char *out = data[1]; \
659-
\
660-
npy_intp in_stride = strides[0] / sizeof(npy_##typename); \
661-
npy_intp out_stride = strides[1]; \
662-
\
663-
while (N--) { \
664-
if (typekind##_to_string((longtype)*in, out) != 0) { \
665-
return -1; \
666-
} \
667-
in += in_stride; \
668-
out += out_stride; \
669-
} \
670-
\
671-
return 0; \
672-
} \
673-
\
674-
static PyType_Slot shortname##2s_slots [] = { \
675-
{NPY_METH_resolve_descriptors, \
676-
&any_to_string_UNSAFE_resolve_descriptors}, \
677-
{NPY_METH_strided_loop, &typename##_to_string}, \
678-
{0, NULL}}; \
679-
\
648+
static char *s2##shortname##_name = "cast_StringDType_to_" #typename; \
649+
\
650+
static int typename##_to_string( \
651+
PyArrayMethod_Context *NPY_UNUSED(context), char *const data[], \
652+
npy_intp const dimensions[], npy_intp const strides[], \
653+
NpyAuxData *NPY_UNUSED(auxdata)) \
654+
{ \
655+
npy_intp N = dimensions[0]; \
656+
npy_##typename *in = (npy_##typename *)data[0]; \
657+
char *out = data[1]; \
658+
\
659+
npy_intp in_stride = strides[0] / sizeof(npy_##typename); \
660+
npy_intp out_stride = strides[1]; \
661+
\
662+
while (N--) { \
663+
if (typekind##_to_string((longtype)*in, out) != 0) { \
664+
return -1; \
665+
} \
666+
in += in_stride; \
667+
out += out_stride; \
668+
} \
669+
\
670+
return 0; \
671+
} \
672+
\
673+
static PyType_Slot shortname##2s_slots [] = { \
674+
{NPY_METH_resolve_descriptors, \
675+
&any_to_string_UNSAFE_resolve_descriptors}, \
676+
{NPY_METH_strided_loop, &typename##_to_string}, \
677+
{0, NULL}}; \
678+
\
680679
static char *shortname##2s_name = "cast_" #typename "_to_StringDType";
681680

682681
#define INT_DTYPES_AND_CAST_SPEC(shortname, typename) \
@@ -695,66 +694,45 @@ uint_to_string(unsigned long long in, char *out)
695694
shortname##2s_name, NPY_UNSAFE_CASTING, NPY_METH_REQUIRES_PYAPI, \
696695
shortname##2s_dtypes, shortname##2s_slots);
697696

698-
STRING_TO_INT(int8, int, i8, NPY_INT8, lli, npy_longlong)
699-
INT_TO_STRING(int8, int, i8, long long)
700-
701-
STRING_TO_INT(int16, int, i16, NPY_INT16, lli, npy_longlong)
702-
INT_TO_STRING(int16, int, i16, long long)
697+
STRING_INT_CASTS(int8, int, i8, NPY_INT8, lli, npy_longlong, long long)
698+
STRING_INT_CASTS(int16, int, i16, NPY_INT16, lli, npy_longlong, long long)
699+
STRING_INT_CASTS(int32, int, i32, NPY_INT32, lli, npy_longlong, long long)
700+
STRING_INT_CASTS(int64, int, i64, NPY_INT64, lli, npy_longlong, long long)
703701

704-
STRING_TO_INT(int32, int, i32, NPY_INT32, lli, npy_longlong)
705-
INT_TO_STRING(int32, int, i32, long long)
706-
707-
STRING_TO_INT(int64, int, i64, NPY_INT64, lli, npy_longlong)
708-
INT_TO_STRING(int64, int, i64, long long)
702+
STRING_INT_CASTS(uint8, uint, u8, NPY_UINT8, llu, npy_ulonglong,
703+
unsigned long long)
704+
STRING_INT_CASTS(uint16, uint, u16, NPY_UINT16, llu, npy_ulonglong,
705+
unsigned long long)
706+
STRING_INT_CASTS(uint32, uint, u32, NPY_UINT32, llu, npy_ulonglong,
707+
unsigned long long)
708+
STRING_INT_CASTS(uint64, uint, u64, NPY_UINT64, llu, npy_ulonglong,
709+
unsigned long long)
709710

710711
#if NPY_SIZEOF_BYTE == NPY_SIZEOF_SHORT
711712
// byte doesn't have a bitsized alias
712-
STRING_TO_INT(byte, int, byte, NPY_BYTE, lli, npy_byte)
713-
INT_TO_STRING(byte, int, byte, long long)
714-
715-
STRING_TO_INT(ubyte, uint, ubyte, NPY_UBYTE, llu, npy_ubyte)
716-
INT_TO_STRING(ubyte, uint, ubyte, unsigned long long)
713+
STRING_INT_CASTS(byte, int, byte, NPY_BYTE, lli, npy_longlong, long long)
714+
STRING_INT_CASTS(ubyte, uint, ubyte, NPY_UBYTE, llu, npy_ulonglong,
715+
unsigned long long)
717716
#endif
718-
719717
#if NPY_SIZEOF_SHORT == NPY_SIZEOF_INT
720718
// short doesn't have a bitsized alias
721-
STRING_TO_INT(short, int, short, NPY_SHORT, lli, npy_short)
722-
INT_TO_STRING(short, int, short, long long)
723-
724-
STRING_TO_INT(ushort, uint, ushort, NPY_USHORT, llu, npy_ushort)
725-
INT_TO_STRING(ushort, uint, ushort, unsigned long long)
719+
STRING_INT_CASTS(short, int, short, NPY_SHORT, lli, npy_longlong, long long)
720+
STRING_INT_CASTS(ushort, uint, ushort, NPY_USHORT, llu, npy_ulonglong,
721+
unsigned long long)
726722
#endif
727-
728723
#if NPY_SIZEOF_INT == NPY_SIZEOF_LONG
729724
// int doesn't have a bitsized alias
730-
STRING_TO_INT(int, int, int, NPY_INT, lli, npy_int)
731-
INT_TO_STRING(int, int, int, long long)
732-
733-
STRING_TO_INT(uint, uint, uint, NPY_UINT, llu, npy_uint)
734-
INT_TO_STRING(uint, uint, uint, unsigned long long)
725+
STRING_INT_CASTS(int, int, int, NPY_INT, lli, npy_longlong, long long)
726+
STRING_INT_CASTS(uint, uint, uint, NPY_UINT, llu, npy_longlong, long long)
735727
#endif
736-
737728
#if NPY_SIZEOF_LONGLONG == NPY_SIZEOF_LONG
738729
// long long doesn't have a bitsized alias
739-
STRING_TO_INT(longlong, int, longlong, NPY_LONGLONG, lli, npy_longlong)
740-
INT_TO_STRING(longlong, int, longlong, long long)
741-
742-
STRING_TO_INT(ulonglong, uint, ulonglong, NPY_ULONGLONG, llu, npy_ulonglong)
743-
INT_TO_STRING(ulonglong, uint, ulonglong, unsigned long long)
730+
STRING_INT_CASTS(longlong, int, longlong, NPY_LONGLONG, lli, npy_longlong,
731+
long long)
732+
STRING_INT_CASTS(ulonglong, uint, ulonglong, NPY_ULONGLONG, llu, npy_ulonglong,
733+
unsigned long long)
744734
#endif
745735

746-
STRING_TO_INT(uint8, uint, u8, NPY_UINT8, llu, npy_ulonglong)
747-
INT_TO_STRING(uint8, uint, u8, unsigned long long)
748-
749-
STRING_TO_INT(uint16, uint, u16, NPY_UINT16, llu, npy_ulonglong)
750-
INT_TO_STRING(uint16, uint, u16, unsigned long long)
751-
752-
STRING_TO_INT(uint32, uint, u32, NPY_UINT32, llu, npy_ulonglong)
753-
INT_TO_STRING(uint32, uint, u32, unsigned long long)
754-
755-
STRING_TO_INT(uint64, uint, u64, NPY_UINT64, llu, npy_ulonglong)
756-
INT_TO_STRING(uint64, uint, u64, unsigned long long)
757-
758736
PyArrayMethod_Spec *
759737
get_cast_spec(const char *name, NPY_CASTING casting,
760738
NPY_ARRAYMETHOD_FLAGS flags, PyArray_DTypeMeta **dtypes,

0 commit comments

Comments
 (0)