diff --git a/src/parallel_io.c b/src/parallel_io.c index cf705eed57d94037bc5a18129181c3c649319f9a..e1a9d2c068040192654a560508b0ede7544f1b3e 100644 --- a/src/parallel_io.c +++ b/src/parallel_io.c @@ -138,7 +138,37 @@ void readArray_chunk(hid_t h_data, hid_t h_plist_id, for (size_t i = 0; i < num_elements; ++i) temp_d[i] *= factor; } else { float* temp_f = (float*)temp; - for (size_t i = 0; i < num_elements; ++i) temp_f[i] *= factor; + +#ifdef SWIFT_DEBUG_CHECKS + float maximum = 0.; + float minimum = FLT_MAX; +#endif + + /* Loop that converts the Units */ + for (size_t i = 0; i < num_elements; ++i) { + +#ifdef SWIFT_DEBUG_CHECKS + /* Find the absolute minimum and maximum values */ + const float abstemp_f = fabsf(temp_f[i]); + if (abstemp_f != 0.f) { + maximum = max(maximum, abstemp_f); + minimum = min(minimum, abstemp_f); + } +#endif + + /* Convert the float units */ + temp_f[i] *= factor; + } + +#ifdef SWIFT_DEBUG_CHECKS + /* The two possible errors: larger than float or smaller + * than float precission. */ + if (factor * maximum > FLT_MAX) { + error("Unit conversion results in numbers larger than floats"); + } else if (factor * minimum < FLT_MIN) { + error("Numbers smaller than float precision"); + } +#endif } } diff --git a/src/serial_io.c b/src/serial_io.c index 147a5e36289ee0109632408de305507fa6d2e99b..8e63f9b72ebe0df5881b1dbf6b43631f9ed79a0e 100644 --- a/src/serial_io.c +++ b/src/serial_io.c @@ -153,7 +153,37 @@ void readArray(hid_t grp, const struct io_props props, size_t N, for (size_t i = 0; i < num_elements; ++i) temp_d[i] *= factor; } else { float* temp_f = temp; - for (size_t i = 0; i < num_elements; ++i) temp_f[i] *= factor; + +#ifdef SWIFT_DEBUG_CHECKS + float maximum = 0.f; + float minimum = FLT_MAX; +#endif + + /* Loop that converts the Units */ + for (size_t i = 0; i < num_elements; ++i) { + +#ifdef SWIFT_DEBUG_CHECKS + /* Find the absolute minimum and maximum values */ + const float abstemp_f = fabsf(temp_f[i]); + if (abstemp_f != 0.f) { + maximum = max(maximum, abstemp_f); + minimum = min(minimum, abstemp_f); + } +#endif + + /* Convert the float units */ + temp_f[i] *= factor; + } + +#ifdef SWIFT_DEBUG_CHECKS + /* The two possible errors: larger than float or smaller + * than float precision. */ + if (factor * maximum > FLT_MAX) { + error("Unit conversion results in numbers larger than floats"); + } else if (factor * minimum < FLT_MIN) { + error("Numbers smaller than float precision"); + } +#endif } } diff --git a/src/single_io.c b/src/single_io.c index 17318d2803c8decd0eb30122322b551007257d08..d0bc8502519a49c0797af1b3dd3fa4f6204a5c66 100644 --- a/src/single_io.c +++ b/src/single_io.c @@ -127,9 +127,40 @@ void readArray(hid_t h_grp, const struct io_props props, size_t N, if (io_is_double_precision(props.type)) { double* temp_d = (double*)temp; for (size_t i = 0; i < num_elements; ++i) temp_d[i] *= unit_factor; + } else { float* temp_f = (float*)temp; - for (size_t i = 0; i < num_elements; ++i) temp_f[i] *= unit_factor; + +#ifdef SWIFT_DEBUG_CHECKS + float maximum = 0.f; + float minimum = FLT_MAX; +#endif + + /* Loop that converts the Units */ + for (size_t i = 0; i < num_elements; ++i) { + +#ifdef SWIFT_DEBUG_CHECKS + /* Find the absolute minimum and maximum values */ + const float abstemp_f = fabsf(temp_f[i]); + if (abstemp_f != 0.f) { + maximum = max(maximum, abstemp_f); + minimum = min(minimum, abstemp_f); + } +#endif + + /* Convert the float units */ + temp_f[i] *= unit_factor; + } + +#ifdef SWIFT_DEBUG_CHECKS + /* The two possible errors: larger than float or smaller + * than float precision. */ + if (unit_factor * maximum > FLT_MAX) { + error("Unit conversion results in numbers larger than floats"); + } else if (unit_factor * minimum < FLT_MIN) { + error("Numbers smaller than float precision"); + } +#endif } }