summaryrefslogtreecommitdiff
path: root/src/sail_lib.ml
diff options
context:
space:
mode:
authorJon French2018-09-14 15:20:46 +0100
committerJon French2018-09-14 15:20:46 +0100
commit9f1b822cb91e17741d6f11a87a374e28ded2960a (patch)
treefa24d00e75c82edf0aa79a73041df8dc18a7d8f1 /src/sail_lib.ml
parent22ed9657ac5d8d1a06602ca8c6cf359786f03254 (diff)
Sail_lib.string_of_bits: print in decimal (properly, with bigints) rather than binary
Diffstat (limited to 'src/sail_lib.ml')
-rw-r--r--src/sail_lib.ml17
1 files changed, 16 insertions, 1 deletions
diff --git a/src/sail_lib.ml b/src/sail_lib.ml
index 1115b55d..03559fba 100644
--- a/src/sail_lib.ml
+++ b/src/sail_lib.ml
@@ -355,6 +355,12 @@ let char_of_bit = function
| B0 -> '0'
| B1 -> '1'
+let int_of_bit = function
+ | B0 -> 0
+ | B1 -> 1
+
+let bigint_of_bit b = Big_int.of_int (int_of_bit b)
+
let string_of_hex = function
| [B0; B0; B0; B0] -> "0"
| [B0; B0; B0; B1] -> "1"
@@ -374,10 +380,19 @@ let string_of_hex = function
| [B1; B1; B1; B1] -> "F"
| _ -> failwith "Cannot convert binary sequence to hex"
+
let string_of_bits bits =
if List.length bits mod 4 == 0
then "0x" ^ String.concat "" (List.map string_of_hex (break 4 bits))
- else "0b" ^ String.concat "" (List.map string_of_bit bits)
+ else
+ let place_values =
+ List.mapi
+ (fun i b -> (Big_int.mul (bigint_of_bit b) (Big_int.pow_int_positive 2 i)))
+ (List.rev bits)
+ in
+ let sum = List.fold_left Big_int.add Big_int.zero place_values in
+ Big_int.to_string sum
+
let hex_slice (str, n, m) =
let bits = List.concat (List.map hex_char (list_of_string (String.sub str 2 (String.length str - 2)))) in