summaryrefslogtreecommitdiffstats
path: root/src/H5Zscaleoffset.c
blob: aeb0be16e4b444ef38f6ad54004408623683deb3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * Copyright by The HDF Group.                                               *
 * Copyright by the Board of Trustees of the University of Illinois.         *
 * All rights reserved.                                                      *
 *                                                                           *
 * This file is part of HDF5.  The full HDF5 copyright notice, including     *
 * terms governing use, modification, and redistribution, is contained in    *
 * the COPYING file, which can be found at the root of the source code       *
 * distribution tree, or in https://support.hdfgroup.org/ftp/HDF5/releases.  *
 * If you do not have access to either file, you may request a copy from     *
 * help@hdfgroup.org.                                                        *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

#define H5Z_PACKAGE		/*suppress error about including H5Zpkg	  */

#include "H5private.h"		/* Generic Functions			*/
#include "H5ACprivate.h"	/* Metadata cache			*/
#include "H5Eprivate.h"		/* Error handling		  	*/
#include "H5Iprivate.h"		/* IDs			  		*/
#include "H5MMprivate.h"	/* Memory management			*/
#include "H5Pprivate.h"         /* Property lists                       */
#include "H5Oprivate.h"         /* Object headers                       */
#include "H5Sprivate.h"		/* Dataspaces         			*/
#include "H5Tprivate.h"		/* Datatypes         			*/
#include "H5Zpkg.h"		/* Data filters				*/

/* Struct of parameters needed for compressing/decompressing one atomic datatype */
typedef struct {
   size_t size;        /* datatype size */
   uint32_t minbits;   /* minimum bits to compress one value of such datatype */
   unsigned mem_order; /* current memory endianness order */
} parms_atomic;

enum H5Z_scaleoffset_t {t_bad=0, t_uchar=1, t_ushort, t_uint, t_ulong, t_ulong_long,
                           t_schar, t_short, t_int, t_long, t_long_long,
                           t_float, t_double};

/* Local function prototypes */
static double H5Z_scaleoffset_rnd(double val);
static htri_t H5Z_can_apply_scaleoffset(hid_t dcpl_id, hid_t type_id, hid_t space_id);
static enum H5Z_scaleoffset_t H5Z_scaleoffset_get_type(unsigned dtype_class,
    unsigned dtype_size, unsigned dtype_sign);
static herr_t H5Z_scaleoffset_set_parms_fillval(H5P_genplist_t *dcpl_plist,
    H5T_t *type, enum H5Z_scaleoffset_t scale_type, unsigned cd_values[],
    int need_convert, hid_t dxpl_id);
static herr_t H5Z_set_local_scaleoffset(hid_t dcpl_id, hid_t type_id, hid_t space_id);
static size_t H5Z_filter_scaleoffset(unsigned flags, size_t cd_nelmts,
    const unsigned cd_values[], size_t nbytes, size_t *buf_size, void **buf);
static void H5Z_scaleoffset_convert(void *buf, unsigned d_nelmts, size_t dtype_size);
static unsigned H5Z_scaleoffset_log2(unsigned long long num);
static void H5Z_scaleoffset_precompress_i(void *data, unsigned d_nelmts,
    enum H5Z_scaleoffset_t type, unsigned filavail, const unsigned cd_values[],
    uint32_t *minbits, unsigned long long *minval);
static void H5Z_scaleoffset_postdecompress_i(void *data, unsigned d_nelmts,
    enum H5Z_scaleoffset_t type, unsigned filavail, const unsigned cd_values[],
    uint32_t minbits, unsigned long long minval);
static herr_t H5Z_scaleoffset_precompress_fd(void *data, unsigned d_nelmts,
    enum H5Z_scaleoffset_t type, unsigned filavail, const unsigned cd_values[],
    uint32_t *minbits, unsigned long long *minval, double D_val);
static herr_t H5Z_scaleoffset_postdecompress_fd(void *data, unsigned d_nelmts,
    enum H5Z_scaleoffset_t type, unsigned filavail, const unsigned cd_values[],
    uint32_t minbits, unsigned long long minval, double D_val);
static void H5Z_scaleoffset_next_byte(size_t *j, unsigned *buf_len);
static void H5Z_scaleoffset_decompress_one_byte(unsigned char *data, size_t data_offset,
    unsigned k, unsigned begin_i, unsigned char *buffer, size_t *j, unsigned *buf_len,
    parms_atomic p, unsigned dtype_len);
static void H5Z_scaleoffset_compress_one_byte(unsigned char *data, size_t data_offset,
    unsigned k, unsigned begin_i, unsigned char *buffer, size_t *j, unsigned *buf_len,
    parms_atomic p, unsigned dtype_len);
static void H5Z_scaleoffset_decompress_one_atomic(unsigned char *data, size_t data_offset,
    unsigned char *buffer, size_t *j, unsigned *buf_len, parms_atomic p);
static void H5Z_scaleoffset_compress_one_atomic(unsigned char *data, size_t data_offset,
    unsigned char *buffer, size_t *j, unsigned *buf_len, parms_atomic p);
static void H5Z_scaleoffset_decompress(unsigned char *data, unsigned d_nelmts,
    unsigned char *buffer, parms_atomic p);
static void H5Z_scaleoffset_compress(unsigned char *data, unsigned d_nelmts, unsigned char *buffer,
    size_t buffer_size, parms_atomic p);

/* This message derives from H5Z */
H5Z_class2_t H5Z_SCALEOFFSET[1] = {{
    H5Z_CLASS_T_VERS,       /* H5Z_class_t version */
    H5Z_FILTER_SCALEOFFSET, /* Filter id number		*/
    1,              /* Assume encoder present: check before registering */
    1,              /* decoder_present flag (set to true) */
    "scaleoffset",		/* Filter name for debugging	*/
    H5Z_can_apply_scaleoffset,	/* The "can apply" callback     */
    H5Z_set_local_scaleoffset,  /* The "set local" callback     */
    H5Z_filter_scaleoffset,	/* The actual filter function	*/
}};

/* Local macros */
#define H5Z_SCALEOFFSET_TOTAL_NPARMS     20   /* Total number of parameters for filter */
#define H5Z_SCALEOFFSET_PARM_SCALETYPE   0    /* "User" parameter for scale type */
#define H5Z_SCALEOFFSET_PARM_SCALEFACTOR 1    /* "User" parameter for scale factor */
#define H5Z_SCALEOFFSET_PARM_NELMTS      2    /* "Local" parameter for number of elements in the chunk */
#define H5Z_SCALEOFFSET_PARM_CLASS       3    /* "Local" parameter for datatype class */
#define H5Z_SCALEOFFSET_PARM_SIZE        4    /* "Local" parameter for datatype size */
#define H5Z_SCALEOFFSET_PARM_SIGN        5    /* "Local" parameter for integer datatype sign */
#define H5Z_SCALEOFFSET_PARM_ORDER       6    /* "Local" parameter for datatype byte order */
#define H5Z_SCALEOFFSET_PARM_FILAVAIL    7    /* "Local" parameter for dataset fill value existence */
#define H5Z_SCALEOFFSET_PARM_FILVAL      8    /* "Local" parameter for start location to store dataset fill value */

#define H5Z_SCALEOFFSET_CLS_INTEGER      0    /* Integer (datatype class) */
#define H5Z_SCALEOFFSET_CLS_FLOAT        1    /* Floatig-point (datatype class) */

#define H5Z_SCALEOFFSET_SGN_NONE         0    /* Unsigned integer type */
#define H5Z_SCALEOFFSET_SGN_2            1    /* Two's complement signed integer type */

#define H5Z_SCALEOFFSET_ORDER_LE         0    /* Little endian (datatype byte order) */
#define H5Z_SCALEOFFSET_ORDER_BE         1    /* Big endian (datatype byte order) */

#define H5Z_SCALEOFFSET_FILL_UNDEFINED   0    /* Fill value is not defined */
#define H5Z_SCALEOFFSET_FILL_DEFINED     1    /* Fill value is defined */

/* Store fill value in cd_values[] */
#define H5Z_scaleoffset_save_filval(type, cd_values, fill_val)                   \
{                                                                                \
    unsigned _i = H5Z_SCALEOFFSET_PARM_FILVAL; /* index into cd_values  */       \
    uint32_t _cd_value;         /* Current cd_value */                           \
    char *_fv_p;                /* Pointer to current byte in fill_val */        \
    size_t _copy_size = 4;      /* # of bytes to copy this iteration */          \
    size_t _size_rem = sizeof(type); /* # of bytes left to copy to cd_values */  \
                                                                                 \
    /* Store the fill value as the last entry in cd_values[]                     \
     * Store byte by byte from least significant byte to most significant byte   \
     * Plenty of space left for the fill value (from index 8 to 19)              \
     * H5O_pline_encode will byte-swap each individual cd value, but we still    \
     * need to swap the cd values as a whole if we are on a BE machine.  Note    \
     * that we need to make sure to put the data only in the lowest 4 bytes of   \
     * each, if sizeof(unsigned) > 4.                                            \
     */                                                                          \
    if(H5T_native_order_g == H5T_ORDER_LE) {                                     \
        _fv_p = (char *)&(fill_val);                                             \
        /* Copy 4 bytes at a time to each cd value */                            \
        do {                                                                     \
            if(_size_rem < 4) {                                                  \
                /* Amount left to copy is smaller than a cd_value, adjust copy   \
                 * size and initialize cd_value as it will not be fully          \
                 * overwritten */                                                \
                _copy_size = _size_rem;                                          \
                _cd_value = (uint32_t)0;                                         \
            } /* end if */                                                       \
                                                                                 \
            /* Copy the value */                                                 \
            HDmemcpy(&_cd_value, _fv_p, _copy_size);                             \
            (cd_values)[_i] = (unsigned)_cd_value;                               \
                                                                                 \
            /* Next field */                                                     \
            _i++;                                                                \
            _fv_p += _copy_size;                                                 \
            _size_rem -= _copy_size;                                             \
        } while(_size_rem);                                                      \
    } /* end if */                                                               \
    else {                                                                       \
        HDassert(H5T_native_order_g == H5T_ORDER_BE);                            \
                                                                                 \
        /* Copy 4 bytes at a time to each cd value, but start at the end         \
         * (highest address) of fill_val */                                      \
        _fv_p = ((char *)&(fill_val)) + sizeof(type) - MIN(4, _size_rem);        \
        while(_size_rem >= 4) {                                                  \
            /* Copy the value */                                                 \
            HDmemcpy(&_cd_value, _fv_p, _copy_size);                             \
            (cd_values)[_i] = (unsigned)_cd_value;                               \
                                                                                 \
            /* Next field */                                                     \
            _i++;                                                                \
            _size_rem -= 4;                                                      \
            if(_size_rem >= 4)                                                   \
                _fv_p -= 4;                                                      \
            else                                                                 \
                _fv_p -= _size_rem;                                              \
        } /* end while */                                                        \
                                                                                 \
        HDassert(_fv_p == (char *)&(fill_val));                                  \
        if(_size_rem) {                                                          \
            /* Amount left to copy is smaller than a cd_value, initialize        \
             * _cd_value as it will not be fully overwritten and copy to the end \
             * of _cd value as it is BE. */                                      \
            _cd_value = (uint32_t)0;                                             \
            HDmemcpy((char *)&_cd_value + 4 - _size_rem, _fv_p, _size_rem);      \
            (cd_values)[_i] = (unsigned)_cd_value;                               \
        } /* end if */                                                           \
    } /* end else */                                                             \
}

/* Set the fill value parameter in cd_values[] for unsigned integer type */
#define H5Z_scaleoffset_set_filval_1(type, dcpl_plist, dt, cd_values, need_convert, dxpl_id)\
{                                                                                    \
    type fill_val;                                                                   \
                                                                                     \
    /* Get dataset fill value */                                                     \
    if(H5P_get_fill_value(dcpl_plist, dt, &fill_val, dxpl_id) < 0)                   \
        HGOTO_ERROR(H5E_PLINE, H5E_CANTGET, FAIL, "unable to get fill value")        \
                                                                                     \
    if(need_convert)                                                                 \
       H5Z_scaleoffset_convert(&fill_val, 1, sizeof(type));                          \
                                                                                     \
    H5Z_scaleoffset_save_filval(type, cd_values, fill_val)                           \
}

/* Set the fill value parameter in cd_values[] for signed integer type */
#define H5Z_scaleoffset_set_filval_2(type, dcpl_plist, dt, cd_values, need_convert, dxpl_id)\
{                                                                                    \
    type fill_val;                                                                   \
                                                                                     \
    /* Get dataset fill value */                                                     \
    if(H5P_get_fill_value(dcpl_plist, dt, &fill_val, dxpl_id) < 0)                   \
        HGOTO_ERROR(H5E_PLINE, H5E_CANTGET, FAIL, "unable to get fill value")        \
                                                                                     \
    if(need_convert)                                                                 \
       H5Z_scaleoffset_convert(&fill_val, 1, sizeof(type));                          \
                                                                                     \
    H5Z_scaleoffset_save_filval(unsigned type, cd_values, fill_val)                  \
}

/* Set the fill value parameter in cd_values[] for character integer type */
#define H5Z_scaleoffset_set_filval_3(type, dcpl_plist, dt, cd_values, need_convert, dxpl_id)\
{                                                                                    \
    type fill_val;                                                                   \
                                                                                     \
    /* Get dataset fill value */                                                     \
    if(H5P_get_fill_value(dcpl_plist, dt, &fill_val, dxpl_id) < 0)                   \
        HGOTO_ERROR(H5E_PLINE, H5E_CANTGET, FAIL, "unable to get fill value")        \
                                                                                     \
    /* Store the fill value as the last entry in cd_values[] */                      \
    (cd_values)[H5Z_SCALEOFFSET_PARM_FILVAL] = (unsigned)((unsigned char)fill_val);  \
}

/* Set the fill value parameter in cd_values[] for floating-point type */
#define H5Z_scaleoffset_set_filval_4(type, dcpl_plist, dt, cd_values, need_convert, dxpl_id)\
{                                                                             \
    type fill_val;                                                            \
                                                                              \
    /* Get dataset fill value */                                              \
    if(H5P_get_fill_value(dcpl_plist, dt, &fill_val, dxpl_id) < 0)            \
        HGOTO_ERROR(H5E_PLINE, H5E_CANTGET, FAIL, "unable to get fill value") \
                                                                              \
    if(need_convert)                                                          \
       H5Z_scaleoffset_convert(&fill_val, 1, sizeof(type));                   \
                                                                              \
    H5Z_scaleoffset_save_filval(type, cd_values, fill_val)                    \
}

/* Get the fill value for integer type */
#define H5Z_scaleoffset_get_filval_1(type, cd_values, fill_val)                  \
{                                                                                \
    unsigned _i = H5Z_SCALEOFFSET_PARM_FILVAL; /* index into cd_values  */       \
    uint32_t _cd_value;         /* Current cd_value */                           \
    char *_fv_p;                /* Pointer to current byte in fill_val */        \
    size_t _copy_size = 4;      /* # of bytes to copy this iteration */          \
    size_t _size_rem = sizeof(type); /* # of bytes left to copy to filval */     \
                                                                                 \
    /* Retrieve the fill value from the last entry in cd_values[]                \
     * Store byte by byte from least significant byte to most significant byte   \
     * Plenty of space left for the fill value (from index 8 to 19)              \
     * H5O_pline_encode will byte-swap each individual cd value, but we still    \
     * need to swap the cd values as a whole if we are on a BE machine.  Note    \
     * that we need to make sure to put the data only in the lowest 4 bytes of   \
     * each, if sizeof(unsigned) > 4.                                            \
     */                                                                          \
    if(H5T_native_order_g == H5T_ORDER_LE) {                                     \
        _fv_p = (char *)&(fill_val);                                             \
        /* Copy 4 bytes at a time to each cd value */                            \
        do {                                                                     \
            if(_size_rem < 4)                                                    \
                /* Amount left to copy is smaller than a cd_value, adjust copy   \
                 * size and initialize cd_value as it will not be fully          \
                 * overwritten */                                                \
                _copy_size = _size_rem;                                          \
                                                                                 \
            /* Copy the value */                                                 \
            _cd_value = (uint32_t)(cd_values)[_i];                               \
            HDmemcpy(_fv_p, &_cd_value, _copy_size);                             \
                                                                                 \
            /* Next field */                                                     \
            _i++;                                                                \
            _fv_p += _copy_size;                                                 \
            _size_rem -= _copy_size;                                             \
        } while(_size_rem);                                                      \
    } /* end if */                                                               \
    else {                                                                       \
        HDassert(H5T_native_order_g == H5T_ORDER_BE);                            \
                                                                                 \
        /* Copy 4 bytes at a time to each cd value, but start at the end         \
         * (highest address) of fill_val */                                      \
        _fv_p = ((char *)&(fill_val)) + sizeof(type) - MIN(4, _size_rem);        \
        while(_size_rem >= 4) {                                                  \
            /* Copy the value */                                                 \
            _cd_value = (uint32_t)(cd_values)[_i];                               \
            HDmemcpy(_fv_p, &_cd_value, _copy_size);                             \
                                                                                 \
            /* Next field */                                                     \
            _i++;                                                                \
            _size_rem -= 4;                                                      \
            if(_size_rem >=4)                                                    \
                _fv_p -= 4;                                                      \
            else                                                                 \
                _fv_p -= _size_rem;                                              \
        } /* end while */                                                        \
                                                                                 \
        HDassert(_fv_p == (char *)&(fill_val));                                  \
        if(_size_rem) {                                                          \
            /* Amount left to copy is smaller than a cd_value, initialize        \
             * _cd_value as it will not be fully overwritten and copy to the end \
             * of _cd value as it is BE. */                                      \
            _cd_value = (uint32_t)(cd_values)[_i];                               \
            HDmemcpy(_fv_p, (char *)&_cd_value + 4 - _size_rem, _size_rem);      \
        } /* end if */                                                           \
    } /* end else */                                                             \
}

/* Get the fill value for floating-point type */
#define H5Z_scaleoffset_get_filval_2(type, cd_values, filval)                          \
{                                                                                      \
   if(sizeof(type) <= sizeof(long long))                                               \
      H5Z_scaleoffset_get_filval_1(type, cd_values, filval)                            \
   else                                                                                \
      HGOTO_ERROR(H5E_PLINE, H5E_BADTYPE, FAIL, "cannot find matched integer dataype") \
}

/* Find maximum and minimum values of a buffer with fill value defined for integer type */
#define H5Z_scaleoffset_max_min_1(i, d_nelmts, buf, filval, max, min)\
{                                                                  \
   i = 0; while(i < d_nelmts && buf[i]== filval) i++;              \
   if(i < d_nelmts) min = max = buf[i];                            \
   for(; i < d_nelmts; i++) {                                      \
      if(buf[i] == filval) continue; /* ignore fill value */       \
      if(buf[i] > max) max = buf[i];                               \
      if(buf[i] < min) min = buf[i];                               \
   }                                                               \
}

/* Find maximum and minimum values of a buffer with fill value undefined */
#define H5Z_scaleoffset_max_min_2(i, d_nelmts, buf, max, min)\
{                                                            \
   min = max = buf[0];                                       \
   for(i = 0; i < d_nelmts; i++) {                           \
      if(buf[i] > max) max = buf[i];                         \
      if(buf[i] < min) min = buf[i];                         \
   }                                                         \
}

/* Find maximum and minimum values of a buffer with fill value defined for floating-point type */
#define H5Z_scaleoffset_max_min_3(i, d_nelmts, buf, filval, max, min, D_val)      \
{                                                                                 \
   i = 0; while(i < d_nelmts && HDfabs(buf[i] - filval) < HDpow(10.0f, -D_val)) i++; \
   if(i < d_nelmts) min = max = buf[i];                                           \
   for(; i < d_nelmts; i++) {                                                     \
      if(HDfabs(buf[i] - filval) < HDpow(10.0f, -D_val))                          \
         continue; /* ignore fill value */                                        \
      if(buf[i] > max) max = buf[i];                                              \
      if(buf[i] < min) min = buf[i];                                              \
   }                                                                              \
}

/* Find minimum value of a buffer with fill value defined for integer type */
#define H5Z_scaleoffset_min_1(i, d_nelmts, buf, filval, min) \
{                                                            \
   i = 0; while(i < d_nelmts && buf[i]== filval) i++;        \
   if(i < d_nelmts) min = buf[i];                            \
   for(; i < d_nelmts; i++) {                                \
      if(buf[i] == filval) continue; /* ignore fill value */ \
      if(buf[i] < min) min = buf[i];                         \
   }                                                         \
}

/* Find minimum value of a buffer with fill value undefined */
#define H5Z_scaleoffset_min_2(i, d_nelmts, buf, min)\
{                                                   \
   min = buf[0];                                    \
   for(i = 0; i < d_nelmts; i++)                    \
      if(buf[i] < min) min = buf[i];                \
}

/* Check and handle special situation for unsigned integer type */
#define H5Z_scaleoffset_check_1(type, max, min, minbits) \
{                                                        \
   if(max - min > (type)(~(type)0 - 2))                  \
   { *minbits = sizeof(type)*8; return; }                \
}

/* Check and handle special situation for signed integer type */
#define H5Z_scaleoffset_check_2(type, max, min, minbits)                   \
{                                                                          \
   if((unsigned type)(max - min) > (unsigned type)(~(unsigned type)0 - 2)) \
   { *minbits = sizeof(type)*8; return; }                                  \
}

/* Check and handle special situation for floating-point type */
#define H5Z_scaleoffset_check_3(i, type, max, min, minbits, D_val)                    \
{                                                                                     \
   if(sizeof(type)==sizeof(int)) {                                                    \
      if(H5Z_scaleoffset_rnd(max*HDpow(10.0f, D_val) - min*HDpow(10.0f, D_val))       \
         > HDpow(2.0f, (double)(sizeof(int)*8 - 1))) {                                \
         *minbits = sizeof(int)*8; goto done;                                         \
      }                                                                               \
   } else if(sizeof(type)==sizeof(long)) {                                            \
      if(H5Z_scaleoffset_rnd(max*HDpow(10.0f, D_val) - min*HDpow(10.0f, D_val))       \
         > HDpow(2.0f, (double)(sizeof(long)*8 - 1))) {                               \
         *minbits = sizeof(long)*8; goto done;                                        \
      }                                                                               \
   } else if(sizeof(type)==sizeof(long long)) {                                       \
      if(H5Z_scaleoffset_rnd(max*HDpow(10.0f, D_val) - min*HDpow(10.0f, D_val))       \
         > HDpow(2.0f, (double)(sizeof(long long)*8 - 1))) {                          \
         *minbits = sizeof(long long)*8; goto done;                                   \
      }                                                                               \
   } else                                                                             \
      HGOTO_ERROR(H5E_PLINE, H5E_BADTYPE, FAIL, "cannot find matched integer dataype")\
}

/* Precompress for unsigned integer type */
#define H5Z_scaleoffset_precompress_1(type, data, d_nelmts, filavail, cd_values, minbits, minval)\
{                                                                                          \
    type *buf = (type *)data, min = 0, max = 0, span, filval = 0;                          \
    unsigned i;                                                                            \
                                                                                           \
    if(filavail == H5Z_SCALEOFFSET_FILL_DEFINED) { /* fill value defined */                \
        H5Z_scaleoffset_get_filval_1(type, cd_values, filval)                              \
        if(*minbits == H5Z_SO_INT_MINBITS_DEFAULT) { /* minbits not set yet, calculate max, min, and minbits */ \
            H5Z_scaleoffset_max_min_1(i, d_nelmts, buf, filval, max, min)                  \
            H5Z_scaleoffset_check_1(type, max, min, minbits)                               \
            span = (type)(max - min + 1);                                                  \
            *minbits = H5Z_scaleoffset_log2((unsigned long long)(span+1));                 \
        } else /* minbits already set, only calculate min */                               \
            H5Z_scaleoffset_min_1(i, d_nelmts, buf, filval, min)                           \
        if(*minbits != sizeof(type)*8) /* change values if minbits != full precision */    \
            for(i = 0; i < d_nelmts; i++)                                                  \
                buf[i] = (type)((buf[i] == filval) ? (((type)1 << *minbits) - 1) : (buf[i] - min)); \
    } else { /* fill value undefined */                                                    \
        if(*minbits == H5Z_SO_INT_MINBITS_DEFAULT ) { /* minbits not set yet, calculate max, min, and minbits */ \
            H5Z_scaleoffset_max_min_2(i, d_nelmts, buf, max, min)                          \
            H5Z_scaleoffset_check_1(type, max, min, minbits)                               \
            span = (type)(max - min + 1);                                                  \
            *minbits = H5Z_scaleoffset_log2((unsigned long long)span);                     \
        } else /* minbits already set, only calculate min */                               \
            H5Z_scaleoffset_min_2(i, d_nelmts, buf, min)                                   \
        if(*minbits != sizeof(type)*8) /* change values if minbits != full precision */    \
            for(i = 0; i < d_nelmts; i++)                                                  \
                buf[i] = (type)(buf[i] - min);                                             \
    }                                                                                      \
    *minval = min;                                                                         \
}

/* Precompress for signed integer type */
#define H5Z_scaleoffset_precompress_2(type, data, d_nelmts, filavail, cd_values, minbits, minval)\
{                                                                                            \
   type *buf = (type *)data, min = 0, max = 0, filval = 0;                                   \
   unsigned type span; unsigned i;                                                           \
                                                                                             \
   if(filavail == H5Z_SCALEOFFSET_FILL_DEFINED) { /* fill value defined */                   \
      H5Z_scaleoffset_get_filval_1(type, cd_values, filval)                                  \
      if(*minbits == H5Z_SO_INT_MINBITS_DEFAULT) { /* minbits not set yet, calculate max, min, and minbits */ \
         H5Z_scaleoffset_max_min_1(i, d_nelmts, buf, filval, max, min)                       \
         H5Z_scaleoffset_check_2(type, max, min, minbits)                                    \
         span = (unsigned type)(max - min + 1);                                              \
         *minbits = H5Z_scaleoffset_log2((unsigned long long)(span + 1));                    \
      } else /* minbits already set, only calculate min */                                   \
         H5Z_scaleoffset_min_1(i, d_nelmts, buf, filval, min)                                \
      if(*minbits != sizeof(type) * 8) /* change values if minbits != full precision */      \
         for(i = 0; i < d_nelmts; i++)                                                       \
            buf[i] = (type)((buf[i] == filval) ? (type)(((unsigned type)1 << *minbits) - 1) : (buf[i] - min)); \
   } else { /* fill value undefined */                                                       \
      if(*minbits == H5Z_SO_INT_MINBITS_DEFAULT ) { /* minbits not set yet, calculate max, min, and minbits */\
         H5Z_scaleoffset_max_min_2(i, d_nelmts, buf, max, min)                               \
         H5Z_scaleoffset_check_2(type, max, min, minbits)                                    \
         span = (unsigned type)(max - min + 1);                                              \
         *minbits = H5Z_scaleoffset_log2((unsigned long long)span);                          \
      } else /* minbits already set, only calculate min */                                   \
         H5Z_scaleoffset_min_2(i, d_nelmts, buf, min)                                        \
      if(*minbits != sizeof(type) * 8) /* change values if minbits != full precision */      \
         for(i = 0; i < d_nelmts; i++)                                                       \
            buf[i] = (type)(buf[i] - min);                                                   \
   }                                                                                         \
   *minval = (unsigned long long)min;                                                        \
}

/* Modify values of data in precompression if fill value defined for floating-point type */
#define H5Z_scaleoffset_modify_1(i, type, buf, d_nelmts, filval, minbits, min, D_val) \
{                                                                                     \
   if(sizeof(type)==sizeof(int))                                                      \
      for(i = 0; i < d_nelmts; i++) {                                                 \
         if(HDfabs(buf[i] - filval) < HDpow(10.0f, -D_val))                           \
            *(int *)&buf[i] = (int)(((unsigned int)1 << *minbits) - 1);               \
         else                                                                         \
            *(int *)&buf[i] = H5Z_scaleoffset_rnd(                                    \
                              buf[i]*HDpow(10.0f, D_val) - min*HDpow(10.0f, D_val));  \
      }                                                                               \
   else if(sizeof(type)==sizeof(long))                                                \
      for(i = 0; i < d_nelmts; i++) {                                                 \
         if(HDfabs(buf[i] - filval) < HDpow(10.0f, -D_val))                           \
            *(long *)&buf[i] = (long)(((unsigned long)1 << *minbits) - 1);            \
         else                                                                         \
            *(long *)&buf[i] = H5Z_scaleoffset_rnd(                                   \
                               buf[i]*HDpow(10.0f, D_val) - min*HDpow(10.0f, D_val)); \
      }                                                                               \
   else if(sizeof(type)==sizeof(long long))                                           \
      for(i = 0; i < d_nelmts; i++) {                                                 \
         if(HDfabs(buf[i] - filval) < HDpow(10.0f, -D_val))                           \
            *(long long *)&buf[i] = (long long)(((unsigned long long)1 << *minbits) - 1); \
         else                                                                         \
            *(long long *)&buf[i] = H5Z_scaleoffset_rnd(                              \
                                    buf[i]*HDpow(10.0f, D_val) - min*HDpow(10.0f, D_val)); \
      }                                                                               \
   else                                                                               \
      HGOTO_ERROR(H5E_PLINE, H5E_BADTYPE, FAIL, "cannot find matched integer dataype")\
}

/* Modify values of data in precompression if fill value undefined for floating-point type */
#define H5Z_scaleoffset_modify_2(i, type, buf, d_nelmts, min, D_val)                  \
{                                                                                     \
   if(sizeof(type)==sizeof(int))                                                      \
      for(i = 0; i < d_nelmts; i++)                                                   \
         *(int *)&buf[i] = H5Z_scaleoffset_rnd(                                       \
                           buf[i]*HDpow(10.0f, D_val) - min*HDpow(10.0f, D_val));     \
   else if(sizeof(type)==sizeof(long))                                                \
      for(i = 0; i < d_nelmts; i++)                                                   \
         *(long *)&buf[i] = H5Z_scaleoffset_rnd(                                      \
                            buf[i]*HDpow(10.0f, D_val) - min*HDpow(10.0f, D_val));    \
   else if(sizeof(type)==sizeof(long long))                                           \
      for(i = 0; i < d_nelmts; i++)                                                   \
         *(long long *)&buf[i] = H5Z_scaleoffset_rnd(                                 \
                                 buf[i]*HDpow(10.0f, D_val) - min*HDpow(10.0f, D_val)); \
   else                                                                               \
      HGOTO_ERROR(H5E_PLINE, H5E_BADTYPE, FAIL, "cannot find matched integer dataype")\
}

/* Save the minimum value for floating-point type */
#define H5Z_scaleoffset_save_min(i, type, minval, min)                                \
{                                                                                     \
    if(sizeof(type) <= sizeof(long long))                                             \
        /* Save min value to corresponding position                                   \
         * byte-order will be swapped as appropriate, but be sure to                  \
         * account for offset in BE if sizes differ                                   \
         */                                                                           \
        if(H5T_native_order_g == H5T_ORDER_LE)                                        \
            HDmemcpy(minval, &min, sizeof(type));                                     \
        else {                                                                        \
            HDassert(H5T_native_order_g == H5T_ORDER_BE);                             \
            HDmemcpy(((char *)minval) + (sizeof(long long) - sizeof(type)),           \
                    &min, sizeof(type));                                              \
        } /* end else */                                                              \
    else                                                                              \
        HGOTO_ERROR(H5E_PLINE, H5E_BADTYPE, FAIL, "cannot find matched integer dataype") \
}

/* Precompress for floating-point type using variable-minimum-bits method */
#define H5Z_scaleoffset_precompress_3(type, data, d_nelmts, filavail, cd_values,         \
                                      minbits, minval, D_val)                            \
{                                                                                        \
   type *buf = (type *)data, min = 0, max = 0, filval = 0;                               \
   unsigned long long span;                                                              \
   unsigned i;                                                                           \
                                                                                         \
   *minval = 0;                                                                          \
   if(filavail == H5Z_SCALEOFFSET_FILL_DEFINED) { /* fill value defined */               \
      H5Z_scaleoffset_get_filval_2(type, cd_values, filval)                              \
      H5Z_scaleoffset_max_min_3(i, d_nelmts, buf, filval, max, min, D_val)               \
      H5Z_scaleoffset_check_3(i, type, max, min, minbits, D_val)                         \
      span = H5Z_scaleoffset_rnd(max * HDpow(10.0f, D_val) - min * HDpow(10.0f, D_val)) + 1; \
      *minbits = H5Z_scaleoffset_log2((unsigned long long)(span + 1));                   \
      if(*minbits != sizeof(type) * 8) /* change values if minbits != full precision */  \
         H5Z_scaleoffset_modify_1(i, type, buf, d_nelmts, filval, minbits, min, D_val)   \
   } else { /* fill value undefined */                                                   \
      H5Z_scaleoffset_max_min_2(i, d_nelmts, buf, max, min)                              \
      H5Z_scaleoffset_check_3(i, type, max, min, minbits, D_val)                         \
      span = H5Z_scaleoffset_rnd(max * HDpow(10.0f, D_val) - min * HDpow(10.0f, D_val)) + 1; \
      *minbits = H5Z_scaleoffset_log2((unsigned long long)span);                         \
      if(*minbits != sizeof(type) * 8) /* change values if minbits != full precision */  \
         H5Z_scaleoffset_modify_2(i, type, buf, d_nelmts, min, D_val)                    \
   }                                                                                     \
   H5Z_scaleoffset_save_min(i, type, minval, min)                                        \
}

/* Postdecompress for unsigned integer type */
#define H5Z_scaleoffset_postdecompress_1(type, data, d_nelmts, filavail, cd_values, minbits, minval)\
{                                                                                 \
   type *buf = (type *)data, filval = 0; unsigned i;                              \
                                                                                  \
   if(filavail == H5Z_SCALEOFFSET_FILL_DEFINED) { /* fill value defined */        \
      H5Z_scaleoffset_get_filval_1(type, cd_values, filval)                       \
      for(i = 0; i < d_nelmts; i++)                                               \
         buf[i] = (type)((buf[i] == (((type)1 << minbits) - 1)) ? filval : (buf[i] + minval)); \
   } else /* fill value undefined */                                              \
      for(i = 0; i < d_nelmts; i++) buf[i] = (type)(buf[i] + (type)(minval));     \
}

/* Postdecompress for signed integer type */
#define H5Z_scaleoffset_postdecompress_2(type, data, d_nelmts, filavail, cd_values, minbits, minval)\
{                                                                                          \
   type *buf = (type *)data, filval = 0;                                                   \
   unsigned i;                                                                             \
                                                                                           \
   if(filavail == H5Z_SCALEOFFSET_FILL_DEFINED) { /* fill value defined */                 \
      H5Z_scaleoffset_get_filval_1(type, cd_values, filval)                                \
      for(i = 0; i < d_nelmts; i++)                                                        \
         buf[i] = (type)(((unsigned type)buf[i] == (((unsigned type)1 << minbits) - 1)) ? filval : (buf[i] + minval));\
   } else /* fill value undefined */                                                       \
      for(i = 0; i < d_nelmts; i++)                                                        \
          buf[i] = (type)(buf[i] + (type)(minval));                                        \
}

/* Retrive minimum value of floating-point type */
#define H5Z_scaleoffset_get_min(type, minval, min)                                 \
{                                                                                  \
    if(sizeof(type) <= sizeof(long long))                                          \
        /* retrieve min value from corresponding position                          \
         * byte-order has already been swapped as appropriate, but be sure to      \
         * account for offset in BE if sizes differ                                \
         */                                                                        \
        if(H5T_native_order_g == H5T_ORDER_LE)                                     \
            HDmemcpy(&min, &minval, sizeof(type));                                 \
        else {                                                                     \
            HDassert(H5T_native_order_g == H5T_ORDER_BE);                          \
            HDmemcpy(&min, ((char *)&minval) + (sizeof(long long)                  \
                    - sizeof(type)), sizeof(type));                                \
        } /* end else */                                                           \
    else                                                                           \
        HGOTO_ERROR(H5E_PLINE, H5E_BADTYPE, FAIL, "cannot find matched integer dataype") \
}

/* Modify values of data in postdecompression if fill value defined for floating-point type */
#define H5Z_scaleoffset_modify_3(i, type, buf, d_nelmts, filval, minbits, min, D_val)     \
{                                                                                         \
   if(sizeof(type)==sizeof(int))                                                          \
      for(i = 0; i < d_nelmts; i++)                                                       \
         buf[i] = (type)((*(int *)&buf[i] == (int)(((unsigned int)1 << minbits) - 1)) ?   \
                  filval : (double)(*(int *)&buf[i]) / HDpow(10.0f, D_val) + min);        \
   else if(sizeof(type)==sizeof(long))                                                    \
      for(i = 0; i < d_nelmts; i++)                                                       \
         buf[i] = (type)((*(long *)&buf[i] == (long)(((unsigned long)1 << minbits) - 1)) ? \
                  filval : (double)(*(long *)&buf[i]) / HDpow(10.0f, D_val) + min);       \
   else if(sizeof(type)==sizeof(long long))                                               \
      for(i = 0; i < d_nelmts; i++)                                                       \
         buf[i] = (type)((*(long long *)&buf[i] == (long long)(((unsigned long long)1 << minbits) - 1)) ? \
                  filval : (double)(*(long long *)&buf[i]) / HDpow(10.0f, D_val) + min);  \
   else                                                                                   \
      HGOTO_ERROR(H5E_PLINE, H5E_BADTYPE, FAIL, "cannot find matched integer dataype")    \
}

/* Modify values of data in postdecompression if fill value undefined for floating-point type */
#define H5Z_scaleoffset_modify_4(i, type, buf, d_nelmts, min, D_val)                   \
{                                                                                      \
   if(sizeof(type)==sizeof(int))                                                       \
      for(i = 0; i < d_nelmts; i++)                                                    \
         buf[i] = (type)((double)(*(int *)&buf[i]) / HDpow(10.0f, D_val) + min);       \
   else if(sizeof(type)==sizeof(long))                                                 \
      for(i = 0; i < d_nelmts; i++)                                                    \
         buf[i] = (type)((double)(*(long *)&buf[i]) / HDpow(10.0f, D_val) + min);      \
   else if(sizeof(type)==sizeof(long long))                                            \
      for(i = 0; i < d_nelmts; i++)                                                    \
         buf[i] = (type)((double)(*(long long *)&buf[i]) / HDpow(10.0f, D_val) + min); \
   else                                                                                \
      HGOTO_ERROR(H5E_PLINE, H5E_BADTYPE, FAIL, "cannot find matched integer dataype") \
}

/* Postdecompress for floating-point type using variable-minimum-bits method */
#define H5Z_scaleoffset_postdecompress_3(type, data, d_nelmts, filavail, cd_values,    \
                                         minbits, minval, D_val)                       \
{                                                                                      \
   type *buf = (type *)data, filval = 0, min = 0;                                      \
   unsigned i;                                                                         \
                                                                                       \
   H5Z_scaleoffset_get_min(type, minval, min)                                          \
                                                                                       \
   if(filavail == H5Z_SCALEOFFSET_FILL_DEFINED) { /* fill value defined */             \
      H5Z_scaleoffset_get_filval_2(type, cd_values, filval)                            \
      H5Z_scaleoffset_modify_3(i, type, buf, d_nelmts, filval, minbits, min, D_val)    \
   } else /* fill value undefined */                                                   \
      H5Z_scaleoffset_modify_4(i, type, buf, d_nelmts, min, D_val)                     \
}


/*-------------------------------------------------------------------------
 * Function:	H5Z_can_apply_scaleoffset
 *
 * Purpose:	Check the parameters for scaleoffset compression for
 *              validity and whether they fit a particular dataset.
 *
 * Return:	Success: Non-negative
 *		Failure: Negative
 *
 * Programmer:  Xiaowen Wu
 *              Friday, February 4, 2005
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
static htri_t
H5Z_can_apply_scaleoffset(hid_t H5_ATTR_UNUSED dcpl_id, hid_t type_id, hid_t H5_ATTR_UNUSED space_id)
{
    const H5T_t	*type;                  /* Datatype */
    H5T_class_t dtype_class;            /* Datatype's class */
    H5T_order_t dtype_order;            /* Datatype's endianness order */
    htri_t ret_value = TRUE;            /* Return value */

    FUNC_ENTER_NOAPI_NOINIT

    /* Get datatype */
    if(NULL == (type = (H5T_t *)H5I_object_verify(type_id, H5I_DATATYPE)))
	HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, FAIL, "not a datatype")

    /* Get datatype's class, for checking the "datatype class" */
    if((dtype_class = H5T_get_class(type, TRUE)) == H5T_NO_CLASS)
	HGOTO_ERROR(H5E_PLINE, H5E_BADTYPE, FAIL, "bad datatype class")

    /* Get datatype's size, for checking the "datatype size" */
    if(H5T_get_size(type) == 0)
	HGOTO_ERROR(H5E_PLINE, H5E_BADTYPE, FAIL, "bad datatype size")

    if(dtype_class == H5T_INTEGER || dtype_class == H5T_FLOAT) {
        /* Get datatype's endianness order */
        if((dtype_order = H5T_get_order(type)) == H5T_ORDER_ERROR)
	    HGOTO_ERROR(H5E_PLINE, H5E_BADTYPE, FAIL, "can't retrieve datatype endianness order")

        /* Range check datatype's endianness order */
        if(dtype_order != H5T_ORDER_LE && dtype_order != H5T_ORDER_BE)
            HGOTO_ERROR(H5E_PLINE, H5E_BADTYPE, FALSE, "bad datatype endianness order")
    } else
        HGOTO_ERROR(H5E_PLINE, H5E_BADTYPE, FALSE, "datatype class not supported by scaleoffset")

done:
    FUNC_LEAVE_NOAPI(ret_value)
} /* end H5Z_can_apply_scaleoffset() */


/*-------------------------------------------------------------------------
 * Function:	H5Z_scaleoffset_get_type
 *
 * Purpose:	Get the specific integer type based on datatype size and sign
 *              or floating-point type based on size
 *
 * Return:	Success: id number of integer type
 *		Failure: 0
 *
 * Programmer:	Xiaowen Wu
 *              Wednesday, April 13, 2005
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
static enum H5Z_scaleoffset_t
H5Z_scaleoffset_get_type(unsigned dtype_class, unsigned dtype_size, unsigned dtype_sign)
{
    enum H5Z_scaleoffset_t type = t_bad; /* integer type */
    enum H5Z_scaleoffset_t ret_value;             /* return value */

    FUNC_ENTER_NOAPI_NOINIT

    if(dtype_class==H5Z_SCALEOFFSET_CLS_INTEGER) {
        if(dtype_sign==H5Z_SCALEOFFSET_SGN_NONE) { /* unsigned integer */
            if     (dtype_size == sizeof(unsigned char))      type = t_uchar;
            else if(dtype_size == sizeof(unsigned short))     type = t_ushort;
            else if(dtype_size == sizeof(unsigned int))       type = t_uint;
            else if(dtype_size == sizeof(unsigned long))      type = t_ulong;
            else if(dtype_size == sizeof(unsigned long long)) type = t_ulong_long;
            else
                HGOTO_ERROR(H5E_PLINE, H5E_BADTYPE, t_bad, "cannot find matched memory dataype")
        }

        if(dtype_sign==H5Z_SCALEOFFSET_SGN_2) { /* signed integer */
            if     (dtype_size == sizeof(signed char)) type = t_schar;
            else if(dtype_size == sizeof(short))       type = t_short;
            else if(dtype_size == sizeof(int))         type = t_int;
            else if(dtype_size == sizeof(long))        type = t_long;
            else if(dtype_size == sizeof(long long))   type = t_long_long;
            else
                HGOTO_ERROR(H5E_PLINE, H5E_BADTYPE, t_bad, "cannot find matched memory dataype")
        }
    }

    if(dtype_class==H5Z_SCALEOFFSET_CLS_FLOAT) {
        if(dtype_size == sizeof(float))       type = t_float;
        else if(dtype_size == sizeof(double)) type = t_double;
        else
            HGOTO_ERROR(H5E_PLINE, H5E_BADTYPE, t_bad, "cannot find matched memory dataype")
    }

    /* Set return value */
    ret_value = type;

done:
    FUNC_LEAVE_NOAPI(ret_value)
}


/*-------------------------------------------------------------------------
 * Function:	H5Z_scaleoffset_set_parms_fillval
 *
 * Purpose:	Get the fill value of the dataset and store in cd_values[]
 *
 * Return:	Success: Non-negative
 *		Failure: Negative
 *
 * Programmer:  Xiaowen Wu
 *              Monday, March 7, 2005
 *
 *-------------------------------------------------------------------------
 */
static herr_t
H5Z_scaleoffset_set_parms_fillval(H5P_genplist_t *dcpl_plist,
    H5T_t *type, enum H5Z_scaleoffset_t scale_type,
    unsigned cd_values[], int need_convert, hid_t dxpl_id)
{
    herr_t ret_value = SUCCEED;          /* Return value */

    FUNC_ENTER_NOAPI_NOINIT

    if(scale_type == t_uchar)
        H5Z_scaleoffset_set_filval_3(unsigned char, dcpl_plist, type, cd_values, need_convert, dxpl_id)
    else if(scale_type == t_ushort)
        H5Z_scaleoffset_set_filval_1(unsigned short, dcpl_plist, type, cd_values, need_convert, dxpl_id)
    else if(scale_type == t_uint)
        H5Z_scaleoffset_set_filval_1(unsigned int, dcpl_plist, type, cd_values, need_convert, dxpl_id)
    else if(scale_type == t_ulong)
        H5Z_scaleoffset_set_filval_1(unsigned long, dcpl_plist, type, cd_values, need_convert, dxpl_id)
    else if(scale_type == t_ulong_long)
        H5Z_scaleoffset_set_filval_1(unsigned long long, dcpl_plist, type, cd_values, need_convert, dxpl_id)
    else if(scale_type == t_schar)
        H5Z_scaleoffset_set_filval_3(signed char, dcpl_plist, type, cd_values, need_convert, dxpl_id)
    else if(scale_type == t_short)
        H5Z_scaleoffset_set_filval_2(short, dcpl_plist, type, cd_values, need_convert, dxpl_id)
    else if(scale_type == t_int)
        H5Z_scaleoffset_set_filval_2(int, dcpl_plist, type, cd_values, need_convert, dxpl_id)
    else if(scale_type == t_long)
        H5Z_scaleoffset_set_filval_2(long, dcpl_plist, type, cd_values, need_convert, dxpl_id)
    else if(scale_type == t_long_long)
        H5Z_scaleoffset_set_filval_2(long long, dcpl_plist, type, cd_values, need_convert, dxpl_id)
    else if(scale_type == t_float)
        H5Z_scaleoffset_set_filval_4(float, dcpl_plist, type, cd_values, need_convert, dxpl_id)
    else if(scale_type == t_double)
        H5Z_scaleoffset_set_filval_4(double, dcpl_plist, type, cd_values, need_convert, dxpl_id)

done:
    FUNC_LEAVE_NOAPI(ret_value)
} /* end H5Z_scaleoffset_set_parms_fillval() */


/*-------------------------------------------------------------------------
 * Function:	H5Z_set_local_scaleoffset
 *
 * Purpose:	Set the "local" dataset parameters for scaleoffset
 *              compression.
 *
 * Return:	Success: Non-negative
 *		Failure: Negative
 *
 * Programmer:	Xiaowen Wu
 *              Friday, February 4, 2005
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
static herr_t
H5Z_set_local_scaleoffset(hid_t dcpl_id, hid_t type_id, hid_t space_id)
{
    H5P_genplist_t *dcpl_plist;     /* Property list pointer */
    H5T_t	*type;              /* Datatype */
    const H5S_t	*ds;                /* Dataspace */
    unsigned flags;                 /* Filter flags */
    size_t cd_nelmts = H5Z_SCALEOFFSET_USER_NPARMS;  /* Number of filter parameters */
    unsigned cd_values[H5Z_SCALEOFFSET_TOTAL_NPARMS]; /* Filter parameters */
    hssize_t npoints;               /* Number of points in the dataspace */
    H5T_class_t dtype_class;        /* Datatype's class */
    H5T_order_t dtype_order;        /* Datatype's endianness order */
    size_t dtype_size;              /* Datatype's size (in bytes) */
    H5T_sign_t dtype_sign;          /* Datatype's sign */
    enum H5Z_scaleoffset_t scale_type; /* Specific datatype */
    H5D_fill_value_t status;        /* Status of fill value in property list */
    herr_t ret_value = SUCCEED;     /* Return value */

    FUNC_ENTER_NOAPI_NOINIT

    /* Get the plist structure */
    if(NULL == (dcpl_plist = H5P_object_verify(dcpl_id, H5P_DATASET_CREATE)))
        HGOTO_ERROR(H5E_ATOM, H5E_BADATOM, FAIL, "can't find object for ID")

    /* Get datatype */
    if(NULL == (type = (H5T_t *)H5I_object_verify(type_id, H5I_DATATYPE)))
	HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, FAIL, "not a datatype")

#ifdef H5_CLEAR_MEMORY
    /* Initialize the parameters to a known state */
    HDmemset(cd_values, 0, sizeof(cd_values));
#endif /* H5_CLEAR_MEMORY */

    /* Get the filter's current parameters */
    if(H5P_get_filter_by_id(dcpl_plist, H5Z_FILTER_SCALEOFFSET, &flags, &cd_nelmts, cd_values, (size_t)0, NULL, NULL) < 0)
	HGOTO_ERROR(H5E_PLINE, H5E_CANTGET, FAIL, "can't get scaleoffset parameters")

    /* Get dataspace */
    if(NULL == (ds = (H5S_t *)H5I_object_verify(space_id, H5I_DATASPACE)))
        HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, FAIL, "not a data space")

    /* Get total number of elements in the chunk */
    if((npoints = H5S_GET_EXTENT_NPOINTS(ds)) < 0)
        HGOTO_ERROR(H5E_PLINE, H5E_CANTGET, FAIL, "unable to get number of points in the dataspace")

    /* Set "local" parameter for this dataset's number of elements */
    H5_CHECKED_ASSIGN(cd_values[H5Z_SCALEOFFSET_PARM_NELMTS], unsigned, npoints, hssize_t);

    /* Get datatype's class */
    if((dtype_class = H5T_get_class(type, TRUE)) == H5T_NO_CLASS)
        HGOTO_ERROR(H5E_PLINE, H5E_BADTYPE, FAIL, "bad datatype class")

    /* Set "local" parameter for datatype's class */
    switch(dtype_class) {
        case H5T_INTEGER:
            cd_values[H5Z_SCALEOFFSET_PARM_CLASS] = H5Z_SCALEOFFSET_CLS_INTEGER;
            break;

        case H5T_FLOAT:
            cd_values[H5Z_SCALEOFFSET_PARM_CLASS] = H5Z_SCALEOFFSET_CLS_FLOAT;
            break;

        case H5T_NO_CLASS:
        case H5T_TIME:
        case H5T_STRING:
        case H5T_BITFIELD:
        case H5T_OPAQUE:
        case H5T_COMPOUND:
        case H5T_REFERENCE:
        case H5T_ENUM:
        case H5T_VLEN:
        case H5T_ARRAY:
        case H5T_NCLASSES:
        default:
            HGOTO_ERROR(H5E_PLINE, H5E_BADTYPE, FAIL, "datatype class not supported by scaleoffset")
    } /* end switch */

    /* Get datatype's size */
    if((dtype_size = H5T_get_size(type)) == 0)
	HGOTO_ERROR(H5E_PLINE, H5E_BADTYPE, FAIL, "bad datatype size")

    /* Set "local" parameter for datatype size */
    cd_values[H5Z_SCALEOFFSET_PARM_SIZE] = dtype_size;

    if(dtype_class == H5T_INTEGER) {
        /* Get datatype's sign */
        if((dtype_sign = H5T_get_sign(type)) == H5T_SGN_ERROR)
            HGOTO_ERROR(H5E_PLINE, H5E_BADTYPE, FAIL, "bad datatype sign")

        /* Set "local" parameter for integer datatype sign */
        switch(dtype_sign) {
            case H5T_SGN_NONE:
                cd_values[H5Z_SCALEOFFSET_PARM_SIGN] = H5Z_SCALEOFFSET_SGN_NONE;
                break;

            case H5T_SGN_2:
                cd_values[H5Z_SCALEOFFSET_PARM_SIGN] = H5Z_SCALEOFFSET_SGN_2;
                break;

            case H5T_SGN_ERROR:
            case H5T_NSGN:
            default:
                HGOTO_ERROR(H5E_PLINE, H5E_BADTYPE, FAIL, "bad integer sign")
        } /* end switch */
    } /* end if */

    /* Get datatype's endianness order */
    if((dtype_order = H5T_get_order(type)) == H5T_ORDER_ERROR)
        HGOTO_ERROR(H5E_PLINE, H5E_BADTYPE, FAIL, "bad datatype endianness order")

    /* Set "local" parameter for datatype endianness */
    switch(dtype_order) {
        case H5T_ORDER_LE:      /* Little-endian byte order */
            cd_values[H5Z_SCALEOFFSET_PARM_ORDER] = H5Z_SCALEOFFSET_ORDER_LE;
            break;

        case H5T_ORDER_BE:      /* Big-endian byte order */
            cd_values[H5Z_SCALEOFFSET_PARM_ORDER] = H5Z_SCALEOFFSET_ORDER_BE;
            break;

        case H5T_ORDER_ERROR:
        case H5T_ORDER_VAX:
        case H5T_ORDER_NONE:
        default:
            HGOTO_ERROR(H5E_PLINE, H5E_BADTYPE, FAIL, "bad datatype endianness order")
    } /* end switch */

    /* Check whether fill value is defined for dataset */
    if(H5P_fill_value_defined(dcpl_plist, &status) < 0)
        HGOTO_ERROR(H5E_PLINE, H5E_CANTGET, FAIL, "unable to determine if fill value is defined")

    /* Set local parameter for availability of fill value */
    if(status == H5D_FILL_VALUE_UNDEFINED)
        cd_values[H5Z_SCALEOFFSET_PARM_FILAVAIL] = H5Z_SCALEOFFSET_FILL_UNDEFINED;
    else {
        int need_convert = FALSE;       /* Flag indicating convertion of byte order */

        cd_values[H5Z_SCALEOFFSET_PARM_FILAVAIL] = H5Z_SCALEOFFSET_FILL_DEFINED;

        /* Check if memory byte order matches dataset datatype byte order */
        if(H5T_native_order_g != dtype_order)
            need_convert = TRUE;

        /* Before getting fill value, get its type */
        if((scale_type = H5Z_scaleoffset_get_type(cd_values[H5Z_SCALEOFFSET_PARM_CLASS],
                cd_values[H5Z_SCALEOFFSET_PARM_SIZE], cd_values[H5Z_SCALEOFFSET_PARM_SIGN])) == 0)
            HGOTO_ERROR(H5E_PLINE, H5E_BADTYPE, FAIL, "cannot use C integer datatype for cast")

        /* Get dataset fill value and store in cd_values[] */
        if(H5Z_scaleoffset_set_parms_fillval(dcpl_plist, type, scale_type, cd_values, need_convert, H5AC_ind_dxpl_id) < 0)
            HGOTO_ERROR(H5E_PLINE, H5E_CANTSET, FAIL, "unable to set fill value")
    } /* end else */

    /* Modify the filter's parameters for this dataset */
    if(H5P_modify_filter(dcpl_plist, H5Z_FILTER_SCALEOFFSET, flags, (size_t)H5Z_SCALEOFFSET_TOTAL_NPARMS, cd_values) < 0)
	HGOTO_ERROR(H5E_PLINE, H5E_CANTSET, FAIL, "can't set local scaleoffset parameters")

done:
    FUNC_LEAVE_NOAPI(ret_value)
} /* end H5Z_set_local_scaleoffset() */


/*-------------------------------------------------------------------------
 * Function:	H5Z_filter_scaleoffset
 *
 * Purpose:	Implement an I/O filter for storing packed integer
 *              data using scale and offset method.
 *
 * Return:	Success: Size of buffer filtered
 *		Failure: 0
 *
 * Programmer:	Xiaowen Wu
 *              Monday, February 7, 2005
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
static size_t
H5Z_filter_scaleoffset(unsigned flags, size_t cd_nelmts, const unsigned cd_values[],
    size_t nbytes, size_t *buf_size, void **buf)
{
    size_t ret_value = 0;           /* return value */
    size_t size_out  = 0;           /* size of output buffer */
    unsigned d_nelmts = 0;          /* number of data elements in the chunk */
    unsigned dtype_class;           /* datatype class */
    unsigned dtype_sign;            /* integer datatype sign */
    unsigned filavail;              /* flag indicating if fill value is defined or not */
    H5Z_SO_scale_type_t scale_type = H5Z_SO_FLOAT_DSCALE;/* scale type */
    int scale_factor = 0;           /* scale factor */
    double D_val = 0.0f;            /* decimal scale factor */
    uint32_t minbits = 0;           /* minimum number of bits to store values */
    unsigned long long minval= 0;   /* minimum value of input buffer */
    enum H5Z_scaleoffset_t type; /* memory type corresponding to dataset datatype */
    int need_convert = FALSE;       /* flag indicating convertion of byte order */
    unsigned char *outbuf = NULL;   /* pointer to new output buffer */
    unsigned buf_offset = 21;       /* buffer offset because of parameters stored in file */
    unsigned i;                     /* index */
    parms_atomic p;                 /* paramters needed for compress/decompress functions */

    FUNC_ENTER_NOAPI_NOINIT

    /* check arguments */
    if(cd_nelmts != H5Z_SCALEOFFSET_TOTAL_NPARMS)
	HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, 0, "invalid scaleoffset number of paramters")

    /* Check if memory byte order matches dataset datatype byte order */
    switch(H5T_native_order_g) {
        case H5T_ORDER_LE:      /* memory is little-endian byte order */
            if(cd_values[H5Z_SCALEOFFSET_PARM_ORDER] == H5Z_SCALEOFFSET_ORDER_BE)
                need_convert = TRUE;
            break;

        case H5T_ORDER_BE:      /* memory is big-endian byte order */
            if(cd_values[H5Z_SCALEOFFSET_PARM_ORDER] == H5Z_SCALEOFFSET_ORDER_LE)
                need_convert = TRUE;
            break;

        case H5T_ORDER_ERROR:
        case H5T_ORDER_VAX:
        case H5T_ORDER_NONE:
        default:
            HGOTO_ERROR(H5E_PLINE, H5E_BADTYPE, 0, "bad H5T_NATIVE_INT endianness order")
    } /* end switch */

    /* copy filter parameters to local variables */
    d_nelmts     = cd_values[H5Z_SCALEOFFSET_PARM_NELMTS];
    dtype_class  = cd_values[H5Z_SCALEOFFSET_PARM_CLASS];
    dtype_sign   = cd_values[H5Z_SCALEOFFSET_PARM_SIGN];
    filavail     = cd_values[H5Z_SCALEOFFSET_PARM_FILAVAIL];
    scale_factor = (int)cd_values[H5Z_SCALEOFFSET_PARM_SCALEFACTOR];
    scale_type   = (H5Z_SO_scale_type_t)cd_values[H5Z_SCALEOFFSET_PARM_SCALETYPE];

    /* check and assign proper values set by user to related parameters
     * scale type can be H5Z_SO_FLOAT_DSCALE (0), H5Z_SO_FLOAT_ESCALE (1) or H5Z_SO_INT (other)
     * H5Z_SO_FLOAT_DSCALE : floating-point type, variable-minimum-bits method,
     *                      scale factor is decimal scale factor
     * H5Z_SO_FLOAT_ESCALE : floating-point type, fixed-minimum-bits method,
     *                      scale factor is the fixed minimum number of bits
     * H5Z_SO_INT          : integer type, scale_factor is minimum number of bits
     */
    if(dtype_class==H5Z_SCALEOFFSET_CLS_FLOAT) { /* floating-point type */
        if(scale_type!=H5Z_SO_FLOAT_DSCALE && scale_type!=H5Z_SO_FLOAT_ESCALE)
            HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, 0, "invalid scale type")
    }

    if(dtype_class==H5Z_SCALEOFFSET_CLS_INTEGER) { /* integer type */
        if(scale_type!=H5Z_SO_INT)
            HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, 0, "invalid scale type")

        /* if scale_factor is less than 0 for integer, library will reset it to 0
         * in this case, library will calculate the minimum-bits
         */
	if(scale_factor < 0) scale_factor = 0;
    }

    /* fixed-minimum-bits method is not implemented and is forbidden */
    if(scale_type==H5Z_SO_FLOAT_ESCALE)
         HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, 0, "E-scaling method not supported")

    if(scale_type==H5Z_SO_FLOAT_DSCALE) { /* floating-point type, variable-minimum-bits */
        D_val = (double)scale_factor;
    } else { /* integer type, or floating-point type with fixed-minimum-bits method */
        if(scale_factor > (int)(cd_values[H5Z_SCALEOFFSET_PARM_SIZE] * 8))
            HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, 0, "minimum number of bits exceeds maximum")

        /* no need to process data */
        if(scale_factor == (int)(cd_values[H5Z_SCALEOFFSET_PARM_SIZE] * 8)) {
            ret_value = *buf_size;
            goto done;
        }
        minbits = (uint32_t)scale_factor;
    }

    /* prepare paramters to pass to compress/decompress functions */
    p.size = cd_values[H5Z_SCALEOFFSET_PARM_SIZE];
    p.mem_order = H5T_native_order_g;

    /* input; decompress */
    if (flags & H5Z_FLAG_REVERSE) {
        /* retrieve values of minbits and minval from input compressed buffer
         * retrieve them corresponding to how they are stored during compression
         */
        uint32_t minbits_mask = 0;
        unsigned long long minval_mask = 0;
        unsigned minval_size = 0;

        minbits = 0;
        for(i = 0; i < 4; i++) {
            minbits_mask = ((unsigned char *)*buf)[i];
            minbits_mask <<= i*8;
            minbits |= minbits_mask;
        }

        /* retrieval of minval takes into consideration situation where sizeof
         * unsigned long long (datatype of minval) may change from compression
         * to decompression, only smaller size is used
         */
        minval_size = sizeof(unsigned long long) <= ((unsigned char *)*buf)[4] ?
                      sizeof(unsigned long long) : ((unsigned char *)*buf)[4];
        minval = 0;
        for(i = 0; i < minval_size; i++) {
            minval_mask = ((unsigned char *)*buf)[5+i];
            minval_mask <<= i*8;
            minval |= minval_mask;
        }

        HDassert(minbits <= p.size * 8);
        p.minbits = minbits;

        /* calculate size of output buffer after decompression */
        size_out = d_nelmts * p.size;

        /* allocate memory space for decompressed buffer */
        if(NULL == (outbuf = (unsigned char *)H5MM_malloc(size_out)))
            HGOTO_ERROR(H5E_RESOURCE, H5E_NOSPACE, 0, "memory allocation failed for scaleoffset decompression")

        /* special case: minbits equal to full precision */
        if(minbits == p.size * 8) {
            HDmemcpy(outbuf, (unsigned char*)(*buf)+buf_offset, size_out);

            /* convert to dataset datatype endianness order if needed */
            if(need_convert)
                H5Z_scaleoffset_convert(outbuf, d_nelmts, p.size);

            *buf = outbuf;
            outbuf = NULL;
            *buf_size = size_out;
            ret_value = size_out;
            goto done;
        }

        /* decompress the buffer if minbits not equal to zero */
        if(minbits != 0)
            H5Z_scaleoffset_decompress(outbuf, d_nelmts, (unsigned char*)(*buf)+buf_offset, p);
        else {
            /* fill value is not defined and all data elements have the same value */
            for(i = 0; i < size_out; i++) outbuf[i] = 0;
        }

        /* before postprocess, get memory type */
        if((type = H5Z_scaleoffset_get_type(dtype_class, (unsigned)p.size, dtype_sign)) == 0)
            HGOTO_ERROR(H5E_PLINE, H5E_BADTYPE, 0, "cannot use C integer datatype for cast")

        /* postprocess after decompression */
        if(dtype_class==H5Z_SCALEOFFSET_CLS_INTEGER)
            H5Z_scaleoffset_postdecompress_i(outbuf, d_nelmts, type, filavail,
                                             cd_values, minbits, minval);

        if(dtype_class==H5Z_SCALEOFFSET_CLS_FLOAT)
            if(scale_type==0) { /* variable-minimum-bits method */
                if(H5Z_scaleoffset_postdecompress_fd(outbuf, d_nelmts, type, filavail,
                        cd_values, minbits, minval, D_val)==FAIL)
                    HGOTO_ERROR(H5E_PLINE, H5E_BADTYPE, 0, "post-decompression failed")
            }

        /* after postprocess, convert to dataset datatype endianness order if needed */
        if(need_convert)
            H5Z_scaleoffset_convert(outbuf, d_nelmts, p.size);
    }
    /* output; compress */
    else {
        HDassert(nbytes == d_nelmts * p.size);

        /* before preprocess, convert to memory endianness order if needed */
        if(need_convert)
            H5Z_scaleoffset_convert(*buf, d_nelmts, p.size);

        /* before preprocess, get memory type */
        if((type = H5Z_scaleoffset_get_type(dtype_class, (unsigned)p.size, dtype_sign))==0)
            HGOTO_ERROR(H5E_PLINE, H5E_BADTYPE, 0, "cannot use C integer datatype for cast")

        /* preprocess before compression */
        if(dtype_class==H5Z_SCALEOFFSET_CLS_INTEGER)
            H5Z_scaleoffset_precompress_i(*buf, d_nelmts, type, filavail,
                                          cd_values, &minbits, &minval);

        if(dtype_class==H5Z_SCALEOFFSET_CLS_FLOAT)
            if(scale_type==0) { /* variable-minimum-bits method */
                if(H5Z_scaleoffset_precompress_fd(*buf, d_nelmts, type, filavail,
                        cd_values, &minbits, &minval, D_val)==FAIL)
                    HGOTO_ERROR(H5E_PLINE, H5E_BADTYPE, 0, "pre-compression failed")
            }

        HDassert(minbits <= p.size * 8);

        /* calculate buffer size after compression
         * minbits and minval are stored in the front of the compressed buffer
         */
        p.minbits = minbits;
        size_out = buf_offset + nbytes * p.minbits / (p.size * 8) + 1; /* may be 1 larger */

        /* allocate memory space for compressed buffer */
        if(NULL == (outbuf = (unsigned char *)H5MM_malloc(size_out)))
            HGOTO_ERROR(H5E_RESOURCE, H5E_NOSPACE, 0, "memory allocation failed for scaleoffset compression")

        /* store minbits and minval in the front of output compressed buffer
         * store byte by byte from least significant byte to most significant byte
         * constant buffer size (21 bytes) is left for these two parameters
         * 4 bytes for minbits, 1 byte for size of minval, 16 bytes for minval
         */
        for(i = 0; i < 4; i++)
            ((unsigned char *)outbuf)[i] = (unsigned char)((minbits & ((uint32_t)0xff << i*8)) >> i*8);

        ((unsigned char *)outbuf)[4] = sizeof(unsigned long long);

        for(i = 0; i < sizeof(unsigned long long); i++)
            ((unsigned char *)outbuf)[5+i] = (unsigned char)((minval & ((unsigned long long)0xff << i*8)) >> i*8);

#ifdef H5_CLEAR_MEMORY
        /* Zero out remaining, unused bytes */
        /* (Looks like an error in the original determination of how many
         *      bytes would be needed for parameters. - QAK, 2010/08/19)
         */
        HDmemset(outbuf + 13, 0, (size_t)8);
#endif /* H5_CLEAR_MEMORY */

        /* special case: minbits equal to full precision */
        if(minbits == p.size * 8) {
            HDmemcpy(outbuf + buf_offset, *buf, nbytes);
            *buf = outbuf;
            outbuf = NULL;
            *buf_size = size_out;
            ret_value = buf_offset + nbytes;
            goto done;
        }

        /* compress the buffer if minbits not equal to zero
         * minbits equal to zero only when fill value is not defined and
         * all data elements have the same value
         */
        if(minbits != 0)
            H5Z_scaleoffset_compress((unsigned char *)*buf, d_nelmts, outbuf + buf_offset, size_out - buf_offset, p);
    }

    /* free the input buffer */
    H5MM_xfree(*buf);

    /* set return values */
    *buf = outbuf;
    outbuf = NULL;
    *buf_size = size_out;
    ret_value = size_out;

done:
    if(outbuf)
        H5MM_xfree(outbuf);
    FUNC_LEAVE_NOAPI(ret_value)
}

/* ============ Scaleoffset Algorithm ===============================================
 * assume one byte has 8 bit
 * assume padding bit is 0
 * assume size of unsigned char is one byte
 * assume one data item of certain datatype is stored continously in bytes
 * atomic datatype is treated on byte basis
 */


/* change byte order of input buffer either from little-endian to big-endian
 * or from big-endian to little-endian  2/21/2005
 */
static void
H5Z_scaleoffset_convert(void *buf, unsigned d_nelmts, size_t dtype_size)
{
   if(dtype_size > 1) {
       unsigned i, j;
       unsigned char *buffer, temp;

       buffer = (unsigned char *)buf;
       for(i = 0; i < d_nelmts * dtype_size; i += dtype_size)
          for(j = 0; j < dtype_size / 2; j++) {
             /* swap pair of bytes */
             temp = buffer[i + j];
             buffer[i + j] = buffer[i + dtype_size - 1 - j];
             buffer[i + dtype_size - 1 - j] = temp;
          } /* end for */
    } /* end if */
} /* end H5Z_scaleoffset_convert() */

/* Round a floating-point value to the nearest integer value 4/19/05 */
/* rounding to the bigger absolute value if val is in the middle,
 0.5 -> 1, -0.5 ->-1
5/9/05, KY */
static double
H5Z_scaleoffset_rnd(double val)
{
    double u_val, l_val;

    u_val = HDceil(val);
    l_val = HDfloor(val);

    if(val > 0) {
        if((u_val - val) <= (val - l_val))
            return u_val;
        else
            return l_val;
    } /* end if */
    else {
        if((val - l_val) <= (u_val - val))
            return l_val;
        else
            return u_val;
    }
} /* H5Z_scaleoffset_rnd() */

/* return ceiling of floating-point log2 function
 * receive unsigned integer as argument 3/10/2005
 */
static unsigned
H5Z_scaleoffset_log2(unsigned long long num)
{
   unsigned v = 0;
   unsigned long long lower_bound = 1; /* is power of 2, largest value <= num */
   unsigned long long val = num;

   while(val >>= 1) {
       v++;
       lower_bound <<= 1;
   }

   if(num == lower_bound)
       return v;
   else
       return v + 1;
}

/* precompress for integer type */
static void
H5Z_scaleoffset_precompress_i(void *data, unsigned d_nelmts, enum H5Z_scaleoffset_t type,
    unsigned filavail, const unsigned cd_values[], uint32_t *minbits, unsigned long long *minval)
{
    if(type ==  t_uchar)
        H5Z_scaleoffset_precompress_1(unsigned char, data, d_nelmts,
                                    filavail, cd_values, minbits, minval)
    else if(type == t_ushort)
        H5Z_scaleoffset_precompress_1(unsigned short, data, d_nelmts,
                                    filavail, cd_values, minbits, minval)
    else if(type == t_uint)
        H5Z_scaleoffset_precompress_1(unsigned int, data, d_nelmts,
                                    filavail, cd_values, minbits, minval)
    else if(type == t_ulong)
        H5Z_scaleoffset_precompress_1(unsigned long, data, d_nelmts,
                                    filavail, cd_values, minbits, minval)
    else if(type == t_ulong_long)
        H5Z_scaleoffset_precompress_1(unsigned long long, data, d_nelmts,
                                    filavail, cd_values, minbits, minval)
    else if(type == t_schar) {
        signed char *buf = (signed char *)data, min = 0, max = 0, filval = 0;
        unsigned char span;
        unsigned i;

        if(filavail == H5Z_SCALEOFFSET_FILL_DEFINED) { /* fill value defined */
            H5Z_scaleoffset_get_filval_1(signed char, cd_values, filval);
            if(*minbits == H5Z_SO_INT_MINBITS_DEFAULT) { /* minbits not set yet, calculate max, min, and minbits */
                H5Z_scaleoffset_max_min_1(i, d_nelmts, buf, filval, max, min)
                if((unsigned char)(max - min) > (unsigned char)(~(unsigned char)0 - 2)) {
                    *minbits = sizeof(signed char)*8;
                    return;
                }
                span = (unsigned char)(max - min + 1);
                *minbits = H5Z_scaleoffset_log2((unsigned long long)(span+1));
            } else /* minbits already set, only calculate min */
                H5Z_scaleoffset_min_1(i, d_nelmts, buf, filval, min)
            if(*minbits != sizeof(signed char)*8) /* change values if minbits != full precision */
                for(i = 0; i < d_nelmts; i++)
                    buf[i] = (signed char)((buf[i] == filval) ? (((unsigned char)1 << *minbits) - 1) : (buf[i] - min));
        } else { /* fill value undefined */
            if(*minbits == H5Z_SO_INT_MINBITS_DEFAULT) { /* minbits not set yet, calculate max, min, and minbits */
                H5Z_scaleoffset_max_min_2(i, d_nelmts, buf, max, min)
                if((unsigned char)(max - min) > (unsigned char)(~(unsigned char)0 - 2)) {
                    *minbits = sizeof(signed char)*8;
                    *minval = (unsigned long long)min;
                    return;
                }
                span = (unsigned char)(max - min + 1);
                *minbits = H5Z_scaleoffset_log2((unsigned long long)span);
            } else /* minbits already set, only calculate min */
                H5Z_scaleoffset_min_2(i, d_nelmts, buf, min)
            if(*minbits != sizeof(signed char) * 8) /* change values if minbits != full precision */
                for(i = 0; i < d_nelmts; i++)
                    buf[i] = (signed char)(buf[i] - min);
        }
        *minval = (unsigned long long)min;
    }
    else if(type == t_short)
        H5Z_scaleoffset_precompress_2(short, data, d_nelmts,
                                    filavail, cd_values, minbits, minval)
    else if(type == t_int)
        H5Z_scaleoffset_precompress_2(int, data, d_nelmts,
                                    filavail, cd_values, minbits, minval)
    else if(type == t_long)
        H5Z_scaleoffset_precompress_2(long, data, d_nelmts,
                                    filavail, cd_values, minbits, minval)
    else if(type == t_long_long)
        H5Z_scaleoffset_precompress_2(long long, data, d_nelmts,
                                    filavail, cd_values, minbits, minval)
}

/* postdecompress for integer type */
static void
H5Z_scaleoffset_postdecompress_i(void *data, unsigned d_nelmts, enum H5Z_scaleoffset_t type,
    unsigned filavail, const unsigned cd_values[], uint32_t minbits, unsigned long long minval)
{
   long long sminval = *(long long*)&minval; /* for signed integer types */

   if(type == t_uchar)
      H5Z_scaleoffset_postdecompress_1(unsigned char, data, d_nelmts, filavail,
                                       cd_values, minbits, minval)
   else if(type == t_ushort)
      H5Z_scaleoffset_postdecompress_1(unsigned short, data, d_nelmts, filavail,
                                       cd_values, minbits, minval)
   else if(type == t_uint)
      H5Z_scaleoffset_postdecompress_1(unsigned int, data, d_nelmts, filavail,
                                       cd_values, minbits, minval)
   else if(type == t_ulong)
      H5Z_scaleoffset_postdecompress_1(unsigned long, data, d_nelmts, filavail,
                                       cd_values, minbits, minval)
   else if(type == t_ulong_long)
      H5Z_scaleoffset_postdecompress_1(unsigned long long, data, d_nelmts, filavail,
                                       cd_values, minbits, minval)
   else if(type == t_schar) {
      signed char *buf = (signed char *)data, filval = 0;
      unsigned i;

      if(filavail == H5Z_SCALEOFFSET_FILL_DEFINED) { /* fill value defined */
         H5Z_scaleoffset_get_filval_1(signed char, cd_values, filval)
         for(i = 0; i < d_nelmts; i++)
            buf[i] = (signed char)((buf[i] == (((unsigned char)1 << minbits) - 1)) ? filval : (buf[i] + sminval));
      } else /* fill value undefined */
         for(i = 0; i < d_nelmts; i++)
             buf[i] = (signed char)(buf[i] + sminval);
   }
   else if(type == t_short)
      H5Z_scaleoffset_postdecompress_2(short, data, d_nelmts, filavail,
                                       cd_values, minbits, sminval)
   else if(type == t_int)
      H5Z_scaleoffset_postdecompress_2(int, data, d_nelmts, filavail,
                                       cd_values, minbits, sminval)
   else if(type == t_long)
      H5Z_scaleoffset_postdecompress_2(long, data, d_nelmts, filavail,
                                       cd_values, minbits, sminval)
   else if(type == t_long_long)
      H5Z_scaleoffset_postdecompress_2(long long, data, d_nelmts, filavail,
                                       cd_values, minbits, sminval)
}

/* precompress for floating-point type, variable-minimum-bits method
   success: non-negative, failure: negative 4/15/05 */
static herr_t
H5Z_scaleoffset_precompress_fd(void *data, unsigned d_nelmts, enum H5Z_scaleoffset_t type,
    unsigned filavail, const unsigned cd_values[], uint32_t *minbits,
    unsigned long long *minval, double D_val)
{
   herr_t ret_value=SUCCEED; /* Return value */

   FUNC_ENTER_NOAPI_NOINIT

   if(type == t_float)
      H5Z_scaleoffset_precompress_3(float, data, d_nelmts,
                                    filavail, cd_values, minbits, minval, D_val)
   else if(type == t_double)
      H5Z_scaleoffset_precompress_3(double, data, d_nelmts,
                                    filavail, cd_values, minbits, minval, D_val)

done:
   FUNC_LEAVE_NOAPI(ret_value)
}

/* postdecompress for floating-point type, variable-minimum-bits method
   success: non-negative, failure: negative 4/15/05 */
static herr_t
H5Z_scaleoffset_postdecompress_fd(void *data, unsigned d_nelmts, enum H5Z_scaleoffset_t type,
    unsigned filavail, const unsigned cd_values[], uint32_t minbits,
    unsigned long long minval, double D_val)
{
   long long sminval = (long long)minval;    /* for signed integer types */
   herr_t ret_value=SUCCEED;                 /* Return value */

   FUNC_ENTER_NOAPI_NOINIT

   if(type == t_float)
      H5Z_scaleoffset_postdecompress_3(float, data, d_nelmts, filavail,
                                       cd_values, minbits, sminval, D_val)
   else if(type == t_double)
      H5Z_scaleoffset_postdecompress_3(double, data, d_nelmts, filavail,
                                       cd_values, minbits, sminval, D_val)

done:
   FUNC_LEAVE_NOAPI(ret_value)
}

static void
H5Z_scaleoffset_next_byte(size_t *j, unsigned *buf_len)
{
   ++(*j);
   *buf_len = 8 * sizeof(unsigned char);
}

static void
H5Z_scaleoffset_decompress_one_byte(unsigned char *data, size_t data_offset,
    unsigned k, unsigned begin_i, unsigned char *buffer, size_t *j, unsigned *buf_len,
    parms_atomic p, unsigned dtype_len)
{
   unsigned dat_len;    /* dat_len is the number of bits to be copied in each data byte */
   unsigned char val;   /* value to be copied in each data byte */

   /* initialize value and bits of unsigned char to be copied */
   val = buffer[*j];
   if(k == begin_i)
      dat_len = 8 - (dtype_len - p.minbits) % 8;
   else
      dat_len = 8;

   if(*buf_len > dat_len) {
      data[data_offset + k] = (unsigned char)((val >> (*buf_len - dat_len)) & ~(~0 << dat_len));
      *buf_len -= dat_len;
   } /* end if */
   else {
      data[data_offset + k] = (unsigned char)((val & ~(~0 << *buf_len)) << (dat_len - *buf_len));
      dat_len -= *buf_len;
      H5Z_scaleoffset_next_byte(j, buf_len);
      if(dat_len == 0)
          return;

      val = buffer[*j];
      data[data_offset + k] |= (unsigned char)((val >> (*buf_len - dat_len)) & ~(~0 << dat_len));
      *buf_len -= dat_len;
   } /* end else */
}

static void
H5Z_scaleoffset_decompress_one_atomic(unsigned char *data, size_t data_offset,
    unsigned char *buffer, size_t *j, unsigned *buf_len, parms_atomic p)
{
   /* begin_i: the index of byte having first significant bit */
   unsigned begin_i;
   unsigned dtype_len;
   int k;

   HDassert(p.minbits > 0);

   dtype_len = p.size * 8;

   if(p.mem_order == H5Z_SCALEOFFSET_ORDER_LE) { /* little endian */
      begin_i = p.size - 1 - (dtype_len - p.minbits) / 8;

      for(k = (int)begin_i; k >= 0; k--)
         H5Z_scaleoffset_decompress_one_byte(data, data_offset, (unsigned)k, begin_i,
                                             buffer, j, buf_len, p, dtype_len);
   }
   else { /* big endian */
      HDassert(p.mem_order == H5Z_SCALEOFFSET_ORDER_BE);

      begin_i = (dtype_len - p.minbits) / 8;

      for(k = (int)begin_i; k <= (int)(p.size - 1); k++)
         H5Z_scaleoffset_decompress_one_byte(data, data_offset, (unsigned)k, begin_i,
                                             buffer, j, buf_len, p, dtype_len);
   }
}

static void
H5Z_scaleoffset_decompress(unsigned char *data, unsigned d_nelmts,
    unsigned char *buffer, parms_atomic p)
{
    /* i: index of data, j: index of buffer,
       buf_len: number of bits to be filled in current byte */
    size_t i, j;
    unsigned buf_len;

    /* must initialize to zeros */
    for(i = 0; i < d_nelmts*p.size; i++)
        data[i] = 0;

    /* initialization before the loop */
    j = 0;
    buf_len = sizeof(unsigned char) * 8;

    /* decompress */
    for(i = 0; i < d_nelmts; i++)
        H5Z_scaleoffset_decompress_one_atomic(data, i * p.size, buffer, &j, &buf_len, p);
}

static void
H5Z_scaleoffset_compress_one_byte(unsigned char *data, size_t data_offset,
    unsigned k, unsigned begin_i, unsigned char *buffer, size_t *j, unsigned *buf_len,
    parms_atomic p, unsigned dtype_len)
{
   unsigned dat_len;    /* dat_len is the number of bits to be copied in each data byte */
   unsigned char val;   /* value to be copied in each data byte */

   /* initialize value and bits of unsigned char to be copied */
   val = data[data_offset + k];
   if(k == begin_i)
      dat_len = 8 - (dtype_len - p.minbits) % 8;
   else
      dat_len = 8;

   if(*buf_len > dat_len) {
      buffer[*j] |= (unsigned char)((val & ~(~0 << dat_len)) << (*buf_len - dat_len));
      *buf_len -= dat_len;
   } else {
      buffer[*j] |= (unsigned char)((val >> (dat_len - *buf_len)) & ~(~0 << *buf_len));
      dat_len -= *buf_len;
      H5Z_scaleoffset_next_byte(j, buf_len);
      if(dat_len == 0)
          return;

      buffer[*j] = (unsigned char)((val & ~(~0 << dat_len)) << (*buf_len - dat_len));
      *buf_len -= dat_len;
   } /* end else */
}

static void
H5Z_scaleoffset_compress_one_atomic(unsigned char *data, size_t data_offset,
    unsigned char *buffer, size_t *j, unsigned *buf_len, parms_atomic p)
{
   /* begin_i: the index of byte having first significant bit */
   unsigned begin_i;
   unsigned dtype_len;
   int k;

   HDassert(p.minbits > 0);

   dtype_len = p.size * 8;

   if(p.mem_order == H5Z_SCALEOFFSET_ORDER_LE) { /* little endian */
      begin_i = p.size - 1 - (dtype_len - p.minbits) / 8;

      for(k = (int)begin_i; k >= 0; k--)
         H5Z_scaleoffset_compress_one_byte(data, data_offset, (unsigned)k, begin_i,
                                           buffer, j, buf_len, p, dtype_len);
   }
   else { /* big endian */
      HDassert(p.mem_order == H5Z_SCALEOFFSET_ORDER_BE);
      begin_i = (dtype_len - p.minbits) / 8;

      for(k = (int)begin_i; k <= (int)(p.size - 1); k++)
         H5Z_scaleoffset_compress_one_byte(data, data_offset, (unsigned)k, begin_i,
                                           buffer, j, buf_len, p, dtype_len);
   }
}

static void
H5Z_scaleoffset_compress(unsigned char *data, unsigned d_nelmts,
    unsigned char *buffer, size_t buffer_size, parms_atomic p)
{
   /* i: index of data, j: index of buffer,
      buf_len: number of bits to be filled in current byte */
   size_t i, j;
   unsigned buf_len;

   /* must initialize buffer to be zeros */
   for(j = 0; j < buffer_size; j++)
      buffer[j] = 0;

   /* initialization before the loop */
   j = 0;
   buf_len = sizeof(unsigned char) * 8;

   /* compress */
   for(i = 0; i < d_nelmts; i++)
       H5Z_scaleoffset_compress_one_atomic(data, i * p.size, buffer, &j, &buf_len, p);
}