Line | Count | Source |
1 | | /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * |
2 | | * Copyright by The HDF Group. * |
3 | | * All rights reserved. * |
4 | | * * |
5 | | * This file is part of HDF5. The full HDF5 copyright notice, including * |
6 | | * terms governing use, modification, and redistribution, is contained in * |
7 | | * the LICENSE file, which can be found at the root of the source code * |
8 | | * distribution tree, or in https://www.hdfgroup.org/licenses. * |
9 | | * If you do not have access to either file, you may request a copy from * |
10 | | * help@hdfgroup.org. * |
11 | | * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ |
12 | | |
13 | | /* |
14 | | * Module Info: This module contains the functionality for floating-point |
15 | | * datatypes in the H5T interface. |
16 | | */ |
17 | | |
18 | | #include "H5Tmodule.h" /* This source code file is part of the H5T module */ |
19 | | |
20 | | #include "H5private.h" /*generic functions */ |
21 | | #include "H5Eprivate.h" /*error handling */ |
22 | | #include "H5Iprivate.h" /*ID functions */ |
23 | | #include "H5Tpkg.h" /*data-type functions */ |
24 | | |
25 | | /*------------------------------------------------------------------------- |
26 | | * Function: H5Tget_fields |
27 | | * |
28 | | * Purpose: Returns information about the locations of the various bit |
29 | | * fields of a floating point datatype. The field positions |
30 | | * are bit positions in the significant region of the datatype. |
31 | | * Bits are numbered with the least significant bit number zero. |
32 | | * |
33 | | * Any (or even all) of the arguments can be null pointers. |
34 | | * |
35 | | * Return: Success: Non-negative, field locations and sizes are |
36 | | * returned through the arguments. |
37 | | * |
38 | | * Failure: Negative |
39 | | * |
40 | | *------------------------------------------------------------------------- |
41 | | */ |
42 | | herr_t |
43 | | H5Tget_fields(hid_t type_id, size_t *spos /*out*/, size_t *epos /*out*/, size_t *esize /*out*/, |
44 | | size_t *mpos /*out*/, size_t *msize /*out*/) |
45 | 1.74k | { |
46 | 1.74k | H5T_t *dt; /* Datatype */ |
47 | 1.74k | herr_t ret_value = SUCCEED; /* Return value */ |
48 | | |
49 | 3.49k | FUNC_ENTER_API(FAIL) |
50 | | |
51 | | /* Check args */ |
52 | 3.49k | if (NULL == (dt = (H5T_t *)H5I_object_verify(type_id, H5I_DATATYPE))) |
53 | 0 | HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, FAIL, "not a datatype"); |
54 | 1.74k | while (dt->shared->parent) |
55 | 0 | dt = dt->shared->parent; /*defer to parent*/ |
56 | 1.74k | if (H5T_FLOAT != dt->shared->type) |
57 | 0 | HGOTO_ERROR(H5E_DATATYPE, H5E_BADTYPE, FAIL, "operation not defined for datatype class"); |
58 | | |
59 | | /* Get values */ |
60 | 1.74k | if (spos) |
61 | 1.74k | *spos = dt->shared->u.atomic.u.f.sign; |
62 | 1.74k | if (epos) |
63 | 1.74k | *epos = dt->shared->u.atomic.u.f.epos; |
64 | 1.74k | if (esize) |
65 | 1.74k | *esize = dt->shared->u.atomic.u.f.esize; |
66 | 1.74k | if (mpos) |
67 | 1.74k | *mpos = dt->shared->u.atomic.u.f.mpos; |
68 | 1.74k | if (msize) |
69 | 1.74k | *msize = dt->shared->u.atomic.u.f.msize; |
70 | | |
71 | 1.74k | done: |
72 | 1.74k | FUNC_LEAVE_API(ret_value) |
73 | 1.74k | } /* end H5Tget_fields() */ |
74 | | |
75 | | /*------------------------------------------------------------------------- |
76 | | * Function: H5Tset_fields |
77 | | * |
78 | | * Purpose: Sets the locations and sizes of the various floating point |
79 | | * bit fields. The field positions are bit positions in the |
80 | | * significant region of the datatype. Bits are numbered with |
81 | | * the least significant bit number zero. |
82 | | * |
83 | | * Fields are not allowed to extend beyond the number of bits of |
84 | | * precision, nor are they allowed to overlap with one another. |
85 | | * |
86 | | * Return: Non-negative on success/Negative on failure |
87 | | * |
88 | | *------------------------------------------------------------------------- |
89 | | */ |
90 | | herr_t |
91 | | H5Tset_fields(hid_t type_id, size_t spos, size_t epos, size_t esize, size_t mpos, size_t msize) |
92 | 0 | { |
93 | 0 | H5T_t *dt; /* Datatype */ |
94 | 0 | herr_t ret_value = SUCCEED; /* Return value */ |
95 | |
|
96 | 0 | FUNC_ENTER_API(FAIL) |
97 | | |
98 | | /* Check args */ |
99 | 0 | if (NULL == (dt = (H5T_t *)H5I_object_verify(type_id, H5I_DATATYPE))) |
100 | 0 | HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, FAIL, "not a datatype"); |
101 | 0 | if (H5T_STATE_TRANSIENT != dt->shared->state) |
102 | 0 | HGOTO_ERROR(H5E_ARGS, H5E_CANTSET, FAIL, "datatype is read-only"); |
103 | 0 | while (dt->shared->parent) |
104 | 0 | dt = dt->shared->parent; /*defer to parent*/ |
105 | 0 | if (H5T_FLOAT != dt->shared->type) |
106 | 0 | HGOTO_ERROR(H5E_DATATYPE, H5E_BADTYPE, FAIL, "operation not defined for datatype class"); |
107 | 0 | if (epos + esize - dt->shared->u.atomic.offset > dt->shared->u.atomic.prec) |
108 | 0 | HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "exponent bit field size/location is invalid"); |
109 | 0 | if (mpos + msize - dt->shared->u.atomic.offset > dt->shared->u.atomic.prec) |
110 | 0 | HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "mantissa bit field size/location is invalid"); |
111 | 0 | if (spos - dt->shared->u.atomic.offset >= dt->shared->u.atomic.prec) |
112 | 0 | HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "sign location is not valid"); |
113 | | |
114 | | /* Check for overlap */ |
115 | 0 | if (spos >= epos && spos < epos + esize) |
116 | 0 | HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "sign bit appears within exponent field"); |
117 | 0 | if (spos >= mpos && spos < mpos + msize) |
118 | 0 | HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "sign bit appears within mantissa field"); |
119 | 0 | if ((mpos < epos && mpos + msize > epos) || (epos < mpos && epos + esize > mpos)) |
120 | 0 | HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "exponent and mantissa fields overlap"); |
121 | | |
122 | | /* Commit */ |
123 | 0 | dt->shared->u.atomic.u.f.sign = spos; |
124 | 0 | dt->shared->u.atomic.u.f.epos = epos; |
125 | 0 | dt->shared->u.atomic.u.f.mpos = mpos; |
126 | 0 | dt->shared->u.atomic.u.f.esize = esize; |
127 | 0 | dt->shared->u.atomic.u.f.msize = msize; |
128 | |
|
129 | 0 | done: |
130 | 0 | FUNC_LEAVE_API(ret_value) |
131 | 0 | } /* end H5Tset_fields() */ |
132 | | |
133 | | /*------------------------------------------------------------------------- |
134 | | * Function: H5Tget_ebias |
135 | | * |
136 | | * Purpose: Retrieves the exponent bias of a floating-point type. |
137 | | * |
138 | | * Return: Success: The bias |
139 | | * |
140 | | * Failure: 0 |
141 | | * |
142 | | *------------------------------------------------------------------------- |
143 | | */ |
144 | | size_t |
145 | | H5Tget_ebias(hid_t type_id) |
146 | 0 | { |
147 | 0 | H5T_t *dt; /* Datatype */ |
148 | 0 | size_t ret_value; /* Return value */ |
149 | |
|
150 | 0 | FUNC_ENTER_API(0) |
151 | | |
152 | | /* Check args */ |
153 | 0 | if (NULL == (dt = (H5T_t *)H5I_object_verify(type_id, H5I_DATATYPE))) |
154 | 0 | HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, 0, "not a datatype"); |
155 | 0 | while (dt->shared->parent) |
156 | 0 | dt = dt->shared->parent; /*defer to parent*/ |
157 | 0 | if (H5T_FLOAT != dt->shared->type) |
158 | 0 | HGOTO_ERROR(H5E_DATATYPE, H5E_BADTYPE, 0, "operation not defined for datatype class"); |
159 | | |
160 | | /* bias */ |
161 | 0 | H5_CHECKED_ASSIGN(ret_value, size_t, dt->shared->u.atomic.u.f.ebias, uint64_t); |
162 | |
|
163 | 0 | done: |
164 | 0 | FUNC_LEAVE_API(ret_value) |
165 | 0 | } /* end H5Tget_ebias() */ |
166 | | |
167 | | /*------------------------------------------------------------------------- |
168 | | * Function: H5Tset_ebias |
169 | | * |
170 | | * Purpose: Sets the exponent bias of a floating-point type. |
171 | | * |
172 | | * Return: Non-negative on success/Negative on failure |
173 | | * |
174 | | *------------------------------------------------------------------------- |
175 | | */ |
176 | | herr_t |
177 | | H5Tset_ebias(hid_t type_id, size_t ebias) |
178 | 0 | { |
179 | 0 | H5T_t *dt; /* Datatype */ |
180 | 0 | herr_t ret_value = SUCCEED; /* Return value */ |
181 | |
|
182 | 0 | FUNC_ENTER_API(FAIL) |
183 | | |
184 | | /* Check args */ |
185 | 0 | if (NULL == (dt = (H5T_t *)H5I_object_verify(type_id, H5I_DATATYPE))) |
186 | 0 | HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, FAIL, "not a datatype"); |
187 | 0 | if (H5T_STATE_TRANSIENT != dt->shared->state) |
188 | 0 | HGOTO_ERROR(H5E_ARGS, H5E_CANTSET, FAIL, "datatype is read-only"); |
189 | 0 | while (dt->shared->parent) |
190 | 0 | dt = dt->shared->parent; /*defer to parent*/ |
191 | 0 | if (H5T_FLOAT != dt->shared->type) |
192 | 0 | HGOTO_ERROR(H5E_DATATYPE, H5E_BADTYPE, FAIL, "operation not defined for datatype class"); |
193 | | |
194 | | /* Commit */ |
195 | 0 | dt->shared->u.atomic.u.f.ebias = ebias; |
196 | |
|
197 | 0 | done: |
198 | 0 | FUNC_LEAVE_API(ret_value) |
199 | 0 | } /* end H5Tset_ebias() */ |
200 | | |
201 | | /*------------------------------------------------------------------------- |
202 | | * Function: H5Tget_norm |
203 | | * |
204 | | * Purpose: Returns the mantisssa normalization of a floating-point data |
205 | | * type. |
206 | | * |
207 | | * Return: Success: Normalization ID |
208 | | * |
209 | | * Failure: H5T_NORM_ERROR (Negative) |
210 | | * |
211 | | *------------------------------------------------------------------------- |
212 | | */ |
213 | | H5T_norm_t |
214 | | H5Tget_norm(hid_t type_id) |
215 | 0 | { |
216 | 0 | H5T_t *dt; /* Datatype */ |
217 | 0 | H5T_norm_t ret_value; /* Return value */ |
218 | |
|
219 | 0 | FUNC_ENTER_API(H5T_NORM_ERROR) |
220 | | |
221 | | /* Check args */ |
222 | 0 | if (NULL == (dt = (H5T_t *)H5I_object_verify(type_id, H5I_DATATYPE))) |
223 | 0 | HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, H5T_NORM_ERROR, "not a datatype"); |
224 | 0 | while (dt->shared->parent) |
225 | 0 | dt = dt->shared->parent; /*defer to parent*/ |
226 | 0 | if (H5T_FLOAT != dt->shared->type) |
227 | 0 | HGOTO_ERROR(H5E_DATATYPE, H5E_BADTYPE, H5T_NORM_ERROR, "operation not defined for datatype class"); |
228 | | |
229 | | /* norm */ |
230 | 0 | ret_value = dt->shared->u.atomic.u.f.norm; |
231 | |
|
232 | 0 | done: |
233 | 0 | FUNC_LEAVE_API(ret_value) |
234 | 0 | } /* end H5Tget_norm() */ |
235 | | |
236 | | /*------------------------------------------------------------------------- |
237 | | * Function: H5Tset_norm |
238 | | * |
239 | | * Purpose: Sets the mantissa normalization method for a floating point |
240 | | * datatype. |
241 | | * |
242 | | * Return: Non-negative on success/Negative on failure |
243 | | * |
244 | | *------------------------------------------------------------------------- |
245 | | */ |
246 | | herr_t |
247 | | H5Tset_norm(hid_t type_id, H5T_norm_t norm) |
248 | 0 | { |
249 | 0 | H5T_t *dt; /* Datatype */ |
250 | 0 | herr_t ret_value = SUCCEED; /* Return value */ |
251 | |
|
252 | 0 | FUNC_ENTER_API(FAIL) |
253 | | |
254 | | /* Check args */ |
255 | 0 | if (NULL == (dt = (H5T_t *)H5I_object_verify(type_id, H5I_DATATYPE))) |
256 | 0 | HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, FAIL, "not a datatype"); |
257 | 0 | if (H5T_STATE_TRANSIENT != dt->shared->state) |
258 | 0 | HGOTO_ERROR(H5E_ARGS, H5E_CANTSET, FAIL, "datatype is read-only"); |
259 | 0 | if (norm < H5T_NORM_IMPLIED || norm > H5T_NORM_NONE) |
260 | 0 | HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "illegal normalization"); |
261 | 0 | while (dt->shared->parent) |
262 | 0 | dt = dt->shared->parent; /*defer to parent*/ |
263 | 0 | if (H5T_FLOAT != dt->shared->type) |
264 | 0 | HGOTO_ERROR(H5E_DATATYPE, H5E_BADTYPE, FAIL, "operation not defined for datatype class"); |
265 | | |
266 | | /* Commit */ |
267 | 0 | dt->shared->u.atomic.u.f.norm = norm; |
268 | |
|
269 | 0 | done: |
270 | 0 | FUNC_LEAVE_API(ret_value) |
271 | 0 | } /* end H5Tset_norm() */ |
272 | | |
273 | | /*------------------------------------------------------------------------- |
274 | | * Function: H5Tget_inpad |
275 | | * |
276 | | * Purpose: If any internal bits of a floating point type are unused |
277 | | * (that is, those significant bits which are not part of the |
278 | | * sign, exponent, or mantissa) then they will be filled |
279 | | * according to the value of this property. |
280 | | * |
281 | | * Return: Success: The internal padding type. |
282 | | * |
283 | | * Failure: H5T_PAD_ERROR (Negative) |
284 | | * |
285 | | *------------------------------------------------------------------------- |
286 | | */ |
287 | | H5T_pad_t |
288 | | H5Tget_inpad(hid_t type_id) |
289 | 0 | { |
290 | 0 | H5T_t *dt; /* Datatype */ |
291 | 0 | H5T_pad_t ret_value; /* Return value */ |
292 | |
|
293 | 0 | FUNC_ENTER_API(H5T_PAD_ERROR) |
294 | | |
295 | | /* Check args */ |
296 | 0 | if (NULL == (dt = (H5T_t *)H5I_object_verify(type_id, H5I_DATATYPE))) |
297 | 0 | HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, H5T_PAD_ERROR, "not a datatype"); |
298 | 0 | while (dt->shared->parent) |
299 | 0 | dt = dt->shared->parent; /*defer to parent*/ |
300 | 0 | if (H5T_FLOAT != dt->shared->type) |
301 | 0 | HGOTO_ERROR(H5E_DATATYPE, H5E_BADTYPE, H5T_PAD_ERROR, "operation not defined for datatype class"); |
302 | | |
303 | | /* pad */ |
304 | 0 | ret_value = dt->shared->u.atomic.u.f.pad; |
305 | |
|
306 | 0 | done: |
307 | 0 | FUNC_LEAVE_API(ret_value) |
308 | 0 | } /* end H5Tget_inpad() */ |
309 | | |
310 | | /*------------------------------------------------------------------------- |
311 | | * Function: H5Tset_inpad |
312 | | * |
313 | | * Purpose: If any internal bits of a floating point type are unused |
314 | | * (that is, those significant bits which are not part of the |
315 | | * sign, exponent, or mantissa) then they will be filled |
316 | | * according to the value of this property. |
317 | | * |
318 | | * Return: Non-negative on success/Negative on failure |
319 | | * |
320 | | *------------------------------------------------------------------------- |
321 | | */ |
322 | | herr_t |
323 | | H5Tset_inpad(hid_t type_id, H5T_pad_t pad) |
324 | 0 | { |
325 | 0 | H5T_t *dt; /* Datatype */ |
326 | 0 | herr_t ret_value = SUCCEED; /* Return value */ |
327 | |
|
328 | 0 | FUNC_ENTER_API(FAIL) |
329 | | |
330 | | /* Check args */ |
331 | 0 | if (NULL == (dt = (H5T_t *)H5I_object_verify(type_id, H5I_DATATYPE))) |
332 | 0 | HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, FAIL, "not a datatype"); |
333 | 0 | if (H5T_STATE_TRANSIENT != dt->shared->state) |
334 | 0 | HGOTO_ERROR(H5E_ARGS, H5E_CANTSET, FAIL, "datatype is read-only"); |
335 | 0 | if (pad < H5T_PAD_ZERO || pad >= H5T_NPAD) |
336 | 0 | HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "illegal internal pad type"); |
337 | 0 | while (dt->shared->parent) |
338 | 0 | dt = dt->shared->parent; /*defer to parent*/ |
339 | 0 | if (H5T_FLOAT != dt->shared->type) |
340 | 0 | HGOTO_ERROR(H5E_DATATYPE, H5E_BADTYPE, FAIL, "operation not defined for datatype class"); |
341 | | |
342 | | /* Commit */ |
343 | 0 | dt->shared->u.atomic.u.f.pad = pad; |
344 | |
|
345 | 0 | done: |
346 | | FUNC_LEAVE_API(ret_value) |
347 | 0 | } /* end H5Tset_inpad() */ |