use macros in klib-macros.h

This commit is contained in:
Zihao Yu 2020-02-07 14:40:20 +08:00
parent 63db97d758
commit 095819199e
28 changed files with 71 additions and 104 deletions

View File

@ -1,6 +1,8 @@
#ifndef __NEMU_H__
#define __NEMU_H__
#include <klib-macros.h>
#include ISA_H // "x86.h", "mips32.h", ...
#if defined(__ISA_X86__)

View File

@ -7,6 +7,10 @@
#define YIELD_INSTR_LEN ((sizeof(YIELD_INSTR)) / 5) // sizeof() counts the '\0' byte
#define SYSCALL_INSTR_LEN YIELD_INSTR_LEN
// if this fails, allocate larger space in trap.S for ucontext
static_assert(sizeof(ucontext_t) < 1024);
static_assert(SYSCALL_INSTR_LEN == 7);
static _Context* (*user_handler)(_Event, _Context*) = NULL;
void __am_asm_trap();
@ -35,17 +39,14 @@ void __am_irq_handle(_Context *c) {
}
static void setup_stack(uintptr_t event, ucontext_t *c) {
uint8_t *rip = (uint8_t *)c->uc_mcontext.gregs[REG_RIP];
void *rip = (void *)c->uc_mcontext.gregs[REG_RIP];
extern uint8_t _start, _etext;
void *sigprocmask_base = &sigprocmask;
// assume the virtual address space of user process is not above 0xffffffff
if (((event == _EVENT_IRQ_IODEV) || (event == _EVENT_IRQ_TIMER)) &&
!((rip >= &_start && rip < &_etext) ||
// Hack here: "+13" points to the instruction after syscall.
// This is the instruction which will trigger the pending signal
// if interrupt is enabled.
(rip == sigprocmask_base + 13) ||
(uintptr_t)rip < 0x100000000ul)) {
int signal_safe = IN_RANGE(rip, RANGE(&_start, &_etext)) || __am_in_userspace(rip) ||
// Hack here: "+13" points to the instruction after syscall. This is the
// instruction which will trigger the pending signal if interrupt is enabled.
(rip == (void *)&sigprocmask + 13);
if (((event == _EVENT_IRQ_IODEV) || (event == _EVENT_IRQ_TIMER)) && !signal_safe) {
// Shared libraries contain code which are not reenterable.
// If the signal comes when executing code in shared libraries,
// the signal handler can not call any function which is not signal-safe,
@ -137,9 +138,6 @@ void __am_init_timer_irq() {
}
int _cte_init(_Context*(*handler)(_Event, _Context*)) {
assert(sizeof(ucontext_t) < 1024); // if this fails, allocate larger space in trap.S for ucontext
assert(SYSCALL_INSTR_LEN == 7);
user_handler = handler;
install_signal_handler();

View File

@ -84,8 +84,7 @@ static void init_platform() {
}
// set up the AM heap
_heap.start = pmem;
_heap.end = pmem + PMEM_SIZE;
_heap = RANGE(pmem, pmem + PMEM_SIZE);
// initialize sigmask for interrupts
ret2 = sigemptyset(&__am_intr_sigmask);

View File

@ -5,6 +5,7 @@
#include <unistd.h>
#include <signal.h>
#include <klib.h>
#include <klib-macros.h>
// the size of red zone of the stack frame, see the amd64 ABI manual for details
#define RED_NONE_SIZE 128

View File

@ -1,6 +1,6 @@
#include "platform.h"
#define USER_SPACE (_Area) { .start = (void *)0x40000000ul, .end = (void *)0xc0000000ul }
#define USER_SPACE RANGE(0x40000000, 0xc0000000)
#define PGSIZE 4096
@ -56,7 +56,7 @@ void __am_switch(_Context *c) {
if (as != NULL) {
// mmap all mappings
list_foreach(pp, as->ptr) {
assert(USER_SPACE.start <= pp->va && pp->va < USER_SPACE.end);
assert(IN_RANGE(pp->va, USER_SPACE));
__am_shm_mmap(pp->va, pp->pa, pp->prot);
pp->is_mapped = true;
}
@ -66,7 +66,7 @@ void __am_switch(_Context *c) {
}
void _map(_AddressSpace *as, void *va, void *pa, int prot) {
assert(USER_SPACE.start <= va && va < USER_SPACE.end);
assert(IN_RANGE(va, USER_SPACE));
assert((uintptr_t)va % PGSIZE == 0);
assert((uintptr_t)pa % PGSIZE == 0);
assert(as != NULL);
@ -113,5 +113,5 @@ void _ucontext(_Context *c, _AddressSpace *as, _Area kstack, void *entry) {
}
int __am_in_userspace(void *addr) {
return vme_enable && (USER_SPACE.start <= addr) && (addr < USER_SPACE.end);
return vme_enable && IN_RANGE(addr, USER_SPACE);
}

View File

@ -4,11 +4,7 @@
#define HEAP_SIZE (8 * 1024 * 1024)
static uint8_t heap[HEAP_SIZE] = {};
_Area _heap = {
.start = heap,
.end = heap + HEAP_SIZE
};
_Area _heap = RANGE(heap, heap + HEAP_SIZE);
void _trm_init() {
}

View File

@ -5,10 +5,7 @@ extern char _heap_start;
extern char _heap_end;
int main(const char *args);
_Area _heap = {
.start = &_heap_start,
.end = &_heap_end,
};
_Area _heap = RANGE(&_heap_start, &_heap_end);
void _putc(char ch) {
outb(SERIAL_PORT, ch);

View File

@ -11,12 +11,10 @@ static void (*pgfree_usr)(void*) = NULL;
static int vme_enable = 0;
static _Area segments[] = { // Kernel memory mappings
{.start = (void*)0x80000000u, .end = (void*)(0x80000000u + PMEM_SIZE)},
{.start = (void*)MMIO_BASE, .end = (void*)(MMIO_BASE + MMIO_SIZE)}
RANGE(0x80000000u, 0x80000000u + PMEM_SIZE),
RANGE(MMIO_BASE, MMIO_BASE + MMIO_SIZE),
};
#define NR_KSEG_MAP (sizeof(segments) / sizeof(segments[0]))
static inline void set_satp(void *pdir) {
asm volatile("csrw satp, %0" : : "r"(0x80000000 | ((uintptr_t)pdir >> 12)));
}
@ -32,7 +30,7 @@ int _vme_init(void* (*pgalloc_f)(size_t), void (*pgfree_f)(void*)) {
}
PTE *ptab = kptabs;
for (i = 0; i < NR_KSEG_MAP; i ++) {
for (i = 0; i < LENGTH(segments); i ++) {
uint32_t pdir_idx = (uintptr_t)segments[i].start / (PGSIZE * NR_PTE);
uint32_t pdir_idx_end = (uintptr_t)segments[i].end / (PGSIZE * NR_PTE);
for (; pdir_idx < pdir_idx_end; pdir_idx ++) {

View File

@ -10,10 +10,7 @@ void __am_uartlite_putchar(char ch);
void __am_init_perfcnt(void);
void __am_show_perfcnt(void);
_Area _heap = {
.start = &_heap_start,
.end = &_heap_end,
};
_Area _heap = RANGE(&_heap_start, &_heap_end);
void _putc(char ch) {
__am_uartlite_putchar(ch);

View File

@ -14,8 +14,6 @@ typedef uint32_t PDE;
#define PDX(va) (((uint32_t)(va) >> PDXSHFT) & 0x3ff)
#define PTX(va) (((uint32_t)(va) >> PTXSHFT) & 0x3ff)
#define OFF(va) ((uint32_t)(va) & 0xfff)
#define ROUNDUP(a, sz) ((((uintptr_t)a)+(sz)-1) & ~((sz)-1))
#define ROUNDDOWN(a, sz) ((((uintptr_t)a)) & ~((sz)-1))
#define PTE_ADDR(pte) ((uint32_t)(pte) & ~0xfff)
#define PGADDR(d, t, o) ((uint32_t)((d) << PDXSHFT | (t) << PTXSHFT | (o)))
#define PG_ALIGN __attribute((aligned(PGSIZE)))
@ -27,12 +25,10 @@ static void (*pgfree_usr)(void*) = NULL;
static int vme_enable = 0;
static _Area segments[] = { // Kernel memory mappings
{.start = (void*)0, .end = (void*)PMEM_SIZE},
{.start = (void*)MMIO_BASE, .end = (void*)(MMIO_BASE + MMIO_SIZE)}
RANGE(0, PMEM_SIZE),
RANGE(MMIO_BASE, MMIO_BASE + MMIO_SIZE),
};
#define NR_KSEG_MAP (sizeof(segments) / sizeof(segments[0]))
int _vme_init(void* (*pgalloc_f)(size_t), void (*pgfree_f)(void*)) {
pgalloc_usr = pgalloc_f;
pgfree_usr = pgfree_f;
@ -45,7 +41,7 @@ int _vme_init(void* (*pgalloc_f)(size_t), void (*pgfree_f)(void*)) {
}
PTE *ptab = kptabs;
for (i = 0; i < NR_KSEG_MAP; i ++) {
for (i = 0; i < LENGTH(segments); i ++) {
uint32_t pdir_idx = (uintptr_t)segments[i].start / (PGSIZE * NR_PTE);
uint32_t pdir_idx_end = (uintptr_t)segments[i].end / (PGSIZE * NR_PTE);
for (; pdir_idx < pdir_idx_end; pdir_idx ++) {

View File

@ -1,15 +1,13 @@
#include <am.h>
#include <benchmark.h>
#include <limits.h>
#include <klib-macros.h>
Benchmark *current;
Setting *setting;
static char *hbrk;
#define ARR_SIZE(a) (sizeof((a)) / sizeof((a)[0]))
#define ROUNDUP(a, sz) ((((uintptr_t)a)+(sz)-1) & ~((sz)-1))
// The benchmark list
#define ENTRY(_name, _sname, _s, _m, _l, _desc) \
@ -84,7 +82,7 @@ int main(const char *args) {
int pass = 1;
uint32_t t0 = uptime();
for (int i = 0; i < ARR_SIZE(benchmarks); i ++) {
for (int i = 0; i < LENGTH(benchmarks); i ++) {
Benchmark *bench = &benchmarks[i];
current = bench;
setting = &bench->settings[setting_id];
@ -120,7 +118,7 @@ int main(const char *args) {
}
uint32_t t1 = uptime();
bench_score /= sizeof(benchmarks) / sizeof(benchmarks[0]);
bench_score /= LENGTH(benchmarks);
printf("==================================================\n");
printf("MicroBench %s", pass ? "PASS" : "FAIL");

View File

@ -1,4 +1,5 @@
#include "klib.h"
#include <klib-macros.h>
#if !defined(__ISA_NATIVE__) || defined(__NATIVE_USE_KLIB__)
static unsigned long int next = 1;
@ -35,7 +36,7 @@ void *malloc(size_t size) {
}
// aligning
size = (size + sizeof(uintptr_t) - 1) & ~(sizeof(uintptr_t) - 1);
size = ROUNDUP(size, sizeof(uintptr_t));
if (head + size >= _heap.end) return NULL;
printf("malloc: size = %d\n", size);

View File

@ -3,14 +3,13 @@
#include <am.h>
#include <klib.h>
#include <klib-macros.h>
#define IOE ({ _ioe_init(); })
#define CTE(h) ({ _Context *h(_Event, _Context *); _cte_init(h); })
#define VME(f1, f2) ({ void *f1(size_t); void f2(void *); _vme_init(f1, f2); })
#define MPE ({ _mpe_init(entry); })
#define LENGTH(arr) (sizeof(arr) / sizeof(arr[0]))
extern void (*entry)();
#define CASE(id, entry_, ...) \

View File

@ -1,9 +1,5 @@
#include <amtest.h>
static inline void putstr(const char *s) {
for (; *s; s ++) _putc(*s);
}
void hello() {
for (int i = 0; i < 10; i ++) {
putstr("Hello, AM World @ " __ISA__ "\n");

View File

@ -73,7 +73,7 @@ void vm_test() {
printf("Code copied to %p (physical %p) execute\n", ptr, up1);
static uint8_t kstk[4096];
_ucontext(&uctx, &prot, (_Area) { kstk, kstk + 4096 } , ptr);
_ucontext(&uctx, &prot, RANGE(kstk, kstk + 4096), ptr);
_intr_write(1);
while (1) ;

View File

@ -32,9 +32,7 @@ _Context* handler(_Event ev, _Context *ctx) {
return current;
}
#define STACK(id) \
(_Area) { .start = &stacks[(id) ][0], \
.end = &stacks[(id) + 1][0], }
#define STACK(id) RANGE(&stacks[(id)][0], &stacks[(id) + 1][0])
int main(){
printf("kcontext test started.\n");

View File

@ -3,6 +3,7 @@
#include <am.h>
#include <klib.h>
#include <klib-macros.h>
__attribute__((noinline))
void nemu_assert(int cond) {

View File

@ -8,7 +8,7 @@ long long add(long long a, long long b) {
long long test_data[] = {0, 1, 2, 0x7fffffffffffffffLL, 0x8000000000000000LL, 0x8000000000000001LL, 0xfffffffffffffffeLL, 0xffffffffffffffffLL};
long long ans[] = {0LL, 0x1LL, 0x2LL, 0x7fffffffffffffffLL, 0x8000000000000000LL, 0x8000000000000001LL, 0xfffffffffffffffeLL, 0xffffffffffffffffLL, 0x1LL, 0x2LL, 0x3LL, 0x8000000000000000LL, 0x8000000000000001LL, 0x8000000000000002LL, 0xffffffffffffffffLL, 0LL, 0x2LL, 0x3LL, 0x4LL, 0x8000000000000001LL, 0x8000000000000002LL, 0x8000000000000003LL, 0LL, 0x1LL, 0x7fffffffffffffffLL, 0x8000000000000000LL, 0x8000000000000001LL, 0xfffffffffffffffeLL, 0xffffffffffffffffLL, 0LL, 0x7ffffffffffffffdLL, 0x7ffffffffffffffeLL, 0x8000000000000000LL, 0x8000000000000001LL, 0x8000000000000002LL, 0xffffffffffffffffLL, 0LL, 0x1LL, 0x7ffffffffffffffeLL, 0x7fffffffffffffffLL, 0x8000000000000001LL, 0x8000000000000002LL, 0x8000000000000003LL, 0LL, 0x1LL, 0x2LL, 0x7fffffffffffffffLL, 0x8000000000000000LL, 0xfffffffffffffffeLL, 0xffffffffffffffffLL, 0LL, 0x7ffffffffffffffdLL, 0x7ffffffffffffffeLL, 0x7fffffffffffffffLL, 0xfffffffffffffffcLL, 0xfffffffffffffffdLL, 0xffffffffffffffffLL, 0LL, 0x1LL, 0x7ffffffffffffffeLL, 0x7fffffffffffffffLL, 0x8000000000000000LL, 0xfffffffffffffffdLL, 0xfffffffffffffffeLL};
#define NR_DATA (sizeof(test_data) / sizeof(test_data[0]))
#define NR_DATA LENGTH(test_data)
int main() {
int i, j, ans_idx = 0;

View File

@ -8,7 +8,7 @@ int add(int a, int b) {
int test_data[] = {0, 1, 2, 0x7fffffff, 0x80000000, 0x80000001, 0xfffffffe, 0xffffffff};
int ans[] = {0, 0x1, 0x2, 0x7fffffff, 0x80000000, 0x80000001, 0xfffffffe, 0xffffffff, 0x1, 0x2, 0x3, 0x80000000, 0x80000001, 0x80000002, 0xffffffff, 0, 0x2, 0x3, 0x4, 0x80000001, 0x80000002, 0x80000003, 0, 0x1, 0x7fffffff, 0x80000000, 0x80000001, 0xfffffffe, 0xffffffff, 0, 0x7ffffffd, 0x7ffffffe, 0x80000000, 0x80000001, 0x80000002, 0xffffffff, 0, 0x1, 0x7ffffffe, 0x7fffffff, 0x80000001, 0x80000002, 0x80000003, 0, 0x1, 0x2, 0x7fffffff, 0x80000000, 0xfffffffe, 0xffffffff, 0, 0x7ffffffd, 0x7ffffffe, 0x7fffffff, 0xfffffffc, 0xfffffffd, 0xffffffff, 0, 0x1, 0x7ffffffe, 0x7fffffff, 0x80000000, 0xfffffffd, 0xfffffffe};
#define NR_DATA (sizeof(test_data) / sizeof(test_data[0]))
#define NR_DATA LENGTH(test_data)
int main() {
int i, j, ans_idx = 0;

View File

@ -14,7 +14,7 @@ int if_else(int n) {
int test_data[] = {-1, 0, 49, 50, 51, 99, 100, 101, 299, 300, 301, 499, 500, 501};
int ans[] = {0, 0, 0, 0, 50, 50, 50, 75, 75, 75, 100, 100, 100, 150};
#define NR_DATA (sizeof(test_data) / sizeof(test_data[0]))
#define NR_DATA LENGTH(test_data)
int main() {
int i, ans_idx = 0;

View File

@ -1,7 +1,5 @@
#include "trap.h"
#define ARR_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
unsigned short mem[] = {
0x0, 0x0258, 0x4abc, 0x7fff, 0x8000, 0x8100, 0xabcd, 0xffff
};
@ -25,20 +23,20 @@ unsigned lwlr_ans[] = {
int main() {
unsigned i;
for(i = 0; i < ARR_SIZE(mem); i ++) {
for(i = 0; i < LENGTH(mem); i ++) {
nemu_assert((short)mem[i] == lh_ans[i]);
}
for(i = 0; i < ARR_SIZE(mem); i ++) {
for(i = 0; i < LENGTH(mem); i ++) {
nemu_assert(mem[i] == lhu_ans[i]);
}
for(i = 0; i < ((ARR_SIZE(mem) / 2) - 1); i ++) {
for(i = 0; i < ((LENGTH(mem) / 2) - 1); i ++) {
unsigned x = ((unsigned*)((void*)mem + 1))[i];
nemu_assert(x == lwlr_ans[i]);
}
for(i = 0; i < ARR_SIZE(mem); i ++) {
for(i = 0; i < LENGTH(mem); i ++) {
mem[i] = ~(1 << (2 * i + 1));
nemu_assert(mem[i] == sh_ans[i]);
}

View File

@ -10,7 +10,7 @@ int max(int x, int y) {
int test_data[] = {0, 1, 2, 0x7fffffff, 0x80000000, 0x80000001, 0xfffffffe, 0xffffffff};
int ans[] = {0, 0x1, 0x2, 0x7fffffff, 0, 0, 0, 0, 0x1, 0x1, 0x2, 0x7fffffff, 0x1, 0x1, 0x1, 0x1, 0x2, 0x2, 0x2, 0x7fffffff, 0x2, 0x2, 0x2, 0x2, 0x7fffffff, 0x7fffffff, 0x7fffffff, 0x7fffffff, 0x7fffffff, 0x7fffffff, 0x7fffffff, 0x7fffffff, 0, 0x1, 0x2, 0x7fffffff, 0x80000000, 0x80000001, 0xfffffffe, 0xffffffff, 0, 0x1, 0x2, 0x7fffffff, 0x80000001, 0x80000001, 0xfffffffe, 0xffffffff, 0, 0x1, 0x2, 0x7fffffff, 0xfffffffe, 0xfffffffe, 0xfffffffe, 0xffffffff, 0, 0x1, 0x2, 0x7fffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff};
#define NR_DATA (sizeof(test_data) / sizeof(test_data[0]))
#define NR_DATA LENGTH(test_data)
int main() {
int i, j, ans_idx = 0;

View File

@ -11,7 +11,7 @@ int min3(int x, int y, int z) {
int test_data[] = {0, 0x7fffffff, 0x80000000, 0xffffffff};
int ans [] = {0, 0, -2147483648, -1, 0, 0, -2147483648, -1, -2147483648, -2147483648, -2147483648, -2147483648, -1, -1, -2147483648, -1, 0, 0, -2147483648, -1, 0, 2147483647, -2147483648, -1, -2147483648, -2147483648, -2147483648, -2147483648, -1, -1, -2147483648, -1, -2147483648, -2147483648, -2147483648, -2147483648, -2147483648, -2147483648, -2147483648, -2147483648, -2147483648, -2147483648, -2147483648, -2147483648, -2147483648, -2147483648, -2147483648, -2147483648, -1, -1, -2147483648, -1, -1, -1, -2147483648, -1, -2147483648, -2147483648, -2147483648, -2147483648, -1, -1, -2147483648, -1};
#define NR_DATA (sizeof(test_data) / sizeof(test_data[0]))
#define NR_DATA LENGTH(test_data)
int main() {
int i, j, k, ans_idx = 0;

View File

@ -8,7 +8,7 @@ long long mul(long long a,long long b) {
int test_data[] = { 0xaeb1c2aa, 0x4500ff2b, 0x877190af, 0x11f42438};
long long ans[] = { 0x19d29ab9db1a18e4LL, 0xea15986d3ac3088eLL, 0x2649e980fc0db236LL, 0xfa4c43da0a4a7d30LL, 0x1299898e2c56b139LL, 0xdf8123d50a319e65LL, 0x4d6dfa84c15dd68LL, 0x38c5d79b9e4357a1LL, 0xf78b91cb1efc4248LL, 0x14255a47fdfcc40LL};
#define NR_DATA (sizeof(test_data) / sizeof(test_data[0]))
#define NR_DATA LENGTH(test_data)
int main() {
int i,j,ans_idx = 0;

View File

@ -1,6 +1,4 @@
#include "trap.h"
#define ARR_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
unsigned test[] = {
0x12345678, 0x98765432, 0x0, 0xeffa1000, 0x7fffffff, 0x80000000, 0x33, 0xffffffff
@ -22,15 +20,15 @@ unsigned srav_ans[] = {
int main() {
unsigned i;
for(i = 0; i < ARR_SIZE(test); i ++) {
for(i = 0; i < LENGTH(test); i ++) {
nemu_assert((test[i] >> 7) == srl_ans[i]);
}
for(i = 0; i < ARR_SIZE(test); i ++) {
for(i = 0; i < LENGTH(test); i ++) {
nemu_assert((unsigned)((int)test[i] >> (i + 4)) == srav_ans[i]);
}
for(i = 0; i < ARR_SIZE(test); i ++) {
for(i = 0; i < LENGTH(test); i ++) {
nemu_assert((test[i] >> (i + 4)) == srlv_ans[i]);
}

View File

@ -8,7 +8,7 @@ long long sub(long long a, long long b) {
long long test_data[] = {0, 1, 2, 0x7fffffffffffffffLL, 0x8000000000000000LL, 0x8000000000000001LL, 0xfffffffffffffffeLL, 0xffffffffffffffffLL};
long long ans[] = {0LL, 0xffffffffffffffffLL, 0xfffffffffffffffeLL, 0x8000000000000001LL, 0x8000000000000000LL, 0x7fffffffffffffffLL, 0x2LL, 0x1LL, 0x1LL, 0LL, 0xffffffffffffffffLL, 0x8000000000000002LL, 0x8000000000000001LL, 0x8000000000000000LL, 0x3LL, 0x2LL, 0x2LL, 0x1LL, 0LL, 0x8000000000000003LL, 0x8000000000000002LL, 0x8000000000000001LL, 0x4LL, 0x3LL, 0x7fffffffffffffffLL, 0x7ffffffffffffffeLL, 0x7ffffffffffffffdLL, 0LL, 0xffffffffffffffffLL, 0xfffffffffffffffeLL, 0x8000000000000001LL, 0x8000000000000000LL, 0x8000000000000000LL, 0x7fffffffffffffffLL, 0x7ffffffffffffffeLL, 0x1LL, 0LL, 0xffffffffffffffffLL, 0x8000000000000002LL, 0x8000000000000001LL, 0x8000000000000001LL, 0x8000000000000000LL, 0x7fffffffffffffffLL, 0x2LL, 0x1LL, 0LL, 0x8000000000000003LL, 0x8000000000000002LL, 0xfffffffffffffffeLL, 0xfffffffffffffffdLL, 0xfffffffffffffffcLL, 0x7fffffffffffffffLL, 0x7ffffffffffffffeLL, 0x7ffffffffffffffdLL, 0LL, 0xffffffffffffffffLL, 0xffffffffffffffffLL, 0xfffffffffffffffeLL, 0xfffffffffffffffdLL, 0x8000000000000000LL, 0x7fffffffffffffffLL, 0x7ffffffffffffffeLL, 0x1LL, 0LL};
#define NR_DATA (sizeof(test_data) / sizeof(test_data[0]))
#define NR_DATA LENGTH(test_data)
int main() {
int i, j, ans_idx = 0;

View File

@ -8,7 +8,6 @@
#define ARRAY_DEF(type, name, smax, smin, umax) \
static const type name [] = {0, smax / 17, smax, smin, smin + 1, umax / 17, smin / 17, umax}
#define ARRAY_LEN(a) (sizeof(a) / sizeof(a[0]))
ARRAY_DEF(char, char_, SCHAR_MAX, SCHAR_MIN, UCHAR_MAX);
ARRAY_DEF(short, short_, SHRT_MAX, SHRT_MIN, USHRT_MAX);
@ -17,7 +16,7 @@ ARRAY_DEF(long, long_, LONG_MAX, LONG_MIN, ULONG_MAX); // wordsize-dependent
ARRAY_DEF(long long, longlong_, LLONG_MAX, LLONG_MIN, ULLONG_MAX);
ARRAY_DEF(uintptr_t, ptr, INTPTR_MAX, INTPTR_MIN, UINTPTR_MAX);// wordsize-dependent
static_assert(ARRAY_LEN(int_) == 8);
static_assert(LENGTH(int_) == 8);
#define TEST_FORMAT(conv, array) \
conv " " conv " " conv " " conv " " conv " " conv " " conv " " conv, \
array[0], array[1], array[2], array[3], array[4], array[5], array[6], array[7]
@ -25,10 +24,6 @@ static_assert(ARRAY_LEN(int_) == 8);
static char buf[1024];
static char *p_buf = NULL;
static void _puts(const char *s) {
while (*s) { _putc(*s ++); }
}
static void reset_buf() {
for (int i = 0; i < sizeof(buf); i ++)
buf[i] = 0;
@ -37,22 +32,22 @@ static void reset_buf() {
static void check_buf(const char *ref) {
if (0 != strcmp(ref, buf)) {
_puts("right: \"");
_puts(ref);
_puts("\"\n");
_puts("wrong: \"");
_puts(buf);
_puts("\"\n");
putstr("right: \"");
putstr(ref);
putstr("\"\n");
putstr("wrong: \"");
putstr(buf);
putstr("\"\n");
_puts("Test fail!\n");
putstr("Test fail!\n");
_halt(1);
}
}
static void test(const char *name, void (*fn)(void)) {
_puts("Testing '");
_puts(name);
_puts("'...\n");
putstr("Testing '");
putstr(name);
putstr("'...\n");
reset_buf();
fn();
@ -121,16 +116,16 @@ static void test_full_format() {
char format[32];
int i, j, k, m, n;
int idx = 0;
for (i = 0; i < ARRAY_LEN(flag); i ++) {
for (j = 0; j < ARRAY_LEN(width); j ++) {
for (k = 0; k < ARRAY_LEN(prec); k ++) {
for (m = 0; m < ARRAY_LEN(len); m ++) {
for (i = 0; i < LENGTH(flag); i ++) {
for (j = 0; j < LENGTH(width); j ++) {
for (k = 0; k < LENGTH(prec); k ++) {
for (m = 0; m < LENGTH(len); m ++) {
for (n = 0; n < STRLEN(CONV); n ++) {
sprintf(format, "%%%s%s%s%s%c", flag[i], width[j], prec[k], len[m], CONV[n]);
reset_buf();
_puts("\tTesting format \"");
_puts(format);
_puts("\"\n");
putstr("\tTesting format \"");
putstr(format);
putstr("\"\n");
sprintf(buf, format, 0xdead12ef);
//printf("\"%s\",\n", buf);
check_buf(full_format_ans[idx]);

View File

@ -121,12 +121,11 @@ void check_divu(uint32_t x, uint32_t y) {
}
int v[] = {0, 1, 2, 3, 0x7fffffff, 0x80000000, 0x80000001, 0xfffffffd, 0xfffffffe, 0xffffffff};
#define NR_DATA(array) (sizeof(array) / sizeof(array[0]))
int main() {
int i, j;
for (i = 0; i < NR_DATA(v); i ++) {
for (j = 1; j < NR_DATA(v); j ++) {
for (i = 0; i < LENGTH(v); i ++) {
for (j = 1; j < LENGTH(v); j ++) {
check_mul(v[i], v[j]);
check_div(v[i], v[j]);
check_divu(v[i], v[j]);