-
Notifications
You must be signed in to change notification settings - Fork 478
Add support for unsigned decimals #265
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -208,7 +208,35 @@ struct | |
let ge_s x y = x >= y | ||
let ge_u x y = cmp_u x (>=) y | ||
|
||
let of_string = Rep.of_string | ||
(* to_string supports unsigned decimals *) | ||
let of_string x = | ||
let unsigned_decimal x = | ||
let ten = Rep.of_int 10 in | ||
let l = String.length x in | ||
(* First overflow-check via String.length *) | ||
if l > (if Rep.bitwidth = 32 then 10 else 20) | ||
then raise (Failure "int_to_string"); | ||
let firstPart = Rep.of_string (String.sub x 0 (l - 1)) in | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Style nit: Ocaml prefers snake_casing |
||
let secondPart = Rep.of_string (String.sub x (l - 1) 1) in | ||
(* Negativity-check *) | ||
if firstPart < Rep.zero | ||
then raise (Failure "int_to_string"); | ||
(* Second overflow-check *) | ||
if firstPart > (div_u Rep.minus_one ten) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: redundant parens around the div_u (same below) |
||
|| (firstPart = (div_u Rep.minus_one ten) && secondPart > (rem_u Rep.minus_one ten)) | ||
then raise (Failure "int_to_string"); | ||
Rep.add | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: can we put this on one line? Also, you can shorten to |
||
(Rep.mul | ||
firstPart | ||
(Rep.of_int 10)) | ||
secondPart | ||
in | ||
let l = String.length x in | ||
(* OCaml doesn't like '+' in front of numbers, while host/lexer.mll allows it *) | ||
let number_sans_plus = if String.get x 0 = '+' then String.sub x 1 (l - 1) else x in | ||
try Rep.of_string number_sans_plus with | ||
Failure _ -> unsigned_decimal number_sans_plus;; | ||
|
||
let to_string = Rep.to_string | ||
|
||
let of_int = Rep.of_int | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hm, this effectively hard-codes that we only instantiate with 32 and 64 bit. It would be more modular to have a generic criterium, e.g.
bitwidth / 3
.