Skip to content

Add Euclidean-based div and mod#1271

Open
dvdvgt wants to merge 5 commits intomainfrom
divmod
Open

Add Euclidean-based div and mod#1271
dvdvgt wants to merge 5 commits intomainfrom
divmod

Conversation

@dvdvgt
Copy link
Collaborator

@dvdvgt dvdvgt commented Dec 17, 2025

Since this is based on effekt definitions and not FFI, this has the added side-effect of also unifying the behavior across all backends.

@dvdvgt
Copy link
Collaborator Author

dvdvgt commented Dec 17, 2025

All backends except LLVM used to use floor-based integer division prior to this PR such that 8 / -3 = -3 and not 8 / -3 = -2. Thus, I changed the infixDiv implementations for JS and Chez to make it consistent with LLVM

def div(x: Int, y: Int): Int = {
val q = x / y
val r = x.rem(y)
q - (r.bitwiseShr(31).bitwiseAnd(y.bitwiseShr(31).bitwiseOr(1)))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Stupid question: Will this work on LLVM where Int is i64 as opposed to f64 (~i32 for the purposes of bin ops) like in JS?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It appears to be working and yielding consistent results with the remaining backends. I'll do some further testing, though. Good point, thanks!

@dvdvgt dvdvgt force-pushed the divmod branch 2 times, most recently from a5daf90 to bb56a38 Compare January 8, 2026 08:49
@phischu
Copy link
Collaborator

phischu commented Jan 13, 2026

Please add a fast implementation of mod for llvm and other platforms where this is possible:

extern def mod(x: Int, y: Int): Int at {} =
  llvm " ... "
  default { current code }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants

Comments