mirror of
https://github.com/vortexgpgpu/vortex.git
synced 2025-04-24 05:47:35 -04:00
printf update
This commit is contained in:
parent
3947fd513f
commit
cb0459a960
6 changed files with 47 additions and 163 deletions
|
@ -7,10 +7,6 @@
|
|||
extern "C" {
|
||||
#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_printf(const char * format, ...);
|
||||
int vx_putchar(int c);
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
#include <vx_print.h>
|
||||
#include <vx_intrinsics.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
|
@ -7,159 +8,46 @@
|
|||
extern "C" {
|
||||
#endif
|
||||
|
||||
static const char* skip_flags(const char* format) {
|
||||
for (;;) {
|
||||
int c = *format++;
|
||||
switch (c) {
|
||||
case '-':
|
||||
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 __attribute__((noinline)) __vprintf(int index, int tid, const char* format, va_list va) {
|
||||
__if (index == tid) {
|
||||
return vprintf(format, va);
|
||||
}__endif
|
||||
return 0;
|
||||
}
|
||||
|
||||
int vx_vprintf(const char* format, va_list va) {
|
||||
if (format == NULL)
|
||||
return -1;
|
||||
int ret = 0;
|
||||
|
||||
const char* p = format;
|
||||
int c = *p++;
|
||||
while (c) {
|
||||
if (c == '%') {
|
||||
p = parse_format(p, va);
|
||||
c = *p++;
|
||||
} else {
|
||||
vx_putchar(c);
|
||||
c = *p++;
|
||||
}
|
||||
// need to execute single-threaded due to potential thread-data dependency
|
||||
// use manual goto loop to disable compiler optimizations affceting split/join placement
|
||||
|
||||
volatile int nt = vx_num_threads();
|
||||
int tid = vx_thread_id();
|
||||
|
||||
for (int i = 0; i < nt; ++i) {
|
||||
ret |= __vprintf(i, tid, format, va);
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
static const char hextoa[] = "0123456789abcdef";
|
||||
int vx_printf(const char * format, ...) {
|
||||
int ret = 0;
|
||||
|
||||
void vx_prints(const char * str) {
|
||||
int c = *str++;
|
||||
while (c) {
|
||||
vx_putchar(c);
|
||||
c = *str++;
|
||||
// need to execute single-threaded due to potential thread-data dependency
|
||||
// use manual goto loop to disable compiler optimizations affceting split/join placement
|
||||
|
||||
volatile int nt = vx_num_threads();
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
void vx_printx(unsigned value) {
|
||||
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);
|
||||
va_end(va);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
#ifdef __cplusplus
|
||||
|
|
|
@ -79,7 +79,7 @@ int main() {
|
|||
vx_spawn_tasks(arguments.numRows * arguments.numColums, mat_add_kernel, &arguments);
|
||||
vx_print_mat(z, arguments.numRows, arguments.numColums);
|
||||
|
||||
vx_prints("Passed!\n");
|
||||
vx_printf("Passed!\n");
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
|
@ -16,7 +16,7 @@ int main() {
|
|||
float isq = 1.0f / sqrt(fNum);
|
||||
vx_printf("fibonacci(%d) = %d\n", Num, fib);
|
||||
vx_printf("invAqrt(%f) = %f\n", fNum, isq);
|
||||
vx_prints("Passed!\n");
|
||||
vx_printf("Passed!\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
|
|
@ -105,8 +105,8 @@ int main() {
|
|||
unsigned index = (i * arguments.numColums) + j;
|
||||
vx_printf("0x%x ", z[index]);
|
||||
}
|
||||
vx_prints("\n");
|
||||
vx_printf("\n");
|
||||
}
|
||||
vx_prints("Passed!\n");
|
||||
vx_printf("Passed!\n");
|
||||
return 0;
|
||||
}
|
|
@ -17,10 +17,10 @@ void test_tmc() {
|
|||
test_tmc_impl();
|
||||
vx_tmc(1);
|
||||
|
||||
vx_printx(tmc_array[0]);
|
||||
vx_printx(tmc_array[1]);
|
||||
vx_printx(tmc_array[2]);
|
||||
vx_printx(tmc_array[3]);
|
||||
vx_printf("%x", tmc_array[0]);
|
||||
vx_printf("%x", tmc_array[1]);
|
||||
vx_printf("%x", tmc_array[2]);
|
||||
vx_printf("%x", tmc_array[3]);
|
||||
|
||||
return;
|
||||
}
|
||||
|
@ -57,10 +57,10 @@ void test_divergence() {
|
|||
|
||||
vx_tmc(1);
|
||||
|
||||
vx_printx(div_arr[0]);
|
||||
vx_printx(div_arr[1]);
|
||||
vx_printx(div_arr[2]);
|
||||
vx_printx(div_arr[3]);
|
||||
vx_printf("%x", div_arr[0]);
|
||||
vx_printf("%x", div_arr[1]);
|
||||
vx_printf("%x", div_arr[2]);
|
||||
vx_printf("%x", div_arr[3]);
|
||||
}
|
||||
|
||||
unsigned wsapwn_arr[4];
|
||||
|
@ -76,8 +76,8 @@ void simple_kernel() {
|
|||
void test_wsapwn() {
|
||||
vx_wspawn(4, (unsigned)simple_kernel);
|
||||
simple_kernel();
|
||||
vx_printx(wsapwn_arr[0]);
|
||||
vx_printx(wsapwn_arr[1]);
|
||||
vx_printx(wsapwn_arr[2]);
|
||||
vx_printx(wsapwn_arr[3]);
|
||||
vx_printf("%x", wsapwn_arr[0]);
|
||||
vx_printf("%x", wsapwn_arr[1]);
|
||||
vx_printf("%x", wsapwn_arr[2]);
|
||||
vx_printf("%x", wsapwn_arr[3]);
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue