storage_number.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #ifndef NETDATA_STORAGE_NUMBER_H
  3. #define NETDATA_STORAGE_NUMBER_H 1
  4. #include "../libnetdata.h"
  5. #ifdef NETDATA_WITHOUT_LONG_DOUBLE
  6. #define powl pow
  7. #define modfl modf
  8. #define llrintl llrint
  9. #define roundl round
  10. #define sqrtl sqrt
  11. #define copysignl copysign
  12. #define strtold strtod
  13. typedef double calculated_number;
  14. #define CALCULATED_NUMBER_FORMAT "%0.7f"
  15. #define CALCULATED_NUMBER_FORMAT_ZERO "%0.0f"
  16. #define CALCULATED_NUMBER_FORMAT_AUTO "%f"
  17. #define LONG_DOUBLE_MODIFIER "f"
  18. typedef double LONG_DOUBLE;
  19. #else // NETDATA_WITHOUT_LONG_DOUBLE
  20. typedef long double calculated_number;
  21. #define CALCULATED_NUMBER_FORMAT "%0.7Lf"
  22. #define CALCULATED_NUMBER_FORMAT_ZERO "%0.0Lf"
  23. #define CALCULATED_NUMBER_FORMAT_AUTO "%Lf"
  24. #define LONG_DOUBLE_MODIFIER "Lf"
  25. typedef long double LONG_DOUBLE;
  26. #endif // NETDATA_WITHOUT_LONG_DOUBLE
  27. //typedef long long calculated_number;
  28. //#define CALCULATED_NUMBER_FORMAT "%lld"
  29. typedef long long collected_number;
  30. #define COLLECTED_NUMBER_FORMAT "%lld"
  31. /*
  32. typedef long double collected_number;
  33. #define COLLECTED_NUMBER_FORMAT "%0.7Lf"
  34. */
  35. #define calculated_number_modf(x, y) modfl(x, y)
  36. #define calculated_number_llrint(x) llrintl(x)
  37. #define calculated_number_round(x) roundl(x)
  38. #define calculated_number_fabs(x) fabsl(x)
  39. #define calculated_number_pow(x, y) powl(x, y)
  40. #define calculated_number_epsilon (calculated_number)0.0000001
  41. #define calculated_number_equal(a, b) (calculated_number_fabs((a) - (b)) < calculated_number_epsilon)
  42. #define calculated_number_isnumber(a) (!(fpclassify(a) & (FP_NAN|FP_INFINITE)))
  43. typedef uint32_t storage_number;
  44. #define STORAGE_NUMBER_FORMAT "%u"
  45. #define SN_ANOMALY_BIT (1 << 24) // the anomaly bit of the value
  46. #define SN_EXISTS_RESET (1 << 25) // the value has been overflown
  47. #define SN_EXISTS_100 (1 << 26) // very large value (multiplier is 100 instead of 10)
  48. #define SN_DEFAULT_FLAGS SN_ANOMALY_BIT
  49. #define SN_EMPTY_SLOT 0x00000000
  50. // When the calculated number is zero and the value is anomalous (ie. it's bit
  51. // is zero) we want to return a storage_number representation that is
  52. // different from the empty slot. We achieve this by mapping zero to
  53. // SN_EXISTS_100. Unpacking the SN_EXISTS_100 value will return zero because
  54. // its fraction field (as well as its exponent factor field) will be zero.
  55. #define SN_ANOMALOUS_ZERO SN_EXISTS_100
  56. // checks
  57. #define does_storage_number_exist(value) (((storage_number) (value)) != SN_EMPTY_SLOT)
  58. #define did_storage_number_reset(value) ((((storage_number) (value)) & SN_EXISTS_RESET) != 0)
  59. storage_number pack_storage_number(calculated_number value, uint32_t flags);
  60. calculated_number unpack_storage_number(storage_number value);
  61. int print_calculated_number(char *str, calculated_number value);
  62. // sign div/mul <--- multiplier / divider ---> 10/100 RESET EXISTS VALUE
  63. #define STORAGE_NUMBER_POSITIVE_MAX_RAW (storage_number)( (0 << 31) | (1 << 30) | (1 << 29) | (1 << 28) | (1<<27) | (1 << 26) | (0 << 25) | (1 << 24) | 0x00ffffff )
  64. #define STORAGE_NUMBER_POSITIVE_MIN_RAW (storage_number)( (0 << 31) | (0 << 30) | (1 << 29) | (1 << 28) | (1<<27) | (0 << 26) | (0 << 25) | (1 << 24) | 0x00000001 )
  65. #define STORAGE_NUMBER_NEGATIVE_MAX_RAW (storage_number)( (1 << 31) | (0 << 30) | (1 << 29) | (1 << 28) | (1<<27) | (0 << 26) | (0 << 25) | (1 << 24) | 0x00000001 )
  66. #define STORAGE_NUMBER_NEGATIVE_MIN_RAW (storage_number)( (1 << 31) | (1 << 30) | (1 << 29) | (1 << 28) | (1<<27) | (1 << 26) | (0 << 25) | (1 << 24) | 0x00ffffff )
  67. // accepted accuracy loss
  68. #define ACCURACY_LOSS_ACCEPTED_PERCENT 0.0001
  69. #define accuracy_loss(t1, t2) (((t1) == (t2) || (t1) == 0.0 || (t2) == 0.0) ? 0.0 : (100.0 - (((t1) > (t2)) ? ((t2) * 100.0 / (t1) ) : ((t1) * 100.0 / (t2)))))
  70. // Maximum acceptable rate of increase for counters. With a rate of 10% netdata can safely detect overflows with a
  71. // period of at least every other 10 samples.
  72. #define MAX_INCREMENTAL_PERCENT_RATE 10
  73. #endif /* NETDATA_STORAGE_NUMBER_H */