From a929a1dde620a680c4e5cddcc95c0aa5deb060ba Mon Sep 17 00:00:00 2001 From: Johannes Schick Date: Fri, 8 May 2020 13:50:35 +0200 Subject: [PATCH 1/2] Add files via upload --- src/grib_bits_any_endian_vector.c | 226 +++++++++++++++++------------- 1 file changed, 131 insertions(+), 95 deletions(-) diff --git a/src/grib_bits_any_endian_vector.c b/src/grib_bits_any_endian_vector.c index 3c3a13582..1d1946994 100644 --- a/src/grib_bits_any_endian_vector.c +++ b/src/grib_bits_any_endian_vector.c @@ -1,5 +1,5 @@ /* - * (C) Copyright 2005- ECMWF. + * Copyright 2005-2019 ECMWF. * * This software is licensed under the terms of the Apache Licence Version 2.0 * which can be obtained at http://www.apache.org/licenses/LICENSE-2.0. @@ -13,123 +13,159 @@ * * ***************************************************************************/ -int grib_decode_long_array(const unsigned char* p, long* bitp, long bitsPerValue, - size_t n_vals, long* val) -{ - long i = 0; - unsigned long lvalue = 0; +int grib_decode_long_array(const unsigned char* p, long *bitp, long bitsPerValue, + size_t n_vals,long* val) { + long i=0; + unsigned long lvalue = 0; - if (bitsPerValue % 8) { - int j = 0; - for (i = 0; i < n_vals; i++) { - lvalue = 0; - for (j = 0; j < bitsPerValue; j++) { - lvalue <<= 1; - if (grib_get_bit(p, *bitp)) - lvalue += 1; - *bitp += 1; - } - val[i] = lvalue; - } + if(bitsPerValue%8) + { + int j=0; + for(i=0;i < n_vals;i++) { + lvalue=0; + for(j=0; j< bitsPerValue;j++){ + lvalue <<= 1; + if(grib_get_bit( p, *bitp)) lvalue += 1; + *bitp += 1; + } + val[i] = lvalue; } - else { - int bc; - int l = bitsPerValue / 8; - size_t o = *bitp / 8; + } else { + int bc; + int l = bitsPerValue/8; + size_t o = *bitp/8; - for (i = 0; i < n_vals; i++) { - lvalue = 0; - lvalue <<= 8; - lvalue |= p[o++]; + for(i=0;i < n_vals;i++) + { + lvalue = 0; + lvalue <<= 8; + lvalue |= p[o++] ; - for (bc = 1; bc < l; bc++) { - lvalue <<= 8; - lvalue |= p[o++]; - } - val[i] = lvalue; - } - *bitp += bitsPerValue * n_vals; + for ( bc=1; bc= 8) { - blen -= 8; - *encoded = (unsigned_val >> blen); - encoded++; - *off += 8; - } + } else{ + //fprintf(stderr, "encoded pointer: %p\n", encoded); + if (bits_per_value == 16 && !((uint64_t)p&3)) { + //fprintf(stderr, "n_vals: %d\n", n_vals); + uint32_t *encoded4byte = (uint32_t *) p; + uint32_t tmp; + unsigned long unsigned_val2 = 0; + for(i=0;i< n_vals;i+=2){ + unsigned_val = (unsigned long)((((val[i]*d)-reference_value)*divisor)+0.5); + unsigned_val2 = (unsigned long)((((val[i+1]*d)-reference_value)*divisor)+0.5); + tmp = (unsigned_val2 & 0xff); + tmp = (tmp << 8) + (unsigned_val2 >> 8 & 0xff); + tmp = (tmp << 8) + (unsigned_val & 0xff); + tmp = (tmp << 8) + ((unsigned_val >> 8) & 0xff); + *encoded4byte = tmp; + encoded4byte++; + *off+=32; + + } + /* remainder */ + if (n_vals % 2) { + int blen=0; + i=n_vals; + blen = bits_per_value; + unsigned_val = (unsigned long)((((val[i]*d)-reference_value)*divisor)+0.5); + while(blen >= 8) + { + blen -= 8; + *encoded = (unsigned_val >> blen); + encoded++; + *off+=8; } + } + + } else { + for(i=0;i< n_vals;i++){ + int blen=0; + blen = bits_per_value; + unsigned_val = (unsigned long)((((val[i]*d)-reference_value)*divisor)+0.5); + while(blen >= 8) + { + blen -= 8; + *encoded = (unsigned_val >> blen); + encoded++; + *off+=8; + } + } } - return GRIB_SUCCESS; + } + return GRIB_SUCCESS; } -int grib_encode_double_array_complex(size_t n_vals, double* val, long nbits, double reference_value, - double* scal, double d, double divisor, unsigned char* p, long* bitp) -{ - return GRIB_NOT_IMPLEMENTED; +int grib_encode_double_array_complex(size_t n_vals,double* val,long nbits,double reference_value, + double* scal,double d,double divisor,unsigned char* p,long *bitp) { + return GRIB_NOT_IMPLEMENTED; } + From f7a05d6e79ed21dfad7ac8d3597a692f48d86a95 Mon Sep 17 00:00:00 2001 From: Johannes Schick Date: Thu, 25 Jun 2020 08:50:00 +0200 Subject: [PATCH 2/2] Add files via upload --- src/grib_bits_any_endian_vector.c | 294 +++++++++++++++--------------- 1 file changed, 146 insertions(+), 148 deletions(-) diff --git a/src/grib_bits_any_endian_vector.c b/src/grib_bits_any_endian_vector.c index 1d1946994..2b2ecdc5f 100644 --- a/src/grib_bits_any_endian_vector.c +++ b/src/grib_bits_any_endian_vector.c @@ -1,5 +1,5 @@ /* - * Copyright 2005-2019 ECMWF. + * (C) Copyright 2005- ECMWF. * * This software is licensed under the terms of the Apache Licence Version 2.0 * which can be obtained at http://www.apache.org/licenses/LICENSE-2.0. @@ -13,159 +13,157 @@ * * ***************************************************************************/ -int grib_decode_long_array(const unsigned char* p, long *bitp, long bitsPerValue, - size_t n_vals,long* val) { - long i=0; - unsigned long lvalue = 0; - - if(bitsPerValue%8) - { - int j=0; - for(i=0;i < n_vals;i++) { - lvalue=0; - for(j=0; j< bitsPerValue;j++){ - lvalue <<= 1; - if(grib_get_bit( p, *bitp)) lvalue += 1; - *bitp += 1; - } - val[i] = lvalue; - } - } else { - int bc; - int l = bitsPerValue/8; - size_t o = *bitp/8; - - for(i=0;i < n_vals;i++) - { - lvalue = 0; - lvalue <<= 8; - lvalue |= p[o++] ; - - for ( bc=1; bc> 8 & 0xff); - tmp = (tmp << 8) + (unsigned_val & 0xff); - tmp = (tmp << 8) + ((unsigned_val >> 8) & 0xff); - *encoded4byte = tmp; - encoded4byte++; - *off+=32; + long i = 0; + unsigned long lvalue = 0; - } - /* remainder */ - if (n_vals % 2) { - int blen=0; - i=n_vals; - blen = bits_per_value; - unsigned_val = (unsigned long)((((val[i]*d)-reference_value)*divisor)+0.5); - while(blen >= 8) - { - blen -= 8; - *encoded = (unsigned_val >> blen); - encoded++; - *off+=8; + /* if (bitsPerValue % 8) { */ + if (bitsPerValue % 8 || (*bitp & 7)) { + int j = 0; + for (i = 0; i < n_vals; i++) { + lvalue = 0; + for (j = 0; j < bitsPerValue; j++) { + lvalue <<= 1; + if (grib_get_bit(p, *bitp)) + lvalue += 1; + *bitp += 1; + } + val[i] = lvalue; } - } - - } else { - for(i=0;i< n_vals;i++){ - int blen=0; - blen = bits_per_value; - unsigned_val = (unsigned long)((((val[i]*d)-reference_value)*divisor)+0.5); - while(blen >= 8) - { - blen -= 8; - *encoded = (unsigned_val >> blen); - encoded++; - *off+=8; + } + else { + int bc; + int l = bitsPerValue / 8; + size_t o = *bitp / 8; + + for (i = 0; i < n_vals; i++) { + lvalue = 0; + lvalue <<= 8; + lvalue |= p[o++]; + + for (bc = 1; bc < l; bc++) { + lvalue <<= 8; + lvalue |= p[o++]; + } + val[i] = lvalue; + } + *bitp += bitsPerValue * n_vals; + } + + return 0; +} + + +int grib_decode_double_array(const unsigned char* p, long* bitp, long bitsPerValue, + double reference_value, double s, double d, + size_t n_vals, double* val) +{ + size_t i = 0; + unsigned long lvalue = 0; + + if (bitsPerValue % 8) { + int j = 0; + for (i = 0; i < n_vals; i++) { + lvalue = 0; + for (j = 0; j < bitsPerValue; j++) { + lvalue <<= 1; + if (grib_get_bit(p, *bitp)) + lvalue += 1; + *bitp += 1; + } + val[i] = (double)(((lvalue * s) + reference_value) * d); + } + } + else { + int bc; + int l = bitsPerValue / 8; + size_t o = 0; + + for (i = 0; i < n_vals; i++) { + lvalue = 0; + lvalue <<= 8; + lvalue |= p[o++]; + + for (bc = 1; bc < l; bc++) { + lvalue <<= 8; + lvalue |= p[o++]; + } + val[i] = (double)(((lvalue * s) + reference_value) * d); + } + } + + return 0; +} + +int grib_decode_double_array_complex(const unsigned char* p, long* bitp, long nbits, double reference_value, double s, double* d, size_t size, double* val) +{ + return GRIB_NOT_IMPLEMENTED; +} + +int grib_encode_double_array(size_t n_vals, const double* val, long bits_per_value, double reference_value, double d, double divisor, unsigned char* p, long* off) +{ + size_t i = 0; + unsigned long unsigned_val = 0; + unsigned char* encoded = p; + if (bits_per_value % 8) { + for (i = 0; i < n_vals; i++) { + unsigned_val = (unsigned long)((((val[i] * d) - reference_value) * divisor) + 0.5); + grib_encode_unsigned_longb(encoded, unsigned_val, off, bits_per_value); + } + } + else { + if (bits_per_value == 16 && !((uint64_t)p&3)) { + uint32_t *encoded4byte = (uint32_t *) p; + uint32_t tmp; + unsigned long unsigned_val2 = 0; + for(i = 0; i < n_vals - 1; i+=2){ + unsigned_val = (unsigned long)((((val[i]*d)-reference_value)*divisor)+0.5); + unsigned_val2 = (unsigned long)((((val[i+1]*d)-reference_value)*divisor)+0.5); + tmp = (unsigned_val2 & 0xff); + tmp = (tmp << 8) + (unsigned_val2 >> 8 & 0xff); + tmp = (tmp << 8) + (unsigned_val & 0xff); + tmp = (tmp << 8) + ((unsigned_val >> 8) & 0xff); + *encoded4byte = tmp; + encoded4byte++; + *off+=32; + } + /* remainder */ + if (n_vals % 2) { + int blen = 0; + i = n_vals-1; + encoded = (unsigned char *) encoded4byte; + blen = bits_per_value; + unsigned_val = (unsigned long)((((val[i]*d)-reference_value)*divisor)+0.5); + while(blen >= 8) + { + blen -= 8; + *encoded = (unsigned_val >> blen); + encoded++; + *off+=8; + } + } + } + else { + for (i = 0; i < n_vals; i++) { + int blen = 0; + blen = bits_per_value; + unsigned_val = (unsigned long)((((val[i] * d) - reference_value) * divisor) + 0.5); + while (blen >= 8) { + blen -= 8; + *encoded = (unsigned_val >> blen); + encoded++; + *off += 8; + } } } } - } - return GRIB_SUCCESS; + return GRIB_SUCCESS; } -int grib_encode_double_array_complex(size_t n_vals,double* val,long nbits,double reference_value, - double* scal,double d,double divisor,unsigned char* p,long *bitp) { - return GRIB_NOT_IMPLEMENTED; +int grib_encode_double_array_complex(size_t n_vals, double* val, long nbits, double reference_value, + double* scal, double d, double divisor, unsigned char* p, long* bitp) +{ + return GRIB_NOT_IMPLEMENTED; } -