/* Create a HDF5 atomic datatype that represents quad precision floatting point numbers. */ hid_t create_ieee_quadprecision_float(const char *byteorder) { hid_t float_id; if (byteorder == NULL) float_id = H5Tcopy(H5T_NATIVE_DOUBLE); else if (strcmp(byteorder, "little") == 0) float_id = H5Tcopy(H5T_IEEE_F64LE); else float_id = H5Tcopy(H5T_IEEE_F64BE); if (float_id < 0) return float_id; if (H5Tset_size(float_id, 16) < 0) return -1; if ((H5Tset_precision(float_id, 128)) < 0) return -1; if (H5Tset_fields(float_id , 127, 112, 15, 0, 112) < 0) return -1; if (H5Tset_ebias(float_id, 16383) < 0) return -1; return float_id; }
//-------------------------------------------------------------------------- // Function: FloatType::setFields ///\brief Sets locations and sizes of floating point bit fields. ///\param spos - OUT: Sign position, i.e., the bit offset of the /// floating-point sign bit. ///\param epos - OUT: Exponent bit position ///\param esize - OUT: Size of exponent, in bits ///\param mpos - OUT: Mantissa bit-position ///\param msize - OUT: Size of mantissa, in bits ///\exception H5::DataTypeIException // Programmer Binh-Minh Ribler - 2000 //-------------------------------------------------------------------------- void FloatType::setFields(size_t spos, size_t epos, size_t esize, size_t mpos, size_t msize) const { herr_t ret_value = H5Tset_fields(id, spos, epos, esize, mpos, msize); if (ret_value < 0) { throw DataTypeIException("FloatType::setFields", "H5Tset_fields failed"); } }
/* Create a HDF5 atomic datatype that represents half precision floatting point numbers defined by numpy as float16. */ hid_t create_ieee_float16(const char *byteorder) { hid_t float_id; if (byteorder == NULL) float_id = H5Tcopy(H5T_NATIVE_FLOAT); else if (strcmp(byteorder, "little") == 0) float_id = H5Tcopy(H5T_IEEE_F32LE); else float_id = H5Tcopy(H5T_IEEE_F32BE); if (float_id < 0) return float_id; if (H5Tset_fields(float_id, 15, 10, 5, 0, 10) < 0) return -1; if (H5Tset_size(float_id, 2) < 0) return -1; if (H5Tset_ebias(float_id, 15) < 0) return -1; return float_id; }
/*------------------------------------------------------------------------- * Function: create_nbit_dsets_float * * Purpose: Create a dataset of FLOAT datatype with nbit filter * * Return: Success: 0 * Failure: -1 * * Programmer: Raymond Lu * 29 March 2011 * * Modifications: * *------------------------------------------------------------------------- */ int create_nbit_dsets_float(hid_t fid, hid_t fsid, hid_t msid) { hid_t dataset = -1; /* dataset handles */ hid_t datatype = -1; hid_t dcpl = -1; size_t precision, offset; float data[NX][NY]; /* data to write */ float fillvalue = -2.2f; hsize_t chunk[RANK] = {CHUNK0, CHUNK1}; int i, j; /* * Data and output buffer initialization. */ for (j = 0; j < NX; j++) { for (i = 0; i < NY; i++) data[j][i] = ((float)(i + j + 1))/3; } /* * Create the dataset creation property list, add the Scale-Offset * filter, set the chunk size, and set the fill value. */ if((dcpl = H5Pcreate(H5P_DATASET_CREATE)) < 0) TEST_ERROR if(H5Pset_nbit(dcpl) < 0) TEST_ERROR if(H5Pset_chunk(dcpl, RANK, chunk) < 0) TEST_ERROR if(H5Pset_fill_value(dcpl, H5T_NATIVE_FLOAT, &fillvalue) < 0) TEST_ERROR /* Define user-defined single-precision floating-point type for dataset. * A 20-bit little-endian data type. */ if((datatype = H5Tcopy(H5T_IEEE_F32LE)) < 0) TEST_ERROR if(H5Tset_fields(datatype, (size_t)26, (size_t)20, (size_t)6, (size_t)7, (size_t)13) < 0) TEST_ERROR offset = 7; if(H5Tset_offset(datatype,offset) < 0) TEST_ERROR precision = 20; if(H5Tset_precision(datatype,precision) < 0) TEST_ERROR if(H5Tset_size(datatype, (size_t)4) < 0) TEST_ERROR if(H5Tset_ebias(datatype, (size_t)31) < 0) TEST_ERROR /* * Create a new dataset within the file using defined dataspace, * user-defined datatype, and default dataset creation properties. */ if((dataset = H5Dcreate2(fid, DATASETNAME22, datatype, fsid, H5P_DEFAULT, dcpl, H5P_DEFAULT)) < 0) TEST_ERROR /* * Write the data to the dataset using default transfer properties. */ if(H5Dwrite(dataset, H5T_NATIVE_FLOAT, msid, fsid, H5P_DEFAULT, data) < 0) TEST_ERROR /* Close dataset */ if(H5Dclose(dataset) < 0) TEST_ERROR /* Now create a dataset with a big-endian type */ if(H5Tset_order(datatype, H5T_ORDER_BE) < 0) TEST_ERROR if((dataset = H5Dcreate2(fid, DATASETNAME23, datatype, fsid, H5P_DEFAULT, dcpl, H5P_DEFAULT)) < 0) TEST_ERROR if(H5Dwrite(dataset, H5T_NATIVE_FLOAT, msid, fsid, H5P_DEFAULT, data) < 0) TEST_ERROR if(H5Dclose(dataset) < 0) TEST_ERROR /* * Close/release resources. */ if(H5Pclose(dcpl) < 0) TEST_ERROR return 0; error: H5E_BEGIN_TRY { H5Pclose(dcpl); H5Dclose(dataset); } H5E_END_TRY; return -1; }