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 @@
#include <cutils/common.h>
#define numbits(type) (sizeof(type)*CHAR_BIT)
#define numbytes(type) (sizeof(type))
#define bitstring(type, value) _bitstring(numbits(type), (type)(value))
/* FIXME: fix out of range calls (bitstring(unsigned char, 256)) */
char* _bitstring(size_t size, uintmax_t value);
#define getbit(value, bit) (((value) >> (bit)) & 1)
#define getbit(type, value, bit) (((value) & (((type)1 << (bit)))) >> (bit))
#define setbit(type, value, bit) ((value) |= ((type)1 << (bit)))
#define clearbit(type, value, bit) ((value) &= (type)~((type)1 << (bit)))
#define setbit(value, bit) ((value) |= ((uintmax_t)1 << (bit)))
#define clearbit(value, bit) ((value) &= ~((uintmax_t)1 << (bit)))
#define setbitc(value, bit, type) ((value) |= ((type)1 << (bit)))
#define clearbitc(value, bit, type) ((value) &= ~((type)1 << (bit)))
#endif /* CUTILS_BITFUNCS_H */
Minclude/cutils/misc.h
@@ -68,14 +68,15 @@ static void memqswap(void* item1, void* item2, void* tmp, size_t length)
memcpy(item2, tmp, length);
}
#if __STDC_VERSION__ >= 201112L
#include <time.h>
#endif
void sleep_ms(unsigned int milliseconds);
#if __STDC_VERSION__ >= 201112L
struct timespec timespec_diff(const struct timespec* old_ts, const struct timespec* new_ts);
#include <time.h>
bool timespec_bigger(const struct timespec* ts1, const struct timespec* ts2);
bool timespec_equal(const struct timespec* ts1, const struct timespec* ts2);
bool timespec_smaller(const struct timespec* ts1, const struct timespec* ts2);
/* subtracts ts2 from ts1, if ts2 > ts1 then zero is returned */
struct timespec timespec_diff(const struct timespec* ts1, const struct timespec* ts2);
struct timespec timespec_add(const struct timespec* ts_1, const struct timespec* ts_2);
uintmax_t timespec_ms(const struct timespec* ts);
#endif
Mmeson.build
@@ -1,7 +1,7 @@
project('cutils', 'c')
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']
CFLAGS = ['-std=c89', '-fstrict-aliasing', '-fPIC', '-Wno-long-long', '-Og'] + WARNINGS
CFLAGS = ['-std=c11', '-fstrict-aliasing', '-fPIC', '-Wno-long-long', '-Og'] + WARNINGS
subdir('include')
subdir('src')
Msrc/bitfuncs.c
@@ -1,24 +1,2 @@
#include <cutils/bitfuncs.h>
char* _bitstring(size_t size, uintmax_t value)
{
char* ret = NULL;
size_t i, pos;
bool leading_zero = true;
for(i = 1, pos = 0; i <= size; i++)
{
byte tmp = (byte)((value & ((uintmax_t)1 << (size-i))) >> (size-i));
if(tmp && leading_zero)
{
leading_zero = false;
ret = malloc(size-i);
if(!ret)
return NULL;
ret[size-i-1] = '\0';
}
if(!leading_zero)
ret[pos++] = tmp ? '1' : '0';
}
return ret;
}
Msrc/math.c
@@ -2,7 +2,7 @@
uintmax_t isqrt(uintmax_t n)
{
uintmax_t start = 1, end = n/2, ans;
uintmax_t start = 1, end = n/2, ans = 0;
if(n == 0 || n == 1)
return n;
@@ -60,9 +60,9 @@ static void primesieve_set(byte* numbers, size_t index)
static bool primesieve_get(byte* numbers, size_t index)
{
return getbit(byte, (numbers[index/8] >> index%8), 0);
return getbit((numbers[index/8] >> index%8), 0);
}
#include <assert.h>
Bytearray* primesieve(uintmax_t limit)
{
size_t i, j, length = limit/8+1;
Msrc/misc.c
@@ -26,20 +26,69 @@ uint32_t ntoh32(uint32_t const net)
#endif
#if __STDC_VERSION__ >= 201112L
struct timespec timespec_diff(const struct timespec* old_ts, const struct timespec* new_ts)
bool timespec_bigger(const struct timespec* ts1, const struct timespec* ts2)
{
struct timespec diff;
if(ts1->tv_sec > ts2->tv_sec)
{
return true;
} else if(ts1->tv_sec < ts2->tv_sec) {
return false;
} else {
if(ts1->tv_nsec > ts2->tv_nsec)
{
return true;
} else if(ts1->tv_nsec < ts2->tv_nsec) {
return false;
} else {
return false;
}
}
}
if ((new_ts->tv_nsec - old_ts->tv_nsec) < 0)
{
diff.tv_sec = new_ts->tv_sec - old_ts->tv_sec - 1;
diff.tv_nsec = new_ts->tv_nsec - old_ts->tv_nsec + 1000000000;
} else {
diff.tv_sec = new_ts->tv_sec - old_ts->tv_sec;
diff.tv_nsec = new_ts->tv_nsec - old_ts->tv_nsec;
}
bool timespec_equal(const struct timespec* ts1, const struct timespec* ts2)
{
if(ts1->tv_sec == ts2->tv_sec && ts1->tv_nsec == ts2->tv_nsec)
{
return true;
} else {
return false;
}
}
bool timespec_smaller(const struct timespec* ts1, const struct timespec* ts2)
{
if(timespec_equal(ts1, ts2))
{
return false;
} else if(timespec_bigger(ts1, ts2)) {
return false;
} else {
return true;
}
}
struct timespec timespec_diff(const struct timespec* ts1, const struct timespec* ts2)
{
struct timespec result;
if((ts1->tv_sec < ts2->tv_sec) ||
((ts1->tv_sec == ts2->tv_sec) &&
(ts1->tv_nsec <= ts2->tv_nsec)))
{
result.tv_sec = result.tv_nsec = 0 ;
} else {
result.tv_sec = ts1->tv_sec - ts2->tv_sec ;
if (ts1->tv_nsec < ts2->tv_nsec) {
result.tv_nsec = ts1->tv_nsec + 1000000000L - ts2->tv_nsec ;
result.tv_sec-- ;
} else {
result.tv_nsec = ts1->tv_nsec - ts2->tv_nsec ;
}
}
return result;
return diff;
}
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)
String* string2;
char* cstring;
size_t tmp;
bool truth;
int truth;
for(truth = false; truth == false || truth == true; truth++)
{
@@ -241,7 +241,7 @@ static void test_timespec(void)
ts2.tv_sec = 10;
ts2.tv_nsec = 999999999;
res = timespec_diff(&ts1, &ts2);
res = timespec_diff(&ts2, &ts1);
assert(res.tv_sec == 10 && res.tv_nsec == 999999998);
ts1.tv_sec = 0;
@@ -249,7 +249,7 @@ static void test_timespec(void)
ts2.tv_sec = 10;
ts2.tv_nsec = 1;
res = timespec_diff(&ts1, &ts2);
res = timespec_diff(&ts2, &ts1);
assert(res.tv_sec == 9 && res.tv_nsec == 2);
ts1.tv_sec = 0;
@@ -257,15 +257,18 @@ static void test_timespec(void)
ts2.tv_sec = 10;
ts2.tv_nsec = 999999999;
res = timespec_diff(&ts1, &ts2);
res = timespec_diff(&ts2, &ts1);
assert(res.tv_sec == 10 && res.tv_nsec == 999999998);
res = timespec_diff(&ts1, &ts2);
assert(res.tv_sec == 0 && res.tv_nsec == 0);
}
#endif
static void test_ll(void)
{
LinkedList* ll;
struct test my_structa, my_structb, *my_structptr;
bool truth;
int truth;
my_structa.a = 5;
my_structa.b = 5;
my_structb.a = 7;
@@ -303,19 +306,20 @@ static void test_ll(void)
static void test_bitfuncs(void)
{
byte tmp = 0;
uintmax_t tmp2 = UINTMAX_MAX;
setbit(byte, tmp, 0);
setbit(tmp, 0);
assert(tmp == 1);
setbit(byte, tmp, 1);
setbitc(tmp, 1, byte);
assert(tmp == 3);
setbit(byte, tmp, 2);
setbit(tmp, 2);
assert(tmp == 7);
clearbit(byte, tmp, 2);
assert(tmp == 3);
clearbit(tmp2, numbits(uintmax_t)-1);
assert(tmp2 == UINTMAX_MAX/2);
assert(getbit(byte, tmp, 0) == 1);
assert(getbit(byte, tmp, 5) == 0);
assert(getbit(tmp, 0) == 1);
assert(getbit(tmp, 5) == 0);
}
int main(int argc, char** argv)