changed bit functions, added timespec compare functions, changed timespec_diff, reworked some code to not give warnings

AuthorKonata <konata@posteo.jp>
Date
Commit6fb96ece58400c6a90109735495a56c3c0dac1c0
Parent8220e00
7 files changed, 93 insertions(+), 60 deletions(-)
Minclude/cutils/bitfuncs.h
@@ -5,13 +5,14 @@
55 #include <cutils/common.h>
66
77 #define numbits(type) (sizeof(type)*CHAR_BIT)
8+#define numbytes(type) (sizeof(type))
89
9-#define bitstring(type, value) _bitstring(numbits(type), (type)(value))
10-/* FIXME: fix out of range calls (bitstring(unsigned char, 256)) */
11-char* _bitstring(size_t size, uintmax_t value);
10+#define getbit(value, bit) (((value) >> (bit)) & 1)
1211
13-#define getbit(type, value, bit) (((value) & (((type)1 << (bit)))) >> (bit))
14-#define setbit(type, value, bit) ((value) |= ((type)1 << (bit)))
15-#define clearbit(type, value, bit) ((value) &= (type)~((type)1 << (bit)))
12+#define setbit(value, bit) ((value) |= ((uintmax_t)1 << (bit)))
13+#define clearbit(value, bit) ((value) &= ~((uintmax_t)1 << (bit)))
14+
15+#define setbitc(value, bit, type) ((value) |= ((type)1 << (bit)))
16+#define clearbitc(value, bit, type) ((value) &= ~((type)1 << (bit)))
1617
1718 #endif /* CUTILS_BITFUNCS_H */
Minclude/cutils/misc.h
@@ -68,14 +68,15 @@ static void memqswap(void* item1, void* item2, void* tmp, size_t length)
6868 memcpy(item2, tmp, length);
6969 }
7070
71-#if __STDC_VERSION__ >= 201112L
72- #include <time.h>
73-#endif
74-
7571 void sleep_ms(unsigned int milliseconds);
7672
7773 #if __STDC_VERSION__ >= 201112L
78- struct timespec timespec_diff(const struct timespec* old_ts, const struct timespec* new_ts);
74+ #include <time.h>
75+ bool timespec_bigger(const struct timespec* ts1, const struct timespec* ts2);
76+ bool timespec_equal(const struct timespec* ts1, const struct timespec* ts2);
77+ bool timespec_smaller(const struct timespec* ts1, const struct timespec* ts2);
78+ /* subtracts ts2 from ts1, if ts2 > ts1 then zero is returned */
79+ struct timespec timespec_diff(const struct timespec* ts1, const struct timespec* ts2);
7980 struct timespec timespec_add(const struct timespec* ts_1, const struct timespec* ts_2);
8081 uintmax_t timespec_ms(const struct timespec* ts);
8182 #endif
Mmeson.build
@@ -1,7 +1,7 @@
11 project('cutils', 'c')
22
33 WARNINGS = ['-Wall', '-Wpedantic', '-Wextra', '-Wnull-dereference', '-Wshadow', '-Wconversion', '-Wstrict-prototypes', '-Wmissing-prototypes', '-Wcast-qual', '-Wstrict-overflow=5', '-Wunreachable-code', '-Wno-unused-parameter', '-Wno-unused-function', '-Wno-long-long']
4-CFLAGS = ['-std=c89', '-fstrict-aliasing', '-fPIC', '-Wno-long-long', '-Og'] + WARNINGS
4+CFLAGS = ['-std=c11', '-fstrict-aliasing', '-fPIC', '-Wno-long-long', '-Og'] + WARNINGS
55
66 subdir('include')
77 subdir('src')
Msrc/bitfuncs.c
@@ -1,24 +1,2 @@
11 #include <cutils/bitfuncs.h>
22
3-char* _bitstring(size_t size, uintmax_t value)
4-{
5- char* ret = NULL;
6- size_t i, pos;
7-
8- bool leading_zero = true;
9- for(i = 1, pos = 0; i <= size; i++)
10- {
11- byte tmp = (byte)((value & ((uintmax_t)1 << (size-i))) >> (size-i));
12- if(tmp && leading_zero)
13- {
14- leading_zero = false;
15- ret = malloc(size-i);
16- if(!ret)
17- return NULL;
18- ret[size-i-1] = '\0';
19- }
20- if(!leading_zero)
21- ret[pos++] = tmp ? '1' : '0';
22- }
23- return ret;
24-}
Msrc/math.c
@@ -2,7 +2,7 @@
22
33 uintmax_t isqrt(uintmax_t n)
44 {
5- uintmax_t start = 1, end = n/2, ans;
5+ uintmax_t start = 1, end = n/2, ans = 0;
66 if(n == 0 || n == 1)
77 return n;
88
@@ -60,9 +60,9 @@ static void primesieve_set(byte* numbers, size_t index)
6060
6161 static bool primesieve_get(byte* numbers, size_t index)
6262 {
63- return getbit(byte, (numbers[index/8] >> index%8), 0);
63+ return getbit((numbers[index/8] >> index%8), 0);
6464 }
65-#include <assert.h>
65+
6666 Bytearray* primesieve(uintmax_t limit)
6767 {
6868 size_t i, j, length = limit/8+1;
Msrc/misc.c
@@ -26,20 +26,69 @@ uint32_t ntoh32(uint32_t const net)
2626 #endif
2727
2828 #if __STDC_VERSION__ >= 201112L
29-struct timespec timespec_diff(const struct timespec* old_ts, const struct timespec* new_ts)
29+
30+bool timespec_bigger(const struct timespec* ts1, const struct timespec* ts2)
3031 {
31- struct timespec diff;
32+ if(ts1->tv_sec > ts2->tv_sec)
33+ {
34+ return true;
35+ } else if(ts1->tv_sec < ts2->tv_sec) {
36+ return false;
37+ } else {
38+ if(ts1->tv_nsec > ts2->tv_nsec)
39+ {
40+ return true;
41+ } else if(ts1->tv_nsec < ts2->tv_nsec) {
42+ return false;
43+ } else {
44+ return false;
45+ }
46+ }
47+}
3248
33- if ((new_ts->tv_nsec - old_ts->tv_nsec) < 0)
34- {
35- diff.tv_sec = new_ts->tv_sec - old_ts->tv_sec - 1;
36- diff.tv_nsec = new_ts->tv_nsec - old_ts->tv_nsec + 1000000000;
37- } else {
38- diff.tv_sec = new_ts->tv_sec - old_ts->tv_sec;
39- diff.tv_nsec = new_ts->tv_nsec - old_ts->tv_nsec;
40- }
49+bool timespec_equal(const struct timespec* ts1, const struct timespec* ts2)
50+{
51+ if(ts1->tv_sec == ts2->tv_sec && ts1->tv_nsec == ts2->tv_nsec)
52+ {
53+ return true;
54+ } else {
55+ return false;
56+ }
57+}
58+
59+bool timespec_smaller(const struct timespec* ts1, const struct timespec* ts2)
60+{
61+ if(timespec_equal(ts1, ts2))
62+ {
63+ return false;
64+ } else if(timespec_bigger(ts1, ts2)) {
65+ return false;
66+ } else {
67+ return true;
68+ }
69+}
70+
71+struct timespec timespec_diff(const struct timespec* ts1, const struct timespec* ts2)
72+{
73+ struct timespec result;
74+
75+ if((ts1->tv_sec < ts2->tv_sec) ||
76+ ((ts1->tv_sec == ts2->tv_sec) &&
77+ (ts1->tv_nsec <= ts2->tv_nsec)))
78+ {
79+ result.tv_sec = result.tv_nsec = 0 ;
80+ } else {
81+ result.tv_sec = ts1->tv_sec - ts2->tv_sec ;
82+ if (ts1->tv_nsec < ts2->tv_nsec) {
83+ result.tv_nsec = ts1->tv_nsec + 1000000000L - ts2->tv_nsec ;
84+ result.tv_sec-- ;
85+ } else {
86+ result.tv_nsec = ts1->tv_nsec - ts2->tv_nsec ;
87+ }
88+ }
89+
90+ return result;
4191
42- return diff;
4392 }
4493
4594 struct timespec timespec_add(const struct timespec* ts_1, const struct timespec* ts_2)
Msrc/test.c
@@ -71,7 +71,7 @@ static void test_string(void)
7171 String* string2;
7272 char* cstring;
7373 size_t tmp;
74- bool truth;
74+ int truth;
7575
7676 for(truth = false; truth == false || truth == true; truth++)
7777 {
@@ -241,7 +241,7 @@ static void test_timespec(void)
241241 ts2.tv_sec = 10;
242242 ts2.tv_nsec = 999999999;
243243
244- res = timespec_diff(&ts1, &ts2);
244+ res = timespec_diff(&ts2, &ts1);
245245 assert(res.tv_sec == 10 && res.tv_nsec == 999999998);
246246
247247 ts1.tv_sec = 0;
@@ -249,7 +249,7 @@ static void test_timespec(void)
249249 ts2.tv_sec = 10;
250250 ts2.tv_nsec = 1;
251251
252- res = timespec_diff(&ts1, &ts2);
252+ res = timespec_diff(&ts2, &ts1);
253253 assert(res.tv_sec == 9 && res.tv_nsec == 2);
254254
255255 ts1.tv_sec = 0;
@@ -257,15 +257,18 @@ static void test_timespec(void)
257257 ts2.tv_sec = 10;
258258 ts2.tv_nsec = 999999999;
259259
260- res = timespec_diff(&ts1, &ts2);
260+ res = timespec_diff(&ts2, &ts1);
261261 assert(res.tv_sec == 10 && res.tv_nsec == 999999998);
262+
263+ res = timespec_diff(&ts1, &ts2);
264+ assert(res.tv_sec == 0 && res.tv_nsec == 0);
262265 }
263266 #endif
264267 static void test_ll(void)
265268 {
266269 LinkedList* ll;
267270 struct test my_structa, my_structb, *my_structptr;
268- bool truth;
271+ int truth;
269272 my_structa.a = 5;
270273 my_structa.b = 5;
271274 my_structb.a = 7;
@@ -303,19 +306,20 @@ static void test_ll(void)
303306 static void test_bitfuncs(void)
304307 {
305308 byte tmp = 0;
309+ uintmax_t tmp2 = UINTMAX_MAX;
306310
307- setbit(byte, tmp, 0);
311+ setbit(tmp, 0);
308312 assert(tmp == 1);
309- setbit(byte, tmp, 1);
313+ setbitc(tmp, 1, byte);
310314 assert(tmp == 3);
311- setbit(byte, tmp, 2);
315+ setbit(tmp, 2);
312316 assert(tmp == 7);
313317
314- clearbit(byte, tmp, 2);
315- assert(tmp == 3);
318+ clearbit(tmp2, numbits(uintmax_t)-1);
319+ assert(tmp2 == UINTMAX_MAX/2);
316320
317- assert(getbit(byte, tmp, 0) == 1);
318- assert(getbit(byte, tmp, 5) == 0);
321+ assert(getbit(tmp, 0) == 1);
322+ assert(getbit(tmp, 5) == 0);
319323 }
320324
321325 int main(int argc, char** argv)