printf update

This commit is contained in:
Blaise Tine
2021-06-28 01:48:21 -04:00
parent 3947fd513f
commit cb0459a960
6 changed files with 47 additions and 163 deletions

View File

@@ -7,10 +7,6 @@
extern "C" { extern "C" {
#endif #endif
void vx_prints(const char * str);
void vx_printx(unsigned value);
void vx_printv(const char * str, unsigned value);
int vx_vprintf(const char* format, va_list va); int vx_vprintf(const char* format, va_list va);
int vx_printf(const char * format, ...); int vx_printf(const char * format, ...);
int vx_putchar(int c); int vx_putchar(int c);

View File

@@ -1,4 +1,5 @@
#include <vx_print.h> #include <vx_print.h>
#include <vx_intrinsics.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdbool.h> #include <stdbool.h>
#include <stdio.h> #include <stdio.h>
@@ -7,159 +8,46 @@
extern "C" { extern "C" {
#endif #endif
static const char* skip_flags(const char* format) { int __attribute__((noinline)) __vprintf(int index, int tid, const char* format, va_list va) {
for (;;) { __if (index == tid) {
int c = *format++; return vprintf(format, va);
switch (c) { }__endif
case '-': return 0;
case '+':
case ' ':
case '#': break;
default : {
return --format;
}
}
}
return NULL;
}
static const char* skip_width(const char* format) {
if (*format == '*') {
++format;
} else {
char *endptr;
strtol(format, &endptr, 10);
format = endptr;
}
return format;
}
static const char* skip_precision(const char* format) {
if (*format == '.') {
++format;
if (*format == '*') {
++format;
} else {
char *endptr;
strtol(format, &endptr, 10);
format = endptr;
}
}
return format;
}
static const char* skip_modifier(const char* format) {
switch (*format) {
case 'h':
format++;
if (*format == 'h') {
format++;
}
break;
case 'l':
++format;
if (*format == 'l') {
++format;
}
break;
case 'j':
case 'z':
case 't':
case 'L':
++format;
break;
default:
break;
}
return format;
}
static const char* parse_format(const char* format, va_list va) {
char buffer[64];
char fmt[64];
const char* p = format;
p = skip_flags(p);
p = skip_width(p);
p = skip_precision(p);
p = skip_modifier(p);
++p;
int i;
fmt[0] = '%';
for (i = 0; i < (p - format); ++i) {
fmt[i+1] = format[i];
}
fmt[i+1] = 0;
int len = vsnprintf(buffer, 256, fmt, va);
for (i = 0; i < len; ++i) {
vx_putchar(buffer[i]);
}
return p;
} }
int vx_vprintf(const char* format, va_list va) { int vx_vprintf(const char* format, va_list va) {
if (format == NULL) int ret = 0;
return -1;
const char* p = format; // need to execute single-threaded due to potential thread-data dependency
int c = *p++; // use manual goto loop to disable compiler optimizations affceting split/join placement
while (c) {
if (c == '%') { volatile int nt = vx_num_threads();
p = parse_format(p, va); int tid = vx_thread_id();
c = *p++;
} else { for (int i = 0; i < nt; ++i) {
vx_putchar(c); ret |= __vprintf(i, tid, format, va);
c = *p++;
}
} }
return (int)(p - format);
}
int vx_printf(const char * format, ...) {
va_list va;
va_start(va, format);
int ret = vx_vprintf(format, va);
va_end(va);
return ret; return ret;
} }
static const char hextoa[] = "0123456789abcdef"; int vx_printf(const char * format, ...) {
int ret = 0;
void vx_prints(const char * str) { // need to execute single-threaded due to potential thread-data dependency
int c = *str++; // use manual goto loop to disable compiler optimizations affceting split/join placement
while (c) {
vx_putchar(c); volatile int nt = vx_num_threads();
c = *str++; int tid = vx_thread_id();
va_list va;
va_start(va, format);
for (int i = 0; i < nt; ++i) {
ret |= __vprintf(i, tid, format, va);
} }
} va_end(va);
void vx_printx(unsigned value) { return ret;
if (value < 16) {
vx_putchar(hextoa[value]);
} else {
int i = 32;
bool start = false;
do {
int temp = (value >> (i - 4)) & 0xf;
if (temp != 0)
start = true;
if (start)
vx_putchar(hextoa[temp]);
i-= 4;
} while (i != 0);
}
vx_putchar('\n');
}
void vx_printv(const char * str, unsigned value) {
vx_prints(str);
vx_printx(value);
} }
#ifdef __cplusplus #ifdef __cplusplus

View File

@@ -79,7 +79,7 @@ int main() {
vx_spawn_tasks(arguments.numRows * arguments.numColums, mat_add_kernel, &arguments); vx_spawn_tasks(arguments.numRows * arguments.numColums, mat_add_kernel, &arguments);
vx_print_mat(z, arguments.numRows, arguments.numColums); vx_print_mat(z, arguments.numRows, arguments.numColums);
vx_prints("Passed!\n"); vx_printf("Passed!\n");
return 0; return 0;
} }

View File

@@ -16,7 +16,7 @@ int main() {
float isq = 1.0f / sqrt(fNum); float isq = 1.0f / sqrt(fNum);
vx_printf("fibonacci(%d) = %d\n", Num, fib); vx_printf("fibonacci(%d) = %d\n", Num, fib);
vx_printf("invAqrt(%f) = %f\n", fNum, isq); vx_printf("invAqrt(%f) = %f\n", fNum, isq);
vx_prints("Passed!\n"); vx_printf("Passed!\n");
return 0; return 0;
} }

View File

@@ -105,8 +105,8 @@ int main() {
unsigned index = (i * arguments.numColums) + j; unsigned index = (i * arguments.numColums) + j;
vx_printf("0x%x ", z[index]); vx_printf("0x%x ", z[index]);
} }
vx_prints("\n"); vx_printf("\n");
} }
vx_prints("Passed!\n"); vx_printf("Passed!\n");
return 0; return 0;
} }

View File

@@ -17,10 +17,10 @@ void test_tmc() {
test_tmc_impl(); test_tmc_impl();
vx_tmc(1); vx_tmc(1);
vx_printx(tmc_array[0]); vx_printf("%x", tmc_array[0]);
vx_printx(tmc_array[1]); vx_printf("%x", tmc_array[1]);
vx_printx(tmc_array[2]); vx_printf("%x", tmc_array[2]);
vx_printx(tmc_array[3]); vx_printf("%x", tmc_array[3]);
return; return;
} }
@@ -57,10 +57,10 @@ void test_divergence() {
vx_tmc(1); vx_tmc(1);
vx_printx(div_arr[0]); vx_printf("%x", div_arr[0]);
vx_printx(div_arr[1]); vx_printf("%x", div_arr[1]);
vx_printx(div_arr[2]); vx_printf("%x", div_arr[2]);
vx_printx(div_arr[3]); vx_printf("%x", div_arr[3]);
} }
unsigned wsapwn_arr[4]; unsigned wsapwn_arr[4];
@@ -76,8 +76,8 @@ void simple_kernel() {
void test_wsapwn() { void test_wsapwn() {
vx_wspawn(4, (unsigned)simple_kernel); vx_wspawn(4, (unsigned)simple_kernel);
simple_kernel(); simple_kernel();
vx_printx(wsapwn_arr[0]); vx_printf("%x", wsapwn_arr[0]);
vx_printx(wsapwn_arr[1]); vx_printf("%x", wsapwn_arr[1]);
vx_printx(wsapwn_arr[2]); vx_printf("%x", wsapwn_arr[2]);
vx_printx(wsapwn_arr[3]); vx_printf("%x", wsapwn_arr[3]);
} }