File: countbitstable.sh

package info (click to toggle)
boolector 3.2.4-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 20,744 kB
  • sloc: ansic: 83,136; cpp: 18,159; sh: 3,668; python: 2,889; makefile: 210
file content (136 lines) | stat: -rwxr-xr-x 2,874 bytes parent folder | download
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#!/bin/bash
# Count bits pop(x) algorithm, table lookup version,
# hacker's delight, page 71
# we verifiy it by cross-checking with the obvios method of counting bits
#    for (s = i = 0; i < n; i++)
#      if (x & (1 << i))
#        s++;

function dec2binbyte
{
  local x=$1
  local ret="`echo "ibase=10; obase=2; $x" | bc`"
  local result=`printf "%08d" $ret`
  echo $result
}

function pop
{
  local x=$1
  local result=0
  while [[ -n "$x" ]]
  do
    if [[ "${x:0:1}" = "1" ]]; then 
      ((result++))
    fi
    x=${x:1}
  done
  echo $result
}



function log_2
{
  local result=0
  local x=$1
  while [[ "$x" -gt 1 ]]
  do  
      ((x >>= 1))
      ((result++))
  done 
  echo $result
}

function is_power_of_2
{
  local x=$1
  ((x = x & (x - 1)))
  if [[ x -eq 0 ]]; then
    echo 1
  else
    echo 0
  fi
}

if [[ $# -ne 1 ]]; then
  echo "Usage ./countbitstable <num-bits>"
  exit 1
fi

numbits=$1
if [[ "$numbits" -lt 8 ]] || [[ ! `is_power_of_2 $numbits` -eq 1 ]]; then
  echo "Number of bits must be a power of 2 >= 8"
  exit 1
fi
numbitslog=`log_2 $numbits`
id=1

echo "$((id++)) var $numbits x"
((idx = id - 1))
((idsum = idx))
((idxorig = idx))
echo "$((id++)) array 8 8"
((idarray = id - 1))
echo "$((id++)) zero $numbits"
((idzero = id - 1))
echo "$((id++)) one $numbits"
((idone = id - 1))
if [[ $numbits -gt 8 ]]; then
  echo "$((id++)) constd $numbitslog 8"
  ((ideight = id - 1))
fi
for ((i = 0; i < 256; i++))
do
  binrep="`dec2binbyte $i`"
  echo "$((id++)) const 8 $binrep"
  ((idindex = id - 1))
  pop="`pop $binrep`"
  echo "$((id++)) const 8 `dec2binbyte $pop`"
  ((lastid = id - 1))
  echo "$((id++)) write 8 8 $idarray $idindex $lastid"
  ((idarray = id - 1))
done

echo "$((id++)) slice 8 $idx 7 0"
((lastid = id - 1))
echo "$((id++)) read 8 $idarray $lastid"
((lastid = id - 1))
echo "$((id++)) uext $numbits $lastid $((numbits - 8))"
((idsum = id - 1))
for ((i = 8; i < $numbits; i += 8))
do
  echo "$((id++)) srl $numbits $idx $ideight"
  ((idx = id - 1))
  echo "$((id++)) slice 8 $idx 7 0"
  ((lastid = id - 1))
  echo "$((id++)) read 8 $idarray $lastid"
  ((lastid = id - 1))
  echo "$((id++)) uext $numbits $lastid $((numbits - 8))"
  ((lastid = id - 1))
  echo "$((id++)) add $numbits $lastid $idsum"
  ((idsum = id - 1))
done
((idresult1 = idsum))

((idsum = idzero))
for ((i = 0; i < $numbits; i++))
do
  echo "$((id++)) constd $numbitslog $i"
  ((lastid = id - 1))
  echo "$((id++)) sll $numbits $idone $lastid"
  ((lastid = id - 1))
  echo "$((id++)) and $numbits $idxorig $lastid"
  ((lastid = id - 1))
  echo "$((id++)) ne 1 $lastid $idzero"
  ((idcond = id - 1))
  echo "$((id++)) inc $numbits $idsum"
  ((lastid = id - 1))
  echo "$((id++)) cond $numbits $idcond $lastid $idsum"
  ((idsum = id - 1))
done
((idresult2 = idsum))

echo "$((id++)) eq 1 $idresult1 $idresult2"
((lastid = id - 1))
echo "$((id++)) root 1 -$lastid"