1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
|
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* Copyright by The HDF Group. *
* All rights reserved. *
* *
* This file is part of HDF5. The full HDF5 copyright notice, including *
* terms governing use, modification, and redistribution, is contained in *
* the COPYING file, which can be found at the root of the source code *
* distribution tree, or in https://www.hdfgroup.org/licenses. *
* If you do not have access to either file, you may request a copy from *
* help@hdfgroup.org. *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/*
* Purpose: Initialize native floating-point datatypes
*/
/****************/
/* Module Setup */
/****************/
#include "H5Tmodule.h" /* This source code file is part of the H5T module */
/***********/
/* Headers */
/***********/
#include "H5private.h" /* Generic Functions */
#include "H5Eprivate.h" /* Error handling */
#include "H5FLprivate.h" /* Free Lists */
#include "H5Iprivate.h" /* IDs */
#include "H5Tpkg.h" /* Datatypes */
/****************/
/* Local Macros */
/****************/
/* This could also go in H5private.h, but this is the only place we
* need to turn off the sanitizers and we don't want to encourage
* this.
*/
#if defined(__has_attribute)
#if __has_attribute(no_sanitize)
#define H5_NO_UBSAN __attribute__((no_sanitize("undefined")))
#else
#define H5_NO_UBSAN
#endif
#else
#define H5_NO_UBSAN
#endif
/*-------------------------------------------------------------------------
* Function: DETECT_F
*
* Purpose: This macro takes a floating point type like `double' and
* a base name like `natd' and detects byte order, mantissa
* location, exponent location, sign bit location, presence or
* absence of implicit mantissa bit, and exponent bias and
* initializes a detected_t structure with those properties.
*-------------------------------------------------------------------------
*/
#define DETECT_F(TYPE, VAR, INFO) \
do { \
TYPE _v1, _v2, _v3; \
unsigned char _buf1[sizeof(TYPE)], _buf3[sizeof(TYPE)]; \
unsigned char _pad_mask[sizeof(TYPE)]; \
unsigned char _byte_mask; \
int _i, _j, _last = (-1); \
\
memset(&INFO, 0, sizeof(INFO)); \
INFO.size = sizeof(TYPE); \
\
/* Initialize padding mask */ \
memset(_pad_mask, 0, sizeof(_pad_mask)); \
\
/* Padding bits. Set a variable to 4.0, then flip each bit and see if \
* the modified variable is equal ("==") to the original. Build a \
* padding bitmask to indicate which bits in the type are padding (i.e. \
* have no effect on the value and should be ignored by subsequent \
* steps). This is necessary because padding bits can change arbitrarily \
* and interfere with detection of the various properties below unless we \
* know to ignore them. */ \
_v1 = (TYPE)4.0L; \
H5MM_memcpy(_buf1, (const void *)&_v1, sizeof(TYPE)); \
for (_i = 0; _i < (int)sizeof(TYPE); _i++) \
for (_byte_mask = (unsigned char)1; _byte_mask; _byte_mask = (unsigned char)(_byte_mask << 1)) { \
_buf1[_i] ^= _byte_mask; \
H5MM_memcpy((void *)&_v2, (const void *)_buf1, sizeof(TYPE)); \
H5_GCC_CLANG_DIAG_OFF("float-equal") \
if (_v1 != _v2) \
_pad_mask[_i] |= _byte_mask; \
H5_GCC_CLANG_DIAG_ON("float-equal") \
_buf1[_i] ^= _byte_mask; \
} \
\
/* Byte Order */ \
for (_i = 0, _v1 = (TYPE)0.0L, _v2 = (TYPE)1.0L; _i < (int)sizeof(TYPE); _i++) { \
_v3 = _v1; \
_v1 += _v2; \
_v2 /= (TYPE)256.0L; \
H5MM_memcpy(_buf1, (const void *)&_v1, sizeof(TYPE)); \
H5MM_memcpy(_buf3, (const void *)&_v3, sizeof(TYPE)); \
_j = H5T__byte_cmp(sizeof(TYPE), _buf3, _buf1, _pad_mask); \
if (_j >= 0) { \
INFO.perm[_i] = _j; \
_last = _i; \
} \
} \
if (H5T__fix_order(sizeof(TYPE), _last, INFO.perm, &INFO.order) < 0) \
HGOTO_ERROR(H5E_DATATYPE, H5E_CANTINIT, FAIL, "failed to detect byte order"); \
\
/* Implicit mantissa bit */ \
_v1 = (TYPE)0.5L; \
_v2 = (TYPE)1.0L; \
if (H5T__imp_bit(sizeof(TYPE), INFO.perm, &_v1, &_v2, _pad_mask, &(INFO.imp)) < 0) \
HGOTO_ERROR(H5E_DATATYPE, H5E_CANTINIT, FAIL, "failed to determine implicit bit"); \
INFO.norm = INFO.imp ? H5T_NORM_IMPLIED : H5T_NORM_NONE; \
\
/* Sign bit */ \
_v1 = (TYPE)1.0L; \
_v2 = (TYPE)-1.0L; \
if (H5T__bit_cmp(sizeof(TYPE), INFO.perm, &_v1, &_v2, _pad_mask, &(INFO.sign)) < 0) \
HGOTO_ERROR(H5E_DATATYPE, H5E_CANTINIT, FAIL, "failed to detect byte order"); \
\
/* Mantissa */ \
INFO.mpos = 0; \
\
_v1 = (TYPE)1.0L; \
_v2 = (TYPE)1.5L; \
if (H5T__bit_cmp(sizeof(TYPE), INFO.perm, &_v1, &_v2, _pad_mask, &(INFO.msize)) < 0) \
HGOTO_ERROR(H5E_DATATYPE, H5E_CANTINIT, FAIL, "failed to detect byte order"); \
INFO.msize += 1 + (unsigned)(INFO.imp ? 0 : 1) - INFO.mpos; \
\
/* Exponent */ \
INFO.epos = INFO.mpos + INFO.msize; \
\
INFO.esize = INFO.sign - INFO.epos; \
\
_v1 = (TYPE)1.0L; \
INFO.ebias = H5T__find_bias(INFO.epos, INFO.esize, INFO.perm, &_v1); \
H5T__set_precision(&(INFO)); \
COMP_ALIGNMENT(TYPE, INFO.comp_align); \
} while (0)
/* Detect alignment for C structure */
#define COMP_ALIGNMENT(TYPE, COMP_ALIGN) \
{ \
struct { \
char c; \
TYPE x; \
} s; \
\
COMP_ALIGN = (unsigned)((char *)(&(s.x)) - (char *)(&s)); \
}
/******************/
/* Local Typedefs */
/******************/
/* Holds detected information about a native floating-point type */
typedef struct H5T_fpoint_det_t {
unsigned size; /* Total byte size */
unsigned prec; /* Meaningful bits */
unsigned offset; /* Bit offset to meaningful bits */
int perm[32]; /* For detection of byte order */
H5T_order_t order; /* byte order */
unsigned sign; /* Location of sign bit */
unsigned mpos, msize, imp; /* Information about mantissa */
H5T_norm_t norm; /* Information about mantissa */
unsigned epos, esize; /* Information about exponent */
unsigned long ebias; /* Exponent bias for floating point */
unsigned comp_align; /* Alignment for structure */
} H5T_fpoint_det_t;
/********************/
/* Package Typedefs */
/********************/
/********************/
/* Local Prototypes */
/********************/
/********************/
/* Public Variables */
/********************/
/*****************************/
/* Library Private Variables */
/*****************************/
/*********************/
/* Package Variables */
/*********************/
/*******************/
/* Local Variables */
/*******************/
/* Functions used in the DETECT_F() macro */
static int H5T__byte_cmp(int, const void *, const void *, const unsigned char *);
static herr_t H5T__bit_cmp(unsigned, int *, void *, void *, const unsigned char *, unsigned *);
static herr_t H5T__fix_order(int, int, int *, H5T_order_t *);
static herr_t H5T__imp_bit(unsigned, int *, void *, void *, const unsigned char *, unsigned *);
static unsigned H5T__find_bias(unsigned, unsigned, int *, void *);
static void H5T__set_precision(H5T_fpoint_det_t *);
/*-------------------------------------------------------------------------
* Function: H5T__byte_cmp
*
* Purpose: Compares two chunks of memory A and B and returns the
* byte index into those arrays of the first byte that
* differs between A and B. Ignores differences where the
* corresponding bit in pad_mask is set to 0.
*
* Return: Success: Index of differing byte.
* Failure: -1 if all bytes are the same.
*-------------------------------------------------------------------------
*/
static int
H5T__byte_cmp(int n, const void *_a, const void *_b, const unsigned char *pad_mask)
{
const unsigned char *a = (const unsigned char *)_a;
const unsigned char *b = (const unsigned char *)_b;
int ret_value = -1;
FUNC_ENTER_PACKAGE_NOERR
for (int i = 0; i < n; i++)
if ((a[i] & pad_mask[i]) != (b[i] & pad_mask[i]))
HGOTO_DONE(i);
done:
FUNC_LEAVE_NOAPI(ret_value)
}
/*-------------------------------------------------------------------------
* Function: H5T__bit_cmp
*
* Purpose: Compares two bit vectors and returns the index for the
* first bit that differs between the two vectors. The
* size of the vector is NBYTES. PERM is a mapping from
* actual order to little endian. Ignores differences where
* the corresponding bit in pad_mask is set to 0.
*
* Sets `first` to the index of the first differing bit
*
* Return: SUCCEED/FAIL
*-------------------------------------------------------------------------
*/
static herr_t
H5T__bit_cmp(unsigned nbytes, int *perm, void *_a, void *_b, const unsigned char *pad_mask, unsigned *first)
{
unsigned char *a = (unsigned char *)_a;
unsigned char *b = (unsigned char *)_b;
unsigned char aa, bb;
herr_t ret_value = SUCCEED;
FUNC_ENTER_PACKAGE
*first = 0;
for (unsigned i = 0; i < nbytes; i++) {
if (perm[i] >= (int)nbytes)
HGOTO_ERROR(H5E_DATATYPE, H5E_CANTINIT, FAIL, "failure in bit comparison");
if ((aa = (unsigned char)(a[perm[i]] & pad_mask[perm[i]])) !=
(bb = (unsigned char)(b[perm[i]] & pad_mask[perm[i]]))) {
for (unsigned j = 0; j < 8; j++, aa >>= 1, bb >>= 1) {
if ((aa & 1) != (bb & 1)) {
*first = i * 8 + j;
HGOTO_DONE(SUCCEED);
}
}
}
}
/* If we got here and didn't set a value, error out */
HGOTO_ERROR(H5E_DATATYPE, H5E_CANTINIT, FAIL, "didn't find a value for `first`");
done:
FUNC_LEAVE_NOAPI(ret_value)
}
/*-------------------------------------------------------------------------
* Function: H5T__fix_order
*
* Purpose: Given an array PERM with elements FIRST through LAST
* initialized with zero origin byte numbers, this function
* creates a permutation vector that maps the actual order
* of a floating point number to little-endian.
*
* This function assumes that the mantissa byte ordering
* implies the total ordering.
*
* Return: SUCCEED/FAIL
*-------------------------------------------------------------------------
*/
static herr_t
H5T__fix_order(int n, int last, int *perm, H5T_order_t *order)
{
herr_t ret_value = SUCCEED;
FUNC_ENTER_PACKAGE
if (last <= 0)
HGOTO_ERROR(H5E_DATATYPE, H5E_CANTINIT, FAIL, "failed to detect byte order");
/* We have at least three points to consider */
if (perm[last] < perm[last - 1] && perm[last - 1] < perm[last - 2]) {
/* Little endian */
*order = H5T_ORDER_LE;
for (int i = 0; i < n; i++)
perm[i] = i;
}
else if (perm[last] > perm[last - 1] && perm[last - 1] > perm[last - 2]) {
/* Big endian */
*order = H5T_ORDER_BE;
for (int i = 0; i < n; i++)
perm[i] = (n - 1) - i;
}
else {
/* Undetermined endianness - defaults to 'VAX' for historical
* reasons, but there are other mixed-endian systems (like ARM
* in rare cases)
*/
if (0 != n % 2)
HGOTO_ERROR(H5E_DATATYPE, H5E_CANTINIT, FAIL, "n is not a power of 2");
*order = H5T_ORDER_VAX;
for (int i = 0; i < n; i += 2) {
perm[i] = (n - 2) - i;
perm[i + 1] = (n - 1) - i;
}
}
done:
FUNC_LEAVE_NOAPI(ret_value)
}
/*-------------------------------------------------------------------------
* Function: H5T__imp_bit
*
* Purpose: Looks for an implicit bit in the mantissa. The value
* of _A should be 1.0 and the value of _B should be 0.5.
* Some floating-point formats discard the most significant
* bit of the mantissa after normalizing since it will always
* be a one (except for 0.0). If this is true for the native
* floating point values stored in _A and _B then the function
* returns non-zero.
*
* This function assumes that the exponent occupies higher
* order bits than the mantissa and that the most significant
* bit of the mantissa is next to the least significant bit
* of the exponent.
*
*
* Return: imp_bit will be set to 1 if the most significant bit
* of the mantissa is discarded (ie, the mantissa has an
* implicit `one' as the most significant bit). Otherwise,
* imp_bit will be set to zero zero.
*
* SUCCEED/FAIL
*-------------------------------------------------------------------------
*/
static herr_t
H5T__imp_bit(unsigned n, int *perm, void *_a, void *_b, const unsigned char *pad_mask, unsigned *imp_bit)
{
unsigned char *a = (unsigned char *)_a;
unsigned char *b = (unsigned char *)_b;
unsigned changed;
unsigned major;
unsigned minor;
unsigned msmb; /* Most significant mantissa bit */
herr_t ret_value = SUCCEED;
FUNC_ENTER_PACKAGE
/* Look for the least significant bit that has changed between
* A and B. This is the least significant bit of the exponent.
*/
if (H5T__bit_cmp(n, perm, a, b, pad_mask, &changed) < 0)
HGOTO_ERROR(H5E_DATATYPE, H5E_CANTINIT, FAIL, "couldn't find LSB");
/* The bit to the right (less significant) of the changed bit should
* be the most significant bit of the mantissa. If it is non-zero
* then the format does not remove the leading `1' of the mantissa.
*/
msmb = changed - 1;
major = msmb / 8;
minor = msmb % 8;
*imp_bit = (a[perm[major]] >> minor) & 0x01 ? 0 : 1;
done:
FUNC_LEAVE_NOAPI(ret_value)
}
/*-------------------------------------------------------------------------
* Function: find_bias
*
* Purpose: Determines the bias of the exponent. This function should
* be called with _A having a value of `1'.
*
* Return: The exponent bias
*-------------------------------------------------------------------------
*/
H5_ATTR_PURE static unsigned
H5T__find_bias(unsigned epos, unsigned esize, int *perm, void *_a)
{
unsigned char *a = (unsigned char *)_a;
unsigned char mask;
unsigned b, shift = 0, nbits, bias = 0;
FUNC_ENTER_PACKAGE_NOERR
while (esize > 0) {
nbits = MIN(esize, (8 - epos % 8));
mask = (unsigned char)((1 << nbits) - 1);
b = (unsigned)(a[perm[epos / 8]] >> (epos % 8)) & mask;
bias |= b << shift;
shift += nbits;
esize -= nbits;
epos += nbits;
}
FUNC_LEAVE_NOAPI(bias)
}
/*-------------------------------------------------------------------------
* Function: H5T__set_precision
*
* Purpose: Determine the precision and offset
*
* Return: void
*-------------------------------------------------------------------------
*/
static void
H5T__set_precision(H5T_fpoint_det_t *d)
{
FUNC_ENTER_PACKAGE_NOERR
d->offset = MIN3(d->mpos, d->epos, d->sign);
d->prec = d->msize + d->esize + 1;
FUNC_LEAVE_NOAPI_VOID
}
/*-------------------------------------------------------------------------
* Function: H5T__init_native_float_types
*
* Purpose: Initialize native floating-point datatypes
*
* Return: Success: non-negative
* Failure: negative
*-------------------------------------------------------------------------
*/
herr_t H5_NO_UBSAN
H5T__init_native_float_types(void)
{
H5T_fpoint_det_t det;
H5T_t *dt = NULL;
herr_t ret_value = SUCCEED;
FUNC_ENTER_PACKAGE
/* H5T_NATIVE_FLOAT */
/* Get the type's characteristics */
memset(&det, 0, sizeof(H5T_fpoint_det_t));
DETECT_F(float, FLOAT, det);
/* Allocate and fill type structure */
if (NULL == (dt = H5T__alloc()))
HGOTO_ERROR(H5E_DATATYPE, H5E_NOSPACE, FAIL, "datatype allocation failed");
dt->shared->state = H5T_STATE_IMMUTABLE;
dt->shared->type = H5T_FLOAT;
dt->shared->size = det.size;
dt->shared->u.atomic.order = det.order;
dt->shared->u.atomic.offset = det.offset;
dt->shared->u.atomic.prec = det.prec;
dt->shared->u.atomic.lsb_pad = H5T_PAD_ZERO;
dt->shared->u.atomic.msb_pad = H5T_PAD_ZERO;
dt->shared->u.atomic.u.f.sign = det.sign;
dt->shared->u.atomic.u.f.epos = det.epos;
dt->shared->u.atomic.u.f.esize = det.esize;
dt->shared->u.atomic.u.f.ebias = det.ebias;
dt->shared->u.atomic.u.f.mpos = det.mpos;
dt->shared->u.atomic.u.f.msize = det.msize;
dt->shared->u.atomic.u.f.norm = det.norm;
dt->shared->u.atomic.u.f.pad = H5T_PAD_ZERO;
/* Register the type and set global variables */
if ((H5T_NATIVE_FLOAT_g = H5I_register(H5I_DATATYPE, dt, false)) < 0)
HGOTO_ERROR(H5E_DATATYPE, H5E_CANTINIT, FAIL, "can't register ID for built-in datatype");
H5T_NATIVE_FLOAT_ALIGN_g = det.comp_align;
/* H5T_NATIVE_DOUBLE */
/* Get the type's characteristics */
memset(&det, 0, sizeof(H5T_fpoint_det_t));
DETECT_F(double, DOUBLE, det);
/* Allocate and fill type structure */
if (NULL == (dt = H5T__alloc()))
HGOTO_ERROR(H5E_DATATYPE, H5E_NOSPACE, FAIL, "datatype allocation failed");
dt->shared->state = H5T_STATE_IMMUTABLE;
dt->shared->type = H5T_FLOAT;
dt->shared->size = det.size;
dt->shared->u.atomic.order = det.order;
dt->shared->u.atomic.offset = det.offset;
dt->shared->u.atomic.prec = det.prec;
dt->shared->u.atomic.lsb_pad = H5T_PAD_ZERO;
dt->shared->u.atomic.msb_pad = H5T_PAD_ZERO;
dt->shared->u.atomic.u.f.sign = det.sign;
dt->shared->u.atomic.u.f.epos = det.epos;
dt->shared->u.atomic.u.f.esize = det.esize;
dt->shared->u.atomic.u.f.ebias = det.ebias;
dt->shared->u.atomic.u.f.mpos = det.mpos;
dt->shared->u.atomic.u.f.msize = det.msize;
dt->shared->u.atomic.u.f.norm = det.norm;
dt->shared->u.atomic.u.f.pad = H5T_PAD_ZERO;
/* Register the type and set global variables */
if ((H5T_NATIVE_DOUBLE_g = H5I_register(H5I_DATATYPE, dt, false)) < 0)
HGOTO_ERROR(H5E_DATATYPE, H5E_CANTINIT, FAIL, "can't register ID for built-in datatype");
H5T_NATIVE_DOUBLE_ALIGN_g = det.comp_align;
/* H5T_NATIVE_LDOUBLE */
/* Get the type's characteristics */
memset(&det, 0, sizeof(H5T_fpoint_det_t));
DETECT_F(long double, LDOUBLE, det);
/* Allocate and fill type structure */
if (NULL == (dt = H5T__alloc()))
HGOTO_ERROR(H5E_DATATYPE, H5E_NOSPACE, FAIL, "datatype allocation failed");
dt->shared->state = H5T_STATE_IMMUTABLE;
dt->shared->type = H5T_FLOAT;
dt->shared->size = det.size;
dt->shared->u.atomic.order = det.order;
dt->shared->u.atomic.offset = det.offset;
dt->shared->u.atomic.prec = det.prec;
dt->shared->u.atomic.lsb_pad = H5T_PAD_ZERO;
dt->shared->u.atomic.msb_pad = H5T_PAD_ZERO;
dt->shared->u.atomic.u.f.sign = det.sign;
dt->shared->u.atomic.u.f.epos = det.epos;
dt->shared->u.atomic.u.f.esize = det.esize;
dt->shared->u.atomic.u.f.ebias = det.ebias;
dt->shared->u.atomic.u.f.mpos = det.mpos;
dt->shared->u.atomic.u.f.msize = det.msize;
dt->shared->u.atomic.u.f.norm = det.norm;
dt->shared->u.atomic.u.f.pad = H5T_PAD_ZERO;
/* Register the type and set global variables */
if ((H5T_NATIVE_LDOUBLE_g = H5I_register(H5I_DATATYPE, dt, false)) < 0)
HGOTO_ERROR(H5E_DATATYPE, H5E_CANTINIT, FAIL, "can't register ID for built-in datatype");
H5T_NATIVE_LDOUBLE_ALIGN_g = det.comp_align;
/* Set the platform's alignment (assumes long double's order
* is true for all types)
*/
H5T_native_order_g = det.order;
done:
if (ret_value < 0) {
if (dt != NULL) {
dt->shared = H5FL_FREE(H5T_shared_t, dt->shared);
dt = H5FL_FREE(H5T_t, dt);
}
}
FUNC_LEAVE_NOAPI(ret_value)
} /* end H5T__init_native_float_types() */
|