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
29inline bool exceeds_integer_limit(const H5::IntType& itype, std::size_t precision, bool is_signed) {
30 const auto sign = itype.getSign();
31 if (sign == H5T_SGN_NONE) {
32 if (is_signed) {
33 return (itype.getPrecision() >= precision); // equality, as one bit of the limiting type is used for the sign.
34 } else {
35 return (itype.getPrecision() > precision);
36 }
37 } else if (sign == H5T_SGN_2) {
38 if (is_signed) {
39 return (itype.getPrecision() > precision);
40 } else {
41 return true;
42 }
43 } else {
44 // Some other as-yet-unsupported integer type (1's complement, perhaps?)
45 // so we'll just err on the side of caution and say it doesn't fit in the limiting unsigned or 2's complement integer.
46 return true;
47 }
48}
49
62inline bool exceeds_integer_limit(const H5::DataSet& data, std::size_t precision, bool is_signed) {
63 if (data.getTypeClass() != H5T_INTEGER) {
64 return true;
65 }
66 H5::IntType itype(data);
67 return exceeds_integer_limit(itype, precision, is_signed);
68}
69
82inline bool exceeds_integer_limit(const H5::Attribute& attr, std::size_t precision, bool is_signed) {
83 if (attr.getTypeClass() != H5T_INTEGER) {
84 return true;
85 }
86 return exceeds_integer_limit(attr.getIntType(), precision, is_signed);
87}
88
92inline bool exceeds_float_limit_by_integer(const H5::IntType& itype, std::size_t precision) {
93 if (precision >= 64) {
94 return exceeds_integer_limit(itype, 53, true);
95 } else if (precision >= 32) {
96 return exceeds_integer_limit(itype, 24, true);
97 } else {
98 return true;
99 }
100}
101
102inline bool exceeds_float_limit_by_float(const H5::FloatType& ftype, std::size_t precision) {
103 // Only considering IEEE-compatible types here.
104 if (precision >= 64) {
105 return !(
106 ftype == H5::PredType::IEEE_F64LE ||
107 ftype == H5::PredType::IEEE_F64BE ||
108 ftype == H5::PredType::IEEE_F32LE ||
109 ftype == H5::PredType::IEEE_F32BE
110 );
111 } else if (precision >= 32) {
112 return !(
113 ftype == H5::PredType::IEEE_F32LE ||
114 ftype == H5::PredType::IEEE_F32BE
115 );
116 } else {
117 return true;
118 }
119}
145inline bool exceeds_float_limit(const H5::DataSet& data, std::size_t precision) {
146 auto tclass = data.getTypeClass();
147 if (tclass == H5T_INTEGER) {
148 return exceeds_float_limit_by_integer(H5::IntType(data), precision);
149 } else if (tclass == H5T_FLOAT) {
150 return exceeds_float_limit_by_float(H5::FloatType(data), precision);
151 } else {
152 return true;
153 }
154}
155
166inline bool exceeds_float_limit(const H5::Attribute& attr, std::size_t precision) {
167 auto tclass = attr.getTypeClass();
168 if (tclass == H5T_INTEGER) {
169 return exceeds_float_limit_by_integer(attr.getIntType(), precision);
170 } else if (tclass == H5T_FLOAT) {
171 return exceeds_float_limit_by_float(attr.getFloatType(), precision);
172 } else {
173 return true;
174 }
175}
176
177}
178
179}
180
181#endif
bool exceeds_integer_limit(const H5::IntType &itype, std::size_t precision, bool is_signed)
Definition exceeds_limit.hpp:29
bool exceeds_float_limit(const H5::DataSet &data, std::size_t precision)
Definition exceeds_limit.hpp:145
Helper functions for ArtifactDB parsing and validation.
Definition Pointer.hpp:22