aboutsummaryrefslogtreecommitdiffstats
path: root/firmware/multest.c
blob: 93e48df8c12ecbbe5dd693f4bfd2dd31939dbde4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// This is free and unencumbered software released into the public domain.
//
// Anyone is free to copy, modify, publish, use, compile, sell, or
// distribute this software, either in source code form or as a compiled
// binary, for any purpose, commercial or non-commercial, and by any
// means.

#include "firmware.h"

static uint32_t xorshift32(void) {
	static uint32_t x = 314159265;
	x ^= x << 13;
	x ^= x >> 17;
	x ^= x << 5;
	return x;
}

void multest(void)
{
	for (int i = 0; i < 10; i++)
	{
		uint32_t a = xorshift32();
		uint32_t b = xorshift32();

		uint64_t au = a, bu = b;
		int64_t as = (int32_t)a, bs = (int32_t)b;

		print_str("input [");
		print_hex(as >> 32, 8);
		print_str("] ");
		print_hex(a, 8);
		print_str(" [");
		print_hex(bs >> 32, 8);
		print_str("] ");
		print_hex(b, 8);
		print_chr('\n');

		uint32_t h_mul, h_mulh, h_mulhsu, h_mulhu;
		print_str("hard   ");

		h_mul = hard_mul(a, b);
		print_hex(h_mul, 8);
		print_str("  ");

		h_mulh = hard_mulh(a, b);
		print_hex(h_mulh, 8);
		print_str("  ");

		h_mulhsu = hard_mulhsu(a, b);
		print_hex(h_mulhsu, 8);
		print_str("  ");

		h_mulhu = hard_mulhu(a, b);
		print_hex(h_mulhu, 8);
		print_chr('\n');

		uint32_t s_mul, s_mulh, s_mulhsu, s_mulhu;
		print_str("soft   ");

		s_mul = a * b;
		print_hex(s_mul, 8);
		print_str("  ");

		s_mulh = (as * bs) >> 32;
		print_hex(s_mulh, 8);
		print_str("  ");

		s_mulhsu = (as * bu) >> 32;
		print_hex(s_mulhsu, 8);
		print_str("  ");

		s_mulhu = (au * bu) >> 32;
		print_hex(s_mulhu, 8);
		print_str("  ");

		if (s_mul != h_mul || s_mulh != h_mulh || s_mulhsu != h_mulhsu || s_mulhu != h_mulhu) {
			print_str("ERROR!\n");
			__asm__ volatile ("ebreak");
			return;
		}

		print_str(" OK\n");
	}
}