| 1 |
/********************************************************************* |
| 2 |
Blosc - Blocked Shuffling and Compression Library |
| 3 |
|
| 4 |
Copyright (c) 2021 Blosc Development Team <[email protected]> |
| 5 |
https://blosc.org |
| 6 |
License: BSD 3-Clause (see LICENSE.txt) |
| 7 |
|
| 8 |
See LICENSE.txt for details about copyright and rights to use. |
| 9 |
**********************************************************************/ |
| 10 |
|
| 11 |
#include "trunc-prec.h" |
| 12 |
#include "blosc2.h" |
| 13 |
|
| 14 |
#include <assert.h> |
| 15 |
#include <stdio.h> |
| 16 |
#include <stdint.h> |
| 17 |
#include <stdlib.h> |
| 18 |
|
| 19 |
#define BITS_MANTISSA_FLOAT 23 |
| 20 |
#define BITS_MANTISSA_DOUBLE 52 |
| 21 |
|
| 22 |
|
| 23 |
int truncate_precision32(int8_t prec_bits, int32_t nelems, |
| 24 |
const int32_t* src, int32_t* dest) { |
| 25 |
// Make sure that we don't remove all the bits in mantissa so that we |
| 26 |
// don't mess with NaNs or Infinite representation in IEEE 754: |
| 27 |
// https://en.wikipedia.org/wiki/NaN |
| 28 |
if ((abs(prec_bits) > BITS_MANTISSA_FLOAT)) { |
| 29 |
BLOSC_TRACE_ERROR("The precision cannot be larger than %d bits for floats (asking for %d bits)", |
| 30 |
BITS_MANTISSA_FLOAT, prec_bits); |
| 31 |
return -1; |
| 32 |
} |
| 33 |
int zeroed_bits = (prec_bits >= 0) ? BITS_MANTISSA_FLOAT - prec_bits : -prec_bits; |
| 34 |
if (zeroed_bits >= BITS_MANTISSA_FLOAT) { |
| 35 |
BLOSC_TRACE_ERROR("The reduction in precision cannot be larger or equal than %d bits for floats (asking for %d bits)", |
| 36 |
BITS_MANTISSA_FLOAT, zeroed_bits); |
| 37 |
return -1; |
| 38 |
} |
| 39 |
int32_t mask = ~((1 << zeroed_bits) - 1); |
| 40 |
for (int i = 0; i < nelems; i++) { |
| 41 |
dest[i] = src[i] & mask; |
| 42 |
} |
| 43 |
return 0; |
| 44 |
} |
| 45 |
|
| 46 |
int truncate_precision64(int8_t prec_bits, int32_t nelems, |
| 47 |
const int64_t* src, int64_t* dest) { |
| 48 |
// Make sure that we don't remove all the bits in mantissa so that we |
| 49 |
// don't mess with NaNs or Infinite representation in IEEE 754: |
| 50 |
// https://en.wikipedia.org/wiki/NaN |
| 51 |
if ((abs(prec_bits) > BITS_MANTISSA_DOUBLE)) { |
| 52 |
BLOSC_TRACE_ERROR("The precision cannot be larger than %d bits for floats (asking for %d bits)", |
| 53 |
BITS_MANTISSA_DOUBLE, prec_bits); |
| 54 |
return -1; |
| 55 |
} |
| 56 |
int zeroed_bits = (prec_bits >= 0) ? BITS_MANTISSA_DOUBLE - prec_bits : -prec_bits; |
| 57 |
if (zeroed_bits >= BITS_MANTISSA_DOUBLE) { |
| 58 |
BLOSC_TRACE_ERROR("The reduction in precision cannot be larger or equal than %d bits for floats (asking for %d bits)", |
| 59 |
BITS_MANTISSA_DOUBLE, zeroed_bits); |
| 60 |
return -1; |
| 61 |
} |
| 62 |
uint64_t mask = ~((1ULL << zeroed_bits) - 1ULL); |
| 63 |
for (int i = 0; i < nelems; i++) { |
| 64 |
dest[i] = (int64_t)(src[i] & mask); |
| 65 |
} |
| 66 |
return 0; |
| 67 |
} |
| 68 |
|
| 69 |
/* Apply the truncate precision to src. This can never fail. */ |
| 70 |
int truncate_precision(int8_t prec_bits, int32_t typesize, int32_t nbytes, |
| 71 |
const uint8_t* src, uint8_t* dest) { |
| 72 |
// Positive values of prec_bits will set absolute precision bits, whereas negative |
| 73 |
// values will reduce the precision bits (similar to Python slicing convention). |
| 74 |
switch (typesize) { |
| 75 |
case 4: |
| 76 |
return truncate_precision32(prec_bits, nbytes / typesize, |
| 77 |
(int32_t *)src, (int32_t *)dest); |
| 78 |
case 8: |
| 79 |
return truncate_precision64(prec_bits, nbytes / typesize, |
| 80 |
(int64_t *)src, (int64_t *)dest); |
| 81 |
default: |
| 82 |
BLOSC_TRACE_ERROR("Error in trunc-prec filter: Precision for typesize %d not handled", |
| 83 |
(int)typesize); |
| 84 |
return -1; |
| 85 |
} |
| 86 |
} |