diff options
Diffstat (limited to 'src/stem/lib')
-rw-r--r-- | src/stem/lib/bitset.c | 34 | ||||
-rw-r--r-- | src/stem/lib/bitset.h | 19 | ||||
-rw-r--r-- | src/stem/lib/stdlib.c | 31 | ||||
-rw-r--r-- | src/stem/lib/stdlib.h | 12 |
4 files changed, 96 insertions, 0 deletions
diff --git a/src/stem/lib/bitset.c b/src/stem/lib/bitset.c new file mode 100644 index 0000000..c13a24d --- /dev/null +++ b/src/stem/lib/bitset.c @@ -0,0 +1,34 @@ +#include "bitset.h" + +void bitset_set(struct bitset* t, uint32_t num) { + uint32_t idx = INDEX_FROM_BIT(num); + uint32_t off = OFFSET_FROM_BIT(num); + t->bits[idx] |= (0x1 << off); +} + +void bitset_clear(struct bitset* t, uint32_t num) { + uint32_t idx = INDEX_FROM_BIT(num); + uint32_t off = OFFSET_FROM_BIT(num); + t->bits[idx] &= ~(0x1 << off); +} + +uint32_t bitset_test(struct bitset* t, uint32_t num) { + uint32_t idx = INDEX_FROM_BIT(num); + uint32_t off = OFFSET_FROM_BIT(num); + return (t->bits[idx] & (0x1 << off)); +} + +uint32_t bitset_firstFree(struct bitset* t) { + uint32_t i, j; + for (i = 0; i < INDEX_FROM_BIT(t->size); i++) { + if (t->bits[i] != 0xFFFFFFFF) { + for (j = 0; j < 32; j++) { + uint32_t toTest = 0x1 << j; + if (!(t->bits[i] & toTest)) { + return i*4*8+j; + } + } + } + } +} + diff --git a/src/stem/lib/bitset.h b/src/stem/lib/bitset.h new file mode 100644 index 0000000..e3fe078 --- /dev/null +++ b/src/stem/lib/bitset.h @@ -0,0 +1,19 @@ +#ifndef DEF_BITSET_H +#define DEF_BITSET_H + +#include "../types.h" + +#define INDEX_FROM_BIT(a) (a/(8*4)) +#define OFFSET_FROM_BIT(a) (a%(8*4)) + +struct bitset { + uint32_t *bits; + uint32_t size; +}; + +void bitset_set(struct bitset* t, uint32_t num); +void bitset_clear(struct bitset* t, uint32_t num); +uint32_t bitset_test(struct bitset* t, uint32_t num); +uint32_t bitset_firstFree(struct bitset* t); + +#endif diff --git a/src/stem/lib/stdlib.c b/src/stem/lib/stdlib.c new file mode 100644 index 0000000..904126e --- /dev/null +++ b/src/stem/lib/stdlib.c @@ -0,0 +1,31 @@ +#include "stdlib.h" + +void *memcpy(void *dest, const void *src, int count) { + int i; + uint8_t *d = (uint8_t*)dest, *s = (uint8_t*)src; + for (i = 0; i < count; i++) { + d[i] = s[i]; + } + return dest; +} + +uint8_t *memset(uint8_t *dest, uint8_t val, int count) { + int i; + for (i = 0; i < count; i++) { + dest[i] = val; + } +} + +uint16_t *memsetw(uint16_t *dest, uint16_t val, int count) { + int i; + for (i = 0; i < count; i++) { + dest[i] = val; + } + return dest; +} + +int strlen(const char *str) { + int i = 0; + while (str[i++]); + return i; +} diff --git a/src/stem/lib/stdlib.h b/src/stem/lib/stdlib.h new file mode 100644 index 0000000..a6d8f48 --- /dev/null +++ b/src/stem/lib/stdlib.h @@ -0,0 +1,12 @@ +#ifndef DEF_STDLIB_H +#define DEF_STDLIB_H + +#include "../types.h" + +void *memcpy(void *dest, const void *src, int count); +uint8_t *memset(uint8_t *dest, uint8_t val, int count); +uint16_t *memsetw(uint16_t *dest, uint16_t val, int count); +int strlen(const char *str); + +#endif + |