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
6#include <stdexcept>
7#include <cstddef>
8
14namespace ritsuko {
15
16namespace hdf5 {
17
28inline bool exceeds_integer_limit(const H5::IntType& itype, std::size_t precision, bool is_signed) {
29 if (itype.getSign() == H5T_SGN_NONE) {
30 if (is_signed) {
31 return (itype.getPrecision() >= precision); // equality, as one bit of the limiting type is used for the sign.
32 } else {
33 return (itype.getPrecision() > precision);
34 }
35 } else {
36 if (is_signed) {
37 return (itype.getPrecision() > precision);
38 } else {
39 return true;
40 }
41 }
42}
43
55inline bool exceeds_integer_limit(const H5::DataSet& data, std::size_t precision, bool is_signed) {
56 if (data.getTypeClass() != H5T_INTEGER) {
57 return true;
58 }
59 H5::IntType itype(data);
60 return exceeds_integer_limit(itype, precision, is_signed);
61}
62
74inline bool exceeds_integer_limit(const H5::Attribute& attr, std::size_t precision, bool is_signed) {
75 if (attr.getTypeClass() != H5T_INTEGER) {
76 return true;
77 }
78 return exceeds_integer_limit(attr.getIntType(), precision, is_signed);
79}
80
84inline bool exceeds_float_limit_by_integer(const H5::IntType& itype, std::size_t precision) {
85 if (precision >= 64) {
86 return exceeds_integer_limit(itype, 53, true);
87 } else if (precision >= 32) {
88 return exceeds_integer_limit(itype, 24, true);
89 } else {
90 return true;
91 }
92}
93
94inline bool exceeds_float_limit_by_float(const H5::FloatType& ftype, std::size_t precision) {
95 // Only considering IEEE-compatible types here.
96 if (precision >= 64) {
97 return !(
98 ftype == H5::PredType::IEEE_F64LE ||
99 ftype == H5::PredType::IEEE_F64BE ||
100 ftype == H5::PredType::IEEE_F32LE ||
101 ftype == H5::PredType::IEEE_F32BE
102 );
103 } else if (precision >= 32) {
104 return !(
105 ftype == H5::PredType::IEEE_F32LE ||
106 ftype == H5::PredType::IEEE_F32BE
107 );
108 } else {
109 return true;
110 }
111}
137inline bool exceeds_float_limit(const H5::DataSet& data, std::size_t precision) {
138 auto tclass = data.getTypeClass();
139 if (tclass == H5T_INTEGER) {
140 return exceeds_float_limit_by_integer(H5::IntType(data), precision);
141 } else if (tclass == H5T_FLOAT) {
142 return exceeds_float_limit_by_float(H5::FloatType(data), precision);
143 } else {
144 return true;
145 }
146}
147
158inline bool exceeds_float_limit(const H5::Attribute& attr, std::size_t precision) {
159 auto tclass = attr.getTypeClass();
160 if (tclass == H5T_INTEGER) {
161 return exceeds_float_limit_by_integer(attr.getIntType(), precision);
162 } else if (tclass == H5T_FLOAT) {
163 return exceeds_float_limit_by_float(attr.getFloatType(), precision);
164 } else {
165 return true;
166 }
167}
168
169}
170
171}
172
173#endif
bool exceeds_integer_limit(const H5::IntType &itype, std::size_t precision, bool is_signed)
Definition exceeds_limit.hpp:28
bool exceeds_float_limit(const H5::DataSet &data, std::size_t precision)
Definition exceeds_limit.hpp:137
Helper functions for ArtifactDB parsing and validation.
Definition Pointer.hpp:21