Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
F
frama-c
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Container Registry
Model registry
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
pub
frama-c
Commits
85989c7c
Commit
85989c7c
authored
6 years ago
by
Loïc Correnson
Committed by
Virgile Prevosto
6 years ago
Browse files
Options
Downloads
Patches
Plain Diff
[lib] hex and bin formatters for integers
parent
b71fe2d1
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/libraries/stdlib/integer.ml
+68
-0
68 additions, 0 deletions
src/libraries/stdlib/integer.ml
src/libraries/stdlib/integer.mli
+14
-1
14 additions, 1 deletion
src/libraries/stdlib/integer.mli
with
82 additions
and
1 deletion
src/libraries/stdlib/integer.ml
+
68
−
0
View file @
85989c7c
...
...
@@ -120,6 +120,74 @@ let popcount = Z.popcount
try
Z
.
of_float
z
with
Z
.
Overflow
->
raise
Too_big
let
bdigits
=
[
|
"0000"
;
(* 0 *)
"0001"
;
(* 1 *)
"0010"
;
(* 2 *)
"0011"
;
(* 3 *)
"0100"
;
(* 4 *)
"0101"
;
(* 5 *)
"0110"
;
(* 6 *)
"0111"
;
(* 7 *)
"1000"
;
(* 8 *)
"1001"
;
(* 9 *)
"1010"
;
(* 10 *)
"1011"
;
(* 11 *)
"1100"
;
(* 12 *)
"1101"
;
(* 13 *)
"1110"
;
(* 14 *)
"1111"
;
(* 15 *)
|
]
let
pp_bin_pos
fmt
r
=
Format
.
pp_print_string
fmt
bdigits
.
(
r
)
let
pp_bin_neg
fmt
r
=
Format
.
pp_print_string
fmt
bdigits
.
(
15
-
r
)
let
pp_hex_pos
fmt
r
=
Format
.
fprintf
fmt
"%04X"
r
let
pp_hex_neg
fmt
r
=
Format
.
fprintf
fmt
"%04X"
(
0xFFFF
-
r
)
let
bmask_bin
=
Z
.
of_int
15
let
bmask_hex
=
Z
.
of_int
0xFFFF
type
digits
=
{
nbits
:
int
;
(* max number of bits *)
bsize
:
int
;
(* bits in each bloc *)
bmask
:
Z
.
t
;
(* block mask *)
sep
:
string
;
pp
:
Format
.
formatter
->
int
->
unit
;
(* print one block *)
}
let
rec
pp_digits
d
fmt
n
v
=
if
gt
v
zero
||
n
<
d
.
nbits
then
begin
let
r
=
Z
.
to_int
(
Z
.
logand
v
d
.
bmask
)
in
let
k
=
d
.
bsize
in
pp_digits
d
fmt
(
n
+
k
)
(
Z
.
shift_right_trunc
v
k
)
;
if
gt
v
d
.
bmask
||
(
n
+
k
)
<
d
.
nbits
then
Format
.
pp_print_string
fmt
d
.
sep
;
d
.
pp
fmt
r
;
end
let
pp_bin
?
(
nbits
=
0
)
?
(
sep
=
""
)
fmt
v
=
if
le
zero
v
then
(
Format
.
pp_print_string
fmt
"0b"
;
pp_digits
{
nbits
;
sep
;
bsize
=
4
;
bmask
=
bmask_bin
;
pp
=
pp_bin_pos
}
fmt
0
v
)
else
(
Format
.
pp_print_string
fmt
"1b"
;
pp_digits
{
nbits
;
sep
;
bsize
=
4
;
bmask
=
bmask_bin
;
pp
=
pp_bin_neg
}
fmt
0
(
Z
.
lognot
v
)
)
let
pp_hex
?
(
nbits
=
0
)
?
(
sep
=
""
)
fmt
v
=
if
le
zero
v
then
(
Format
.
pp_print_string
fmt
"0x"
;
pp_digits
{
nbits
;
sep
;
bsize
=
16
;
bmask
=
bmask_hex
;
pp
=
pp_hex_pos
}
fmt
0
v
)
else
(
Format
.
pp_print_string
fmt
"1x"
;
pp_digits
{
nbits
;
sep
;
bsize
=
16
;
bmask
=
bmask_hex
;
pp
=
pp_hex_neg
}
fmt
0
(
Z
.
lognot
v
)
)
let
pretty
?
(
hexa
=
false
)
fmt
v
=
let
rec
aux
v
=
if
gt
v
two_power_60
then
...
...
This diff is collapsed.
Click to expand it.
src/libraries/stdlib/integer.mli
+
14
−
1
View file @
85989c7c
...
...
@@ -150,8 +150,21 @@ val popcount: t -> int
val
pretty
:
?
hexa
:
bool
->
t
Pretty_utils
.
formatter
val
pp_bin
:
?
nbits
:
int
->
?
sep
:
string
->
t
Pretty_utils
.
formatter
(** Print binary format. Digits are output by blocs of 4 bits
separated by [~sep] with at least [~nbits] total bits.
Positive values are preffixed with ["0b"] and negative values
are printed as their 2-complement ([lnot]) with prefix ["1b"]. *)
val
pp_hex
:
?
nbits
:
int
->
?
sep
:
string
->
t
Pretty_utils
.
formatter
(** Print hexadecimal format. Digits are output by blocs of 16 bits
(4 hex digits) separated by [~sep] with at least [~nbits] total bits.
Positive values are preffixed with ["0x"] and negative values
are printed as their 2-complement ([lnot]) with prefix ["1x"]. *)
(*
Local Variables:
compile-command: "make -C ../../.."
End:
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment