storage_number.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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. storage_number r = flags & SN_ALL_FLAGS;
  12. // The isnormal() macro shall determine whether its argument value
  13. // is normal (neither zero, subnormal, infinite, nor NaN).
  14. if(unlikely(!isnormal(value)))
  15. goto RET_SN;
  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 += (1 << 31); // the sign bit 32
  22. n = -n;
  23. }
  24. if(n / 10000000.0 > 0x00ffffff) {
  25. factor = 100;
  26. r |= SN_EXISTS_100;
  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 += (1 << 30) + (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. goto RET_SN;
  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 += (0 << 30) + (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. RET_SN:
  73. if (r == SN_EMPTY_SLOT)
  74. r = SN_ANOMALOUS_ZERO;
  75. return r;
  76. }
  77. // Lookup table to make storage number unpacking efficient.
  78. NETDATA_DOUBLE unpack_storage_number_lut10x[4 * 8];
  79. __attribute__((constructor)) void initialize_lut(void) {
  80. // The lookup table is partitioned in 4 subtables based on the
  81. // values of the factor and exp bits.
  82. for (int i = 0; i < 8; i++) {
  83. // factor = 0
  84. unpack_storage_number_lut10x[0 * 8 + i] = 1 / pow(10, i); // exp = 0
  85. unpack_storage_number_lut10x[1 * 8 + i] = pow(10, i); // exp = 1
  86. // factor = 1
  87. unpack_storage_number_lut10x[2 * 8 + i] = 1 / pow(100, i); // exp = 0
  88. unpack_storage_number_lut10x[3 * 8 + i] = pow(100, i); // exp = 1
  89. }
  90. }
  91. /*
  92. int print_netdata_double(char *str, NETDATA_DOUBLE value)
  93. {
  94. char *wstr = str;
  95. int sign = (value < 0) ? 1 : 0;
  96. if(sign) value = -value;
  97. #ifdef STORAGE_WITH_MATH
  98. // without llrintl() there are rounding problems
  99. // for example 0.9 becomes 0.89
  100. unsigned long long uvalue = (unsigned long long int) llrintl(value * (NETDATA_DOUBLE)100000);
  101. #else
  102. unsigned long long uvalue = value * (NETDATA_DOUBLE)100000;
  103. #endif
  104. wstr = print_number_llu_r_smart(str, uvalue);
  105. // make sure we have 6 bytes at least
  106. while((wstr - str) < 6) *wstr++ = '0';
  107. // put the sign back
  108. if(sign) *wstr++ = '-';
  109. // reverse it
  110. char *begin = str, *end = --wstr, aux;
  111. while (end > begin) aux = *end, *end-- = *begin, *begin++ = aux;
  112. // wstr--;
  113. // strreverse(str, wstr);
  114. // remove trailing zeros
  115. int decimal = 5;
  116. while(decimal > 0 && *wstr == '0') {
  117. *wstr-- = '\0';
  118. decimal--;
  119. }
  120. // terminate it, one position to the right
  121. // to let space for a dot
  122. wstr[2] = '\0';
  123. // make space for the dot
  124. int i;
  125. for(i = 0; i < decimal ;i++) {
  126. wstr[1] = wstr[0];
  127. wstr--;
  128. }
  129. // put the dot
  130. if(wstr[2] == '\0') { wstr[1] = '\0'; decimal--; }
  131. else wstr[1] = '.';
  132. // return the buffer length
  133. return (int) ((wstr - str) + 2 + decimal );
  134. }
  135. */
  136. int print_netdata_double(char *str, NETDATA_DOUBLE value) {
  137. // info("printing number " NETDATA_DOUBLE_FORMAT, value);
  138. char integral_str[50], fractional_str[50];
  139. char *wstr = str;
  140. if(unlikely(value < 0)) {
  141. *wstr++ = '-';
  142. value = -value;
  143. }
  144. NETDATA_DOUBLE integral, fractional;
  145. #ifdef STORAGE_WITH_MATH
  146. fractional = modfndd(value, &integral) * 10000000.0;
  147. #else
  148. fractional = ((unsigned long long)(value * 10000000ULL) % 10000000ULL);
  149. #endif
  150. unsigned long long integral_int = (unsigned long long)integral;
  151. unsigned long long fractional_int = (unsigned long long)llrintndd(fractional);
  152. if(unlikely(fractional_int >= 10000000)) {
  153. integral_int += 1;
  154. fractional_int -= 10000000;
  155. }
  156. // info("integral " NETDATA_DOUBLE_FORMAT " (%llu), fractional " NETDATA_DOUBLE_FORMAT " (%llu)", integral, integral_int, fractional, fractional_int);
  157. char *istre;
  158. if(unlikely(integral_int == 0)) {
  159. integral_str[0] = '0';
  160. istre = &integral_str[1];
  161. }
  162. else
  163. // convert the integral part to string (reversed)
  164. istre = print_number_llu_r_smart(integral_str, integral_int);
  165. // copy reversed the integral string
  166. istre--;
  167. while( istre >= integral_str ) *wstr++ = *istre--;
  168. if(likely(fractional_int != 0)) {
  169. // add a dot
  170. *wstr++ = '.';
  171. // convert the fractional part to string (reversed)
  172. char *fstre = print_number_llu_r_smart(fractional_str, fractional_int);
  173. // prepend zeros to reach 7 digits length
  174. int decimal = 7;
  175. int len = (int)(fstre - fractional_str);
  176. while(len < decimal) {
  177. *wstr++ = '0';
  178. len++;
  179. }
  180. char *begin = fractional_str;
  181. while(begin < fstre && *begin == '0') begin++;
  182. // copy reversed the fractional string
  183. fstre--;
  184. while( fstre >= begin ) *wstr++ = *fstre--;
  185. }
  186. *wstr = '\0';
  187. // info("printed number '%s'", str);
  188. return (int)(wstr - str);
  189. }