Skip to content

Commit 4c406fa

Browse files
committed
Move hdf5 types into another file.
1 parent 61f288f commit 4c406fa

File tree

4 files changed

+209
-200
lines changed

4 files changed

+209
-200
lines changed

include/binsparse/binsparse.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
#include <binsparse/detail/detail.h>
1414
#include <binsparse/error.h>
1515
#include <binsparse/generate.h>
16-
#include <binsparse/hdf5_wrapper.h>
1716
#include <binsparse/matrix.h>
1817
#include <binsparse/matrix_market/matrix_market_inspector.h>
1918
#include <binsparse/matrix_market/matrix_market_read.h>
Lines changed: 208 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,208 @@
1+
/*
2+
* SPDX-FileCopyrightText: 2024 Binsparse Developers
3+
*
4+
* SPDX-License-Identifier: BSD-3-Clause
5+
*/
6+
7+
#pragma once
8+
9+
#include <binsparse/types.h>
10+
#include <hdf5.h>
11+
12+
static inline hid_t bsp_get_hdf5_standard_type(bsp_type_t type) {
13+
if (type == BSP_UINT8) {
14+
return H5T_STD_U8LE;
15+
} else if (type == BSP_UINT16) {
16+
return H5T_STD_U16LE;
17+
} else if (type == BSP_UINT32) {
18+
return H5T_STD_U32LE;
19+
} else if (type == BSP_UINT64) {
20+
return H5T_STD_U64LE;
21+
} else if (type == BSP_INT8) {
22+
return H5T_STD_I8LE;
23+
} else if (type == BSP_INT16) {
24+
return H5T_STD_I16LE;
25+
} else if (type == BSP_INT32) {
26+
return H5T_STD_I32LE;
27+
} else if (type == BSP_INT64) {
28+
return H5T_STD_I64LE;
29+
} else if (type == BSP_FLOAT32) {
30+
return H5T_IEEE_F32LE;
31+
} else if (type == BSP_FLOAT64) {
32+
return H5T_IEEE_F64LE;
33+
} else if (type == BSP_BINT8) {
34+
return H5T_STD_I8LE;
35+
} else {
36+
return H5I_INVALID_HID;
37+
}
38+
}
39+
40+
static inline bsp_type_t bsp_get_bsp_type(hid_t type) {
41+
H5T_class_t cl = H5Tget_class(type);
42+
H5T_order_t order = H5Tget_order(type);
43+
H5T_sign_t sign = H5Tget_sign(type);
44+
size_t size = H5Tget_size(type);
45+
46+
if (cl == H5T_INTEGER) {
47+
if (sign == H5T_SGN_NONE) {
48+
if (size == 1) {
49+
return BSP_UINT8;
50+
} else if (size == 2) {
51+
return BSP_UINT16;
52+
} else if (size == 4) {
53+
return BSP_UINT32;
54+
} else if (size == 8) {
55+
return BSP_UINT64;
56+
} else {
57+
return BSP_INVALID_TYPE;
58+
}
59+
} else /* if (sign == H5T_SGN_2) */ {
60+
if (size == 1) {
61+
return BSP_INT8;
62+
} else if (size == 2) {
63+
return BSP_INT16;
64+
} else if (size == 4) {
65+
return BSP_INT32;
66+
} else if (size == 8) {
67+
return BSP_INT64;
68+
} else {
69+
return BSP_INVALID_TYPE;
70+
}
71+
}
72+
} else if (cl == H5T_FLOAT) {
73+
if (size == 4) {
74+
return BSP_FLOAT32;
75+
} else if (size == 8) {
76+
return BSP_FLOAT64;
77+
} else {
78+
return BSP_INVALID_TYPE;
79+
}
80+
} else {
81+
return BSP_INVALID_TYPE;
82+
}
83+
}
84+
85+
// NOTE: This code is a bit silly, but it seems to be the only
86+
// way to generically determine the HDF5 native types for
87+
// stdint's fixed width integer types.
88+
static inline hid_t bsp_get_hdf5_native_type(bsp_type_t type) {
89+
if (type == BSP_INT8 || type == BSP_BINT8) {
90+
if (sizeof(int8_t) == sizeof(char)) {
91+
return H5T_NATIVE_CHAR;
92+
} else if (sizeof(int8_t) == sizeof(short)) {
93+
return H5T_NATIVE_SHORT;
94+
} else if (sizeof(int8_t) == sizeof(int)) {
95+
return H5T_NATIVE_INT;
96+
} else if (sizeof(int8_t) == sizeof(long)) {
97+
return H5T_NATIVE_LONG;
98+
} else if (sizeof(int8_t) == sizeof(long long)) {
99+
return H5T_NATIVE_LLONG;
100+
} else {
101+
assert(false);
102+
}
103+
} else if (type == BSP_INT16) {
104+
if (sizeof(int16_t) == sizeof(char)) {
105+
return H5T_NATIVE_CHAR;
106+
} else if (sizeof(int16_t) == sizeof(short)) {
107+
return H5T_NATIVE_SHORT;
108+
} else if (sizeof(int16_t) == sizeof(int)) {
109+
return H5T_NATIVE_INT;
110+
} else if (sizeof(int32_t) == sizeof(long)) {
111+
return H5T_NATIVE_LONG;
112+
} else if (sizeof(int64_t) == sizeof(long long)) {
113+
return H5T_NATIVE_LLONG;
114+
} else {
115+
assert(false);
116+
}
117+
} else if (type == BSP_INT32) {
118+
if (sizeof(int32_t) == sizeof(char)) {
119+
return H5T_NATIVE_CHAR;
120+
} else if (sizeof(int32_t) == sizeof(short)) {
121+
return H5T_NATIVE_SHORT;
122+
} else if (sizeof(int32_t) == sizeof(int)) {
123+
return H5T_NATIVE_INT;
124+
} else if (sizeof(int32_t) == sizeof(long)) {
125+
return H5T_NATIVE_LONG;
126+
} else if (sizeof(int32_t) == sizeof(long long)) {
127+
return H5T_NATIVE_LLONG;
128+
} else {
129+
assert(false);
130+
}
131+
} else if (type == BSP_INT64) {
132+
if (sizeof(int64_t) == sizeof(char)) {
133+
return H5T_NATIVE_CHAR;
134+
} else if (sizeof(int64_t) == sizeof(short)) {
135+
return H5T_NATIVE_SHORT;
136+
} else if (sizeof(int64_t) == sizeof(int)) {
137+
return H5T_NATIVE_INT;
138+
} else if (sizeof(int64_t) == sizeof(long)) {
139+
return H5T_NATIVE_LONG;
140+
} else if (sizeof(int64_t) == sizeof(long long)) {
141+
return H5T_NATIVE_LLONG;
142+
} else {
143+
assert(false);
144+
}
145+
} else if (type == BSP_UINT8) {
146+
if (sizeof(uint8_t) == sizeof(unsigned char)) {
147+
return H5T_NATIVE_UCHAR;
148+
} else if (sizeof(uint8_t) == sizeof(unsigned short)) {
149+
return H5T_NATIVE_USHORT;
150+
} else if (sizeof(uint8_t) == sizeof(unsigned int)) {
151+
return H5T_NATIVE_UINT;
152+
} else if (sizeof(uint8_t) == sizeof(unsigned long)) {
153+
return H5T_NATIVE_ULONG;
154+
} else if (sizeof(uint8_t) == sizeof(unsigned long long)) {
155+
return H5T_NATIVE_ULLONG;
156+
} else {
157+
assert(false);
158+
}
159+
} else if (type == BSP_UINT16) {
160+
if (sizeof(uint16_t) == sizeof(unsigned char)) {
161+
return H5T_NATIVE_UCHAR;
162+
} else if (sizeof(uint16_t) == sizeof(unsigned short)) {
163+
return H5T_NATIVE_USHORT;
164+
} else if (sizeof(uint16_t) == sizeof(unsigned int)) {
165+
return H5T_NATIVE_UINT;
166+
} else if (sizeof(uint16_t) == sizeof(unsigned long)) {
167+
return H5T_NATIVE_ULONG;
168+
} else if (sizeof(uint16_t) == sizeof(unsigned long long)) {
169+
return H5T_NATIVE_ULLONG;
170+
} else {
171+
assert(false);
172+
}
173+
} else if (type == BSP_UINT32) {
174+
if (sizeof(uint32_t) == sizeof(unsigned char)) {
175+
return H5T_NATIVE_UCHAR;
176+
} else if (sizeof(uint32_t) == sizeof(unsigned short)) {
177+
return H5T_NATIVE_USHORT;
178+
} else if (sizeof(uint32_t) == sizeof(unsigned int)) {
179+
return H5T_NATIVE_UINT;
180+
} else if (sizeof(uint32_t) == sizeof(unsigned long)) {
181+
return H5T_NATIVE_ULONG;
182+
} else if (sizeof(uint32_t) == sizeof(unsigned long long)) {
183+
return H5T_NATIVE_ULLONG;
184+
} else {
185+
assert(false);
186+
}
187+
} else if (type == BSP_UINT64) {
188+
if (sizeof(uint64_t) == sizeof(unsigned char)) {
189+
return H5T_NATIVE_UCHAR;
190+
} else if (sizeof(uint64_t) == sizeof(unsigned short)) {
191+
return H5T_NATIVE_USHORT;
192+
} else if (sizeof(uint64_t) == sizeof(unsigned int)) {
193+
return H5T_NATIVE_UINT;
194+
} else if (sizeof(uint64_t) == sizeof(unsigned long)) {
195+
return H5T_NATIVE_ULONG;
196+
} else if (sizeof(uint64_t) == sizeof(unsigned long long)) {
197+
return H5T_NATIVE_ULLONG;
198+
} else {
199+
assert(false);
200+
}
201+
} else if (type == BSP_FLOAT32) {
202+
return H5T_NATIVE_FLOAT;
203+
} else if (type == BSP_FLOAT64) {
204+
return H5T_NATIVE_DOUBLE;
205+
} else {
206+
return H5I_INVALID_HID;
207+
}
208+
}

include/binsparse/hdf5_wrapper.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
#include <unistd.h>
1515

16+
#include <binsparse/detail/hdf5_types.h>
1617
#include <binsparse/detail/shm_tools.h>
1718

1819
#if __STDC_VERSION__ >= 201112L

0 commit comments

Comments
 (0)