]> arthur.barton.de Git - netdata.git/blob - src/inlined.h
faster number parsing
[netdata.git] / src / inlined.h
1 #ifndef NETDATA_INLINED_H
2 #define NETDATA_INLINED_H
3
4 #include "common.h"
5
6 #ifdef HAVE_STMT_EXPR_NON_EXISTING
7 // GCC extension to define a function as a preprocessor macro
8
9 #define simple_hash(name) ({                                         \
10     register unsigned char *__hash_source = (unsigned char *)(name); \
11     register uint32_t __hash_value = 0x811c9dc5;                     \
12     while (*__hash_source) {                                         \
13         __hash_value *= 16777619;                                    \
14         __hash_value ^= (uint32_t) *__hash_source++;                 \
15     }                                                                \
16     __hash_value;                                                    \
17 })
18
19 #define simple_uhash(name) ({                                        \
20     register unsigned char *__hash_source = (unsigned char *)(name); \
21     register uint32_t __hash_value = 0x811c9dc5, __hash_char;        \
22     while ((__hash_char = *__hash_source++)) {                       \
23         if (unlikely(__hash_char >= 'A' && __hash_char <= 'Z'))      \
24             __hash_char += 'a' - 'A';                                \
25         __hash_value *= 16777619;                                    \
26         __hash_value ^= __hash_char;                                 \
27     }                                                                \
28     __hash_value;                                                    \
29 })
30
31 #else /* ! HAVE_STMT_EXPR */
32
33 // for faster execution, allow the compiler to inline
34 // these functions that are called to hash strings
35 static inline uint32_t simple_hash(const char *name) {
36     register unsigned char *s = (unsigned char *) name;
37     register uint32_t hval = 0x811c9dc5;
38     while (*s) {
39         hval *= 16777619;
40         hval ^= (uint32_t) *s++;
41     }
42     return hval;
43 }
44
45 static inline uint32_t simple_uhash(const char *name) {
46     register unsigned char *s = (unsigned char *) name;
47     register uint32_t hval = 0x811c9dc5, c;
48     while ((c = *s++)) {
49         if (unlikely(c >= 'A' && c <= 'Z')) c += 'a' - 'A';
50         hval *= 16777619;
51         hval ^= c;
52     }
53     return hval;
54 }
55
56 #endif /* HAVE_STMT_EXPR */
57
58 static inline long str2l(const char *s) {
59     register long n = 0;
60     register char c, negative = (*s == '-');
61
62     for(c = (negative)?*(++s):*s; c >= '0' && c <= '9' ; c = *(++s)) {
63         n *= 10;
64         n += c - '0';
65     }
66
67     if(unlikely(negative))
68         return -n;
69
70     return n;
71 }
72
73 static inline unsigned long str2ul(const char *s) {
74     register unsigned long n = 0;
75     register char c;
76     for(c = *s; c >= '0' && c <= '9' ; c = *(++s)) {
77         n *= 10;
78         n += c - '0';
79     }
80     return n;
81 }
82
83 static inline unsigned long long str2ull(const char *s) {
84     register unsigned long long n = 0;
85     register char c;
86     for(c = *s; c >= '0' && c <= '9' ; c = *(++s)) {
87         n *= 10;
88         n += c - '0';
89     }
90     return n;
91 }
92
93 static inline int read_single_number_file(const char *filename, unsigned long long *result) {
94     char buffer[1024 + 1];
95
96     int fd = open(filename, O_RDONLY, 0666);
97     if(unlikely(fd == -1)) {
98         *result = 0;
99         return 1;
100     }
101
102     ssize_t r = read(fd, buffer, 1024);
103     if(unlikely(r == -1)) {
104         *result = 0;
105         close(fd);
106         return 2;
107     }
108
109     close(fd);
110     *result = strtoull(buffer, NULL, 0);
111     return 0;
112 }
113
114 #endif //NETDATA_INLINED_H