storage_number.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #include "../libnetdata.h"
  3. storage_number pack_storage_number(NETDATA_DOUBLE value, SN_FLAGS flags) {
  4. // bit 32 = sign 0:positive, 1:negative
  5. // bit 31 = 0:divide, 1:multiply
  6. // bit 30, 29, 28 = (multiplier or divider) 0-7 (8 total)
  7. // bit 27 SN_EXISTS_100
  8. // bit 26 SN_EXISTS_RESET
  9. // bit 25 SN_ANOMALY_BIT = 0: anomalous, 1: not anomalous
  10. // bit 24 to bit 1 = the value
  11. if(unlikely(fpclassify(value) == FP_NAN || fpclassify(value) == FP_INFINITE))
  12. return SN_EMPTY_SLOT;
  13. storage_number r = flags & SN_USER_FLAGS;
  14. if(unlikely(fpclassify(value) == FP_ZERO || fpclassify(value) == FP_SUBNORMAL))
  15. return r;
  16. int m = 0;
  17. NETDATA_DOUBLE n = value, factor = 10;
  18. // if the value is negative
  19. // add the sign bit and make it positive
  20. if(n < 0) {
  21. r += SN_FLAG_NEGATIVE; // the sign bit 32
  22. n = -n;
  23. }
  24. if(n / 10000000.0 > 0x00ffffff) {
  25. factor = 100;
  26. r |= SN_FLAG_NOT_EXISTS_MUL100;
  27. }
  28. // make its integer part fit in 0x00ffffff
  29. // by dividing it by 10 up to 7 times
  30. // and increasing the multiplier
  31. while(m < 7 && n > (NETDATA_DOUBLE)0x00ffffff) {
  32. n /= factor;
  33. m++;
  34. }
  35. if(m) {
  36. // the value was too big, and we divided it
  37. // so, we add a multiplier to unpack it
  38. r += SN_FLAG_MULTIPLY + (m << 27); // the multiplier m
  39. if(n > (NETDATA_DOUBLE)0x00ffffff) {
  40. #ifdef NETDATA_INTERNAL_CHECKS
  41. error("Number " NETDATA_DOUBLE_FORMAT " is too big.", value);
  42. #endif
  43. r += 0x00ffffff;
  44. return r;
  45. }
  46. }
  47. else {
  48. // 0x0019999e is the number that can be multiplied
  49. // by 10 to give 0x00ffffff
  50. // while the value is below 0x0019999e we can
  51. // multiply it by 10, up to 7 times, increasing
  52. // the multiplier
  53. while(m < 7 && n < (NETDATA_DOUBLE)0x0019999e) {
  54. n *= 10;
  55. m++;
  56. }
  57. if (unlikely(n > (NETDATA_DOUBLE)0x00ffffff)) {
  58. n /= 10;
  59. m--;
  60. }
  61. // the value was small enough, and we multiplied it
  62. // so, we add a divider to unpack it
  63. r += (m << 27); // the divider m
  64. }
  65. #ifdef STORAGE_WITH_MATH
  66. // without this there are rounding problems
  67. // example: 0.9 becomes 0.89
  68. r += lrint((double) n);
  69. #else
  70. r += (storage_number)n;
  71. #endif
  72. return r;
  73. }
  74. // Lookup table to make storage number unpacking efficient.
  75. NETDATA_DOUBLE unpack_storage_number_lut10x[4 * 8];
  76. __attribute__((constructor)) void initialize_lut(void) {
  77. // The lookup table is partitioned in 4 subtables based on the
  78. // values of the factor and exp bits.
  79. for (int i = 0; i < 8; i++) {
  80. // factor = 0
  81. unpack_storage_number_lut10x[0 * 8 + i] = 1 / pow(10, i); // exp = 0
  82. unpack_storage_number_lut10x[1 * 8 + i] = pow(10, i); // exp = 1
  83. // factor = 1
  84. unpack_storage_number_lut10x[2 * 8 + i] = 1 / pow(100, i); // exp = 0
  85. unpack_storage_number_lut10x[3 * 8 + i] = pow(100, i); // exp = 1
  86. }
  87. }
  88. /*
  89. int print_netdata_double(char *str, NETDATA_DOUBLE value)
  90. {
  91. char *wstr = str;
  92. int sign = (value < 0) ? 1 : 0;
  93. if(sign) value = -value;
  94. #ifdef STORAGE_WITH_MATH
  95. // without llrintl() there are rounding problems
  96. // for example 0.9 becomes 0.89
  97. unsigned long long uvalue = (unsigned long long int) llrintl(value * (NETDATA_DOUBLE)100000);
  98. #else
  99. unsigned long long uvalue = value * (NETDATA_DOUBLE)100000;
  100. #endif
  101. wstr = print_number_llu_r_smart(str, uvalue);
  102. // make sure we have 6 bytes at least
  103. while((wstr - str) < 6) *wstr++ = '0';
  104. // put the sign back
  105. if(sign) *wstr++ = '-';
  106. // reverse it
  107. char *begin = str, *end = --wstr, aux;
  108. while (end > begin) aux = *end, *end-- = *begin, *begin++ = aux;
  109. // wstr--;
  110. // strreverse(str, wstr);
  111. // remove trailing zeros
  112. int decimal = 5;
  113. while(decimal > 0 && *wstr == '0') {
  114. *wstr-- = '\0';
  115. decimal--;
  116. }
  117. // terminate it, one position to the right
  118. // to let space for a dot
  119. wstr[2] = '\0';
  120. // make space for the dot
  121. int i;
  122. for(i = 0; i < decimal ;i++) {
  123. wstr[1] = wstr[0];
  124. wstr--;
  125. }
  126. // put the dot
  127. if(wstr[2] == '\0') { wstr[1] = '\0'; decimal--; }
  128. else wstr[1] = '.';
  129. // return the buffer length
  130. return (int) ((wstr - str) + 2 + decimal );
  131. }
  132. */
  133. int print_netdata_double(char *str, NETDATA_DOUBLE value) {
  134. // info("printing number " NETDATA_DOUBLE_FORMAT, value);
  135. char integral_str[50], fractional_str[50];
  136. char *wstr = str;
  137. if(unlikely(value < 0)) {
  138. *wstr++ = '-';
  139. value = -value;
  140. }
  141. NETDATA_DOUBLE integral, fractional;
  142. #ifdef STORAGE_WITH_MATH
  143. fractional = modfndd(value, &integral) * 10000000.0;
  144. #else
  145. integral = (NETDATA_DOUBLE)((unsigned long long)(value * 10000000ULL) / 10000000ULL);
  146. fractional = (NETDATA_DOUBLE)((unsigned long long)(value * 10000000ULL) % 10000000ULL);
  147. #endif
  148. unsigned long long integral_int = (unsigned long long)integral;
  149. unsigned long long fractional_int = (unsigned long long)llrintndd(fractional);
  150. if(unlikely(fractional_int >= 10000000)) {
  151. integral_int += 1;
  152. fractional_int -= 10000000;
  153. }
  154. // info("integral " NETDATA_DOUBLE_FORMAT " (%llu), fractional " NETDATA_DOUBLE_FORMAT " (%llu)", integral, integral_int, fractional, fractional_int);
  155. char *istre;
  156. if(unlikely(integral_int == 0)) {
  157. integral_str[0] = '0';
  158. istre = &integral_str[1];
  159. }
  160. else
  161. // convert the integral part to string (reversed)
  162. istre = print_number_llu_r_smart(integral_str, integral_int);
  163. // copy reversed the integral string
  164. istre--;
  165. while( istre >= integral_str ) *wstr++ = *istre--;
  166. if(likely(fractional_int != 0)) {
  167. // add a dot
  168. *wstr++ = '.';
  169. // convert the fractional part to string (reversed)
  170. char *fstre = print_number_llu_r_smart(fractional_str, fractional_int);
  171. // prepend zeros to reach 7 digits length
  172. int decimal = 7;
  173. int len = (int)(fstre - fractional_str);
  174. while(len < decimal) {
  175. *wstr++ = '0';
  176. len++;
  177. }
  178. char *begin = fractional_str;
  179. while(begin < fstre && *begin == '0') begin++;
  180. // copy reversed the fractional string
  181. fstre--;
  182. while( fstre >= begin ) *wstr++ = *fstre--;
  183. }
  184. *wstr = '\0';
  185. // info("printed number '%s'", str);
  186. return (int)(wstr - str);
  187. }