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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
[@@@ocaml.flambda_o3]
open Base
type t = Bits0.t
let number_of_data_bytes (t : t) = Bits0.number_of_data_bytes t
let unsafe_get_int64 = Bits0.unsafe_get_int64
let unsafe_set_int64 = Bits0.unsafe_set_int64
let get_int64 = Bits0.get_int64
let set_int64 = Bits0.set_int64
module Expert = struct
let unsafe_underlying_repr (t : t) = (t :> Bytes.t)
let offset_for_data = 8
end
module Mutable = struct
include Bits0
let number_of_data_bytes (t : t) = Bits0.number_of_data_bytes t
let is_empty a = width a = 0
let of_constant t = t
let to_constant t = t
let to_string t = Constant.to_binary_string t
let to_int t = Constant.to_int t
let copy ~src ~dst =
let words = words src in
for i = 0 to words - 1 do
unsafe_set_int64 dst i (unsafe_get_int64 src i)
done
;;
let wire _ = empty
let ( -- ) a _ = a
let vdd = of_constant (Constant.of_int ~width:1 1)
let gnd = of_constant (Constant.of_int ~width:1 0)
let ( &: ) c a b =
let words = words a in
for i = 0 to words - 1 do
let x = Int64.( land ) (unsafe_get_int64 a i) (unsafe_get_int64 b i) in
unsafe_set_int64 c i x
done
;;
let ( |: ) c a b =
let words = words a in
for i = 0 to words - 1 do
let x = Int64.( lor ) (unsafe_get_int64 a i) (unsafe_get_int64 b i) in
unsafe_set_int64 c i x
done
;;
let ( ^: ) c a b =
let words = words a in
for i = 0 to words - 1 do
let x = Int64.( lxor ) (unsafe_get_int64 a i) (unsafe_get_int64 b i) in
unsafe_set_int64 c i x
done
;;
let ( ~: ) c a =
let words = words a in
for i = 0 to words - 1 do
unsafe_set_int64 c i (Int64.( lxor ) (-1L) (unsafe_get_int64 a i))
done;
mask c
;;
external add_stub : t -> t -> t -> unit = "hardcaml_bits_add" [@@noalloc]
let[@inline always] rec add_iter words dst a_in b_in i carry =
if i = words
then ()
else (
let a = unsafe_get_int64 a_in i in
let b = unsafe_get_int64 b_in i in
let lo =
Int64.((a land 0xFFFF_FFFFL) + (b land 0xFFFF_FFFFL) + Int64.of_int carry)
in
let carry = Int64.(lo lsr 32) in
let lo = Int64.(lo land 0xFFFF_FFFFL) in
let a = Int64.(a lsr 32) in
let b = Int64.(b lsr 32) in
let hi = Int64.(a + b + carry) in
let carry = Int64.(hi lsr 32) in
unsafe_set_int64 dst i Int64.(lo lor (hi lsl 32));
add_iter words dst a_in b_in (i + 1) (Int64.to_int_trunc carry))
;;
let add_ocaml dst a b =
let words = words dst in
add_iter words dst a b 0 0;
mask dst
;;
let ( +: ) =
match Sys.backend_type with
| Native | Bytecode -> add_stub
| Other _ -> add_ocaml
;;
external sub_stub : t -> t -> t -> unit = "hardcaml_bits_sub" [@@noalloc]
let rec sub_iter words dst a_in b_in i borrow =
if i = words
then ()
else (
let a = unsafe_get_int64 a_in i in
let b = unsafe_get_int64 b_in i in
let lo =
Int64.((a land 0xFFFF_FFFFL) - (b land 0xFFFF_FFFFL) - Int64.of_int borrow)
in
let borrow = Int64.((lo lsr 32) land 1L) in
let lo = Int64.(lo land 0xFFFF_FFFFL) in
let a = Int64.(a lsr 32) in
let b = Int64.(b lsr 32) in
let hi = Int64.(a - b - borrow) in
let borrow = Int64.((hi lsr 32) land 1L) in
unsafe_set_int64 dst i Int64.(lo lor (hi lsl 32));
sub_iter words dst a_in b_in (i + 1) (Int64.to_int_trunc borrow))
;;
let sub_ocaml dst a b =
let words = words dst in
sub_iter words dst a b 0 0;
mask dst
;;
let ( -: ) =
match Sys.backend_type with
| Native | Bytecode -> sub_stub
| Other _ -> sub_ocaml
;;
let rec eq words i a b =
if i = words
then 1
else if Int64.( = ) (unsafe_get_int64 a i) (unsafe_get_int64 b i)
then eq words (i + 1) a b
else 0
;;
let ( ==: ) c a b =
let words = words a in
unsafe_set_int64 c 0 (Int64.of_int (eq words 0 a b))
;;
let rec neq words i a b =
if i = words
then 0L
else if Int64.( = ) (unsafe_get_int64 a i) (unsafe_get_int64 b i)
then neq words (i + 1) a b
else 1L
;;
let ( <>: ) c a b =
let words = words a in
unsafe_set_int64 c 0 (neq words 0 a b)
;;
let rec lt i a b =
if i < 0
then 0
else (
match
Stdlib.Int64.unsigned_compare (unsafe_get_int64 a i) (unsafe_get_int64 b i)
with
| -1 -> 1
| 0 -> lt (i - 1) a b
| _ -> 0)
;;
let ( <: ) c a b =
let words = words a in
if width a <= 63
then (
let result =
if Int64.( < ) (unsafe_get_int64 a 0) (unsafe_get_int64 b 0) then 1L else 0L
in
unsafe_set_int64 c 0 result)
else unsafe_set_int64 c 0 (Int64.of_int (lt (words - 1) a b))
;;
let[@cold] raise_mux_of_empty_list () =
raise_s [%message "Bits.mux unexpected empty list"]
;;
let rec mux_find idx n l =
match l with
| [] -> raise_mux_of_empty_list ()
| [ h ] -> h
| h :: t -> if idx = n then h else mux_find idx (n + 1) t
;;
let mux dst sel l =
let idx = to_int sel in
copy ~src:(mux_find idx 0 l) ~dst
;;
let cat2 a_width a b =
let b_width = width b in
let a_words = words_of_width a_width in
let b_words = words_of_width b_width in
let a_bits = a_width land width_mask in
if a_bits = 0
then
for i = 0 to b_words - 1 do
unsafe_set_int64 a (a_words + i) (unsafe_get_int64 b i)
done
else (
let x = ref (unsafe_get_int64 a (a_words - 1)) in
for i = 0 to b_words - 1 do
let y = unsafe_get_int64 b i in
unsafe_set_int64 a (a_words - 1 + i) Int64.(!x lor (y lsl a_bits));
x := Int64.O.(y lsr Int.(64 - a_bits))
done;
let num_bits_in_last_word = b_width land 63 in
let num_bits_in_last_word =
if num_bits_in_last_word = 0 then 64 else num_bits_in_last_word
in
if num_bits_in_last_word > 64 - a_bits
then unsafe_set_int64 a (a_words + b_words - 1) !x)
;;
let rec cat_iter_back width_ c l =
match l with
| [] -> ()
| h :: t ->
let width_ = width_ - width h in
cat_iter_back width_ c t;
cat2 width_ c h
;;
let concat c l = cat_iter_back (width c) c l
let concat_rev_array c l =
let acc_width = ref 0 in
for i = 0 to Array.length l - 1 do
let h = Array.unsafe_get l i in
let width = width h in
cat2 !acc_width c h;
acc_width := !acc_width + width
done
;;
let word w = w lsr log_bits_per_word
let select dst src h l =
let words = words dst in
let s_bits = l land width_mask in
let lo_word = word l in
let hi_word = word h in
if s_bits = 0
then
for i = 0 to words - 1 do
unsafe_set_int64 dst i (unsafe_get_int64 src (lo_word + i))
done
else (
let a = ref (unsafe_get_int64 src lo_word) in
for i = 0 to words - 1 do
let b =
if lo_word + i >= hi_word then 0L else unsafe_get_int64 src (lo_word + i + 1)
in
let x = Int64.O.((b lsl Int.O.(64 - s_bits)) lor (!a lsr s_bits)) in
unsafe_set_int64 dst i x;
a := b
done);
mask dst
;;
module Multiplication = struct
external umul : t -> t -> t -> unit = "hardcaml_bits_umul" [@@noalloc]
external smul : t -> t -> t -> unit = "hardcaml_bits_smul" [@@noalloc]
let[@inline always] unsafe_get32 d i =
Int64.( land ) (Int64.of_int32 (unsafe_get_int32 d i)) 0xFFFF_FFFFL
;;
let[@inline always] unsafe_set32 d i v = unsafe_set_int32 d i (Int32.of_int64_trunc v)
let umul_stub dst a b =
umul dst a b;
mask dst
;;
(** Array multiplication. *)
let rec umul_inner_loop w u v m i j k =
if i = m
then k
else (
let u' = unsafe_get32 u i in
let v' = unsafe_get32 v j in
let w' = unsafe_get32 w (i + j) in
let t = Int64.((u' * v') + w' + k) in
unsafe_set32 w (i + j) t;
umul_inner_loop w u v m (i + 1) j Int64.(t lsr 32))
;;
let rec umul_outer_loop w u v m n j =
if j = n
then ()
else (
let k = umul_inner_loop w u v m 0 j 0L in
unsafe_set32 w (j + m) k;
umul_outer_loop w u v m n (j + 1))
;;
let umul_core w u v m n = umul_outer_loop w u v m n 0
let mul_dst () =
let dst = ref (create 64) in
let setup_dst w =
if width !dst < w
then dst := create w
else
for i = 0 to words_of_width w - 1 do
unsafe_set_int64 !dst i 0L
done
in
dst, setup_dst
;;
let sign_extend width word =
let width = width land width_mask in
let top_bit_shift = width - 1 in
if width = 0 || Int64.equal Int64.(word land (1L lsl top_bit_shift)) 0L
then word
else Int64.((-1L lsl width) lor word)
;;
let smul_arg () =
let arg = ref (create 64) in
let setup_arg arg_in =
let width_arg = width arg_in in
if width !arg < width_arg then arg := create width_arg;
let words = words_of_width width_arg in
for i = 0 to words - 2 do
unsafe_set_int64 !arg i (unsafe_get_int64 arg_in i)
done;
unsafe_set_int64
!arg
(words - 1)
(sign_extend width_arg (unsafe_get_int64 arg_in (words - 1)))
in
arg, setup_arg
;;
let umul_ocaml =
let dst', setup_dst = mul_dst () in
fun dst a b ->
let dst_bits =
(words_of_width (width a) + words_of_width (width b)) * bits_per_word
in
setup_dst dst_bits;
umul_core !dst' a b (words a lsl 1) (words b lsl 1);
blit_data dst !dst';
mask dst
;;
let smul_stub dst a b =
smul dst a b;
mask dst
;;
let rec smul_signed_correct w v n m j b =
if j = n
then ()
else (
let jm = j + m in
let t = Int64.(unsafe_get32 w jm - unsafe_get32 v j - Int64.of_int_exn b) in
unsafe_set32 w jm t;
smul_signed_correct w v n m (j + 1) Int64.(t lsr 63 |> to_int_trunc))
;;
let smul_core w u v m n =
umul_core w u v m n;
if Int64.equal (Int64.( land ) (unsafe_get32 u (m - 1)) 0x8000_0000L) 0x8000_0000L
then smul_signed_correct w v n m 0 0;
if Int64.equal (Int64.( land ) (unsafe_get32 v (n - 1)) 0x8000_0000L) 0x8000_0000L
then smul_signed_correct w u m n 0 0
;;
let smul_ocaml =
let dst', setup_dst = mul_dst () in
let a', setup_a = smul_arg () in
let b', setup_b = smul_arg () in
fun dst a b ->
let dst_bits =
(words_of_width (width a) + words_of_width (width b)) * bits_per_word
in
setup_dst dst_bits;
setup_a a;
setup_b b;
smul_core !dst' !a' !b' (words a lsl 1) (words b lsl 1);
blit_data dst !dst';
mask dst
;;
end
let ( *: ) =
match Sys.backend_type with
| Native | Bytecode -> Multiplication.umul_stub
| Other _ -> Multiplication.umul_ocaml
;;
let ( *+ ) =
match Sys.backend_type with
| Native | Bytecode -> Multiplication.smul_stub
| Other _ -> Multiplication.smul_ocaml
;;
let num_words = words
let to_bits t =
let result = create (width t) in
copy ~src:t ~dst:result;
result
;;
let copy_bits = copy
module Comb = Comb.Make (struct
type t = Bits0.t
let equal = Bits0.Comparable.equal
let empty = empty
let is_empty = is_empty
let width = width
let of_constant = of_constant
let to_constant = to_constant
let add_widths w y = w + width y
let concat_msb l =
let w = List.fold l ~init:0 ~f:add_widths in
let c = create w in
concat c l;
c
;;
let mux sel l =
let idx = to_int sel in
mux_find idx 0 l
;;
let select s h l =
let w = h - l + 1 in
let c = create w in
select c s h l;
c
;;
let ( -- ) = ( -- )
let ( &: ) a b =
let c = create (width a) in
( &: ) c a b;
c
;;
let ( |: ) a b =
let c = create (width a) in
( |: ) c a b;
c
;;
let ( ^: ) a b =
let c = create (width a) in
( ^: ) c a b;
c
;;
let ( ~: ) a =
let c = create (width a) in
( ~: ) c a;
c
;;
let ( +: ) a b =
let c = create (width a) in
( +: ) c a b;
c
;;
let ( -: ) a b =
let c = create (width a) in
( -: ) c a b;
c
;;
let ( *: ) a b =
let c = create (width a + width b) in
( *: ) c a b;
c
;;
let ( *+ ) a b =
let c = create (width a + width b) in
( *+ ) c a b;
c
;;
let ( ==: ) a b =
let c = create 1 in
( ==: ) c a b;
c
;;
let ( <: ) a b =
let c = create 1 in
( <: ) c a b;
c
;;
let to_string = to_string
let sexp_of_t (s : t) = [%sexp (to_constant s |> Constant.to_binary_string : string)]
end)
end
include (Mutable.Comb : Comb.S with type t := Bits0.t)
include Bits0.Comparable
let to_int x = Constant.to_int x
let to_int32 x = Constant.to_int32 x
let zero w = Bits0.create w
let pp fmt t = Stdlib.Format.fprintf fmt "%s" (to_bstr t)
module _ = Pretty_printer.Register (struct
type nonrec t = Bits0.t
let module_name = "Hardcaml.Bits"
let to_string = to_bstr
end)