Skip to content
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

arithmetic with carry #1162

Open
porcuquine opened this issue Feb 21, 2024 · 0 comments
Open

arithmetic with carry #1162

porcuquine opened this issue Feb 21, 2024 · 0 comments
Labels
CO-Compilers Compilers track

Comments

@porcuquine
Copy link
Collaborator

In order to be able to implement bignums in Lurk directly, we need support for multiplication with carry. Although we can detect overflow for addition, it would be better to have equivalent direct support.

I propose we add new variants of + and * that only work with u64 (and other UInt types, in the future). We might want a variant for - as well. Again, * is the urgent need.

I'll propose that we name these these operators *~, +~, and -~ but I'm open to other suggestions. These should return a pair of u64 values. The first value should be the same value as is returned by * and + respectively. The second should contain the 'carry' values or nil. The reason to prefer nil to an explicit 0u64 is to make checking for carry as cheap as possible.

See example usages with expected results:

user> (*~ (- 0u64 1u64) 123)
[? iterations] => (0xffffffffffffff85 . 122)

user> (*~ (- 0u64 1u64) 1u64)
[? iterations] => (0xffffffffffffffffu64)

user> (+~ (- 0u64 1u64) 10u64)
[? iterations] => (9u64 . 1)

user> (+~ 1u64 1u64)
[? iterations] => (2u64)

user> (-~ 10u64 1u64)
[? iterations] => (9u64)

user> (-~ 1u64 10u64)
[? iterations] => (0xfffffffffffffff7 . 1)
@porcuquine porcuquine added the CO-Compilers Compilers track label Feb 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CO-Compilers Compilers track
Projects
None yet
Development

No branches or pull requests

1 participant