many things, cosmo build!
This commit is contained in:
@@ -0,0 +1,138 @@
|
||||
/*
|
||||
Generic std parts, building on top of platform specifics.
|
||||
*/
|
||||
|
||||
#include "../fix.h"
|
||||
#include "../std.h"
|
||||
#include "../str.h"
|
||||
#include <stdarg.h>
|
||||
#include <stdint.h>
|
||||
|
||||
static uint8_t com_std_any_format_u32(uint32_t v, char buf[10]) {
|
||||
uint8_t c = 0;
|
||||
if (v == 0) {
|
||||
buf[10 - (++c)] = '0';
|
||||
} else {
|
||||
while (v > 0) {
|
||||
buf[10 - (++c)] = '0' + (v % 10);
|
||||
v /= 10;
|
||||
}
|
||||
}
|
||||
return c;
|
||||
}
|
||||
|
||||
static uint8_t com_std_any_format_i32(int32_t v, char buf[11]) {
|
||||
bool neg = v < 0;
|
||||
uint8_t c = 0;
|
||||
if (v == 0) {
|
||||
buf[11 - (++c)] = '0';
|
||||
} else {
|
||||
while (v > 0) {
|
||||
buf[11 - (++c)] = '0' + (v % 10);
|
||||
v /= 10;
|
||||
}
|
||||
}
|
||||
if (neg)
|
||||
buf[11 - (++c)] = '-';
|
||||
return c;
|
||||
}
|
||||
|
||||
/* Just assume 5 points of precision. */
|
||||
static uint8_t com_std_any_format_f64(double v, char buf[11 + 1 + 5]) {
|
||||
bool neg = v < 0;
|
||||
uint8_t c = 0;
|
||||
double frac_part = v - (double)(int32_t)v;
|
||||
for (int i = 0; i < 5; i++) {
|
||||
frac_part *= 10.0;
|
||||
uint8_t digit = (unsigned int)frac_part;
|
||||
buf[17 - (++c)] = digit + '0';
|
||||
frac_part -= digit;
|
||||
}
|
||||
buf[17 - (++c)] = '.';
|
||||
c += com_std_any_format_i32((int32_t)v, &buf[c]);
|
||||
return c;
|
||||
}
|
||||
|
||||
/* We can make it buffered, but who cares, it's only for testing. */
|
||||
void com_std_print_(int32_t start, ...) {
|
||||
va_list args;
|
||||
va_start(args, start);
|
||||
|
||||
bool afterstring = false;
|
||||
char type = start;
|
||||
do {
|
||||
|
||||
/* Put space if not proceeding string. */
|
||||
if (!afterstring && type != 'c')
|
||||
com_std_write(COM_STD_OUTFD, " ", 1);
|
||||
|
||||
afterstring = false;
|
||||
|
||||
switch (type) {
|
||||
|
||||
/* uint32_t case. */
|
||||
case 'u': {
|
||||
uint32_t const v = va_arg(args, uint32_t);
|
||||
char buf[10];
|
||||
uint8_t c = com_std_any_format_u32(v, buf);
|
||||
com_std_write(COM_STD_OUTFD, &buf[sizeof buf - c], c);
|
||||
break;
|
||||
}
|
||||
|
||||
/* null terminated string case. */
|
||||
case 'c': {
|
||||
char const *const v = va_arg(args, char *);
|
||||
com_std_write(COM_STD_OUTFD, v, com_str_ext_len(v));
|
||||
afterstring = true;
|
||||
break;
|
||||
}
|
||||
|
||||
/* TODO: FIX THIS */
|
||||
/* com_fix case. */
|
||||
case 'f': {
|
||||
com_fix_t const v = va_arg(args, com_fix_t);
|
||||
char buf[11];
|
||||
|
||||
int32_t const int_part = com_fix_int(v);
|
||||
uint8_t c = com_std_any_format_i32(int_part, buf);
|
||||
com_std_write(COM_STD_OUTFD, &buf[sizeof buf - c], c);
|
||||
|
||||
uint32_t const decimal_val = (com_fix_frac(v) * 10000) >> 16;
|
||||
uint8_t cd = com_std_any_format_u32(decimal_val, buf);
|
||||
buf[sizeof buf - (++cd)] = '.';
|
||||
com_std_write(COM_STD_OUTFD, &buf[sizeof buf - cd], cd);
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
/* double floating point case. */
|
||||
case 'd': {
|
||||
double const v = va_arg(args, double);
|
||||
char buf[17];
|
||||
|
||||
uint8_t c = com_std_any_format_f64(v, buf);
|
||||
com_std_write(COM_STD_OUTFD, &buf[sizeof buf - c], c);
|
||||
}
|
||||
|
||||
/* End of things. */
|
||||
case '.':
|
||||
break;
|
||||
|
||||
default:
|
||||
com_std_assert(false && "Unhandled format type");
|
||||
}
|
||||
|
||||
type = va_arg(args, int32_t);
|
||||
|
||||
} while (type != '.');
|
||||
|
||||
com_std_write(COM_STD_OUTFD, "\n", 1);
|
||||
|
||||
va_end(args);
|
||||
}
|
||||
|
||||
void com_std_print_str(char const *str, uint32_t len, bool newline) {
|
||||
com_std_write(COM_STD_OUTFD, str, len);
|
||||
if (newline)
|
||||
com_std_write(COM_STD_OUTFD, "\n", 1);
|
||||
}
|
||||
+13
-8
@@ -1,7 +1,9 @@
|
||||
#include "../std.h"
|
||||
#include "../str.h"
|
||||
#include "any.c"
|
||||
#include <assert.h>
|
||||
#include <poll.h>
|
||||
#include <stdarg.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/socket.h>
|
||||
@@ -11,13 +13,7 @@
|
||||
void *com_std_alloc(void *ptr, size_t size) {
|
||||
return size != 0 ? realloc(ptr, size) : (free(ptr), NULL);
|
||||
}
|
||||
void com_std_assert(bool holds) { assert(holds); }
|
||||
void com_std_printf(char const *fmt, ...) {
|
||||
va_list args;
|
||||
va_start(args, fmt);
|
||||
vprintf(fmt, args);
|
||||
va_end(args);
|
||||
}
|
||||
|
||||
double com_std_sqrt(double v) { return sqrt(v); }
|
||||
double com_std_fabs(double v) { return fabs(v); }
|
||||
double com_std_sin(double v) { return sin(v); }
|
||||
@@ -43,6 +39,15 @@ int32_t com_std_read(com_std_fd fd, void *data, uint32_t upto) {
|
||||
return fread(data, 1, upto, fd);
|
||||
}
|
||||
|
||||
bool com_std_has_data(com_std_fd fd) {
|
||||
struct pollfd pfd = {.fd = (intptr_t)fd, .events = POLLIN};
|
||||
int ret = poll(&pfd, 1, 0);
|
||||
if (ret > 0) {
|
||||
return pfd.revents & POLLIN;
|
||||
} else
|
||||
return false;
|
||||
}
|
||||
|
||||
bool com_std_sfwd(com_std_fd fd, uint32_t bytes) {
|
||||
com_std_assert(fd != NULL && bytes != 0);
|
||||
return fseek(fd, bytes, SEEK_CUR) == 0;
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
|
||||
#include "../std.h"
|
||||
#include <fcntl.h>
|
||||
#include <poll.h>
|
||||
#include <stdint.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/un.h>
|
||||
@@ -44,6 +45,23 @@ inline int32_t com_std_read(com_std_fd fd, void *data, uint32_t upto) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
inline bool com_std_has_data(com_std_fd fd) {
|
||||
struct pollfd pfd = {
|
||||
.fd = (intptr_t)fd,
|
||||
.events = POLLIN,
|
||||
};
|
||||
|
||||
long ret;
|
||||
__asm__ volatile("syscall"
|
||||
: "=a"(ret)
|
||||
: "a"(7), "D"(&pfd), "S"(1), "d"(0)
|
||||
: "rcx", "r11", "memory");
|
||||
if (ret > 0) {
|
||||
return pfd.revents & POLLIN;
|
||||
} else
|
||||
return false;
|
||||
}
|
||||
|
||||
inline bool com_std_sfwd(com_std_fd fd, uint32_t bytes) {
|
||||
long ret;
|
||||
__asm__ volatile("syscall"
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
|
||||
#include "../std.h"
|
||||
#include <fcntl.h>
|
||||
#include <poll.h>
|
||||
#include <stdint.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/un.h>
|
||||
@@ -44,6 +45,23 @@ inline int32_t com_std_read(com_std_fd fd, void *data, uint32_t upto) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
inline bool com_std_has_data(com_std_fd fd) {
|
||||
struct pollfd pfd = {
|
||||
.fd = (intptr_t)fd,
|
||||
.events = POLLIN,
|
||||
};
|
||||
|
||||
long ret;
|
||||
__asm__ volatile("int $0x80"
|
||||
: "=a"(ret)
|
||||
: "a"(168), "b"(&pfd), "c"(1), "d"(0)
|
||||
: "memory", "cc");
|
||||
if (ret > 0) {
|
||||
return pfd.revents & POLLIN;
|
||||
} else
|
||||
return false;
|
||||
}
|
||||
|
||||
inline bool com_std_sfwd(com_std_fd fd, uint32_t bytes) {
|
||||
long ret;
|
||||
__asm__ volatile("int $0x80"
|
||||
|
||||
+3
-31
@@ -2,41 +2,13 @@
|
||||
Generic linux std parts.
|
||||
*/
|
||||
|
||||
#include "../def.h"
|
||||
#include "any.c"
|
||||
#include "nolibc.c"
|
||||
|
||||
#include "../std.h"
|
||||
#include "../str.h"
|
||||
#include <stdarg.h>
|
||||
#include <stdint.h>
|
||||
|
||||
void *com_std_alloc(void *ptr, size_t size) { return NULL; }
|
||||
void com_std_assert(bool holds) {
|
||||
/* TODO: Write line info to stderr as well. */
|
||||
if (!holds)
|
||||
com_std_exit(-1);
|
||||
}
|
||||
|
||||
/* TODO: Figure out a way to make it easy to deal with. */
|
||||
void com_std_printf(char const *fmt, ...) {
|
||||
com_std_write((void *)1, fmt, com_str_ext_len(fmt));
|
||||
}
|
||||
|
||||
void com_std_print_str(char const *str, uint32_t len, bool newline) {
|
||||
com_std_write((void *)1, str, len);
|
||||
if (newline)
|
||||
com_std_write((void *)1, "\n", 1);
|
||||
}
|
||||
|
||||
double com_std_sqrt(double v) { return __builtin_sqrt(v); }
|
||||
double com_std_fabs(double v) { return __builtin_fabs(v); }
|
||||
#if COM_DEF_COMPILER == COM_DEF_COMPILER_CLANG
|
||||
double com_std_sin(double v) { return __builtin_sin(v); }
|
||||
double com_std_cos(double v) { return __builtin_cos(v); }
|
||||
#else
|
||||
/* TODO: Use fsin/fcos, no matter how slow. */
|
||||
double com_std_sin(double v) { return 0; }
|
||||
double com_std_cos(double v) { return 0; }
|
||||
#endif
|
||||
|
||||
// void memcpy(void *__restrict dest, const void *__restrict src, size_t n) {
|
||||
// __builtin_memcpy(dest, src, n);
|
||||
// };
|
||||
|
||||
@@ -0,0 +1,46 @@
|
||||
/*
|
||||
Generic no libc std parts.
|
||||
*/
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
/* Compiler depends on them with low optimization level. */
|
||||
void *memcpy(void *__restrict dest, const void *__restrict src, size_t n) {
|
||||
for (size_t i = 0; i < n; ++i)
|
||||
((char *)dest)[i] = ((char *)src)[i];
|
||||
return dest;
|
||||
};
|
||||
|
||||
void *memset(void *__restrict dest, int s, size_t n) {
|
||||
for (size_t i = 0; i < n; ++i)
|
||||
((char *)dest)[i] = s;
|
||||
return dest;
|
||||
};
|
||||
|
||||
size_t strlen(char const *s) {
|
||||
char const *p = s;
|
||||
for (; *p != 0; ++p) {
|
||||
}
|
||||
return p - s;
|
||||
}
|
||||
|
||||
double com_std_sin(double v) {
|
||||
double result;
|
||||
__asm__ __volatile__("fldl %1\n\t"
|
||||
"fsin\n\t"
|
||||
"fstpl %0"
|
||||
: "=g"(result)
|
||||
: "m"(v)
|
||||
: "st", "st(1)");
|
||||
return result;
|
||||
}
|
||||
|
||||
double com_std_cos(double v) {
|
||||
double result;
|
||||
__asm__ __volatile__("fldl %1\n\t"
|
||||
"fcos\n\t"
|
||||
"fstpl %0"
|
||||
: "=g"(result)
|
||||
: "m"(v));
|
||||
return result;
|
||||
}
|
||||
+2
-6
@@ -1,11 +1,11 @@
|
||||
#include "../std.h"
|
||||
|
||||
#include "any.c"
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
|
||||
void *com_std_alloc(void *ptr, size_t size) { return NULL; }
|
||||
void com_std_assert(bool holds) {}
|
||||
void com_std_printf(char const *fmt, ...) {}
|
||||
|
||||
double com_std_sqrt(double v) { return __builtin_sqrt(v); }
|
||||
double com_std_fabs(double v) { return __builtin_fabs(v); }
|
||||
@@ -35,7 +35,3 @@ void com_std_exit(uint32_t status) {
|
||||
__builtin_unreachable();
|
||||
}
|
||||
void com_std_close(com_std_fd fd) { return; }
|
||||
|
||||
// void memcpy(void *__restrict dest, const void *__restrict src, size_t n) {
|
||||
// __builtin_memcpy(dest, src, n);
|
||||
// };
|
||||
|
||||
@@ -13,5 +13,6 @@ uint64_t com_timer_count_ns(void) {
|
||||
void com_timer_profile(uint64_t starttime, const char *what) {
|
||||
uint64_t const endtime = com_timer_count_ns();
|
||||
uint64_t const diff = endtime - starttime;
|
||||
com_std_printf("profile \"%s\" took %fms\n", what, (double)diff / 1000000);
|
||||
com_std_print('c', "Profile ", 'c', what, 'c', " took ", 'd',
|
||||
(double)diff / 1000000, 'c', "ms");
|
||||
}
|
||||
|
||||
@@ -11,4 +11,12 @@
|
||||
#define COM_CFG_WIDTH 640
|
||||
#define COM_CFG_HEIGHT 480
|
||||
|
||||
#if COM_DEF_TARGET == COM_DEF_TARGET_WASM
|
||||
#define COM_CFG_COLORS 4
|
||||
#else
|
||||
#define COM_CFG_COLORS 3
|
||||
#endif
|
||||
|
||||
#define COM_CFG_FBSIZE (COM_CFG_WIDTH * COM_CFG_HEIGHT * COM_CFG_COLORS)
|
||||
|
||||
#endif
|
||||
|
||||
@@ -6,6 +6,9 @@
|
||||
#ifndef COM_DEF_H
|
||||
#define COM_DEF_H
|
||||
|
||||
#define COM_DEF_STRLIT_(x) #x
|
||||
#define COM_DEF_STRLIT(x) COM_DEF_STRLIT_(x)
|
||||
|
||||
#define COM_DEF_COMPILER_CLANG 1
|
||||
#define COM_DEF_COMPILER_GCC 2
|
||||
|
||||
@@ -17,6 +20,7 @@
|
||||
|
||||
#define COM_DEF_TARGET_LINUX 1
|
||||
#define COM_DEF_TARGET_WASM 2
|
||||
#define COM_DEF_TARGET_COSMO 3
|
||||
|
||||
#if defined(__wasm__)
|
||||
#define COM_DEF_TARGET COM_DEF_TARGET_WASM
|
||||
@@ -24,6 +28,9 @@
|
||||
#elif defined(__linux__)
|
||||
#define COM_DEF_TARGET COM_DEF_TARGET_LINUX
|
||||
#define com_def_export_sym(m_s) __attribute__((visibility("default")))
|
||||
#elif defined(__COSMOPOLITAN__)
|
||||
#define COM_DEF_TARGET COM_DEF_TARGET_COSMO
|
||||
#define com_def_export_sym(m_s) __attribute__((visibility("default")))
|
||||
#else
|
||||
#error Unknown target.
|
||||
#endif
|
||||
@@ -31,6 +38,7 @@
|
||||
#define COM_DEF_CPU_AMD64 1
|
||||
#define COM_DEF_CPU_X86 2
|
||||
#define COM_DEF_CPU_WASM32 3
|
||||
#define COM_DEF_CPU_AARCH64 4
|
||||
|
||||
#if defined(__wasm__)
|
||||
#define COM_DEF_CPU COM_DEF_CPU_WASM32
|
||||
@@ -44,6 +52,10 @@
|
||||
#define COM_DEF_CPU COM_DEF_CPU_X86
|
||||
#define COM_DEF_ALIGNMENT 16
|
||||
#define COM_DEF_CPU_LITEND 1
|
||||
#elif defined(__aarch64__)
|
||||
#define COM_DEF_CPU COM_DEF_CPU_AARCH64
|
||||
#define COM_DEF_ALIGNMENT 16
|
||||
#define COM_DEF_CPU_LITEND 1
|
||||
#else
|
||||
#error Unknown cpu.
|
||||
#endif
|
||||
|
||||
@@ -71,20 +71,6 @@ com_fix_t com_fix_sqrt(com_fix_t a) {
|
||||
: (int32_t)(x >> (-final_shift));
|
||||
}
|
||||
|
||||
void com_fix_print(com_fix_t a) {
|
||||
if (a < 0) {
|
||||
com_std_printf("-");
|
||||
a = -a;
|
||||
}
|
||||
int32_t int_part = a >> 16;
|
||||
int32_t frac_part = a & 0xFFFF;
|
||||
|
||||
// Convert fractional 16-bit part to a decimal value (up to 4 decimal places)
|
||||
uint32_t decimal_val = (frac_part * 10000) >> 16;
|
||||
|
||||
com_std_printf("%d.%04u\n", int_part, decimal_val);
|
||||
}
|
||||
|
||||
void com_fix_run_bench(void) {
|
||||
uint64_t start = com_timer_count_ns();
|
||||
for (int i = 5000000; i--;) {
|
||||
@@ -118,77 +104,3 @@ void com_fix_run_bench(void) {
|
||||
}
|
||||
com_timer_profile(start, "fixed: sincos");
|
||||
}
|
||||
|
||||
void com_fix_run_tests(void) {
|
||||
{
|
||||
double max_sqrt_deviation = 0.0f;
|
||||
com_fix_t sqrt_accumulator = 0;
|
||||
com_fix_t sqrt_test_step = COM_FIX_FRACUNIT >> 2; /* Quarter step */
|
||||
for (int i = 0; i < COM_FIX_FRACUNIT << 1; ++i) {
|
||||
com_fix_t sqrt = com_fix_sqrt(sqrt_accumulator);
|
||||
double const deviation =
|
||||
com_std_sqrt(com_fix_as_float(sqrt_accumulator)) -
|
||||
com_fix_as_float(sqrt);
|
||||
if (deviation > max_sqrt_deviation)
|
||||
max_sqrt_deviation = deviation;
|
||||
sqrt_accumulator += sqrt_test_step;
|
||||
}
|
||||
com_std_printf("max sqrt deviation: %f\n", max_sqrt_deviation);
|
||||
}
|
||||
|
||||
{
|
||||
double max_sin_deviation = 0.0f;
|
||||
com_fix_t sin_accumulator = -COM_FIX_PI * 4;
|
||||
com_fix_t sin_test_step = (COM_FIX_PI << 1) >> 10;
|
||||
for (int i = 0; i < 1024 * 32; ++i) {
|
||||
com_fix_t sin = com_fix_sin(sin_accumulator);
|
||||
double const deviation =
|
||||
com_std_fabs(com_std_sin(com_fix_as_float(sin_accumulator)) -
|
||||
com_fix_as_float(sin));
|
||||
if (deviation > max_sin_deviation)
|
||||
max_sin_deviation = deviation;
|
||||
sin_accumulator += sin_test_step;
|
||||
}
|
||||
com_std_printf("max sin deviation: %f\n", max_sin_deviation);
|
||||
}
|
||||
|
||||
{
|
||||
double max_cos_deviation = 0.0f;
|
||||
com_fix_t cos_accumulator = -COM_FIX_PI * 4;
|
||||
com_fix_t cos_test_step = (COM_FIX_PI << 1) >> 10;
|
||||
for (int i = 0; i < 1024 * 32; ++i) {
|
||||
com_fix_t cos = com_fix_cos(cos_accumulator);
|
||||
double const deviation =
|
||||
com_std_fabs(com_std_cos(com_fix_as_float(cos_accumulator)) -
|
||||
com_fix_as_float(cos));
|
||||
if (deviation > max_cos_deviation)
|
||||
max_cos_deviation = deviation;
|
||||
cos_accumulator += cos_test_step;
|
||||
}
|
||||
com_std_printf("max cos deviation: %f\n", max_cos_deviation);
|
||||
}
|
||||
|
||||
{
|
||||
double max_sin_deviation = 0.0f;
|
||||
double max_cos_deviation = 0.0f;
|
||||
com_fix_t sincos_accumulator = -COM_FIX_PI * 4;
|
||||
com_fix_t sincos_test_step = (COM_FIX_PI << 1) >> 10;
|
||||
for (int i = 0; i < 1024 * 32; ++i) {
|
||||
com_fix_t sin, cos;
|
||||
com_fix_sincos(sincos_accumulator, &sin, &cos);
|
||||
double const sin_deviation =
|
||||
com_std_fabs(com_std_sin(com_fix_as_float(sincos_accumulator)) -
|
||||
com_fix_as_float(sin));
|
||||
double const cos_deviation =
|
||||
com_std_fabs(com_std_cos(com_fix_as_float(sincos_accumulator)) -
|
||||
com_fix_as_float(cos));
|
||||
if (sin_deviation > max_sin_deviation)
|
||||
max_sin_deviation = sin_deviation;
|
||||
if (cos_deviation > max_cos_deviation)
|
||||
max_cos_deviation = cos_deviation;
|
||||
sincos_accumulator += sincos_test_step;
|
||||
}
|
||||
com_std_printf("max sincos deviations: %f %f\n", max_sin_deviation,
|
||||
max_cos_deviation);
|
||||
}
|
||||
}
|
||||
|
||||
+3
-5
@@ -23,8 +23,6 @@ typedef int32_t com_fix_t;
|
||||
|
||||
extern const uint16_t com_fix_sin_lut[128];
|
||||
|
||||
void com_fix_print(com_fix_t a);
|
||||
|
||||
void com_fix_run_tests(void);
|
||||
void com_fix_run_bench(void);
|
||||
|
||||
@@ -32,9 +30,9 @@ static inline com_fix_t com_fix_add(com_fix_t a, com_fix_t b) { return a + b; }
|
||||
|
||||
static inline com_fix_t com_fix_sub(com_fix_t a, com_fix_t b) { return a - b; }
|
||||
|
||||
static inline com_fix_t com_fix_int(com_fix_t a) {
|
||||
return a >> COM_FIX_FRACBITS;
|
||||
}
|
||||
static inline int16_t com_fix_int(com_fix_t a) { return a >> COM_FIX_FRACBITS; }
|
||||
|
||||
static inline uint16_t com_fix_frac(com_fix_t a) { return a & 0xFFFF; }
|
||||
|
||||
static inline com_fix_t com_fix_mul(com_fix_t a, com_fix_t b) {
|
||||
return (com_fix_t)(((int64_t)a * (int64_t)b) >> COM_FIX_FRACBITS);
|
||||
|
||||
+9
-9
@@ -91,9 +91,9 @@ static inline void com_gem_draw_triangle(com_pnt_t v0, com_pnt_t v1,
|
||||
// If the point is inside or on all edges, draw the pixel
|
||||
// (Assumes Counter Clockwise vertex ordering)
|
||||
if (ws.a[0] >= 0 && ws.a[1] >= 0 && ws.a[2] >= 0) {
|
||||
out[(y * COM_CFG_WIDTH + x) * 4 + 0] = 125;
|
||||
out[(y * COM_CFG_WIDTH + x) * 4 + 1] = 125;
|
||||
out[(y * COM_CFG_WIDTH + x) * 4 + 2] = 125;
|
||||
out[(y * COM_CFG_WIDTH + x) * COM_CFG_COLORS + 0] = 125;
|
||||
out[(y * COM_CFG_WIDTH + x) * COM_CFG_COLORS + 1] = 125;
|
||||
out[(y * COM_CFG_WIDTH + x) * COM_CFG_COLORS + 2] = 125;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -147,9 +147,9 @@ com_gem_draw_triangle_textured_edge(com_pnt_t v0, com_pnt_t v1, com_pnt_t v2,
|
||||
// uint32_t bidx = com_zord_order(x, y);
|
||||
uint32_t bidx = y * COM_CFG_WIDTH + x;
|
||||
|
||||
out[bidx * 4 + 0] = tex[idx * 3 + 0];
|
||||
out[bidx * 4 + 1] = tex[idx * 3 + 1];
|
||||
out[bidx * 4 + 2] = tex[idx * 3 + 2];
|
||||
out[bidx * COM_CFG_COLORS + 0] = tex[idx * 3 + 0];
|
||||
out[bidx * COM_CFG_COLORS + 1] = tex[idx * 3 + 1];
|
||||
out[bidx * COM_CFG_COLORS + 2] = tex[idx * 3 + 2];
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -170,9 +170,9 @@ static inline void draw_span(int32_t x_start_fp, int32_t x_end_fp, int y,
|
||||
for (int x = x1; x < x2; x++) {
|
||||
uint32_t idx = 0 * 32 + 0;
|
||||
uint32_t bidx = y * COM_CFG_WIDTH + x;
|
||||
out[bidx * 4 + 0] = tex[idx * 3 + 0];
|
||||
out[bidx * 4 + 1] = tex[idx * 3 + 1];
|
||||
out[bidx * 4 + 2] = tex[idx * 3 + 2];
|
||||
out[bidx * COM_CFG_COLORS + 0] = tex[idx * 3 + 0];
|
||||
out[bidx * COM_CFG_COLORS + 1] = tex[idx * 3 + 1];
|
||||
out[bidx * COM_CFG_COLORS + 2] = tex[idx * 3 + 2];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -111,9 +111,4 @@ com_pnt_edge_weight_calc_precomp(com_pnt_t v0, com_pnt_t v1, com_pnt_t v2,
|
||||
precomp[2].a[1] * (p.a[0] - v0.a[0])}};
|
||||
}
|
||||
|
||||
static inline void com_pnt_print(com_pnt_t a) {
|
||||
com_fix_print(a.a[0]);
|
||||
com_fix_print(a.a[1]);
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
+48
-2
@@ -10,6 +10,16 @@
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#if COM_DEF_TARGET == COM_DEF_TARGET_WASM
|
||||
#define COM_STD_INFD 0
|
||||
#define COM_STD_OUTFD 1
|
||||
#else
|
||||
/* TODO: Make it more graceful, probably with static func. */
|
||||
#include <unistd.h>
|
||||
#define COM_STD_INFD (fdopen(STDIN_FILENO, "r"))
|
||||
#define COM_STD_OUTFD (fdopen(STDOUT_FILENO, "w"))
|
||||
#endif
|
||||
|
||||
/* Generic system interface, linux/bsd derived. */
|
||||
typedef void *com_std_fd;
|
||||
#define COM_STD_OPEN_READ (1 << 0)
|
||||
@@ -17,6 +27,7 @@ typedef void *com_std_fd;
|
||||
com_std_fd com_std_open(char const *path, uint32_t flags);
|
||||
bool com_std_write(com_std_fd fd, void const *data, uint32_t bytes);
|
||||
int32_t com_std_read(com_std_fd fd, void *data, uint32_t upto);
|
||||
bool com_std_has_data(com_std_fd);
|
||||
bool com_std_sfwd(com_std_fd fd, uint32_t bytes);
|
||||
com_std_fd com_std_socket(bool isnetwork);
|
||||
bool com_std_connect(com_std_fd sock, bool isnetwork, char const *address);
|
||||
@@ -31,9 +42,44 @@ void *com_std_alloc(void *ptr, size_t size);
|
||||
/* Note: Functions listed below are only available for dev builds. */
|
||||
/* Trigonometry functions are for reference testing of our own fixed point
|
||||
* implementations. Do not use otherwise. */
|
||||
void com_std_assert(bool holds);
|
||||
void com_std_printf(char const *fmt, ...);
|
||||
#define com_std_assert(p_h) \
|
||||
do { \
|
||||
if (!(p_h)) { \
|
||||
const char msg[] = \
|
||||
"Assertion failed at "__FILE__ \
|
||||
":" COM_DEF_STRLIT(__LINE__) ": " COM_DEF_STRLIT(p_h); \
|
||||
com_std_print_str(msg, sizeof msg, true); \
|
||||
com_std_exit(-1); \
|
||||
} \
|
||||
} while (0)
|
||||
|
||||
/*
|
||||
Basic printing utility that doesn't utilize format string.
|
||||
Instead a vector of arguments is passed and concatenated, based on type.
|
||||
Newline is implied at the end.
|
||||
|
||||
Example: com_std_print("My unit32_t: ", 'u', value, 's', " is here");
|
||||
|
||||
Format specifiers:
|
||||
'u' = uint32_t
|
||||
'i' = int32_t
|
||||
'd' = float (upcast to double with vargs)
|
||||
'c' = null terminated C string
|
||||
|
||||
Our own types:
|
||||
'f' = com_fix value
|
||||
'p' = com_pnt pointer
|
||||
'v' = com_vec pointer
|
||||
's' = pointer to com_str structure
|
||||
'b' = pointer to com_buf structure
|
||||
|
||||
'.' is placed at the end implicitly with macro,
|
||||
it is needed to know when to end.
|
||||
*/
|
||||
void com_std_print_(int32_t start, ...);
|
||||
#define com_std_print(...) com_std_print_(__VA_ARGS__, '.')
|
||||
void com_std_print_str(char const *str, uint32_t len, bool newline);
|
||||
|
||||
double com_std_sqrt(double v);
|
||||
double com_std_fabs(double v);
|
||||
double com_std_sin(double v);
|
||||
|
||||
+121
@@ -0,0 +1,121 @@
|
||||
/*
|
||||
Centralized test suite.
|
||||
*/
|
||||
|
||||
#include "bits.h"
|
||||
#include "fix.h"
|
||||
#include "std.h"
|
||||
#include <stdbool.h>
|
||||
|
||||
#define TEST(p_e) \
|
||||
do { \
|
||||
if (!(p_e)) { \
|
||||
com_std_print('c', COM_DEF_STRLIT(p_e) " failed!"); \
|
||||
anyfail = true; \
|
||||
} \
|
||||
} while (0)
|
||||
|
||||
static bool com_test_fix(void) {
|
||||
bool anyfail = false;
|
||||
{
|
||||
double max_sqrt_deviation = 0.0f;
|
||||
com_fix_t sqrt_accumulator = 0;
|
||||
com_fix_t sqrt_test_step = COM_FIX_FRACUNIT >> 2; /* Quarter step */
|
||||
for (int i = 0; i < COM_FIX_FRACUNIT << 1; ++i) {
|
||||
com_fix_t sqrt = com_fix_sqrt(sqrt_accumulator);
|
||||
double const deviation =
|
||||
com_std_sqrt(com_fix_as_float(sqrt_accumulator)) -
|
||||
com_fix_as_float(sqrt);
|
||||
if (deviation > max_sqrt_deviation)
|
||||
max_sqrt_deviation = deviation;
|
||||
sqrt_accumulator += sqrt_test_step;
|
||||
}
|
||||
com_std_print('c', "Max sqrt deviation: ", 'd', max_sqrt_deviation);
|
||||
}
|
||||
|
||||
{
|
||||
double max_sin_deviation = 0.0f;
|
||||
com_fix_t sin_accumulator = -COM_FIX_PI * 4;
|
||||
com_fix_t sin_test_step = (COM_FIX_PI << 1) >> 10;
|
||||
for (int i = 0; i < 1024 * 32; ++i) {
|
||||
com_fix_t sin = com_fix_sin(sin_accumulator);
|
||||
double const deviation =
|
||||
com_std_fabs(com_std_sin(com_fix_as_float(sin_accumulator)) -
|
||||
com_fix_as_float(sin));
|
||||
if (deviation > max_sin_deviation)
|
||||
max_sin_deviation = deviation;
|
||||
sin_accumulator += sin_test_step;
|
||||
}
|
||||
com_std_print('c', "Max sin deviation: ", 'd', max_sin_deviation);
|
||||
}
|
||||
|
||||
{
|
||||
double max_cos_deviation = 0.0f;
|
||||
com_fix_t cos_accumulator = -COM_FIX_PI * 4;
|
||||
com_fix_t cos_test_step = (COM_FIX_PI << 1) >> 10;
|
||||
for (int i = 0; i < 1024 * 32; ++i) {
|
||||
com_fix_t cos = com_fix_cos(cos_accumulator);
|
||||
double const deviation =
|
||||
com_std_fabs(com_std_cos(com_fix_as_float(cos_accumulator)) -
|
||||
com_fix_as_float(cos));
|
||||
if (deviation > max_cos_deviation)
|
||||
max_cos_deviation = deviation;
|
||||
cos_accumulator += cos_test_step;
|
||||
}
|
||||
com_std_print('c', "Max cos deviation: ", 'd', max_cos_deviation);
|
||||
}
|
||||
|
||||
{
|
||||
double max_sin_deviation = 0.0f;
|
||||
double max_cos_deviation = 0.0f;
|
||||
com_fix_t sincos_accumulator = -COM_FIX_PI * 4;
|
||||
com_fix_t sincos_test_step = (COM_FIX_PI << 1) >> 10;
|
||||
for (int i = 0; i < 1024 * 32; ++i) {
|
||||
com_fix_t sin, cos;
|
||||
com_fix_sincos(sincos_accumulator, &sin, &cos);
|
||||
double const sin_deviation =
|
||||
com_std_fabs(com_std_sin(com_fix_as_float(sincos_accumulator)) -
|
||||
com_fix_as_float(sin));
|
||||
double const cos_deviation =
|
||||
com_std_fabs(com_std_cos(com_fix_as_float(sincos_accumulator)) -
|
||||
com_fix_as_float(cos));
|
||||
if (sin_deviation > max_sin_deviation)
|
||||
max_sin_deviation = sin_deviation;
|
||||
if (cos_deviation > max_cos_deviation)
|
||||
max_cos_deviation = cos_deviation;
|
||||
sincos_accumulator += sincos_test_step;
|
||||
}
|
||||
com_std_print('c', "Max sincos deviations: ", 'd', max_sin_deviation, 'd',
|
||||
max_cos_deviation);
|
||||
}
|
||||
|
||||
return anyfail;
|
||||
}
|
||||
|
||||
static bool com_test_bits(void) {
|
||||
bool anyfail = false;
|
||||
|
||||
TEST(com_bits_nalign(0, 4) == 0);
|
||||
TEST(com_bits_nalign(1, 4) == 4);
|
||||
TEST(com_bits_nalign(2, 4) == 4);
|
||||
TEST(com_bits_nalign(3, 4) == 4);
|
||||
TEST(com_bits_nalign(4, 4) == 4);
|
||||
TEST(com_bits_nalign(5, 4) == 8);
|
||||
|
||||
return anyfail;
|
||||
}
|
||||
|
||||
static bool com_test_run(void) {
|
||||
bool anyfail = false;
|
||||
if (com_test_fix()) {
|
||||
com_std_print('c', "com_fix tests failed!");
|
||||
anyfail = true;
|
||||
}
|
||||
if (com_test_bits()) {
|
||||
com_std_print('c', "com_bits tests failed!");
|
||||
anyfail = true;
|
||||
}
|
||||
return !anyfail;
|
||||
}
|
||||
|
||||
#undef TEST
|
||||
@@ -93,10 +93,4 @@ static inline com_fix_t com_vec_max(com_vec_t a) {
|
||||
return com_fix_max(com_fix_max(a.a[0], a.a[1]), a.a[2]);
|
||||
}
|
||||
|
||||
static inline void com_vec_print(com_vec_t a) {
|
||||
com_fix_print(a.a[0]);
|
||||
com_fix_print(a.a[1]);
|
||||
com_fix_print(a.a[2]);
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user