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

compiler-rt: alu: add saturated shift left for i8 #134 #140

Merged
merged 5 commits into from
Feb 10, 2025

Conversation

wzmuda
Copy link
Contributor

@wzmuda wzmuda commented Feb 3, 2025

Summary

Implement the following polyfills:

  • __llvm_ushl_sat_i8_i8
  • __llvm_sshl_sat_i8_i8

Also do some minor refactoring:

  • ashr for i8 had unnecessarily complicated negative test case, so simplify it,
  • shl for i128 has unnecessary complicated implementation, so simplify it,
  • move sign extension to an utility function instead of a closure in smul.

Details

I couldn't figure out how llvm.sshl.sat.i8 actually works so I wrote this small program to generate test cases and then I figured out the logic based on them. I did the same trick for the unsigned version to validate my understanding too.

declare i8 @llvm.sshl.sat.i8(i8, i8) #0

define i8 @sshl_sat_i8(i8 %a, i8 %b) #0 {
  %1 = call i8 @llvm.sshl.sat.i8(i8 %a, i8 %b)
  ret i8 %1
}

attributes #0 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
#include <stdio.h>
#include <stdint.h>

extern uint8_t sshl_sat_i8(uint8_t a, uint8_t b);

void print_binary(uint8_t value) {
    printf("0b");
    for(int i = 7; i >= 0; i--){
        printf("%u", (value >> i) & 1);
    }
}

int main() {
    uint8_t values[] = {128, 255, 127, 1, 15, 0, 0xaa, 0x55};
    for(size_t v = 0; v < sizeof(values)/sizeof(values[0]); v++) {
        uint8_t lhs = values[v];
        for(int rhs = 0; rhs <= 7; rhs++) {
            uint8_t expected = sshl_sat_i8(lhs, rhs);
            printf("TestCaseTwoArgs { lhs: ");
            print_binary(lhs);
            printf(", rhs: %d, expected: ", rhs);
            print_binary(expected);
            printf(" },\n");
        }
        printf("\n");
    }

    return 0;
}
clang -c sshl_sat.c -o sshl_sat.o
clang -c sshl_sat.ll -o sshl_sat.ll.o
clang sshl_sat.o sshl_sat.ll.o -o sshl_sat

Checklist

  • Code is formatted by Rustfmt or scarb fmt.
  • Documentation has been updated if necessary.

@wzmuda wzmuda force-pushed the wz/8b-arith-shl-sat branch 3 times, most recently from c64abd4 to ab78024 Compare February 9, 2025 23:15
Remove the custom implementation for 128-bit inputs, as it's enough to
use overflowing multiplication in the generic implementation. All tests
pass.

Signed-off-by: Wojciech Zmuda <[email protected]>
The test is considered passed at the very first panic that occurs.
Remove the array of test cases and leave just one case, because the
other cases did not participate in the test.

Signed-off-by: Wojciech Zmuda <[email protected]>
@wzmuda wzmuda force-pushed the wz/8b-arith-shl-sat branch from ab78024 to eb48b0b Compare February 10, 2025 20:20
@wzmuda wzmuda marked this pull request as ready for review February 10, 2025 20:40
@wzmuda wzmuda requested a review from a team as a code owner February 10, 2025 20:40
Copy link
Collaborator

@iamrecursion iamrecursion left a comment

Choose a reason for hiding this comment

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

Looks good, but I have some very crucial asks for documentation.

compiler-rt/src/alu/sshl_sat.cairo Show resolved Hide resolved
compiler-rt/src/alu/ushl_sat.cairo Show resolved Hide resolved
compiler-rt/src/utils.cairo Outdated Show resolved Hide resolved
Implement the following polyfill:
- `__llvm_ushl_sat_i8_i8`

Signed-off-by: Wojciech Zmuda <[email protected]>
Sign extension is useful when dealing with signed version of polyfills
so move it from a closure to a public function so it can be accessed
elsewhere.

Signed-off-by: Wojciech Zmuda <[email protected]>
Implement the following polyfill:
- `__llvm_sshl_sat_i8_i8`

Signed-off-by: Wojciech Zmuda <[email protected]>
@wzmuda wzmuda force-pushed the wz/8b-arith-shl-sat branch from eb48b0b to e9312da Compare February 10, 2025 22:31
Copy link
Collaborator

@iamrecursion iamrecursion left a comment

Choose a reason for hiding this comment

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

LGTM!

compiler-rt/src/alu/sshl_sat.cairo Show resolved Hide resolved
@wzmuda wzmuda merged commit 60bd614 into main Feb 10, 2025
10 checks passed
@wzmuda wzmuda deleted the wz/8b-arith-shl-sat branch February 10, 2025 22:56
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.

2 participants