-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbin_util.pas
205 lines (168 loc) · 3.87 KB
/
bin_util.pas
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
unit Bin_Util;
interface
type
Float_16 = word;
Function decompress_Float_16(value : Float_16) : single;
Function BitToLeft(DW : DWord ; N :integer) : DWord;
Function BitToRight(DW : DWord ; N :integer) : DWord;
Function ORL(DW1, DW2 : DWord) : DWord;
Function ANDL(DW1, DW2 : DWord) : DWord;
Function XORL(DW1, DW2 : DWord) : DWord;
implementation
const
Tab_Bit : array[0..pred(8*4)] of DWord =
($1, $2, $4, $8, $10, $20, $40, $80,
$100, $200, $400, $800, $1000, $2000, $4000, $8000,
$10000, $20000, $40000, $80000, $100000, $200000, $400000, $800000,
$1000000, $2000000, $4000000, $8000000, $10000000, $20000000, $40000000, $80000000);
type
T_Bool32 = array[0..pred(8*4)] of boolean;
{ décale les bit de DW de N vers la gauche }
Function BitToLeft(DW : DWord ; N :integer) : DWord;
var
i : integer;
begin
for i:=1 to N do DW:=DW*2;
BitToLeft:=DW;
end;
{ décale les bit de DW de N vers la droite }
Function BitToRight(DW : DWord ; N :integer) : DWord;
var
i : integer;
begin
for i:=1 to N do DW:=DW div 2;
BitToRight:=DW;
end;
{ retourne un OR logique bit à bit entre DW1 et DW2 }
Function ORL(DW1, DW2 : DWord) : DWord;
var
DW3 : DWord;
Bool1, Bool2 : T_Bool32;
i : integer;
begin
for i:=0 to 31 do begin
{ remplir Bool1 }
if DW1>=Tab_Bit[31-i] then begin
Bool1[31-i]:=true;
dec(DW1,Tab_Bit[31-i]);
end
else
Bool1[31-i]:=false;
{ remplir Bool2 }
if DW2>=Tab_Bit[31-i] then begin
Bool2[31-i]:=true;
dec(DW2,Tab_Bit[31-i]);
end
else
Bool2[31-i]:=false;
end;
DW3:=0;
for i:=0 to 31 do
if (Bool1[i] or Bool2[i]) then inc(DW3,Tab_Bit[i]);
ORL:=DW3;
end;
{ retourne un AND logique bit à bit entre DW1 et DW2 }
Function ANDL(DW1, DW2 : DWord) : DWord;
var
DW3 : DWord;
Bool1, Bool2 : T_Bool32;
i : integer;
begin
for i:=0 to 31 do begin
{ remplir Bool1 }
if DW1>=Tab_Bit[31-i] then begin
Bool1[31-i]:=true;
dec(DW1,Tab_Bit[31-i]);
end
else
Bool1[31-i]:=false;
{ remplir Bool2 }
if DW2>=Tab_Bit[31-i] then begin
Bool2[31-i]:=true;
dec(DW2,Tab_Bit[31-i]);
end
else
Bool2[31-i]:=false;
end;
DW3:=0;
for i:=0 to 31 do
if (Bool1[i] and Bool2[i]) then inc(DW3,Tab_Bit[i]);
ANDL:=DW3;
end;
{ retourne un XOR logique bit à bit entre DW1 et DW2 }
Function XORL(DW1, DW2 : DWord) : DWord;
var
DW3 : DWord;
Bool1, Bool2 : T_Bool32;
i : integer;
begin
for i:=0 to 31 do begin
{ remplir Bool1 }
if DW1>=Tab_Bit[31-i] then begin
Bool1[31-i]:=true;
dec(DW1,Tab_Bit[31-i]);
end
else
Bool1[31-i]:=false;
{ remplir Bool2 }
if DW2>=Tab_Bit[31-i] then begin
Bool2[31-i]:=true;
dec(DW2,Tab_Bit[31-i]);
end
else
Bool2[31-i]:=false;
end;
DW3:=0;
for i:=0 to 31 do
if (Bool1[i] xor Bool2[i]) then inc(DW3,Tab_Bit[i]);
XORL:=DW3;
end;
Function decompress_Float_16(value : Float_16) : single;
var
IVF1 : word absolute value;
S,E,M : DWord;
R : single absolute S;
ABCDEFG : string;
begin
S:=ANDL(BitToRight(IVF1,15), $00000001);
E:=ANDL(BitToRight(IVF1,10), $0000001F);
M:=ANDL(IVF1, $000003FF);
if E=0 then begin
if m=0 then begin
decompress_Float_16:=BitToLeft(S,31);
exit;
end
else
begin
while ANDL(M,$00000400)=0 do begin
M:=BitToLeft(M,1);
if E > 0 then begin
dec(E);
end;
end;
end;
end
else
begin
if E=31 then begin
if M=0 then begin
{ inf }
decompress_Float_16:=0;
exit;
end
else
begin
{ nan }
decompress_Float_16:=0;
exit;
end;
end;
end;
S:=BitToLeft(S,31);
E:=BitToLeft((E+127)-15,23);
M:=BitToLeft(M,13);
S:=ORL(ORL(S,E),M);
decompress_Float_16:=R;
end;
begin
end.