Skip to content

Commit c1d7bcc

Browse files
Use compiler's sizeof(type) to determine byte-size of types (#462)
Expanded the test checking consistency between NumPy's and dpctl's notions
1 parent a826e04 commit c1d7bcc

File tree

2 files changed

+39
-24
lines changed

2 files changed

+39
-24
lines changed

dpctl/tensor/_types.pxi

Lines changed: 37 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -36,35 +36,17 @@ cdef int UAR_CDOUBLE = 15
3636
cdef int UAR_TYPE_SENTINEL = 17
3737
cdef int UAR_HALF = 23
3838

39-
cdef str _make_typestr(int typenum):
40-
"""
41-
Make typestring from type number
42-
"""
43-
cdef type_to_str = ['|b1', '|i1', '|u1', '|i2', '|u2',
44-
'|i4', '|u4', '|i4', '|u4', '|i8', '|u8',
45-
'|f4', '|f8', '', '|c8', '|c16', '']
46-
47-
if (typenum < 0):
48-
return ""
49-
if (typenum > 16):
50-
if (typenum == 23):
51-
return "|f2"
52-
return ""
53-
54-
return type_to_str[typenum]
55-
56-
5739
cdef int type_bytesize(int typenum):
5840
"""
5941
NPY_BOOL=0 : 1
6042
NPY_BYTE=1 : 1
6143
NPY_UBYTE=2 : 1
6244
NPY_SHORT=3 : 2
6345
NPY_USHORT=4 : 2
64-
NPY_INT=5 : 4
65-
NPY_UINT=6 : 4
66-
NPY_LONG=7 : 4
67-
NPY_ULONG=8 : 4
46+
NPY_INT=5 : sizeof(int)
47+
NPY_UINT=6 : sizeof(unsigned int)
48+
NPY_LONG=7 : sizeof(long)
49+
NPY_ULONG=8 : sizeof(unsigned long)
6850
NPY_LONGLONG=9 : 8
6951
NPY_ULONGLONG=10 : 8
7052
NPY_FLOAT=11 : 4
@@ -76,7 +58,21 @@ cdef int type_bytesize(int typenum):
7658
NPY_HALF=23 : 2
7759
"""
7860
cdef int *type_to_bytesize = [
79-
1, 1, 1, 2, 2, 4, 4, 4, 4, 8, 8, 4, 8, -1, 8, 16, -1]
61+
1,
62+
sizeof(char),
63+
sizeof(unsigned char),
64+
sizeof(short),
65+
sizeof(unsigned short),
66+
sizeof(int),
67+
sizeof(unsigned int),
68+
sizeof(long),
69+
sizeof(unsigned long),
70+
sizeof(long long),
71+
sizeof(unsigned long long),
72+
sizeof(float),
73+
sizeof(double), -1,
74+
sizeof(float complex),
75+
sizeof(double complex), -1]
8076

8177
if typenum < 0:
8278
return -1
@@ -88,6 +84,24 @@ cdef int type_bytesize(int typenum):
8884
return type_to_bytesize[typenum]
8985

9086

87+
cdef str _make_typestr(int typenum):
88+
"""
89+
Make typestring from type number
90+
"""
91+
cdef type_to_str = ["|b", "|i", "|u", "|i", "|u",
92+
"|i", "|u", "|i", "|u", "|i", "|u",
93+
"|f", "|f", "", "|c", "|c", ""]
94+
95+
if (typenum < 0):
96+
return ""
97+
if (typenum > 16):
98+
if (typenum == 23):
99+
return "|f2"
100+
return ""
101+
102+
return type_to_str[typenum] + str(type_bytesize(typenum))
103+
104+
91105
cdef int typenum_from_format(str s) except *:
92106
"""
93107
Internal utility to convert string describing type format

dpctl/tests/test_usm_ndarray_ctor.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,8 @@ def test_allocate_usm_ndarray(shape, usm_type):
7777
],
7878
)
7979
def test_dtypes(dtype):
80-
dpt.usm_ndarray((1,), dtype=dtype)
80+
Xusm = dpt.usm_ndarray((1,), dtype=dtype)
81+
assert Xusm.itemsize == np.dtype(dtype).itemsize
8182

8283

8384
def test_properties():

0 commit comments

Comments
 (0)