Does a lot of work towards the PIT

Added a math library, with clamp and POW.
added printf support for printing doubles
added a few helper functions in PIT for calcaulting the time in seconds of a clock cycle based on divisor
This commit is contained in:
2025-06-06 22:01:18 -04:00
parent 378f7ef23d
commit f9c2ea2f2b
10 changed files with 215 additions and 0 deletions

13
libc/include/math.h Normal file
View File

@ -0,0 +1,13 @@
#include <stdint.h>
#ifndef MATH_H
#define MATH_H
int pow(int base, int exp);
uint8_t clamp_u8(uint8_t val, uint8_t min, uint8_t max);
uint16_t clamp_u16(uint16_t val, uint16_t min, uint16_t max);
uint32_t clamp_u32(uint32_t val, uint32_t min, uint32_t max);
uint64_t clamp_u64(uint64_t val, uint64_t min, uint64_t max);
#endif

View File

@ -20,5 +20,7 @@ char* u8toa(uint8_t num, char* buf, int base);
char* itoa(int num, char* buf, int base);
char* utoa(unsigned int num, char* buf, int base);
char* dtoa(double n, char* buf, int afterpoint);
#endif

39
libc/math/clamp.c Normal file
View File

@ -0,0 +1,39 @@
#include <stdint.h>
#include <math.h>
uint8_t clamp_u8(uint8_t val, uint8_t min, uint8_t max)
{
if (val > max)
return max;
if (val < min)
return min;
return val;
}
uint16_t clamp_u16(uint16_t val, uint16_t min, uint16_t max)
{
if (val > max)
return max;
if (val < min)
return min;
return val;
}
uint32_t clamp_u32(uint32_t val, uint32_t min, uint32_t max)
{
if (val > max)
return max;
if (val < min)
return min;
return val;
}
uint64_t clamp_u64(uint64_t val, uint64_t min, uint64_t max)
{
if (val > max)
return max;
if (val < min)
return min;
return val;
}

9
libc/math/pow.c Normal file
View File

@ -0,0 +1,9 @@
#include <math.h>
int pow(int base, int exp)
{
int res = base;
for (int i = 0; i < exp; i++)
res *= base;
return res;
}

View File

@ -77,6 +77,18 @@ int printf(const char* restrict format, ...) {
if (!print(buffer, len))
return -1;
written += len;
} else if (*format == 'f') {
format++;
double d = (double) va_arg(parameters, double);
dtoa(d, buffer, 5);
size_t len = strlen(buffer);
if (maxrem < len) {
// TODO
return -1;
}
if (!print(buffer, len))
return -1;
written += len;
} else if (*format == 'u') {
format++;
unsigned int i = (unsigned int) va_arg(parameters, unsigned int);

View File

@ -1,5 +1,7 @@
#include <stdbool.h>
#include <stdint.h>
#include <string.h>
#include <math.h>
static void reverse(char* buf, int len)
{
@ -273,3 +275,35 @@ char* utoa(unsigned int num, char* buf, int base)
reverse(buf, i); // reverse, since we did it backwards!
return buf;
}
static int dbl_to_str(int x, char* buf, int d)
{
int i = 0;
while (x) {
buf[i++] = (x % 10) + '0';
x = x / 10;
}
while (i < d)
buf[i++] = '0';
reverse(buf, i);
buf[i] = '\0';
return i;
}
char* dtoa(double n, char* buf, int afterpoint)
{
int whole = (int) n;
double decimals = n - (double) whole;
int i = dbl_to_str(whole, buf, 0);
if (afterpoint != 0) {
buf[i] = '.'; // add the decimal
decimals = decimals * pow(10, afterpoint);
dbl_to_str((int) decimals, buf + i + 1, afterpoint + 1);
}
return buf;
}