-
Notifications
You must be signed in to change notification settings - Fork 53
/
utils.c
103 lines (90 loc) · 1.97 KB
/
utils.c
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include <stdint.h>
#include "serial.h"
#include "printf.h"
static inline int _isprint(char c)
{
return c > 32 && c < 127;
}
#define HEX_CHARS "0123456789abcdef"
void serial_puth(uint32_t pair, int digits)
{
if (digits >= 8)
serial_putc(HEX_CHARS[(pair >> 28) & 0xf]);
if (digits >= 7)
serial_putc(HEX_CHARS[(pair >> 24) & 0xf]);
if (digits >= 6)
serial_putc(HEX_CHARS[(pair >> 20) & 0xf]);
if (digits >= 5)
serial_putc(HEX_CHARS[(pair >> 16) & 0xf]);
if (digits >= 4)
serial_putc(HEX_CHARS[(pair >> 12) & 0xf]);
if (digits >= 3)
serial_putc(HEX_CHARS[(pair >> 8) & 0xf]);
if (digits >= 2)
serial_putc(HEX_CHARS[(pair >> 4) & 0xf]);
if (digits >= 1)
serial_putc(HEX_CHARS[(pair >> 0) & 0xf]);
}
int serial_print_hex_offset(const void *block, int count, int offset)
{
int byte;
const uint8_t *b = block;
count += offset;
b -= offset;
for ( ; offset < count; offset += 16) {
serial_puth((uint32_t)b + offset, 8);
for (byte = 0; byte < 16; byte++) {
if (byte == 8)
serial_putc(' ');
serial_putc(' ');
if (offset + byte < count)
serial_puth(b[offset + byte] & 0xff, 2);
else
serial_puts(" ");
}
serial_puts(" |");
for (byte = 0; byte < 16 && byte + offset < count; byte++)
serial_putc(_isprint(b[offset + byte]) ?
b[offset + byte] :
'.');
serial_puts("|\n");
}
return 0;
}
int serial_print_hex(const void *block, int count)
{
return serial_print_hex_offset(block, count, 0);
}
uint32_t __div64_32(uint64_t *n, uint32_t base)
{
uint64_t rem = *n;
uint64_t b = base;
uint64_t res, d = 1;
uint32_t high = rem >> 32;
/* Reduce the thing a bit first */
res = 0;
if (high >= base) {
high /= base;
res = (uint64_t) high << 32;
rem -= (uint64_t) (high*base) << 32;
}
while ((int64_t)b > 0 && b < rem) {
b = b+b;
d = d+d;
}
do {
if (rem >= b) {
rem -= b;
res += d;
}
b >>= 1;
d >>= 1;
} while (d);
*n = res;
return rem;
}
void __div0 (void)
{
printf("Division by 0. Hanging.\n");
while(1);
}