ritsuko
Helper utilities for ArtifactDB C++ code
Loading...
Searching...
No Matches
exceeds_limit.hpp
Go to the documentation of this file.
1#ifndef RITSUKO_HDF5_FORBID_LARGE_INTEGERS_HPP
2#define RITSUKO_HDF5_FORBID_LARGE_INTEGERS_HPP
3
4#include "H5Cpp.h"
5#include <stdexcept>
6
12namespace ritsuko {
13
14namespace hdf5 {
15
27inline bool exceeds_integer_limit(const H5::IntType& itype, size_t precision, bool is_signed) {
28 if (itype.getSign() == H5T_SGN_NONE) {
29 if (is_signed) {
30 return (itype.getPrecision() >= precision); // equality, as one bit of the limiting type is used for the sign.
31 } else {
32 return (itype.getPrecision() > precision);
33 }
34 } else {
35 if (is_signed) {
36 return (itype.getPrecision() > precision);
37 } else {
38 return true;
39 }
40 }
41}
42
52inline bool exceeds_integer_limit(const H5::DataSet& handle, size_t precision, bool is_signed) {
53 if (handle.getTypeClass() != H5T_INTEGER) {
54 return true;
55 }
56 H5::IntType itype(handle);
57 return exceeds_integer_limit(itype, precision, is_signed);
58}
59
69inline bool exceeds_integer_limit(const H5::Attribute& handle, size_t precision, bool is_signed) {
70 if (handle.getTypeClass() != H5T_INTEGER) {
71 return true;
72 }
73 return exceeds_integer_limit(handle.getIntType(), precision, is_signed);
74}
75
79inline bool exceeds_float_limit_by_integer(const H5::IntType& itype, size_t precision) {
80 if (precision >= 64) {
81 return exceeds_integer_limit(itype, 52, true);
82 } else if (precision >= 32) {
83 return exceeds_integer_limit(itype, 24, true);
84 } else {
85 return true;
86 }
87}
88
89inline bool exceeds_float_limit_by_float(const H5::FloatType& ftype, size_t precision) {
90 // Only considering IEEE-compatible types here.
91 if (precision >= 64) {
92 return !(
93 ftype == H5::PredType::IEEE_F64LE ||
94 ftype == H5::PredType::IEEE_F64BE ||
95 ftype == H5::PredType::IEEE_F32LE ||
96 ftype == H5::PredType::IEEE_F32BE
97 );
98 } else if (precision >= 32) {
99 return !(
100 ftype == H5::PredType::IEEE_F32LE ||
101 ftype == H5::PredType::IEEE_F32BE
102 );
103 } else {
104 return true;
105 }
106}
127inline bool exceeds_float_limit(const H5::DataSet& handle, size_t precision) {
128 auto tclass = handle.getTypeClass();
129 if (tclass == H5T_INTEGER) {
130 return exceeds_float_limit_by_integer(H5::IntType(handle), precision);
131 } else if (tclass == H5T_FLOAT) {
132 return exceeds_float_limit_by_float(H5::FloatType(handle), precision);
133 } else {
134 return true;
135 }
136}
137
147inline bool exceeds_float_limit(const H5::Attribute& handle, size_t precision) {
148 auto tclass = handle.getTypeClass();
149 if (tclass == H5T_INTEGER) {
150 return exceeds_float_limit_by_integer(handle.getIntType(), precision);
151 } else if (tclass == H5T_FLOAT) {
152 return exceeds_float_limit_by_float(handle.getFloatType(), precision);
153 } else {
154 return true;
155 }
156}
157
158}
159
160}
161
162#endif
bool exceeds_integer_limit(const H5::IntType &itype, size_t precision, bool is_signed)
Definition exceeds_limit.hpp:27
bool exceeds_float_limit(const H5::DataSet &handle, size_t precision)
Definition exceeds_limit.hpp:127
Assorted helper functions for parsing and validation.
Definition choose_missing_placeholder.hpp:15