forked from jviki/dtree
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbcd_arith.c
More file actions
48 lines (40 loc) · 699 Bytes
/
bcd_arith.c
File metadata and controls
48 lines (40 loc) · 699 Bytes
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
#include "bcd_arith.h"
#include <string.h>
#include <assert.h>
#define BCD_ZERO "00"
void bcd_init(bcd_t n)
{
// copy all the zstring including the '\0'
memcpy((void *) n, (void *) BCD_ZERO, strlen(BCD_ZERO) + 1);
assert(n[strlen(BCD_ZERO)] == '\0');
}
/**
* Uses decadic digits.
*/
static
int bcd_dec_inc(bcd_t n)
{
size_t len = strlen(BCD_ZERO);
for(size_t i = len; i > 0; --i) {
if(n[i - 1] == '9') {
n[i - 1] = '0';
}
else {
n[i - 1] += 1;
break;
}
}
return bcd_iszero(n);
}
int bcd_inc(bcd_t n)
{
return bcd_dec_inc(n);
}
int bcd_iszero(bcd_t n)
{
return !memcmp(n, BCD_ZERO, strlen(BCD_ZERO));
}
const char *bcd_tostr(bcd_t n)
{
return (const char *) n;
}