mirror of
https://github.com/Unidata/netcdf-c.git
synced 2025-02-23 16:59:54 +08:00
type conversion with quantize between float and double
This commit is contained in:
parent
5d1aa2a926
commit
1e6ad091b6
@ -553,16 +553,23 @@ nc4_quantize_data(const void *src, void *dest, const nc_type src_type,
|
||||
/* Bit Set mask for OR: Put ones into bits to be set, zeros in
|
||||
* untouched bits. */
|
||||
msk_f32_u32_one = ~msk_f32_u32_zro;
|
||||
|
||||
|
||||
/* Copy the data into our buffer. */
|
||||
for (fp = (float *)src, fp1 = dest; count < len; count++)
|
||||
if (src_type == NC_FLOAT)
|
||||
{
|
||||
*fp1 = *fp;
|
||||
/* Move to next float. */
|
||||
fp1++;
|
||||
fp++;
|
||||
for (fp = (float *)src, fp1 = dest; count < len; count++)
|
||||
*fp1++ = *fp++;
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
for (dp = (double *)src, fp1 = dest; count < len; count++)
|
||||
{
|
||||
if (isgreater(*dp, X_FLOAT_MAX) || isless(*dp, X_FLOAT_MIN))
|
||||
(*range_error)++;
|
||||
*fp1++ = *dp++;
|
||||
}
|
||||
}
|
||||
|
||||
/* Bit-Groom: alternately shave and set LSBs */
|
||||
op1.fp = (float *)dest;
|
||||
u32_ptr = op1.ui32p;
|
||||
@ -573,7 +580,7 @@ nc4_quantize_data(const void *src, void *dest, const nc_type src_type,
|
||||
if (op1.fp[idx] != mss_val_cmp_flt && u32_ptr[idx] != 0U) /* Never quantize upwards floating point values of zero */
|
||||
u32_ptr[idx] |= msk_f32_u32_one;
|
||||
}
|
||||
else
|
||||
else /* dest_type == NC_DOUBLE */
|
||||
{
|
||||
bit_xpl_nbr_sgn = bit_xpl_nbr_sgn_dbl;
|
||||
bit_xpl_nbr_zro = bit_xpl_nbr_sgn - prc_bnr_xpl_rqr;
|
||||
@ -595,8 +602,13 @@ nc4_quantize_data(const void *src, void *dest, const nc_type src_type,
|
||||
* untouched bits. */
|
||||
msk_f64_u64_one =~ msk_f64_u64_zro;
|
||||
|
||||
for (dp = (double *)src, dp1 = dest; count < len; count++)
|
||||
*dp1++ = *dp++;
|
||||
/* Copy the data into our buffer. */
|
||||
if (src_type == NC_FLOAT)
|
||||
for (fp = (float *)src, dp1 = dest; count < len; count++)
|
||||
*dp1++ = *dp++;
|
||||
else
|
||||
for (dp = (double *)src, dp1 = dest; count < len; count++)
|
||||
*dp1++ = *dp++;
|
||||
|
||||
/* Bit-Groom: alternately shave and set LSBs. */
|
||||
op1.dp = (double *)dest;
|
||||
@ -1333,8 +1345,9 @@ nc4_convert_type(const void *src, void *dest, const nc_type src_type,
|
||||
case NC_DOUBLE:
|
||||
if (quantize_mode == NC_QUANTIZE_BITGROOM)
|
||||
{
|
||||
for (fp = (float *)src, dp = dest; count < len; count++)
|
||||
*dp++ = *fp++;
|
||||
if ((ret = nc4_quantize_data(src, dest, src_type, dest_type, len, range_error,
|
||||
fill_value, strict_nc3, quantize_mode, nsd)))
|
||||
return ret;
|
||||
}
|
||||
else
|
||||
{
|
||||
|
@ -432,9 +432,10 @@ main(int argc, char **argv)
|
||||
float float_in;
|
||||
double double_in;
|
||||
union FU fin;
|
||||
/* union FU fout; */
|
||||
union FU fout;
|
||||
union DU dfin;
|
||||
/* union DU dfout; */
|
||||
union DU dfout;
|
||||
int nsd_att_in;
|
||||
|
||||
/* Open the file and check metadata. */
|
||||
if (nc_open(FILE_NAME, NC_WRITE, &ncid)) ERR;
|
||||
@ -443,18 +444,24 @@ main(int argc, char **argv)
|
||||
if (nc_inq_var_quantize(ncid, 1, &quantize_mode_in, &nsd_in)) ERR;
|
||||
if (quantize_mode_in != NC_QUANTIZE_BITGROOM || nsd_in != NSD_3) ERR;
|
||||
|
||||
/* Each var now has an attribute describing the quantize settings. */
|
||||
if (nc_get_att_int(ncid, 0, NC_QUANTIZE_ATT_NAME, &nsd_att_in)) ERR;
|
||||
if (nsd_att_in != NSD_3) ERR;
|
||||
if (nc_get_att_int(ncid, 1, NC_QUANTIZE_ATT_NAME, &nsd_att_in)) ERR;
|
||||
if (nsd_att_in != NSD_3) ERR;
|
||||
|
||||
/* Check the data. */
|
||||
if (nc_get_var(ncid, varid1, &float_in)) ERR;
|
||||
if (nc_get_var(ncid, varid2, &double_in)) ERR;
|
||||
/* fout.f = float_data[0]; */
|
||||
fout.f = float_data[0];
|
||||
fin.f = float_in;
|
||||
/* dfout.d = double_data[0]; */
|
||||
dfout.d = double_data[0];
|
||||
dfin.d = double_in;
|
||||
/* printf ("\nfloat_data: %10f : 0x%x float_data_in: %10f : 0x%x\n", */
|
||||
/* float_data[0], fout.u, float_data[0], fin.u); */
|
||||
printf ("\nfloat_data: %10f : 0x%x float_data_in: %10f : 0x%x\n",
|
||||
float_data[0], fout.u, float_data[0], fin.u);
|
||||
/* if (fin.u != 0x3f8e3000) ERR; */
|
||||
/* printf ("\ndouble_data: %15g : 0x%16lx double_data_in: %15g : 0x%lx\n", */
|
||||
/* double_data[0], dfout.u, double_data[0], dfin.u); */
|
||||
printf ("\ndouble_data: %15g : 0x%16lx double_data_in: %15g : 0x%lx\n",
|
||||
double_data[0], dfout.u, double_data[0], dfin.u);
|
||||
/* if (dfin.u != 0x3ff1c60000000000) ERR; */
|
||||
|
||||
/* Close the file again. */
|
||||
|
Loading…
Reference in New Issue
Block a user