8000 MAINT: remove duplicated integer code · numpy/numpy@2f7c97c · GitHub
[go: up one dir, main page]

Skip to content

Commit 2f7c97c

Browse files
committed
MAINT: remove duplicated integer code
Our historical integer names contain a duplicate for example long and longlong are int64 on 64 bit platforms or int and long are int32 on 32 bit platforms. This causes quite significant code bloat, in particular for functions compiled for multiple instruction sets. This duplication can't be removed as it is part of our API but we can avoid some of the bloat by only generating code once for each real type and correctly mapping them to the right slot in the ufuncs type list. This is achieved by defining appropriate macros for the current platform and creating the correct function names via macro concatenation.
1 parent 4497d47 commit 2f7c97c

File tree

3 files changed

+41
-17
lines changed

3 files changed

+41
-17
lines changed

numpy/core/code_generators/generate_umath.py

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -943,22 +943,22 @@ def make_arrays(funcdict):
943943
tname = english_upper(chartoname[t.type])
944944
datalist.append('(void *)NULL')
945945
funclist.append(
946-
'%s_%s_%s_%s' % (tname, t.in_, t.out, name))
946+
'NPY_FUNCNAME(%s,%s_%s_%s)' % (tname, t.in_, t.out, name))
947947
elif isinstance(t.func_data, FuncNameSuffix):
948948
datalist.append('(void *)NULL')
949949
tname = english_upper(chartoname[t.type])
950950
funclist.append(
951-
'%s_%s_%s' % (tname, name, t.func_data.suffix))
951+
'NPY_FUNCNAME(%s,%s_%s)' % (tname, name, t.func_data.suffix))
952952
else:
953953
datalist.append('(void *)NULL')
954954
tname = english_upper(chartoname[t.type])
955-
funclist.append('%s_%s' % (tname, name))
955+
funclist.append('NPY_FUNCNAME(%s,%s)' % (tname, name))
956956
if t.simd is not None:
957957
for vt in t.simd:
958958
code2list.append("""\
959959
#ifdef HAVE_ATTRIBUTE_TARGET_{ISA}
960960
if (NPY_CPU_SUPPORTS_{ISA}) {{
961-
{fname}_functions[{idx}] = {type}_{fname}_{isa};
961+
{fname}_functions[{idx}] = NPY_FUNCNAME({type},{fname}_{isa});
962962
}}
963963
#endif
964964
""".format(ISA=vt.upper(), isa=vt, fname=name, type=tname, idx=k))
@@ -1021,13 +1021,39 @@ def make_code(funcdict, filename):
10211021
code3 = make_ufuncs(funcdict)
10221022
code2 = indent(code2, 4)
10231023
code3 = indent(code3, 4)
1024+
1025+
# map integer names to functions to remove duplication of e.g.
1026+
# long and longlong being equal to int64 on 64 bit architectures
1027+
typedefs = ""
1028+
undefs = ""
1029+
for name in ('BYTE', 'SHORT', 'INT', 'LONG', 'LONGLONG'):
1030+
undefs += """\
1031+
#undef {name}
1032+
#undef U{name}
1033+
""".format(name=name)
1034+
for size in (1, 2, 4, 8):
1035+
typedefs += """\
1036+
#if NPY_SIZEOF_{name} == {size}
1037+
#define {name} INT{bits}
1038+
#define U{name} UINT{bits}
1039+
#endif
1040+
""".format(name=name, size=size, bits=size * 8)
1041+
10241042
code = r"""
10251043
10261044
/** Warning this file is autogenerated!!!
10271045
10281046
Please make changes to the code generator program (%s)
10291047
**/
10301048
1049+
/* concatenate function names from arguments that can be macros */
1050+
#define NPY_FUNCNAME__(type, name) type##name
1051+
#define NPY_FUNCNAME_(type, name) NPY_FUNCNAME__(type, name)
1052+
#define NPY_FUNCNAME(type, name) NPY_FUNCNAME_(type, _##name)
1053+
1054+
/* type macros used to build function integer names */
1055+
%s
1056+
10311057
%s
10321058
10331059
static void
@@ -1037,7 +1063,8 @@ def make_code(funcdict, filename):
10371063
%s
10381064
%s
10391065
}
1040-
""" % (filename, code1, code2, code3)
1066+
1067+
%s""" % (filename, typedefs, code1, code2, code3, undefs)
10411068
return code
10421069

10431070

numpy/core/src/umath/loops.c.src

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -812,13 +812,10 @@ BOOL__ones_like(char **args, npy_intp *dimensions, npy_intp *steps, void *NPY_UN
812812
*/
813813

814814
/**begin repeat
815-
* #TYPE = BYTE, UBYTE, SHORT, USHORT, INT, UINT,
816-
* LONG, ULONG, LONGLONG, ULONGLONG#
817-
* #type = npy_byte, npy_ubyte, npy_short, npy_ushort, npy_int, npy_uint,
818-
* npy_long, npy_ulong, npy_longlong, npy_ulonglong#
819-
* #ftype = npy_float, npy_float, npy_float, npy_float, npy_double, npy_double,
820-
* npy_double, npy_double, npy_double, npy_double#
821-
* #SIGNED = 1, 0, 1, 0, 1, 0, 1, 0, 1, 0#
815+
* #TYPE = INT8, UINT8, INT16, UINT16, INT32, UINT32, INT64, UINT64#
816+
* #type = npy_int8, npy_uint8, npy_int16, npy_uint16, npy_int32, npy_uint32,
817+
* npy_int64, npy_uint64#
818+
* #SIGNED = 1, 0, 1, 0, 1, 0, 1, 0#
822819
*/
823820

824821
#define @TYPE@_floor_divide @TYPE@_divide
@@ -1043,8 +1040,8 @@ NPY_NO_EXPORT void
10431040
/**end repeat**/
10441041

10451042
/**begin repeat
1046-
* #TYPE = BYTE, SHORT, INT, LONG, LONGLONG#
1047-
* #type = npy_byte, npy_short, npy_int, npy_long, npy_longlong#
1043+
* #TYPE = INT8, INT16, INT32, INT64#
1044+
* #type = npy_int8, npy_int16, npy_int32, npy_int64#
10481045
*/
10491046

10501047
NPY_NO_EXPORT NPY_GCC_OPT_3 void
@@ -1111,8 +1108,8 @@ NPY_NO_EXPORT void
11111108
/**end repeat**/
11121109

11131110
/**begin repeat
1114-
* #TYPE = UBYTE, USHORT, UINT, ULONG, ULONGLONG#
1115-
* #type = npy_ubyte, npy_ushort, npy_uint, npy_ulong, npy_ulonglong#
1111+
* #TYPE = UINT8, UINT16, UINT32, UINT64#
1112+
* #type = npy_uint8, npy_uint16, npy_uint32, npy_uint64#
11161113
*/
11171114

11181115
NPY_NO_EXPORT void

numpy/core/src/umath/loops.h.src

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ BOOL__ones_like(char **args, npy_intp *dimensions, npy_intp *steps, void *NPY_UN
4545
*/
4646

4747
/**begin repeat
48-
* #TYPE = BYTE, SHORT, INT, LONG, LONGLONG#
48+
* #TYPE = INT8, INT16, INT32, INT64#
4949
*/
5050

5151
/**begin repeat1

0 commit comments

Comments
 (0)
0