summaryrefslogtreecommitdiffstats
path: root/util.c
blob: d8632c54e26943654142103f2bc9e6e562c3763c (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
#include "project.h"

int set_parity (uint8_t *d, unsigned s, unsigned e)
{
  unsigned i;
  uint8_t p = 0;

  for (i = s; i <= e; ++i)
    p ^= d[i];

  return !p;
}


void  bcd_set (uint8_t *d, unsigned s, unsigned e, unsigned v)
{
  unsigned c, i, w;


  w = v % 10;

  for (i = e, c = 1 ; i >= s; --i) {

    d[i] = !! (c & w) ;

    if (c & 0x77777777)
      c <<= 1;
    else {
      c >>= 3;
      v = v / 10;
      w = v % 10;
    }
  }
}


int check_parity (uint8_t *d, unsigned s, unsigned e, uint8_t p)
{
  unsigned i;

  for (i = s; i <= e; ++i)
    p ^= d[i];

  return !p;
}


unsigned bcd (uint8_t *d, unsigned s, unsigned e)
{
  unsigned ret = 0, c, i;

  for (i = e, c = 1 ; i >= s; --i) {

    if (d[i]) ret += c;


    if (c & 0x77777777)
      c <<= 1;
    else {
      c >>= 3;
      c *= 10;
    }
  }

  return ret;
}