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

ASR: add support for "nearest" #4024

Open
certik opened this issue May 17, 2024 · 1 comment
Open

ASR: add support for "nearest" #4024

certik opened this issue May 17, 2024 · 1 comment

Comments

@certik
Copy link
Contributor

certik commented May 17, 2024

Most likely this has to be a new expr node, something like RealNearest. Let's first figure out an LLVM (and WASM) instructions to compute it, as well as x86/arm assembly instructions, so that we know how to compute this. And then design a nice ASR node for this.

@Pranavchiku
Copy link
Contributor

Pranavchiku commented May 18, 2024

This is one way to do it in LLVM, although generated by ChatGPT, it looks reasonably correct.

Edit: Not sure though, will need to dig deeper.

; Declare the function signature
define double @nearest(double %x, double %s) {
entry:
    ; Get the sign bit of %s
    %sign_bit = fcmp olt double %s, 0.0

    ; Compute the positive nearest value
    %positive_nearest = call double @llvm.nearbyint.f64(double %x)

    ; Compute the negative nearest value
    %negative_nearest = call double @llvm.nearbyint.f64(double -%x)
    %negative_nearest = fneg double %negative_nearest

    ; Select the result based on the sign of %s
    %result = select i1 %sign_bit, double %negative_nearest, double %positive_nearest

    ; Return the result
    ret double %result
}

; Declare the LLVM intrinsic function for rounding to the nearest integer
declare double @llvm.nearbyint.f64(double)

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

No branches or pull requests

2 participants