/src/netcdf-c/libsrc4/nc4var.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* Copyright 2003-2018, University Corporation for Atmospheric |
2 | | * Research. See COPYRIGHT file for copying and redistribution |
3 | | * conditions.*/ |
4 | | /** |
5 | | * @file |
6 | | * @internal This file is part of netcdf-4, a netCDF-like interface |
7 | | * for HDF5, or a HDF5 backend for netCDF, depending on your point of |
8 | | * view. This file handles the NetCDF-4 variable functions. |
9 | | * |
10 | | * @author Ed Hartnett, Dennis Heimbigner, Ward Fisher |
11 | | */ |
12 | | |
13 | | #include "config.h" |
14 | | #include "nc4internal.h" |
15 | | #include "nc4dispatch.h" |
16 | | #ifdef USE_HDF5 |
17 | | #include "hdf5internal.h" |
18 | | #endif |
19 | | #include <math.h> |
20 | | |
21 | | /** @internal Default size for unlimited dim chunksize. */ |
22 | 0 | #define DEFAULT_1D_UNLIM_SIZE (4096) |
23 | | |
24 | | /* Define log_e for 10 and 2. Prefer constants defined in math.h, |
25 | | * however, GCC environments can have hard time defining M_LN10/M_LN2 |
26 | | * despite finding math.h */ |
27 | | #ifndef M_LN10 |
28 | | # define M_LN10 2.30258509299404568402 /**< log_e 10 */ |
29 | | #endif /* M_LN10 */ |
30 | | #ifndef M_LN2 |
31 | | # define M_LN2 0.69314718055994530942 /**< log_e 2 */ |
32 | | #endif /* M_LN2 */ |
33 | | |
34 | | /** Used in quantize code. Number of explicit bits in significand for |
35 | | * floats. Bits 0-22 of SP significands are explicit. Bit 23 is |
36 | | * implicitly 1. Currently redundant with NC_QUANTIZE_MAX_FLOAT_NSB |
37 | | * and with limits.h/climit (FLT_MANT_DIG-1) */ |
38 | 0 | #define BIT_XPL_NBR_SGN_FLT (23) |
39 | | |
40 | | /** Used in quantize code. Number of explicit bits in significand for |
41 | | * doubles. Bits 0-51 of DP significands are explicit. Bit 52 is |
42 | | * implicitly 1. Currently redundant with NC_QUANTIZE_MAX_DOUBLE_NSB |
43 | | * and with limits.h/climit (DBL_MANT_DIG-1) */ |
44 | 0 | #define BIT_XPL_NBR_SGN_DBL (52) |
45 | | |
46 | | /** Pointer union for floating point and bitmask types. */ |
47 | | typedef union { /* ptr_unn */ |
48 | | float *fp; |
49 | | double *dp; |
50 | | unsigned int *ui32p; |
51 | | unsigned long long *ui64p; |
52 | | void *vp; |
53 | | } ptr_unn; |
54 | | |
55 | | /** |
56 | | * @internal This is called by nc_get_var_chunk_cache(). Get chunk |
57 | | * cache size for a variable. |
58 | | * |
59 | | * @param ncid File ID. |
60 | | * @param varid Variable ID. |
61 | | * @param sizep Gets size in bytes of cache. |
62 | | * @param nelemsp Gets number of element slots in cache. |
63 | | * @param preemptionp Gets cache swapping setting. |
64 | | * |
65 | | * @returns ::NC_NOERR No error. |
66 | | * @returns ::NC_EBADID Bad ncid. |
67 | | * @returns ::NC_ENOTVAR Invalid variable ID. |
68 | | * @returns ::NC_ENOTNC4 Not a netCDF-4 file. |
69 | | * @author Ed Hartnett |
70 | | */ |
71 | | int |
72 | | NC4_get_var_chunk_cache(int ncid, int varid, size_t *sizep, |
73 | | size_t *nelemsp, float *preemptionp) |
74 | 0 | { |
75 | 0 | NC *nc; |
76 | 0 | NC_GRP_INFO_T *grp; |
77 | 0 | NC_FILE_INFO_T *h5; |
78 | 0 | NC_VAR_INFO_T *var; |
79 | 0 | int retval; |
80 | | |
81 | | /* Find info for this file and group, and set pointer to each. */ |
82 | 0 | if ((retval = nc4_find_nc_grp_h5(ncid, &nc, &grp, &h5))) |
83 | 0 | return retval; |
84 | 0 | assert(nc && grp && h5); |
85 | | |
86 | | /* Find the var. */ |
87 | 0 | var = (NC_VAR_INFO_T*)ncindexith(grp->vars,varid); |
88 | 0 | if(!var) |
89 | 0 | return NC_ENOTVAR; |
90 | 0 | assert(var && var->hdr.id == varid); |
91 | | |
92 | | /* Give the user what they want. */ |
93 | 0 | if (sizep) |
94 | 0 | *sizep = var->chunkcache.size; |
95 | 0 | if (nelemsp) |
96 | 0 | *nelemsp = var->chunkcache.nelems; |
97 | 0 | if (preemptionp) |
98 | 0 | *preemptionp = var->chunkcache.preemption; |
99 | |
|
100 | 0 | return NC_NOERR; |
101 | 0 | } |
102 | | |
103 | | /** |
104 | | * @internal A wrapper for NC4_get_var_chunk_cache(), we need this |
105 | | * version for fortran. |
106 | | * |
107 | | * @param ncid File ID. |
108 | | * @param varid Variable ID. |
109 | | * @param sizep Gets size in MB of cache. |
110 | | * @param nelemsp Gets number of element slots in cache. |
111 | | * @param preemptionp Gets cache swapping setting. |
112 | | * |
113 | | * @returns ::NC_NOERR No error. |
114 | | * @returns ::NC_EBADID Bad ncid. |
115 | | * @returns ::NC_ENOTVAR Invalid variable ID. |
116 | | * @returns ::NC_ENOTNC4 Not a netCDF-4 file. |
117 | | * @author Ed Hartnett |
118 | | */ |
119 | | int |
120 | | nc_get_var_chunk_cache_ints(int ncid, int varid, int *sizep, |
121 | | int *nelemsp, int *preemptionp) |
122 | 0 | { |
123 | 0 | size_t real_size, real_nelems; |
124 | 0 | float real_preemption; |
125 | 0 | int ret; |
126 | |
|
127 | 0 | if ((ret = NC4_get_var_chunk_cache(ncid, varid, &real_size, |
128 | 0 | &real_nelems, &real_preemption))) |
129 | 0 | return ret; |
130 | | |
131 | 0 | if (sizep) |
132 | 0 | *sizep = real_size / MEGABYTE; |
133 | 0 | if (nelemsp) |
134 | 0 | *nelemsp = (int)real_nelems; |
135 | 0 | if(preemptionp) |
136 | 0 | *preemptionp = (int)(real_preemption * 100); |
137 | |
|
138 | 0 | return NC_NOERR; |
139 | 0 | } |
140 | | |
141 | | /** |
142 | | * @internal Get all the information about a variable. Pass NULL for |
143 | | * whatever you don't care about. This is the internal function called |
144 | | * by nc_inq_var(), nc_inq_var_deflate(), nc_inq_var_fletcher32(), |
145 | | * nc_inq_var_chunking(), nc_inq_var_chunking_ints(), |
146 | | * nc_inq_var_fill(), nc_inq_var_endian(), nc_inq_var_filter(), and |
147 | | * nc_inq_var_szip(). |
148 | | * |
149 | | * @param ncid File ID. |
150 | | * @param varid Variable ID. |
151 | | * @param name Gets name. |
152 | | * @param xtypep Gets type. |
153 | | * @param ndimsp Gets number of dims. |
154 | | * @param dimidsp Gets array of dim IDs. |
155 | | * @param nattsp Gets number of attributes. |
156 | | * @param shufflep Gets shuffle setting. |
157 | | * @param deflatep Gets deflate setting. |
158 | | * @param deflate_levelp Gets deflate level. |
159 | | * @param fletcher32p Gets fletcher32 setting. |
160 | | * @param storagep Gets storage setting. |
161 | | * @param chunksizesp Gets chunksizes. |
162 | | * @param no_fill Gets fill mode. |
163 | | * @param fill_valuep Gets fill value. |
164 | | * @param endiannessp Gets one of ::NC_ENDIAN_BIG ::NC_ENDIAN_LITTLE |
165 | | * ::NC_ENDIAN_NATIVE |
166 | | * @param idp Pointer to memory to store filter id. |
167 | | * @param nparamsp Pointer to memory to store filter parameter count. |
168 | | * @param params Pointer to vector of unsigned integers into which |
169 | | * to store filter parameters. |
170 | | * |
171 | | * @returns ::NC_NOERR No error. |
172 | | * @returns ::NC_EBADID Bad ncid. |
173 | | * @returns ::NC_ENOTVAR Bad varid. |
174 | | * @returns ::NC_ENOMEM Out of memory. |
175 | | * @returns ::NC_EINVAL Invalid input. |
176 | | * @author Ed Hartnett, Dennis Heimbigner |
177 | | */ |
178 | | int |
179 | | NC4_inq_var_all(int ncid, int varid, char *name, nc_type *xtypep, |
180 | | int *ndimsp, int *dimidsp, int *nattsp, |
181 | | int *shufflep, int *deflatep, int *deflate_levelp, |
182 | | int *fletcher32p, int *storagep, size_t *chunksizesp, |
183 | | int *no_fill, void *fill_valuep, int *endiannessp, |
184 | | unsigned int *idp, size_t *nparamsp, unsigned int *params) |
185 | 0 | { |
186 | 0 | NC_GRP_INFO_T *grp; |
187 | 0 | NC_FILE_INFO_T *h5; |
188 | 0 | NC_VAR_INFO_T *var; |
189 | 0 | int d; |
190 | 0 | int retval; |
191 | |
|
192 | 0 | LOG((2, "%s: ncid 0x%x varid %d", __func__, ncid, varid)); |
193 | | |
194 | | /* Find info for this file and group, and set pointer to each. */ |
195 | 0 | if ((retval = nc4_find_nc_grp_h5(ncid, NULL, &grp, &h5))) |
196 | 0 | return retval; |
197 | 0 | assert(grp && h5); |
198 | | |
199 | | /* If the varid is -1, find the global atts and call it a day. */ |
200 | 0 | if (varid == NC_GLOBAL && nattsp) |
201 | 0 | { |
202 | 0 | *nattsp = ncindexcount(grp->att); |
203 | 0 | return NC_NOERR; |
204 | 0 | } |
205 | | |
206 | | /* Find the var. */ |
207 | 0 | if (!(var = (NC_VAR_INFO_T *)ncindexith(grp->vars, varid))) |
208 | 0 | return NC_ENOTVAR; |
209 | 0 | assert(var && var->hdr.id == varid); |
210 | | |
211 | | /* Copy the data to the user's data buffers. */ |
212 | 0 | if (name) |
213 | 0 | strcpy(name, var->hdr.name); |
214 | 0 | if (xtypep) |
215 | 0 | *xtypep = var->type_info->hdr.id; |
216 | 0 | if (ndimsp) |
217 | 0 | *ndimsp = var->ndims; |
218 | 0 | if (dimidsp) |
219 | 0 | for (d = 0; d < var->ndims; d++) |
220 | 0 | dimidsp[d] = var->dimids[d]; |
221 | 0 | if (nattsp) |
222 | 0 | *nattsp = ncindexcount(var->att); |
223 | | |
224 | | /* Did the user want the chunksizes? */ |
225 | 0 | if (var->storage == NC_CHUNKED && chunksizesp) |
226 | 0 | { |
227 | 0 | for (d = 0; d < var->ndims; d++) |
228 | 0 | { |
229 | 0 | chunksizesp[d] = var->chunksizes[d]; |
230 | 0 | LOG((4, "chunksizesp[%d]=%d", d, chunksizesp[d])); |
231 | 0 | } |
232 | 0 | } |
233 | | |
234 | | /* Did the user inquire about the storage? */ |
235 | 0 | if (storagep) |
236 | 0 | *storagep = var->storage; |
237 | | |
238 | | /* Filter stuff. */ |
239 | 0 | if (shufflep) { |
240 | 0 | retval = nc_inq_var_filter_info(ncid,varid,H5Z_FILTER_SHUFFLE,0,NULL); |
241 | 0 | if(retval && retval != NC_ENOFILTER) return retval; |
242 | 0 | *shufflep = (retval == NC_NOERR?1:0); |
243 | 0 | } |
244 | 0 | if (fletcher32p) { |
245 | 0 | retval = nc_inq_var_filter_info(ncid,varid,H5Z_FILTER_FLETCHER32,0,NULL); |
246 | 0 | if(retval && retval != NC_ENOFILTER) return retval; |
247 | 0 | *fletcher32p = (retval == NC_NOERR?1:0); |
248 | 0 | } |
249 | 0 | if (deflatep) |
250 | 0 | return NC_EFILTER; |
251 | | |
252 | 0 | if (idp) { |
253 | 0 | return NC_EFILTER; |
254 | 0 | } |
255 | | |
256 | | /* Fill value stuff. */ |
257 | 0 | if (no_fill) |
258 | 0 | *no_fill = (int)var->no_fill; |
259 | | |
260 | | /* Don't do a thing with fill_valuep if no_fill mode is set for |
261 | | * this var, or if fill_valuep is NULL. */ |
262 | 0 | if (!var->no_fill && fill_valuep) |
263 | 0 | { |
264 | | /* Do we have a fill value for this var? */ |
265 | 0 | if (var->fill_value) |
266 | 0 | { |
267 | 0 | int xtype = var->type_info->hdr.id; |
268 | 0 | if((retval = nc_copy_data(ncid,xtype,var->fill_value,1,fill_valuep))) return retval; |
269 | 0 | } |
270 | 0 | else |
271 | 0 | { |
272 | 0 | if ((retval = nc4_get_default_fill_value(var->type_info, fill_valuep))) |
273 | 0 | return retval; |
274 | 0 | } |
275 | 0 | } |
276 | | |
277 | | /* Does the user want the endianness of this variable? */ |
278 | 0 | if (endiannessp) |
279 | 0 | *endiannessp = var->endianness; |
280 | |
|
281 | 0 | return NC_NOERR; |
282 | 0 | } |
283 | | |
284 | | /** |
285 | | * @internal Inquire about chunking settings for a var. This is used |
286 | | * by the fortran API. |
287 | | * |
288 | | * @param ncid File ID. |
289 | | * @param varid Variable ID. |
290 | | * @param storagep Gets contiguous setting. |
291 | | * @param chunksizesp Gets chunksizes. |
292 | | * |
293 | | * @returns ::NC_NOERR No error. |
294 | | * @returns ::NC_EBADID Bad ncid. |
295 | | * @returns ::NC_ENOTVAR Invalid variable ID. |
296 | | * @returns ::NC_EINVAL Invalid input |
297 | | * @returns ::NC_ENOMEM Out of memory. |
298 | | * @author Ed Hartnett |
299 | | */ |
300 | | int |
301 | | nc_inq_var_chunking_ints(int ncid, int varid, int *storagep, int *chunksizesp) |
302 | 0 | { |
303 | 0 | NC_VAR_INFO_T *var; |
304 | 0 | size_t *cs = NULL; |
305 | 0 | int i, retval; |
306 | | |
307 | | /* Get pointer to the var. */ |
308 | 0 | if ((retval = nc4_find_grp_h5_var(ncid, varid, NULL, NULL, &var))) |
309 | 0 | return retval; |
310 | 0 | assert(var); |
311 | | |
312 | | /* Allocate space for the size_t copy of the chunksizes array. */ |
313 | 0 | if (var->ndims) |
314 | 0 | if (!(cs = malloc(var->ndims * sizeof(size_t)))) |
315 | 0 | return NC_ENOMEM; |
316 | | |
317 | | /* Call the netcdf-4 version directly. */ |
318 | 0 | retval = NC4_inq_var_all(ncid, varid, NULL, NULL, NULL, NULL, NULL, |
319 | 0 | NULL, NULL, NULL, NULL, storagep, cs, NULL, |
320 | 0 | NULL, NULL, NULL, NULL, NULL); |
321 | | |
322 | | /* Copy from size_t array. */ |
323 | 0 | if (!retval && chunksizesp && var->storage == NC_CHUNKED) |
324 | 0 | { |
325 | 0 | for (i = 0; i < var->ndims; i++) |
326 | 0 | { |
327 | 0 | chunksizesp[i] = (int)cs[i]; |
328 | 0 | if (cs[i] > NC_MAX_INT) |
329 | 0 | retval = NC_ERANGE; |
330 | 0 | } |
331 | 0 | } |
332 | |
|
333 | 0 | if (var->ndims) |
334 | 0 | free(cs); |
335 | 0 | return retval; |
336 | 0 | } |
337 | | |
338 | | /** |
339 | | * @internal Find the ID of a variable, from the name. This function |
340 | | * is called by nc_inq_varid(). |
341 | | * |
342 | | * @param ncid File ID. |
343 | | * @param name Name of the variable. |
344 | | * @param varidp Gets variable ID. |
345 | | |
346 | | * @returns ::NC_NOERR No error. |
347 | | * @returns ::NC_EBADID Bad ncid. |
348 | | * @returns ::NC_ENOTVAR Bad variable ID. |
349 | | */ |
350 | | int |
351 | | NC4_inq_varid(int ncid, const char *name, int *varidp) |
352 | 0 | { |
353 | 0 | NC *nc; |
354 | 0 | NC_GRP_INFO_T *grp; |
355 | 0 | NC_VAR_INFO_T *var; |
356 | 0 | char norm_name[NC_MAX_NAME + 1]; |
357 | 0 | int retval; |
358 | |
|
359 | 0 | if (!name) |
360 | 0 | return NC_EINVAL; |
361 | 0 | if (!varidp) |
362 | 0 | return NC_NOERR; |
363 | | |
364 | 0 | LOG((2, "%s: ncid 0x%x name %s", __func__, ncid, name)); |
365 | | |
366 | | /* Find info for this file and group, and set pointer to each. */ |
367 | 0 | if ((retval = nc4_find_nc_grp_h5(ncid, &nc, &grp, NULL))) |
368 | 0 | return retval; |
369 | | |
370 | | /* Normalize name. */ |
371 | 0 | if ((retval = nc4_normalize_name(name, norm_name))) |
372 | 0 | return retval; |
373 | | |
374 | | /* Find var of this name. */ |
375 | 0 | var = (NC_VAR_INFO_T*)ncindexlookup(grp->vars,norm_name); |
376 | 0 | if(var) |
377 | 0 | { |
378 | 0 | *varidp = var->hdr.id; |
379 | 0 | return NC_NOERR; |
380 | 0 | } |
381 | 0 | return NC_ENOTVAR; |
382 | 0 | } |
383 | | |
384 | | /** |
385 | | * @internal |
386 | | * |
387 | | * This function will change the parallel access of a variable from |
388 | | * independent to collective. |
389 | | * |
390 | | * @param ncid File ID. |
391 | | * @param varid Variable ID. |
392 | | * @param par_access NC_COLLECTIVE or NC_INDEPENDENT. |
393 | | * |
394 | | * @returns ::NC_NOERR No error. |
395 | | * @returns ::NC_EBADID Invalid ncid passed. |
396 | | * @returns ::NC_ENOTVAR Invalid varid passed. |
397 | | * @returns ::NC_ENOPAR LFile was not opened with nc_open_par/nc_create_par. |
398 | | * @returns ::NC_EINVAL Invalid par_access specified. |
399 | | * @returns ::NC_NOERR for success |
400 | | * @author Ed Hartnett, Dennis Heimbigner |
401 | | */ |
402 | | int |
403 | | NC4_var_par_access(int ncid, int varid, int par_access) |
404 | 0 | { |
405 | 0 | #ifndef USE_PARALLEL4 |
406 | 0 | NC_UNUSED(ncid); |
407 | 0 | NC_UNUSED(varid); |
408 | 0 | NC_UNUSED(par_access); |
409 | 0 | return NC_ENOPAR; |
410 | | #else |
411 | | NC *nc; |
412 | | NC_GRP_INFO_T *grp; |
413 | | NC_FILE_INFO_T *h5; |
414 | | NC_VAR_INFO_T *var; |
415 | | int retval; |
416 | | |
417 | | LOG((1, "%s: ncid 0x%x varid %d par_access %d", __func__, ncid, |
418 | | varid, par_access)); |
419 | | |
420 | | if (par_access != NC_INDEPENDENT && par_access != NC_COLLECTIVE) |
421 | | return NC_EINVAL; |
422 | | |
423 | | /* Find info for this file and group, and set pointer to each. */ |
424 | | if ((retval = nc4_find_nc_grp_h5(ncid, &nc, &grp, &h5))) |
425 | | return retval; |
426 | | |
427 | | /* This function only for files opened with nc_open_par or nc_create_par. */ |
428 | | if (!h5->parallel) |
429 | | return NC_ENOPAR; |
430 | | |
431 | | /* Find the var, and set its preference. */ |
432 | | var = (NC_VAR_INFO_T*)ncindexith(grp->vars,varid); |
433 | | if (!var) return NC_ENOTVAR; |
434 | | assert(var->hdr.id == varid); |
435 | | |
436 | | /* If zlib, shuffle, or fletcher32 filters are in use, then access |
437 | | * must be collective. Fail an attempt to set such a variable to |
438 | | * independent access. */ |
439 | | if (nclistlength((NClist*)var->filters) > 0 && |
440 | | par_access == NC_INDEPENDENT) |
441 | | return NC_EINVAL; |
442 | | |
443 | | if (par_access) |
444 | | var->parallel_access = NC_COLLECTIVE; |
445 | | else |
446 | | var->parallel_access = NC_INDEPENDENT; |
447 | | return NC_NOERR; |
448 | | #endif /* USE_PARALLEL4 */ |
449 | 0 | } |
450 | | |
451 | | /** |
452 | | * @internal Copy data from one buffer to another, performing |
453 | | * appropriate data conversion. |
454 | | * |
455 | | * This function will copy data from one buffer to another, in |
456 | | * accordance with the types. Range errors will be noted, and the fill |
457 | | * value used (or the default fill value if none is supplied) for |
458 | | * values that overflow the type. |
459 | | * |
460 | | * This function applies quantization to float and double data, if |
461 | | * desired. The code to do this is derived from the corresponding |
462 | | * filter in the CCR project (e.g., |
463 | | * https://github.com/ccr/ccr/blob/master/hdf5_plugins/BITGROOM/src/H5Zbitgroom.c). |
464 | | * |
465 | | * @param src Pointer to source of data. |
466 | | * @param dest Pointer that gets data. |
467 | | * @param src_type Type ID of source data. |
468 | | * @param dest_type Type ID of destination data. |
469 | | * @param len Number of elements of data to copy. |
470 | | * @param range_error Pointer that gets 1 if there was a range error. |
471 | | * @param fill_value The fill value. |
472 | | * @param strict_nc3 Non-zero if strict model in effect. |
473 | | * @param quantize_mode May be ::NC_NOQUANTIZE, ::NC_QUANTIZE_BITGROOM, |
474 | | * ::NC_QUANTIZE_GRANULARBR, or ::NC_QUANTIZE_BITROUND. |
475 | | * @param nsd Number of significant digits for quantize. Ignored |
476 | | * unless quantize_mode is ::NC_QUANTIZE_BITGROOM, |
477 | | * ::NC_QUANTIZE_GRANULARBR, or ::NC_QUANTIZE_BITROUND |
478 | | * |
479 | | * @returns ::NC_NOERR No error. |
480 | | * @returns ::NC_EBADTYPE Type not found. |
481 | | * @author Ed Hartnett, Dennis Heimbigner |
482 | | */ |
483 | | int |
484 | | nc4_convert_type(const void *src, void *dest, const nc_type src_type, |
485 | | const nc_type dest_type, const size_t len, int *range_error, |
486 | | const void *fill_value, int strict_nc3, int quantize_mode, |
487 | | int nsd) |
488 | 0 | { |
489 | | /* These vars are used with quantize feature. */ |
490 | 0 | const double bit_per_dgt = M_LN10 / M_LN2; /* 3.32 [frc] Bits per decimal digit of precision = log2(10) */ |
491 | 0 | const double dgt_per_bit= M_LN2 / M_LN10; /* 0.301 [frc] Decimal digits per bit of precision = log10(2) */ |
492 | 0 | double mnt; /* [frc] Mantissa, 0.5 <= mnt < 1.0 */ |
493 | 0 | double mnt_fabs; /* [frc] fabs(mantissa) */ |
494 | 0 | double mnt_log10_fabs; /* [frc] log10(fabs(mantissa))) */ |
495 | 0 | double val; /* [frc] Copy of input value to avoid indirection */ |
496 | 0 | double mss_val_cmp_dbl; /* Missing value for comparison to double precision values */ |
497 | 0 | float mss_val_cmp_flt; /* Missing value for comparison to single precision values */ |
498 | 0 | int bit_xpl_nbr_zro; /* [nbr] Number of explicit bits to zero */ |
499 | 0 | int dgt_nbr; /* [nbr] Number of digits before decimal point */ |
500 | 0 | int qnt_pwr; /* [nbr] Power of two in quantization mask: qnt_msk = 2^qnt_pwr */ |
501 | 0 | int xpn_bs2; /* [nbr] Binary exponent xpn_bs2 in val = sign(val) * 2^xpn_bs2 * mnt, 0.5 < mnt <= 1.0 */ |
502 | 0 | size_t idx; |
503 | 0 | unsigned int *u32_ptr; |
504 | 0 | unsigned int msk_f32_u32_zro; |
505 | 0 | unsigned int msk_f32_u32_one; |
506 | 0 | unsigned int msk_f32_u32_hshv; |
507 | 0 | unsigned long long int *u64_ptr; |
508 | 0 | unsigned long long int msk_f64_u64_zro; |
509 | 0 | unsigned long long int msk_f64_u64_one; |
510 | 0 | unsigned long long int msk_f64_u64_hshv; |
511 | 0 | unsigned short prc_bnr_xpl_rqr; /* [nbr] Explicitly represented binary digits required to retain */ |
512 | 0 | ptr_unn op1; /* I/O [frc] Values to quantize */ |
513 | | |
514 | 0 | char *cp, *cp1; |
515 | 0 | float *fp, *fp1; |
516 | 0 | double *dp, *dp1; |
517 | 0 | int *ip, *ip1; |
518 | 0 | short *sp, *sp1; |
519 | 0 | signed char *bp, *bp1; |
520 | 0 | unsigned char *ubp, *ubp1; |
521 | 0 | unsigned short *usp, *usp1; |
522 | 0 | unsigned int *uip, *uip1; |
523 | 0 | long long *lip, *lip1; |
524 | 0 | unsigned long long *ulip, *ulip1; |
525 | 0 | size_t count = 0; |
526 | |
|
527 | 0 | *range_error = 0; |
528 | 0 | LOG((3, "%s: len %d src_type %d dest_type %d", __func__, len, src_type, |
529 | 0 | dest_type)); |
530 | | |
531 | | /* If quantize is in use, set up some values. Quantize can only be |
532 | | * used when the destination type is NC_FLOAT or NC_DOUBLE. */ |
533 | 0 | if (quantize_mode != NC_NOQUANTIZE) |
534 | 0 | { |
535 | 0 | assert(dest_type == NC_FLOAT || dest_type == NC_DOUBLE); |
536 | | |
537 | | /* Parameters shared by all quantization codecs */ |
538 | 0 | if (dest_type == NC_FLOAT) |
539 | 0 | { |
540 | | /* Determine the fill value. */ |
541 | 0 | if (fill_value) |
542 | 0 | mss_val_cmp_flt = *(float *)fill_value; |
543 | 0 | else |
544 | 0 | mss_val_cmp_flt = NC_FILL_FLOAT; |
545 | |
|
546 | 0 | } |
547 | 0 | else |
548 | 0 | { |
549 | | |
550 | | /* Determine the fill value. */ |
551 | 0 | if (fill_value) |
552 | 0 | mss_val_cmp_dbl = *(double *)fill_value; |
553 | 0 | else |
554 | 0 | mss_val_cmp_dbl = NC_FILL_DOUBLE; |
555 | |
|
556 | 0 | } |
557 | | |
558 | | /* Set parameters used by BitGroom and BitRound here, outside value loop. |
559 | | Equivalent parameters used by GranularBR are set inside value loop, |
560 | | since keep bits and thus masks can change for every value. */ |
561 | 0 | if (quantize_mode == NC_QUANTIZE_BITGROOM || |
562 | 0 | quantize_mode == NC_QUANTIZE_BITROUND ) |
563 | 0 | { |
564 | |
|
565 | 0 | if (quantize_mode == NC_QUANTIZE_BITGROOM){ |
566 | | |
567 | | /* BitGroom interprets nsd as number of significant decimal digits |
568 | | * Must convert that to number of significant bits to preserve |
569 | | * How many bits to preserve? Being conservative, we round up the |
570 | | * exact binary digits of precision. Add one because the first bit |
571 | | * is implicit not explicit but corner cases prevent our taking |
572 | | * advantage of this. */ |
573 | 0 | prc_bnr_xpl_rqr = (unsigned short)ceil(nsd * bit_per_dgt) + 1; |
574 | |
|
575 | 0 | }else if (quantize_mode == NC_QUANTIZE_BITROUND){ |
576 | | |
577 | | /* BitRound interprets nsd as number of significant binary digits (bits) */ |
578 | 0 | prc_bnr_xpl_rqr = nsd; |
579 | | |
580 | 0 | } |
581 | | |
582 | 0 | if (dest_type == NC_FLOAT) |
583 | 0 | { |
584 | |
|
585 | 0 | bit_xpl_nbr_zro = BIT_XPL_NBR_SGN_FLT - prc_bnr_xpl_rqr; |
586 | | |
587 | | /* Create mask */ |
588 | 0 | msk_f32_u32_zro = 0u; /* Zero all bits */ |
589 | 0 | msk_f32_u32_zro = ~msk_f32_u32_zro; /* Turn all bits to ones */ |
590 | | |
591 | | /* BitShave mask for AND: Left shift zeros into bits to be |
592 | | * rounded, leave ones in untouched bits. */ |
593 | 0 | msk_f32_u32_zro <<= bit_xpl_nbr_zro; |
594 | | |
595 | | /* BitSet mask for OR: Put ones into bits to be set, zeros in |
596 | | * untouched bits. */ |
597 | 0 | msk_f32_u32_one = ~msk_f32_u32_zro; |
598 | | |
599 | | /* BitRound mask for ADD: Set one bit: the MSB of LSBs */ |
600 | 0 | msk_f32_u32_hshv=msk_f32_u32_one & (msk_f32_u32_zro >> 1); |
601 | |
|
602 | 0 | } |
603 | 0 | else |
604 | 0 | { |
605 | |
|
606 | 0 | bit_xpl_nbr_zro = BIT_XPL_NBR_SGN_DBL - prc_bnr_xpl_rqr; |
607 | | /* Create mask. */ |
608 | 0 | msk_f64_u64_zro = 0ul; /* Zero all bits. */ |
609 | 0 | msk_f64_u64_zro = ~msk_f64_u64_zro; /* Turn all bits to ones. */ |
610 | | |
611 | | /* BitShave mask for AND: Left shift zeros into bits to be |
612 | | * rounded, leave ones in untouched bits. */ |
613 | 0 | msk_f64_u64_zro <<= bit_xpl_nbr_zro; |
614 | | |
615 | | /* BitSet mask for OR: Put ones into bits to be set, zeros in |
616 | | * untouched bits. */ |
617 | 0 | msk_f64_u64_one =~ msk_f64_u64_zro; |
618 | | |
619 | | /* BitRound mask for ADD: Set one bit: the MSB of LSBs */ |
620 | 0 | msk_f64_u64_hshv = msk_f64_u64_one & (msk_f64_u64_zro >> 1); |
621 | |
|
622 | 0 | } |
623 | |
|
624 | 0 | } |
625 | | |
626 | 0 | } /* endif quantize */ |
627 | | |
628 | | /* OK, this is ugly. If you can think of anything better, I'm open |
629 | | to suggestions! |
630 | | |
631 | | Note that we don't use a default fill value for type |
632 | | NC_BYTE. This is because Lord Voldemort cast a nofilleramous spell |
633 | | at Harry Potter, but it bounced off his scar and hit the netcdf-4 |
634 | | code. |
635 | | */ |
636 | 0 | switch (src_type) |
637 | 0 | { |
638 | 0 | case NC_CHAR: |
639 | 0 | switch (dest_type) |
640 | 0 | { |
641 | 0 | case NC_CHAR: |
642 | 0 | for (cp = (char *)src, cp1 = dest; count < len; count++) |
643 | 0 | *cp1++ = *cp++; |
644 | 0 | break; |
645 | 0 | default: |
646 | 0 | LOG((0, "%s: Unknown destination type.", __func__)); |
647 | 0 | } |
648 | 0 | break; |
649 | | |
650 | 0 | case NC_BYTE: |
651 | 0 | switch (dest_type) |
652 | 0 | { |
653 | 0 | case NC_BYTE: |
654 | 0 | for (bp = (signed char *)src, bp1 = dest; count < len; count++) |
655 | 0 | *bp1++ = *bp++; |
656 | 0 | break; |
657 | 0 | case NC_UBYTE: |
658 | 0 | for (bp = (signed char *)src, ubp = dest; count < len; count++) |
659 | 0 | { |
660 | 0 | if (*bp < 0) |
661 | 0 | (*range_error)++; |
662 | 0 | *ubp++ = *bp++; |
663 | 0 | } |
664 | 0 | break; |
665 | 0 | case NC_SHORT: |
666 | 0 | for (bp = (signed char *)src, sp = dest; count < len; count++) |
667 | 0 | *sp++ = *bp++; |
668 | 0 | break; |
669 | 0 | case NC_USHORT: |
670 | 0 | for (bp = (signed char *)src, usp = dest; count < len; count++) |
671 | 0 | { |
672 | 0 | if (*bp < 0) |
673 | 0 | (*range_error)++; |
674 | 0 | *usp++ = *bp++; |
675 | 0 | } |
676 | 0 | break; |
677 | 0 | case NC_INT: |
678 | 0 | for (bp = (signed char *)src, ip = dest; count < len; count++) |
679 | 0 | *ip++ = *bp++; |
680 | 0 | break; |
681 | 0 | case NC_UINT: |
682 | 0 | for (bp = (signed char *)src, uip = dest; count < len; count++) |
683 | 0 | { |
684 | 0 | if (*bp < 0) |
685 | 0 | (*range_error)++; |
686 | 0 | *uip++ = *bp++; |
687 | 0 | } |
688 | 0 | break; |
689 | 0 | case NC_INT64: |
690 | 0 | for (bp = (signed char *)src, lip = dest; count < len; count++) |
691 | 0 | *lip++ = *bp++; |
692 | 0 | break; |
693 | 0 | case NC_UINT64: |
694 | 0 | for (bp = (signed char *)src, ulip = dest; count < len; count++) |
695 | 0 | { |
696 | 0 | if (*bp < 0) |
697 | 0 | (*range_error)++; |
698 | 0 | *ulip++ = *bp++; |
699 | 0 | } |
700 | 0 | break; |
701 | 0 | case NC_FLOAT: |
702 | 0 | for (bp = (signed char *)src, fp = dest; count < len; count++) |
703 | 0 | *fp++ = *bp++; |
704 | 0 | break; |
705 | 0 | case NC_DOUBLE: |
706 | 0 | for (bp = (signed char *)src, dp = dest; count < len; count++) |
707 | 0 | *dp++ = *bp++; |
708 | 0 | break; |
709 | 0 | default: |
710 | 0 | LOG((0, "%s: unexpected dest type. src_type %d, dest_type %d", |
711 | 0 | __func__, src_type, dest_type)); |
712 | 0 | return NC_EBADTYPE; |
713 | 0 | } |
714 | 0 | break; |
715 | | |
716 | 0 | case NC_UBYTE: |
717 | 0 | switch (dest_type) |
718 | 0 | { |
719 | 0 | case NC_BYTE: |
720 | 0 | for (ubp = (unsigned char *)src, bp = dest; count < len; count++) |
721 | 0 | { |
722 | 0 | if (!strict_nc3 && *ubp > X_SCHAR_MAX) |
723 | 0 | (*range_error)++; |
724 | 0 | *bp++ = *ubp++; |
725 | 0 | } |
726 | 0 | break; |
727 | 0 | case NC_SHORT: |
728 | 0 | for (ubp = (unsigned char *)src, sp = dest; count < len; count++) |
729 | 0 | *sp++ = *ubp++; |
730 | 0 | break; |
731 | 0 | case NC_UBYTE: |
732 | 0 | for (ubp = (unsigned char *)src, ubp1 = dest; count < len; count++) |
733 | 0 | *ubp1++ = *ubp++; |
734 | 0 | break; |
735 | 0 | case NC_USHORT: |
736 | 0 | for (ubp = (unsigned char *)src, usp = dest; count < len; count++) |
737 | 0 | *usp++ = *ubp++; |
738 | 0 | break; |
739 | 0 | case NC_INT: |
740 | 0 | for (ubp = (unsigned char *)src, ip = dest; count < len; count++) |
741 | 0 | *ip++ = *ubp++; |
742 | 0 | break; |
743 | 0 | case NC_UINT: |
744 | 0 | for (ubp = (unsigned char *)src, uip = dest; count < len; count++) |
745 | 0 | *uip++ = *ubp++; |
746 | 0 | break; |
747 | 0 | case NC_INT64: |
748 | 0 | for (ubp = (unsigned char *)src, lip = dest; count < len; count++) |
749 | 0 | *lip++ = *ubp++; |
750 | 0 | break; |
751 | 0 | case NC_UINT64: |
752 | 0 | for (ubp = (unsigned char *)src, ulip = dest; count < len; count++) |
753 | 0 | *ulip++ = *ubp++; |
754 | 0 | break; |
755 | 0 | case NC_FLOAT: |
756 | 0 | for (ubp = (unsigned char *)src, fp = dest; count < len; count++) |
757 | 0 | *fp++ = *ubp++; |
758 | 0 | break; |
759 | 0 | case NC_DOUBLE: |
760 | 0 | for (ubp = (unsigned char *)src, dp = dest; count < len; count++) |
761 | 0 | *dp++ = *ubp++; |
762 | 0 | break; |
763 | 0 | default: |
764 | 0 | LOG((0, "%s: unexpected dest type. src_type %d, dest_type %d", |
765 | 0 | __func__, src_type, dest_type)); |
766 | 0 | return NC_EBADTYPE; |
767 | 0 | } |
768 | 0 | break; |
769 | | |
770 | 0 | case NC_SHORT: |
771 | 0 | switch (dest_type) |
772 | 0 | { |
773 | 0 | case NC_UBYTE: |
774 | 0 | for (sp = (short *)src, ubp = dest; count < len; count++) |
775 | 0 | { |
776 | 0 | if (*sp > X_UCHAR_MAX || *sp < 0) |
777 | 0 | (*range_error)++; |
778 | 0 | *ubp++ = *sp++; |
779 | 0 | } |
780 | 0 | break; |
781 | 0 | case NC_BYTE: |
782 | 0 | for (sp = (short *)src, bp = dest; count < len; count++) |
783 | 0 | { |
784 | 0 | if (*sp > X_SCHAR_MAX || *sp < X_SCHAR_MIN) |
785 | 0 | (*range_error)++; |
786 | 0 | *bp++ = *sp++; |
787 | 0 | } |
788 | 0 | break; |
789 | 0 | case NC_SHORT: |
790 | 0 | for (sp = (short *)src, sp1 = dest; count < len; count++) |
791 | 0 | *sp1++ = *sp++; |
792 | 0 | break; |
793 | 0 | case NC_USHORT: |
794 | 0 | for (sp = (short *)src, usp = dest; count < len; count++) |
795 | 0 | { |
796 | 0 | if (*sp < 0) |
797 | 0 | (*range_error)++; |
798 | 0 | *usp++ = *sp++; |
799 | 0 | } |
800 | 0 | break; |
801 | 0 | case NC_INT: |
802 | 0 | for (sp = (short *)src, ip = dest; count < len; count++) |
803 | 0 | *ip++ = *sp++; |
804 | 0 | break; |
805 | 0 | case NC_UINT: |
806 | 0 | for (sp = (short *)src, uip = dest; count < len; count++) |
807 | 0 | { |
808 | 0 | if (*sp < 0) |
809 | 0 | (*range_error)++; |
810 | 0 | *uip++ = *sp++; |
811 | 0 | } |
812 | 0 | break; |
813 | 0 | case NC_INT64: |
814 | 0 | for (sp = (short *)src, lip = dest; count < len; count++) |
815 | 0 | *lip++ = *sp++; |
816 | 0 | break; |
817 | 0 | case NC_UINT64: |
818 | 0 | for (sp = (short *)src, ulip = dest; count < len; count++) |
819 | 0 | { |
820 | 0 | if (*sp < 0) |
821 | 0 | (*range_error)++; |
822 | 0 | *ulip++ = *sp++; |
823 | 0 | } |
824 | 0 | break; |
825 | 0 | case NC_FLOAT: |
826 | 0 | for (sp = (short *)src, fp = dest; count < len; count++) |
827 | 0 | *fp++ = *sp++; |
828 | 0 | break; |
829 | 0 | case NC_DOUBLE: |
830 | 0 | for (sp = (short *)src, dp = dest; count < len; count++) |
831 | 0 | *dp++ = *sp++; |
832 | 0 | break; |
833 | 0 | default: |
834 | 0 | LOG((0, "%s: unexpected dest type. src_type %d, dest_type %d", |
835 | 0 | __func__, src_type, dest_type)); |
836 | 0 | return NC_EBADTYPE; |
837 | 0 | } |
838 | 0 | break; |
839 | | |
840 | 0 | case NC_USHORT: |
841 | 0 | switch (dest_type) |
842 | 0 | { |
843 | 0 | case NC_UBYTE: |
844 | 0 | for (usp = (unsigned short *)src, ubp = dest; count < len; count++) |
845 | 0 | { |
846 | 0 | if (*usp > X_UCHAR_MAX) |
847 | 0 | (*range_error)++; |
848 | 0 | *ubp++ = *usp++; |
849 | 0 | } |
850 | 0 | break; |
851 | 0 | case NC_BYTE: |
852 | 0 | for (usp = (unsigned short *)src, bp = dest; count < len; count++) |
853 | 0 | { |
854 | 0 | if (*usp > X_SCHAR_MAX) |
855 | 0 | (*range_error)++; |
856 | 0 | *bp++ = *usp++; |
857 | 0 | } |
858 | 0 | break; |
859 | 0 | case NC_SHORT: |
860 | 0 | for (usp = (unsigned short *)src, sp = dest; count < len; count++) |
861 | 0 | { |
862 | 0 | if (*usp > X_SHORT_MAX) |
863 | 0 | (*range_error)++; |
864 | 0 | *sp++ = *usp++; |
865 | 0 | } |
866 | 0 | break; |
867 | 0 | case NC_USHORT: |
868 | 0 | for (usp = (unsigned short *)src, usp1 = dest; count < len; count++) |
869 | 0 | *usp1++ = *usp++; |
870 | 0 | break; |
871 | 0 | case NC_INT: |
872 | 0 | for (usp = (unsigned short *)src, ip = dest; count < len; count++) |
873 | 0 | *ip++ = *usp++; |
874 | 0 | break; |
875 | 0 | case NC_UINT: |
876 | 0 | for (usp = (unsigned short *)src, uip = dest; count < len; count++) |
877 | 0 | *uip++ = *usp++; |
878 | 0 | break; |
879 | 0 | case NC_INT64: |
880 | 0 | for (usp = (unsigned short *)src, lip = dest; count < len; count++) |
881 | 0 | *lip++ = *usp++; |
882 | 0 | break; |
883 | 0 | case NC_UINT64: |
884 | 0 | for (usp = (unsigned short *)src, ulip = dest; count < len; count++) |
885 | 0 | *ulip++ = *usp++; |
886 | 0 | break; |
887 | 0 | case NC_FLOAT: |
888 | 0 | for (usp = (unsigned short *)src, fp = dest; count < len; count++) |
889 | 0 | *fp++ = *usp++; |
890 | 0 | break; |
891 | 0 | case NC_DOUBLE: |
892 | 0 | for (usp = (unsigned short *)src, dp = dest; count < len; count++) |
893 | 0 | *dp++ = *usp++; |
894 | 0 | break; |
895 | 0 | default: |
896 | 0 | LOG((0, "%s: unexpected dest type. src_type %d, dest_type %d", |
897 | 0 | __func__, src_type, dest_type)); |
898 | 0 | return NC_EBADTYPE; |
899 | 0 | } |
900 | 0 | break; |
901 | | |
902 | 0 | case NC_INT: |
903 | 0 | switch (dest_type) |
904 | 0 | { |
905 | 0 | case NC_UBYTE: |
906 | 0 | for (ip = (int *)src, ubp = dest; count < len; count++) |
907 | 0 | { |
908 | 0 | if (*ip > X_UCHAR_MAX || *ip < 0) |
909 | 0 | (*range_error)++; |
910 | 0 | *ubp++ = *ip++; |
911 | 0 | } |
912 | 0 | break; |
913 | 0 | case NC_BYTE: |
914 | 0 | for (ip = (int *)src, bp = dest; count < len; count++) |
915 | 0 | { |
916 | 0 | if (*ip > X_SCHAR_MAX || *ip < X_SCHAR_MIN) |
917 | 0 | (*range_error)++; |
918 | 0 | *bp++ = *ip++; |
919 | 0 | } |
920 | 0 | break; |
921 | 0 | case NC_SHORT: |
922 | 0 | for (ip = (int *)src, sp = dest; count < len; count++) |
923 | 0 | { |
924 | 0 | if (*ip > X_SHORT_MAX || *ip < X_SHORT_MIN) |
925 | 0 | (*range_error)++; |
926 | 0 | *sp++ = *ip++; |
927 | 0 | } |
928 | 0 | break; |
929 | 0 | case NC_USHORT: |
930 | 0 | for (ip = (int *)src, usp = dest; count < len; count++) |
931 | 0 | { |
932 | 0 | if (*ip > X_USHORT_MAX || *ip < 0) |
933 | 0 | (*range_error)++; |
934 | 0 | *usp++ = *ip++; |
935 | 0 | } |
936 | 0 | break; |
937 | 0 | case NC_INT: /* src is int */ |
938 | 0 | for (ip = (int *)src, ip1 = dest; count < len; count++) |
939 | 0 | { |
940 | 0 | if (*ip > X_INT_MAX || *ip < X_INT_MIN) |
941 | 0 | (*range_error)++; |
942 | 0 | *ip1++ = *ip++; |
943 | 0 | } |
944 | 0 | break; |
945 | 0 | case NC_UINT: |
946 | 0 | for (ip = (int *)src, uip = dest; count < len; count++) |
947 | 0 | { |
948 | 0 | if (*ip > X_UINT_MAX || *ip < 0) |
949 | 0 | (*range_error)++; |
950 | 0 | *uip++ = *ip++; |
951 | 0 | } |
952 | 0 | break; |
953 | 0 | case NC_INT64: |
954 | 0 | for (ip = (int *)src, lip = dest; count < len; count++) |
955 | 0 | *lip++ = *ip++; |
956 | 0 | break; |
957 | 0 | case NC_UINT64: |
958 | 0 | for (ip = (int *)src, ulip = dest; count < len; count++) |
959 | 0 | { |
960 | 0 | if (*ip < 0) |
961 | 0 | (*range_error)++; |
962 | 0 | *ulip++ = *ip++; |
963 | 0 | } |
964 | 0 | break; |
965 | 0 | case NC_FLOAT: |
966 | 0 | for (ip = (int *)src, fp = dest; count < len; count++) |
967 | 0 | *fp++ = *ip++; |
968 | 0 | break; |
969 | 0 | case NC_DOUBLE: |
970 | 0 | for (ip = (int *)src, dp = dest; count < len; count++) |
971 | 0 | *dp++ = *ip++; |
972 | 0 | break; |
973 | 0 | default: |
974 | 0 | LOG((0, "%s: unexpected dest type. src_type %d, dest_type %d", |
975 | 0 | __func__, src_type, dest_type)); |
976 | 0 | return NC_EBADTYPE; |
977 | 0 | } |
978 | 0 | break; |
979 | | |
980 | 0 | case NC_UINT: |
981 | 0 | switch (dest_type) |
982 | 0 | { |
983 | 0 | case NC_UBYTE: |
984 | 0 | for (uip = (unsigned int *)src, ubp = dest; count < len; count++) |
985 | 0 | { |
986 | 0 | if (*uip > X_UCHAR_MAX) |
987 | 0 | (*range_error)++; |
988 | 0 | *ubp++ = *uip++; |
989 | 0 | } |
990 | 0 | break; |
991 | 0 | case NC_BYTE: |
992 | 0 | for (uip = (unsigned int *)src, bp = dest; count < len; count++) |
993 | 0 | { |
994 | 0 | if (*uip > X_SCHAR_MAX) |
995 | 0 | (*range_error)++; |
996 | 0 | *bp++ = *uip++; |
997 | 0 | } |
998 | 0 | break; |
999 | 0 | case NC_SHORT: |
1000 | 0 | for (uip = (unsigned int *)src, sp = dest; count < len; count++) |
1001 | 0 | { |
1002 | 0 | if (*uip > X_SHORT_MAX) |
1003 | 0 | (*range_error)++; |
1004 | 0 | *sp++ = *uip++; |
1005 | 0 | } |
1006 | 0 | break; |
1007 | 0 | case NC_USHORT: |
1008 | 0 | for (uip = (unsigned int *)src, usp = dest; count < len; count++) |
1009 | 0 | { |
1010 | 0 | if (*uip > X_USHORT_MAX) |
1011 | 0 | (*range_error)++; |
1012 | 0 | *usp++ = *uip++; |
1013 | 0 | } |
1014 | 0 | break; |
1015 | 0 | case NC_INT: |
1016 | 0 | for (uip = (unsigned int *)src, ip = dest; count < len; count++) |
1017 | 0 | { |
1018 | 0 | if (*uip > X_INT_MAX) |
1019 | 0 | (*range_error)++; |
1020 | 0 | *ip++ = *uip++; |
1021 | 0 | } |
1022 | 0 | break; |
1023 | 0 | case NC_UINT: |
1024 | 0 | for (uip = (unsigned int *)src, uip1 = dest; count < len; count++) |
1025 | 0 | { |
1026 | 0 | if (*uip > X_UINT_MAX) |
1027 | 0 | (*range_error)++; |
1028 | 0 | *uip1++ = *uip++; |
1029 | 0 | } |
1030 | 0 | break; |
1031 | 0 | case NC_INT64: |
1032 | 0 | for (uip = (unsigned int *)src, lip = dest; count < len; count++) |
1033 | 0 | *lip++ = *uip++; |
1034 | 0 | break; |
1035 | 0 | case NC_UINT64: |
1036 | 0 | for (uip = (unsigned int *)src, ulip = dest; count < len; count++) |
1037 | 0 | *ulip++ = *uip++; |
1038 | 0 | break; |
1039 | 0 | case NC_FLOAT: |
1040 | 0 | for (uip = (unsigned int *)src, fp = dest; count < len; count++) |
1041 | 0 | *fp++ = *uip++; |
1042 | 0 | break; |
1043 | 0 | case NC_DOUBLE: |
1044 | 0 | for (uip = (unsigned int *)src, dp = dest; count < len; count++) |
1045 | 0 | *dp++ = *uip++; |
1046 | 0 | break; |
1047 | 0 | default: |
1048 | 0 | LOG((0, "%s: unexpected dest type. src_type %d, dest_type %d", |
1049 | 0 | __func__, src_type, dest_type)); |
1050 | 0 | return NC_EBADTYPE; |
1051 | 0 | } |
1052 | 0 | break; |
1053 | | |
1054 | 0 | case NC_INT64: |
1055 | 0 | switch (dest_type) |
1056 | 0 | { |
1057 | 0 | case NC_UBYTE: |
1058 | 0 | for (lip = (long long *)src, ubp = dest; count < len; count++) |
1059 | 0 | { |
1060 | 0 | if (*lip > X_UCHAR_MAX || *lip < 0) |
1061 | 0 | (*range_error)++; |
1062 | 0 | *ubp++ = *lip++; |
1063 | 0 | } |
1064 | 0 | break; |
1065 | 0 | case NC_BYTE: |
1066 | 0 | for (lip = (long long *)src, bp = dest; count < len; count++) |
1067 | 0 | { |
1068 | 0 | if (*lip > X_SCHAR_MAX || *lip < X_SCHAR_MIN) |
1069 | 0 | (*range_error)++; |
1070 | 0 | *bp++ = *lip++; |
1071 | 0 | } |
1072 | 0 | break; |
1073 | 0 | case NC_SHORT: |
1074 | 0 | for (lip = (long long *)src, sp = dest; count < len; count++) |
1075 | 0 | { |
1076 | 0 | if (*lip > X_SHORT_MAX || *lip < X_SHORT_MIN) |
1077 | 0 | (*range_error)++; |
1078 | 0 | *sp++ = *lip++; |
1079 | 0 | } |
1080 | 0 | break; |
1081 | 0 | case NC_USHORT: |
1082 | 0 | for (lip = (long long *)src, usp = dest; count < len; count++) |
1083 | 0 | { |
1084 | 0 | if (*lip > X_USHORT_MAX || *lip < 0) |
1085 | 0 | (*range_error)++; |
1086 | 0 | *usp++ = *lip++; |
1087 | 0 | } |
1088 | 0 | break; |
1089 | 0 | case NC_UINT: |
1090 | 0 | for (lip = (long long *)src, uip = dest; count < len; count++) |
1091 | 0 | { |
1092 | 0 | if (*lip > X_UINT_MAX || *lip < 0) |
1093 | 0 | (*range_error)++; |
1094 | 0 | *uip++ = *lip++; |
1095 | 0 | } |
1096 | 0 | break; |
1097 | 0 | case NC_INT: |
1098 | 0 | for (lip = (long long *)src, ip = dest; count < len; count++) |
1099 | 0 | { |
1100 | 0 | if (*lip > X_INT_MAX || *lip < X_INT_MIN) |
1101 | 0 | (*range_error)++; |
1102 | 0 | *ip++ = *lip++; |
1103 | 0 | } |
1104 | 0 | break; |
1105 | 0 | case NC_INT64: |
1106 | 0 | for (lip = (long long *)src, lip1 = dest; count < len; count++) |
1107 | 0 | *lip1++ = *lip++; |
1108 | 0 | break; |
1109 | 0 | case NC_UINT64: |
1110 | 0 | for (lip = (long long *)src, ulip = dest; count < len; count++) |
1111 | 0 | { |
1112 | 0 | if (*lip < 0) |
1113 | 0 | (*range_error)++; |
1114 | 0 | *ulip++ = *lip++; |
1115 | 0 | } |
1116 | 0 | break; |
1117 | 0 | case NC_FLOAT: |
1118 | 0 | for (lip = (long long *)src, fp = dest; count < len; count++) |
1119 | 0 | *fp++ = *lip++; |
1120 | 0 | break; |
1121 | 0 | case NC_DOUBLE: |
1122 | 0 | for (lip = (long long *)src, dp = dest; count < len; count++) |
1123 | 0 | *dp++ = *lip++; |
1124 | 0 | break; |
1125 | 0 | default: |
1126 | 0 | LOG((0, "%s: unexpected dest type. src_type %d, dest_type %d", |
1127 | 0 | __func__, src_type, dest_type)); |
1128 | 0 | return NC_EBADTYPE; |
1129 | 0 | } |
1130 | 0 | break; |
1131 | | |
1132 | 0 | case NC_UINT64: |
1133 | 0 | switch (dest_type) |
1134 | 0 | { |
1135 | 0 | case NC_UBYTE: |
1136 | 0 | for (ulip = (unsigned long long *)src, ubp = dest; count < len; count++) |
1137 | 0 | { |
1138 | 0 | if (*ulip > X_UCHAR_MAX) |
1139 | 0 | (*range_error)++; |
1140 | 0 | *ubp++ = *ulip++; |
1141 | 0 | } |
1142 | 0 | break; |
1143 | 0 | case NC_BYTE: |
1144 | 0 | for (ulip = (unsigned long long *)src, bp = dest; count < len; count++) |
1145 | 0 | { |
1146 | 0 | if (*ulip > X_SCHAR_MAX) |
1147 | 0 | (*range_error)++; |
1148 | 0 | *bp++ = *ulip++; |
1149 | 0 | } |
1150 | 0 | break; |
1151 | 0 | case NC_SHORT: |
1152 | 0 | for (ulip = (unsigned long long *)src, sp = dest; count < len; count++) |
1153 | 0 | { |
1154 | 0 | if (*ulip > X_SHORT_MAX) |
1155 | 0 | (*range_error)++; |
1156 | 0 | *sp++ = *ulip++; |
1157 | 0 | } |
1158 | 0 | break; |
1159 | 0 | case NC_USHORT: |
1160 | 0 | for (ulip = (unsigned long long *)src, usp = dest; count < len; count++) |
1161 | 0 | { |
1162 | 0 | if (*ulip > X_USHORT_MAX) |
1163 | 0 | (*range_error)++; |
1164 | 0 | *usp++ = *ulip++; |
1165 | 0 | } |
1166 | 0 | break; |
1167 | 0 | case NC_UINT: |
1168 | 0 | for (ulip = (unsigned long long *)src, uip = dest; count < len; count++) |
1169 | 0 | { |
1170 | 0 | if (*ulip > X_UINT_MAX) |
1171 | 0 | (*range_error)++; |
1172 | 0 | *uip++ = *ulip++; |
1173 | 0 | } |
1174 | 0 | break; |
1175 | 0 | case NC_INT: |
1176 | 0 | for (ulip = (unsigned long long *)src, ip = dest; count < len; count++) |
1177 | 0 | { |
1178 | 0 | if (*ulip > X_INT_MAX) |
1179 | 0 | (*range_error)++; |
1180 | 0 | *ip++ = *ulip++; |
1181 | 0 | } |
1182 | 0 | break; |
1183 | 0 | case NC_INT64: |
1184 | 0 | for (ulip = (unsigned long long *)src, lip = dest; count < len; count++) |
1185 | 0 | { |
1186 | 0 | if (*ulip > X_INT64_MAX) |
1187 | 0 | (*range_error)++; |
1188 | 0 | *lip++ = *ulip++; |
1189 | 0 | } |
1190 | 0 | break; |
1191 | 0 | case NC_UINT64: |
1192 | 0 | for (ulip = (unsigned long long *)src, ulip1 = dest; count < len; count++) |
1193 | 0 | *ulip1++ = *ulip++; |
1194 | 0 | break; |
1195 | 0 | case NC_FLOAT: |
1196 | 0 | for (ulip = (unsigned long long *)src, fp = dest; count < len; count++) |
1197 | 0 | *fp++ = *ulip++; |
1198 | 0 | break; |
1199 | 0 | case NC_DOUBLE: |
1200 | 0 | for (ulip = (unsigned long long *)src, dp = dest; count < len; count++) |
1201 | 0 | *dp++ = *ulip++; |
1202 | 0 | break; |
1203 | 0 | default: |
1204 | 0 | LOG((0, "%s: unexpected dest type. src_type %d, dest_type %d", |
1205 | 0 | __func__, src_type, dest_type)); |
1206 | 0 | return NC_EBADTYPE; |
1207 | 0 | } |
1208 | 0 | break; |
1209 | | |
1210 | 0 | case NC_FLOAT: |
1211 | 0 | switch (dest_type) |
1212 | 0 | { |
1213 | 0 | case NC_UBYTE: |
1214 | 0 | for (fp = (float *)src, ubp = dest; count < len; count++) |
1215 | 0 | { |
1216 | 0 | if (*fp > X_UCHAR_MAX || *fp < 0) |
1217 | 0 | (*range_error)++; |
1218 | 0 | *ubp++ = *fp++; |
1219 | 0 | } |
1220 | 0 | break; |
1221 | 0 | case NC_BYTE: |
1222 | 0 | for (fp = (float *)src, bp = dest; count < len; count++) |
1223 | 0 | { |
1224 | 0 | if (*fp > (double)X_SCHAR_MAX || *fp < (double)X_SCHAR_MIN) |
1225 | 0 | (*range_error)++; |
1226 | 0 | *bp++ = *fp++; |
1227 | 0 | } |
1228 | 0 | break; |
1229 | 0 | case NC_SHORT: |
1230 | 0 | for (fp = (float *)src, sp = dest; count < len; count++) |
1231 | 0 | { |
1232 | 0 | if (*fp > (double)X_SHORT_MAX || *fp < (double)X_SHORT_MIN) |
1233 | 0 | (*range_error)++; |
1234 | 0 | *sp++ = *fp++; |
1235 | 0 | } |
1236 | 0 | break; |
1237 | 0 | case NC_USHORT: |
1238 | 0 | for (fp = (float *)src, usp = dest; count < len; count++) |
1239 | 0 | { |
1240 | 0 | if (*fp > X_USHORT_MAX || *fp < 0) |
1241 | 0 | (*range_error)++; |
1242 | 0 | *usp++ = *fp++; |
1243 | 0 | } |
1244 | 0 | break; |
1245 | 0 | case NC_UINT: |
1246 | 0 | for (fp = (float *)src, uip = dest; count < len; count++) |
1247 | 0 | { |
1248 | 0 | if (*fp > X_UINT_MAX || *fp < 0) |
1249 | 0 | (*range_error)++; |
1250 | 0 | *uip++ = *fp++; |
1251 | 0 | } |
1252 | 0 | break; |
1253 | 0 | case NC_INT: |
1254 | 0 | for (fp = (float *)src, ip = dest; count < len; count++) |
1255 | 0 | { |
1256 | 0 | if (*fp > (double)X_INT_MAX || *fp < (double)X_INT_MIN) |
1257 | 0 | (*range_error)++; |
1258 | 0 | *ip++ = *fp++; |
1259 | 0 | } |
1260 | 0 | break; |
1261 | 0 | case NC_INT64: |
1262 | 0 | for (fp = (float *)src, lip = dest; count < len; count++) |
1263 | 0 | { |
1264 | 0 | if (*fp > X_INT64_MAX || *fp <X_INT64_MIN) |
1265 | 0 | (*range_error)++; |
1266 | 0 | *lip++ = *fp++; |
1267 | 0 | } |
1268 | 0 | break; |
1269 | 0 | case NC_UINT64: |
1270 | 0 | for (fp = (float *)src, lip = dest; count < len; count++) |
1271 | 0 | { |
1272 | 0 | if (*fp > X_UINT64_MAX || *fp < 0) |
1273 | 0 | (*range_error)++; |
1274 | 0 | *lip++ = *fp++; |
1275 | 0 | } |
1276 | 0 | break; |
1277 | 0 | case NC_FLOAT: |
1278 | 0 | for (fp = (float *)src, fp1 = dest; count < len; count++) |
1279 | 0 | *fp1++ = *fp++; |
1280 | 0 | break; |
1281 | 0 | case NC_DOUBLE: |
1282 | 0 | for (fp = (float *)src, dp = dest; count < len; count++) |
1283 | 0 | *dp++ = *fp++; |
1284 | 0 | break; |
1285 | 0 | default: |
1286 | 0 | LOG((0, "%s: unexpected dest type. src_type %d, dest_type %d", |
1287 | 0 | __func__, src_type, dest_type)); |
1288 | 0 | return NC_EBADTYPE; |
1289 | 0 | } |
1290 | 0 | break; |
1291 | | |
1292 | 0 | case NC_DOUBLE: |
1293 | 0 | switch (dest_type) |
1294 | 0 | { |
1295 | 0 | case NC_UBYTE: |
1296 | 0 | for (dp = (double *)src, ubp = dest; count < len; count++) |
1297 | 0 | { |
1298 | 0 | if (*dp > X_UCHAR_MAX || *dp < 0) |
1299 | 0 | (*range_error)++; |
1300 | 0 | *ubp++ = *dp++; |
1301 | 0 | } |
1302 | 0 | break; |
1303 | 0 | case NC_BYTE: |
1304 | 0 | for (dp = (double *)src, bp = dest; count < len; count++) |
1305 | 0 | { |
1306 | 0 | if (*dp > X_SCHAR_MAX || *dp < X_SCHAR_MIN) |
1307 | 0 | (*range_error)++; |
1308 | 0 | *bp++ = *dp++; |
1309 | 0 | } |
1310 | 0 | break; |
1311 | 0 | case NC_SHORT: |
1312 | 0 | for (dp = (double *)src, sp = dest; count < len; count++) |
1313 | 0 | { |
1314 | 0 | if (*dp > X_SHORT_MAX || *dp < X_SHORT_MIN) |
1315 | 0 | (*range_error)++; |
1316 | 0 | *sp++ = *dp++; |
1317 | 0 | } |
1318 | 0 | break; |
1319 | 0 | case NC_USHORT: |
1320 | 0 | for (dp = (double *)src, usp = dest; count < len; count++) |
1321 | 0 | { |
1322 | 0 | if (*dp > X_USHORT_MAX || *dp < 0) |
1323 | 0 | (*range_error)++; |
1324 | 0 | *usp++ = *dp++; |
1325 | 0 | } |
1326 | 0 | break; |
1327 | 0 | case NC_UINT: |
1328 | 0 | for (dp = (double *)src, uip = dest; count < len; count++) |
1329 | 0 | { |
1330 | 0 | if (*dp > X_UINT_MAX || *dp < 0) |
1331 | 0 | (*range_error)++; |
1332 | 0 | *uip++ = *dp++; |
1333 | 0 | } |
1334 | 0 | break; |
1335 | 0 | case NC_INT: |
1336 | 0 | for (dp = (double *)src, ip = dest; count < len; count++) |
1337 | 0 | { |
1338 | 0 | if (*dp > X_INT_MAX || *dp < X_INT_MIN) |
1339 | 0 | (*range_error)++; |
1340 | 0 | *ip++ = *dp++; |
1341 | 0 | } |
1342 | 0 | break; |
1343 | 0 | case NC_INT64: |
1344 | 0 | for (dp = (double *)src, lip = dest; count < len; count++) |
1345 | 0 | { |
1346 | 0 | if (*dp > X_INT64_MAX || *dp < X_INT64_MIN) |
1347 | 0 | (*range_error)++; |
1348 | 0 | *lip++ = *dp++; |
1349 | 0 | } |
1350 | 0 | break; |
1351 | 0 | case NC_UINT64: |
1352 | 0 | for (dp = (double *)src, lip = dest; count < len; count++) |
1353 | 0 | { |
1354 | 0 | if (*dp > X_UINT64_MAX || *dp < 0) |
1355 | 0 | (*range_error)++; |
1356 | 0 | *lip++ = *dp++; |
1357 | 0 | } |
1358 | 0 | break; |
1359 | 0 | case NC_FLOAT: |
1360 | 0 | for (dp = (double *)src, fp = dest; count < len; count++) |
1361 | 0 | { |
1362 | 0 | if (isgreater(*dp, X_FLOAT_MAX) || isless(*dp, X_FLOAT_MIN)) |
1363 | 0 | (*range_error)++; |
1364 | 0 | *fp++ = *dp++; |
1365 | 0 | } |
1366 | 0 | break; |
1367 | 0 | case NC_DOUBLE: |
1368 | 0 | for (dp = (double *)src, dp1 = dest; count < len; count++) |
1369 | 0 | *dp1++ = *dp++; |
1370 | 0 | break; |
1371 | 0 | default: |
1372 | 0 | LOG((0, "%s: unexpected dest type. src_type %d, dest_type %d", |
1373 | 0 | __func__, src_type, dest_type)); |
1374 | 0 | return NC_EBADTYPE; |
1375 | 0 | } |
1376 | 0 | break; |
1377 | | |
1378 | 0 | default: |
1379 | 0 | LOG((0, "%s: unexpected src type. src_type %d, dest_type %d", |
1380 | 0 | __func__, src_type, dest_type)); |
1381 | 0 | return NC_EBADTYPE; |
1382 | 0 | } |
1383 | | |
1384 | | /* If quantize is in use, determine masks, copy the data, do the |
1385 | | * quantization. */ |
1386 | 0 | if (quantize_mode == NC_QUANTIZE_BITGROOM) |
1387 | 0 | { |
1388 | 0 | if (dest_type == NC_FLOAT) |
1389 | 0 | { |
1390 | | /* BitGroom: alternately shave and set LSBs */ |
1391 | 0 | op1.fp = (float *)dest; |
1392 | 0 | u32_ptr = op1.ui32p; |
1393 | 0 | for (idx = 0L; idx < len; idx += 2L) |
1394 | 0 | if (op1.fp[idx] != mss_val_cmp_flt) |
1395 | 0 | u32_ptr[idx] &= msk_f32_u32_zro; |
1396 | 0 | for (idx = 1L; idx < len; idx += 2L) |
1397 | 0 | if (op1.fp[idx] != mss_val_cmp_flt && u32_ptr[idx] != 0U) /* Never quantize upwards floating point values of zero */ |
1398 | 0 | u32_ptr[idx] |= msk_f32_u32_one; |
1399 | 0 | } |
1400 | 0 | else |
1401 | 0 | { |
1402 | | /* BitGroom: alternately shave and set LSBs. */ |
1403 | 0 | op1.dp = (double *)dest; |
1404 | 0 | u64_ptr = op1.ui64p; |
1405 | 0 | for (idx = 0L; idx < len; idx += 2L) |
1406 | 0 | if (op1.dp[idx] != mss_val_cmp_dbl) |
1407 | 0 | u64_ptr[idx] &= msk_f64_u64_zro; |
1408 | 0 | for (idx = 1L; idx < len; idx += 2L) |
1409 | 0 | if (op1.dp[idx] != mss_val_cmp_dbl && u64_ptr[idx] != 0ULL) /* Never quantize upwards floating point values of zero */ |
1410 | 0 | u64_ptr[idx] |= msk_f64_u64_one; |
1411 | 0 | } |
1412 | 0 | } /* endif BitGroom */ |
1413 | |
|
1414 | 0 | if (quantize_mode == NC_QUANTIZE_BITROUND) |
1415 | 0 | { |
1416 | 0 | if (dest_type == NC_FLOAT) |
1417 | 0 | { |
1418 | | /* BitRound: Quantize to user-specified NSB with IEEE-rounding */ |
1419 | 0 | op1.fp = (float *)dest; |
1420 | 0 | u32_ptr = op1.ui32p; |
1421 | 0 | for (idx = 0L; idx < len; idx++){ |
1422 | 0 | if (op1.fp[idx] != mss_val_cmp_flt){ |
1423 | 0 | u32_ptr[idx] += msk_f32_u32_hshv; /* Add 1 to the MSB of LSBs, carry 1 to mantissa or even exponent */ |
1424 | 0 | u32_ptr[idx] &= msk_f32_u32_zro; /* Shave it */ |
1425 | 0 | } |
1426 | 0 | } |
1427 | 0 | } |
1428 | 0 | else |
1429 | 0 | { |
1430 | | /* BitRound: Quantize to user-specified NSB with IEEE-rounding */ |
1431 | 0 | op1.dp = (double *)dest; |
1432 | 0 | u64_ptr = op1.ui64p; |
1433 | 0 | for (idx = 0L; idx < len; idx++){ |
1434 | 0 | if (op1.dp[idx] != mss_val_cmp_dbl){ |
1435 | 0 | u64_ptr[idx] += msk_f64_u64_hshv; /* Add 1 to the MSB of LSBs, carry 1 to mantissa or even exponent */ |
1436 | 0 | u64_ptr[idx] &= msk_f64_u64_zro; /* Shave it */ |
1437 | 0 | } |
1438 | 0 | } |
1439 | 0 | } |
1440 | 0 | } /* endif BitRound */ |
1441 | | |
1442 | 0 | if (quantize_mode == NC_QUANTIZE_GRANULARBR) |
1443 | 0 | { |
1444 | 0 | if (dest_type == NC_FLOAT) |
1445 | 0 | { |
1446 | | /* Granular BitRound */ |
1447 | 0 | op1.fp = (float *)dest; |
1448 | 0 | u32_ptr = op1.ui32p; |
1449 | 0 | for (idx = 0L; idx < len; idx++) |
1450 | 0 | { |
1451 | | |
1452 | 0 | if((val = op1.fp[idx]) != mss_val_cmp_flt && u32_ptr[idx] != 0U) |
1453 | 0 | { |
1454 | 0 | mnt = frexp(val, &xpn_bs2); /* DGG19 p. 4102 (8) */ |
1455 | 0 | mnt_fabs = fabs(mnt); |
1456 | 0 | mnt_log10_fabs = log10(mnt_fabs); |
1457 | | /* 20211003 Continuous determination of dgt_nbr improves CR by ~10% */ |
1458 | 0 | dgt_nbr = (int)floor(xpn_bs2 * dgt_per_bit + mnt_log10_fabs) + 1; /* DGG19 p. 4102 (8.67) */ |
1459 | 0 | qnt_pwr = (int)floor(bit_per_dgt * (dgt_nbr - nsd)); /* DGG19 p. 4101 (7) */ |
1460 | 0 | prc_bnr_xpl_rqr = mnt_fabs == 0.0 ? 0 : abs((int)floor(xpn_bs2 - bit_per_dgt*mnt_log10_fabs) - qnt_pwr); /* Protect against mnt = -0.0 */ |
1461 | 0 | prc_bnr_xpl_rqr--; /* 20211003 Reduce formula result by 1 bit: Passes all tests, improves CR by ~10% */ |
1462 | |
|
1463 | 0 | bit_xpl_nbr_zro = BIT_XPL_NBR_SGN_FLT - prc_bnr_xpl_rqr; |
1464 | 0 | msk_f32_u32_zro = 0u; /* Zero all bits */ |
1465 | 0 | msk_f32_u32_zro = ~msk_f32_u32_zro; /* Turn all bits to ones */ |
1466 | | /* Bit Shave mask for AND: Left shift zeros into bits to be rounded, leave ones in untouched bits */ |
1467 | 0 | msk_f32_u32_zro <<= bit_xpl_nbr_zro; |
1468 | | /* Bit Set mask for OR: Put ones into bits to be set, zeros in untouched bits */ |
1469 | 0 | msk_f32_u32_one = ~msk_f32_u32_zro; |
1470 | 0 | msk_f32_u32_hshv = msk_f32_u32_one & (msk_f32_u32_zro >> 1); /* Set one bit: the MSB of LSBs */ |
1471 | 0 | u32_ptr[idx] += msk_f32_u32_hshv; /* Add 1 to the MSB of LSBs, carry 1 to mantissa or even exponent */ |
1472 | 0 | u32_ptr[idx] &= msk_f32_u32_zro; /* Shave it */ |
1473 | |
|
1474 | 0 | } /* !mss_val_cmp_flt */ |
1475 | |
|
1476 | 0 | } |
1477 | 0 | } |
1478 | 0 | else |
1479 | 0 | { |
1480 | | /* Granular BitRound */ |
1481 | 0 | op1.dp = (double *)dest; |
1482 | 0 | u64_ptr = op1.ui64p; |
1483 | 0 | for (idx = 0L; idx < len; idx++) |
1484 | 0 | { |
1485 | |
|
1486 | 0 | if((val = op1.dp[idx]) != mss_val_cmp_dbl && u64_ptr[idx] != 0ULL) |
1487 | 0 | { |
1488 | 0 | mnt = frexp(val, &xpn_bs2); /* DGG19 p. 4102 (8) */ |
1489 | 0 | mnt_fabs = fabs(mnt); |
1490 | 0 | mnt_log10_fabs = log10(mnt_fabs); |
1491 | | /* 20211003 Continuous determination of dgt_nbr improves CR by ~10% */ |
1492 | 0 | dgt_nbr = (int)floor(xpn_bs2 * dgt_per_bit + mnt_log10_fabs) + 1; /* DGG19 p. 4102 (8.67) */ |
1493 | 0 | qnt_pwr = (int)floor(bit_per_dgt * (dgt_nbr - nsd)); /* DGG19 p. 4101 (7) */ |
1494 | 0 | prc_bnr_xpl_rqr = mnt_fabs == 0.0 ? 0 : abs((int)floor(xpn_bs2 - bit_per_dgt*mnt_log10_fabs) - qnt_pwr); /* Protect against mnt = -0.0 */ |
1495 | 0 | prc_bnr_xpl_rqr--; /* 20211003 Reduce formula result by 1 bit: Passes all tests, improves CR by ~10% */ |
1496 | |
|
1497 | 0 | bit_xpl_nbr_zro = BIT_XPL_NBR_SGN_DBL - prc_bnr_xpl_rqr; |
1498 | 0 | msk_f64_u64_zro = 0ull; /* Zero all bits */ |
1499 | 0 | msk_f64_u64_zro = ~msk_f64_u64_zro; /* Turn all bits to ones */ |
1500 | | /* Bit Shave mask for AND: Left shift zeros into bits to be rounded, leave ones in untouched bits */ |
1501 | 0 | msk_f64_u64_zro <<= bit_xpl_nbr_zro; |
1502 | | /* Bit Set mask for OR: Put ones into bits to be set, zeros in untouched bits */ |
1503 | 0 | msk_f64_u64_one = ~msk_f64_u64_zro; |
1504 | 0 | msk_f64_u64_hshv = msk_f64_u64_one & (msk_f64_u64_zro >> 1); /* Set one bit: the MSB of LSBs */ |
1505 | 0 | u64_ptr[idx] += msk_f64_u64_hshv; /* Add 1 to the MSB of LSBs, carry 1 to mantissa or even exponent */ |
1506 | 0 | u64_ptr[idx] &= msk_f64_u64_zro; /* Shave it */ |
1507 | |
|
1508 | 0 | } /* !mss_val_cmp_dbl */ |
1509 | |
|
1510 | 0 | } |
1511 | 0 | } |
1512 | 0 | } /* endif GranularBR */ |
1513 | |
|
1514 | 0 | return NC_NOERR; |
1515 | 0 | } |
1516 | | |
1517 | | /** |
1518 | | * @internal What fill value should be used for a variable? |
1519 | | * |
1520 | | * @param h5 Pointer to HDF5 file info struct. |
1521 | | * @param var Pointer to variable info struct. |
1522 | | * @param fillp Pointer that gets pointer to fill value. |
1523 | | * |
1524 | | * @returns NC_NOERR No error. |
1525 | | * @returns NC_ENOMEM Out of memory. |
1526 | | * @author Ed Hartnett |
1527 | | */ |
1528 | | int |
1529 | | nc4_get_fill_value(NC_FILE_INFO_T *h5, NC_VAR_INFO_T *var, void **fillp) |
1530 | 0 | { |
1531 | 0 | size_t size; |
1532 | 0 | int retval; |
1533 | | |
1534 | | /* Find out how much space we need for this type's fill value. */ |
1535 | 0 | if (var->type_info->nc_type_class == NC_VLEN) |
1536 | 0 | size = sizeof(nc_vlen_t); |
1537 | 0 | else if (var->type_info->nc_type_class == NC_STRING) |
1538 | 0 | size = sizeof(char *); |
1539 | 0 | else |
1540 | 0 | { |
1541 | 0 | if ((retval = nc4_get_typelen_mem(h5, var->type_info->hdr.id, &size))) |
1542 | 0 | return retval; |
1543 | 0 | } |
1544 | 0 | assert(size); |
1545 | | |
1546 | | /* Allocate the space. */ |
1547 | 0 | if (!((*fillp) = calloc(1, size))) |
1548 | 0 | return NC_ENOMEM; |
1549 | | |
1550 | | /* If the user has set a fill_value for this var, use, otherwise |
1551 | | * find the default fill value. */ |
1552 | 0 | if (var->fill_value) |
1553 | 0 | { |
1554 | 0 | LOG((4, "Found a fill value for var %s", var->hdr.name)); |
1555 | 0 | if (var->type_info->nc_type_class == NC_VLEN) |
1556 | 0 | { |
1557 | 0 | nc_vlen_t *in_vlen = (nc_vlen_t *)(var->fill_value), *fv_vlen = (nc_vlen_t *)(*fillp); |
1558 | 0 | size_t basetypesize = 0; |
1559 | |
|
1560 | 0 | if((retval=nc4_get_typelen_mem(h5, var->type_info->u.v.base_nc_typeid, &basetypesize))) |
1561 | 0 | return retval; |
1562 | | |
1563 | 0 | fv_vlen->len = in_vlen->len; |
1564 | 0 | if (!(fv_vlen->p = malloc(basetypesize * in_vlen->len))) |
1565 | 0 | { |
1566 | 0 | free(*fillp); |
1567 | 0 | *fillp = NULL; |
1568 | 0 | return NC_ENOMEM; |
1569 | 0 | } |
1570 | 0 | memcpy(fv_vlen->p, in_vlen->p, in_vlen->len * basetypesize); |
1571 | 0 | } |
1572 | 0 | else if (var->type_info->nc_type_class == NC_STRING) |
1573 | 0 | { |
1574 | 0 | if (*(char **)var->fill_value) |
1575 | 0 | if (!(**(char ***)fillp = strdup(*(char **)var->fill_value))) |
1576 | 0 | { |
1577 | 0 | free(*fillp); |
1578 | 0 | *fillp = NULL; |
1579 | 0 | return NC_ENOMEM; |
1580 | 0 | } |
1581 | 0 | } |
1582 | 0 | else |
1583 | 0 | memcpy((*fillp), var->fill_value, size); |
1584 | 0 | } |
1585 | 0 | else |
1586 | 0 | { |
1587 | 0 | if (nc4_get_default_fill_value(var->type_info, *fillp)) |
1588 | 0 | { |
1589 | | /* Note: release memory, but don't return error on failure */ |
1590 | 0 | free(*fillp); |
1591 | 0 | *fillp = NULL; |
1592 | 0 | } |
1593 | 0 | } |
1594 | | |
1595 | 0 | return NC_NOERR; |
1596 | 0 | } |
1597 | | |
1598 | | /** |
1599 | | * @internal Get the length, in bytes, of one element of a type in |
1600 | | * memory. |
1601 | | * |
1602 | | * @param h5 Pointer to HDF5 file info struct. |
1603 | | * @param xtype NetCDF type ID. |
1604 | | * @param len Pointer that gets length in bytes. |
1605 | | * |
1606 | | * @returns NC_NOERR No error. |
1607 | | * @returns NC_EBADTYPE Type not found. |
1608 | | * @author Ed Hartnett |
1609 | | */ |
1610 | | int |
1611 | | nc4_get_typelen_mem(NC_FILE_INFO_T *h5, nc_type xtype, size_t *len) |
1612 | 0 | { |
1613 | 0 | NC_TYPE_INFO_T *type; |
1614 | 0 | int retval; |
1615 | |
|
1616 | 0 | LOG((4, "%s xtype: %d", __func__, xtype)); |
1617 | 0 | assert(len); |
1618 | | |
1619 | | /* If this is an atomic type, the answer is easy. */ |
1620 | 0 | switch (xtype) |
1621 | 0 | { |
1622 | 0 | case NC_BYTE: |
1623 | 0 | case NC_CHAR: |
1624 | 0 | case NC_UBYTE: |
1625 | 0 | *len = sizeof(char); |
1626 | 0 | return NC_NOERR; |
1627 | 0 | case NC_SHORT: |
1628 | 0 | case NC_USHORT: |
1629 | 0 | *len = sizeof(short); |
1630 | 0 | return NC_NOERR; |
1631 | 0 | case NC_INT: |
1632 | 0 | case NC_UINT: |
1633 | 0 | *len = sizeof(int); |
1634 | 0 | return NC_NOERR; |
1635 | 0 | case NC_FLOAT: |
1636 | 0 | *len = sizeof(float); |
1637 | 0 | return NC_NOERR; |
1638 | 0 | case NC_DOUBLE: |
1639 | 0 | *len = sizeof(double); |
1640 | 0 | return NC_NOERR; |
1641 | 0 | case NC_INT64: |
1642 | 0 | case NC_UINT64: |
1643 | 0 | *len = sizeof(long long); |
1644 | 0 | return NC_NOERR; |
1645 | 0 | case NC_STRING: |
1646 | 0 | *len = sizeof(char *); |
1647 | 0 | return NC_NOERR; |
1648 | 0 | } |
1649 | | |
1650 | | /* See if var is compound type. */ |
1651 | 0 | if ((retval = nc4_find_type(h5, xtype, &type))) |
1652 | 0 | return retval; |
1653 | | |
1654 | 0 | if (!type) |
1655 | 0 | return NC_EBADTYPE; |
1656 | | |
1657 | 0 | *len = type->size; |
1658 | |
|
1659 | 0 | LOG((5, "type->size: %d", type->size)); |
1660 | |
|
1661 | 0 | return NC_NOERR; |
1662 | 0 | } |
1663 | | |
1664 | | |
1665 | | /** |
1666 | | * @internal Check a set of chunksizes to see if they specify a chunk |
1667 | | * that is too big. |
1668 | | * |
1669 | | * @param grp Pointer to the group info. |
1670 | | * @param var Pointer to the var info. |
1671 | | * @param chunksizes Array of chunksizes to check. |
1672 | | * |
1673 | | * @returns ::NC_NOERR No error. |
1674 | | * @returns ::NC_EBADID Bad ncid. |
1675 | | * @returns ::NC_ENOTVAR Invalid variable ID. |
1676 | | * @returns ::NC_EBADCHUNK Bad chunksize. |
1677 | | */ |
1678 | | int |
1679 | | nc4_check_chunksizes(NC_GRP_INFO_T *grp, NC_VAR_INFO_T *var, const size_t *chunksizes) |
1680 | 0 | { |
1681 | 0 | double dprod; |
1682 | 0 | size_t type_len; |
1683 | 0 | int d; |
1684 | 0 | int retval; |
1685 | |
|
1686 | 0 | if ((retval = nc4_get_typelen_mem(grp->nc4_info, var->type_info->hdr.id, &type_len))) |
1687 | 0 | return retval; |
1688 | 0 | if (var->type_info->nc_type_class == NC_VLEN) |
1689 | 0 | dprod = (double)sizeof(nc_vlen_t); |
1690 | 0 | else |
1691 | 0 | dprod = (double)type_len; |
1692 | 0 | for (d = 0; d < var->ndims; d++) |
1693 | 0 | dprod *= (double)chunksizes[d]; |
1694 | |
|
1695 | 0 | if (dprod > (double) NC_MAX_UINT) |
1696 | 0 | return NC_EBADCHUNK; |
1697 | | |
1698 | 0 | return NC_NOERR; |
1699 | 0 | } |
1700 | | |
1701 | | /** |
1702 | | * @internal Determine some default chunksizes for a variable. |
1703 | | * |
1704 | | * @param grp Pointer to the group info. |
1705 | | * @param var Pointer to the var info. |
1706 | | * |
1707 | | * @returns ::NC_NOERR for success |
1708 | | * @returns ::NC_EBADID Bad ncid. |
1709 | | * @returns ::NC_ENOTVAR Invalid variable ID. |
1710 | | * @author Ed Hartnett, Dennis Heimbigner |
1711 | | */ |
1712 | | int |
1713 | | nc4_find_default_chunksizes2(NC_GRP_INFO_T *grp, NC_VAR_INFO_T *var) |
1714 | 0 | { |
1715 | 0 | int d; |
1716 | 0 | size_t type_size; |
1717 | 0 | float num_values = 1, num_unlim = 0; |
1718 | 0 | int retval; |
1719 | 0 | size_t suggested_size; |
1720 | | #ifdef LOGGING |
1721 | | double total_chunk_size; |
1722 | | #endif |
1723 | |
|
1724 | 0 | if (var->type_info->nc_type_class == NC_STRING) |
1725 | 0 | type_size = sizeof(char *); |
1726 | 0 | else |
1727 | 0 | type_size = var->type_info->size; |
1728 | |
|
1729 | | #ifdef LOGGING |
1730 | | /* Later this will become the total number of bytes in the default |
1731 | | * chunk. */ |
1732 | | total_chunk_size = (double) type_size; |
1733 | | #endif |
1734 | |
|
1735 | 0 | if(var->chunksizes == NULL) { |
1736 | 0 | if((var->chunksizes = calloc(1,sizeof(size_t)*var->ndims)) == NULL) |
1737 | 0 | return NC_ENOMEM; |
1738 | 0 | } |
1739 | | |
1740 | | /* How many values in the variable (or one record, if there are |
1741 | | * unlimited dimensions). */ |
1742 | 0 | for (d = 0; d < var->ndims; d++) |
1743 | 0 | { |
1744 | 0 | assert(var->dim[d]); |
1745 | 0 | if (! var->dim[d]->unlimited) |
1746 | 0 | num_values *= (float)var->dim[d]->len; |
1747 | 0 | else { |
1748 | 0 | num_unlim++; |
1749 | 0 | var->chunksizes[d] = 1; /* overwritten below, if all dims are unlimited */ |
1750 | 0 | } |
1751 | 0 | } |
1752 | | /* Special case to avoid 1D vars with unlim dim taking huge amount |
1753 | | of space (DEFAULT_CHUNK_SIZE bytes). Instead we limit to about |
1754 | | 4KB */ |
1755 | 0 | if (var->ndims == 1 && num_unlim == 1) { |
1756 | 0 | if (DEFAULT_CHUNK_SIZE / type_size <= 0) |
1757 | 0 | suggested_size = 1; |
1758 | 0 | else if (DEFAULT_CHUNK_SIZE / type_size > DEFAULT_1D_UNLIM_SIZE) |
1759 | 0 | suggested_size = DEFAULT_1D_UNLIM_SIZE; |
1760 | 0 | else |
1761 | 0 | suggested_size = DEFAULT_CHUNK_SIZE / type_size; |
1762 | 0 | var->chunksizes[0] = suggested_size / type_size; |
1763 | 0 | LOG((4, "%s: name %s dim %d DEFAULT_CHUNK_SIZE %d num_values %f type_size %d " |
1764 | 0 | "chunksize %ld", __func__, var->hdr.name, d, DEFAULT_CHUNK_SIZE, num_values, type_size, var->chunksizes[0])); |
1765 | 0 | } |
1766 | 0 | if (var->ndims > 1 && var->ndims == num_unlim) { /* all dims unlimited */ |
1767 | 0 | suggested_size = pow((double)DEFAULT_CHUNK_SIZE/type_size, 1.0/(double)(var->ndims)); |
1768 | 0 | for (d = 0; d < var->ndims; d++) |
1769 | 0 | { |
1770 | 0 | var->chunksizes[d] = suggested_size ? suggested_size : 1; |
1771 | 0 | LOG((4, "%s: name %s dim %d DEFAULT_CHUNK_SIZE %d num_values %f type_size %d " |
1772 | 0 | "chunksize %ld", __func__, var->hdr.name, d, DEFAULT_CHUNK_SIZE, num_values, type_size, var->chunksizes[d])); |
1773 | 0 | } |
1774 | 0 | } |
1775 | | |
1776 | | /* Pick a chunk length for each dimension, if one has not already |
1777 | | * been picked above. */ |
1778 | 0 | for (d = 0; d < var->ndims; d++) |
1779 | 0 | if (!var->chunksizes[d]) |
1780 | 0 | { |
1781 | 0 | suggested_size = (pow((double)DEFAULT_CHUNK_SIZE/(num_values * type_size), |
1782 | 0 | 1.0/(double)(var->ndims - num_unlim)) * var->dim[d]->len - .5); |
1783 | 0 | if (suggested_size > var->dim[d]->len) |
1784 | 0 | suggested_size = var->dim[d]->len; |
1785 | 0 | var->chunksizes[d] = suggested_size ? suggested_size : 1; |
1786 | 0 | LOG((4, "%s: name %s dim %d DEFAULT_CHUNK_SIZE %d num_values %f type_size %d " |
1787 | 0 | "chunksize %ld", __func__, var->hdr.name, d, DEFAULT_CHUNK_SIZE, num_values, type_size, var->chunksizes[d])); |
1788 | 0 | } |
1789 | |
|
1790 | | #ifdef LOGGING |
1791 | | /* Find total chunk size. */ |
1792 | | for (d = 0; d < var->ndims; d++) |
1793 | | total_chunk_size *= (double) var->chunksizes[d]; |
1794 | | LOG((4, "total_chunk_size %f", total_chunk_size)); |
1795 | | #endif |
1796 | | |
1797 | | /* But did this result in a chunk that is too big? */ |
1798 | 0 | retval = nc4_check_chunksizes(grp, var, var->chunksizes); |
1799 | 0 | if (retval) |
1800 | 0 | { |
1801 | | /* Other error? */ |
1802 | 0 | if (retval != NC_EBADCHUNK) |
1803 | 0 | return retval; |
1804 | | |
1805 | | /* Chunk is too big! Reduce each dimension by half and try again. */ |
1806 | 0 | for ( ; retval == NC_EBADCHUNK; retval = nc4_check_chunksizes(grp, var, var->chunksizes)) |
1807 | 0 | for (d = 0; d < var->ndims; d++) |
1808 | 0 | var->chunksizes[d] = var->chunksizes[d]/2 ? var->chunksizes[d]/2 : 1; |
1809 | 0 | } |
1810 | | |
1811 | | /* Do we have any big data overhangs? They can be dangerous to |
1812 | | * babies, the elderly, or confused campers who have had too much |
1813 | | * beer. */ |
1814 | 0 | for (d = 0; d < var->ndims; d++) |
1815 | 0 | { |
1816 | 0 | size_t num_chunks; |
1817 | 0 | size_t overhang; |
1818 | 0 | assert(var->chunksizes[d] > 0); |
1819 | 0 | num_chunks = (var->dim[d]->len + var->chunksizes[d] - 1) / var->chunksizes[d]; |
1820 | 0 | if(num_chunks > 0) { |
1821 | 0 | overhang = (num_chunks * var->chunksizes[d]) - var->dim[d]->len; |
1822 | 0 | var->chunksizes[d] -= overhang / num_chunks; |
1823 | 0 | } |
1824 | 0 | } |
1825 | | |
1826 | | |
1827 | 0 | return NC_NOERR; |
1828 | 0 | } |
1829 | | |
1830 | | /** |
1831 | | * @internal Get the default fill value for an atomic type. Memory for |
1832 | | * fill_value must already be allocated, or you are DOOMED! |
1833 | | * |
1834 | | * @param tinfo type object |
1835 | | * @param fill_value Pointer that gets the default fill value. |
1836 | | * |
1837 | | * @returns NC_NOERR No error. |
1838 | | * @returns NC_EINVAL Can't find atomic type. |
1839 | | * @author Ed Hartnett |
1840 | | */ |
1841 | | int |
1842 | | nc4_get_default_fill_value(NC_TYPE_INFO_T* tinfo, void *fill_value) |
1843 | 0 | { |
1844 | 0 | if(tinfo->hdr.id > NC_NAT && tinfo->hdr.id <= NC_MAX_ATOMIC_TYPE) |
1845 | 0 | return nc4_get_default_atomic_fill_value(tinfo->hdr.id,fill_value); |
1846 | 0 | #ifdef USE_NETCDF4 |
1847 | 0 | switch(tinfo->nc_type_class) { |
1848 | 0 | case NC_ENUM: |
1849 | 0 | return nc4_get_default_atomic_fill_value(tinfo->u.e.base_nc_typeid,fill_value); |
1850 | 0 | case NC_OPAQUE: |
1851 | 0 | case NC_VLEN: |
1852 | 0 | case NC_COMPOUND: |
1853 | 0 | if(fill_value) |
1854 | 0 | memset(fill_value,0,tinfo->size); |
1855 | 0 | break; |
1856 | 0 | default: return NC_EBADTYPE; |
1857 | 0 | } |
1858 | 0 | #endif |
1859 | 0 | return NC_NOERR; |
1860 | 0 | } |
1861 | | |
1862 | | /** |
1863 | | * @internal Get the default fill value for an atomic type. Memory for |
1864 | | * fill_value must already be allocated, or you are DOOMED! |
1865 | | * |
1866 | | * @param xtype type id |
1867 | | * @param fill_value Pointer that gets the default fill value. |
1868 | | * |
1869 | | * @returns NC_NOERR No error. |
1870 | | * @returns NC_EINVAL Can't find atomic type. |
1871 | | * @author Ed Hartnett |
1872 | | */ |
1873 | | int |
1874 | | nc4_get_default_atomic_fill_value(nc_type xtype, void *fill_value) |
1875 | 0 | { |
1876 | 0 | switch (xtype) |
1877 | 0 | { |
1878 | 0 | case NC_CHAR: |
1879 | 0 | *(char *)fill_value = NC_FILL_CHAR; |
1880 | 0 | break; |
1881 | | |
1882 | 0 | case NC_STRING: |
1883 | 0 | *(char **)fill_value = strdup(NC_FILL_STRING); |
1884 | 0 | break; |
1885 | | |
1886 | 0 | case NC_BYTE: |
1887 | 0 | *(signed char *)fill_value = NC_FILL_BYTE; |
1888 | 0 | break; |
1889 | | |
1890 | 0 | case NC_SHORT: |
1891 | 0 | *(short *)fill_value = NC_FILL_SHORT; |
1892 | 0 | break; |
1893 | | |
1894 | 0 | case NC_INT: |
1895 | 0 | *(int *)fill_value = NC_FILL_INT; |
1896 | 0 | break; |
1897 | | |
1898 | 0 | case NC_UBYTE: |
1899 | 0 | *(unsigned char *)fill_value = NC_FILL_UBYTE; |
1900 | 0 | break; |
1901 | | |
1902 | 0 | case NC_USHORT: |
1903 | 0 | *(unsigned short *)fill_value = NC_FILL_USHORT; |
1904 | 0 | break; |
1905 | | |
1906 | 0 | case NC_UINT: |
1907 | 0 | *(unsigned int *)fill_value = NC_FILL_UINT; |
1908 | 0 | break; |
1909 | | |
1910 | 0 | case NC_INT64: |
1911 | 0 | *(long long *)fill_value = NC_FILL_INT64; |
1912 | 0 | break; |
1913 | | |
1914 | 0 | case NC_UINT64: |
1915 | 0 | *(unsigned long long *)fill_value = NC_FILL_UINT64; |
1916 | 0 | break; |
1917 | | |
1918 | 0 | case NC_FLOAT: |
1919 | 0 | *(float *)fill_value = NC_FILL_FLOAT; |
1920 | 0 | break; |
1921 | | |
1922 | 0 | case NC_DOUBLE: |
1923 | 0 | *(double *)fill_value = NC_FILL_DOUBLE; |
1924 | 0 | break; |
1925 | | |
1926 | 0 | default: |
1927 | 0 | return NC_EINVAL; |
1928 | 0 | } |
1929 | 0 | return NC_NOERR; |
1930 | 0 | } |
1931 | | |