changed bit functions, added timespec compare functions, changed timespec_diff, reworked some code to not give warnings
Minclude/cutils/bitfuncs.h
| @@ -5,13 +5,14 @@ | |||
|---|---|---|---|
| 5 | 5 | #include <cutils/common.h> | |
| 6 | 6 | ||
| 7 | 7 | #define numbits(type) (sizeof(type)*CHAR_BIT) | |
| 8 | + | #define numbytes(type) (sizeof(type)) | |
| 8 | 9 | ||
| 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) | |
| 12 | 11 | ||
| 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))) | |
| 16 | 17 | ||
| 17 | 18 | #endif /* CUTILS_BITFUNCS_H */ | |
Minclude/cutils/misc.h
| @@ -68,14 +68,15 @@ static void memqswap(void* item1, void* item2, void* tmp, size_t length) | |||
|---|---|---|---|
| 68 | 68 | memcpy(item2, tmp, length); | |
| 69 | 69 | } | |
| 70 | 70 | ||
| 71 | - | #if __STDC_VERSION__ >= 201112L | |
| 72 | - | #include <time.h> | |
| 73 | - | #endif | |
| 74 | - | ||
| 75 | 71 | void sleep_ms(unsigned int milliseconds); | |
| 76 | 72 | ||
| 77 | 73 | #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); | |
| 79 | 80 | struct timespec timespec_add(const struct timespec* ts_1, const struct timespec* ts_2); | |
| 80 | 81 | uintmax_t timespec_ms(const struct timespec* ts); | |
| 81 | 82 | #endif | |
Mmeson.build
| @@ -1,7 +1,7 @@ | |||
|---|---|---|---|
| 1 | 1 | project('cutils', 'c') | |
| 2 | 2 | ||
| 3 | 3 | 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 | |
| 5 | 5 | ||
| 6 | 6 | subdir('include') | |
| 7 | 7 | subdir('src') | |
Msrc/bitfuncs.c
| @@ -1,24 +1,2 @@ | |||
|---|---|---|---|
| 1 | 1 | #include <cutils/bitfuncs.h> | |
| 2 | 2 | ||
| 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 @@ | |||
|---|---|---|---|
| 2 | 2 | ||
| 3 | 3 | uintmax_t isqrt(uintmax_t n) | |
| 4 | 4 | { | |
| 5 | - | uintmax_t start = 1, end = n/2, ans; | |
| 5 | + | uintmax_t start = 1, end = n/2, ans = 0; | |
| 6 | 6 | if(n == 0 || n == 1) | |
| 7 | 7 | return n; | |
| 8 | 8 | ||
| @@ -60,9 +60,9 @@ static void primesieve_set(byte* numbers, size_t index) | |||
|---|---|---|---|
| 60 | 60 | ||
| 61 | 61 | static bool primesieve_get(byte* numbers, size_t index) | |
| 62 | 62 | { | |
| 63 | - | return getbit(byte, (numbers[index/8] >> index%8), 0); | |
| 63 | + | return getbit((numbers[index/8] >> index%8), 0); | |
| 64 | 64 | } | |
| 65 | - | #include <assert.h> | |
| 65 | + | ||
| 66 | 66 | Bytearray* primesieve(uintmax_t limit) | |
| 67 | 67 | { | |
| 68 | 68 | size_t i, j, length = limit/8+1; | |
Msrc/misc.c
| @@ -26,20 +26,69 @@ uint32_t ntoh32(uint32_t const net) | |||
|---|---|---|---|
| 26 | 26 | #endif | |
| 27 | 27 | ||
| 28 | 28 | #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) | |
| 30 | 31 | { | |
| 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 | + | } | |
| 32 | 48 | ||
| 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; | |
| 41 | 91 | ||
| 42 | - | return diff; | |
| 43 | 92 | } | |
| 44 | 93 | ||
| 45 | 94 | 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) | |||
|---|---|---|---|
| 71 | 71 | String* string2; | |
| 72 | 72 | char* cstring; | |
| 73 | 73 | size_t tmp; | |
| 74 | - | bool truth; | |
| 74 | + | int truth; | |
| 75 | 75 | ||
| 76 | 76 | for(truth = false; truth == false || truth == true; truth++) | |
| 77 | 77 | { | |
| @@ -241,7 +241,7 @@ static void test_timespec(void) | |||
|---|---|---|---|
| 241 | 241 | ts2.tv_sec = 10; | |
| 242 | 242 | ts2.tv_nsec = 999999999; | |
| 243 | 243 | ||
| 244 | - | res = timespec_diff(&ts1, &ts2); | |
| 244 | + | res = timespec_diff(&ts2, &ts1); | |
| 245 | 245 | assert(res.tv_sec == 10 && res.tv_nsec == 999999998); | |
| 246 | 246 | ||
| 247 | 247 | ts1.tv_sec = 0; | |
| @@ -249,7 +249,7 @@ static void test_timespec(void) | |||
|---|---|---|---|
| 249 | 249 | ts2.tv_sec = 10; | |
| 250 | 250 | ts2.tv_nsec = 1; | |
| 251 | 251 | ||
| 252 | - | res = timespec_diff(&ts1, &ts2); | |
| 252 | + | res = timespec_diff(&ts2, &ts1); | |
| 253 | 253 | assert(res.tv_sec == 9 && res.tv_nsec == 2); | |
| 254 | 254 | ||
| 255 | 255 | ts1.tv_sec = 0; | |
| @@ -257,15 +257,18 @@ static void test_timespec(void) | |||
|---|---|---|---|
| 257 | 257 | ts2.tv_sec = 10; | |
| 258 | 258 | ts2.tv_nsec = 999999999; | |
| 259 | 259 | ||
| 260 | - | res = timespec_diff(&ts1, &ts2); | |
| 260 | + | res = timespec_diff(&ts2, &ts1); | |
| 261 | 261 | 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); | |
| 262 | 265 | } | |
| 263 | 266 | #endif | |
| 264 | 267 | static void test_ll(void) | |
| 265 | 268 | { | |
| 266 | 269 | LinkedList* ll; | |
| 267 | 270 | struct test my_structa, my_structb, *my_structptr; | |
| 268 | - | bool truth; | |
| 271 | + | int truth; | |
| 269 | 272 | my_structa.a = 5; | |
| 270 | 273 | my_structa.b = 5; | |
| 271 | 274 | my_structb.a = 7; | |
| @@ -303,19 +306,20 @@ static void test_ll(void) | |||
|---|---|---|---|
| 303 | 306 | static void test_bitfuncs(void) | |
| 304 | 307 | { | |
| 305 | 308 | byte tmp = 0; | |
| 309 | + | uintmax_t tmp2 = UINTMAX_MAX; | |
| 306 | 310 | ||
| 307 | - | setbit(byte, tmp, 0); | |
| 311 | + | setbit(tmp, 0); | |
| 308 | 312 | assert(tmp == 1); | |
| 309 | - | setbit(byte, tmp, 1); | |
| 313 | + | setbitc(tmp, 1, byte); | |
| 310 | 314 | assert(tmp == 3); | |
| 311 | - | setbit(byte, tmp, 2); | |
| 315 | + | setbit(tmp, 2); | |
| 312 | 316 | assert(tmp == 7); | |
| 313 | 317 | ||
| 314 | - | clearbit(byte, tmp, 2); | |
| 315 | - | assert(tmp == 3); | |
| 318 | + | clearbit(tmp2, numbits(uintmax_t)-1); | |
| 319 | + | assert(tmp2 == UINTMAX_MAX/2); | |
| 316 | 320 | ||
| 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); | |
| 319 | 323 | } | |
| 320 | 324 | ||
| 321 | 325 | int main(int argc, char** argv) | |