================ @@ -3680,6 +3680,37 @@ the arguments. Both arguments and the result have the bitwidth specified by the name of the builtin. These builtins can be used within constant expressions. +``__builtin_stdc_rotate_left`` and ``__builtin_stdc_rotate_right`` +------------------------------------------------------------------ + +**Syntax**: + +.. code-block:: c + + __builtin_stdc_rotate_left(value, count) + __builtin_stdc_rotate_right(value, count) + +**Description**: + +These builtins rotate the bits in ``value`` by ``count`` positions. The +``__builtin_stdc_rotate_left`` builtin rotates bits to the left, while +``__builtin_stdc_rotate_right`` rotates bits to the right. These builtins +accept any unsigned integer type, including ``_BitInt`` types. The rotation +count is treated as an unsigned value and taken modulo the bit-width of the +value being rotated. Negative rotation counts are converted to their unsigned +equivalent before the modulo operation. These builtins can be used within ---------------- chaitanyav wrote:
Tested it thoroughly [test_rotate_1.txt](https://github.com/user-attachments/files/22571013/test_rotate_1.txt) ```bash Testing edge cases for __builtin_stdc_rotate_{left,right} ======================================================= Testing large _BitInt types... _BitInt(64) rotate left by 63: PASS _BitInt(96) rotate left by 95: PASS _BitInt(128) rotate left by 127: PASS Testing odd bit widths... _BitInt(37) rotate left by 36: PASS _BitInt(73) rotate left by 72: PASS _BitInt(127) rotate left by 126: PASS Testing extreme rotation amounts... Rotate by UINT_MAX: PASS Rotate by INT_MIN: PASS Rotate by 10000: PASS Testing boundary rotations... Rotate by 0: PASS Rotate by bit width (32): PASS Rotate by 2x bit width (64): PASS Rotate left by -1 vs rotate right by 1: PASS Testing small _BitInt types... _BitInt(1) rotate: PASS _BitInt(2) rotate left by 1: PASS _BitInt(3) rotate left by 2: PASS Testing pattern verification... Alternating pattern rotate: PASS All-ones pattern rotate: PASS Single bit walking: PASS Testing left/right rotation consistency... Left/right consistency: PASS Left/right equivalence: PASS Running performance stress test... Performance stress test (10000 rotations): FAIL Testing maximum _BitInt(128) values... _BitInt(128) high bit rotate: PASS _BitInt(128) lower bits rotate: PASS Testing zero and minimal bit cases... Large shift on small type: PASS Negative rotation on small type: PASS ======================================================= All tests completed. ``` ### __BitInt tests [test_rotate_2.txt](https://github.com/user-attachments/files/22571014/test_rotate_2.txt) ```bash Testing both compile-time (static assertions) and runtime paths Static assertions passed - compile-time evaluation works! === Runtime Edge Case Tests === PASS: Large shift: 341 << 1000 PASS: Large negative: 341 << -1000 PASS: Max value rotate PASS: Zero rotate === Different Bit Widths Runtime Tests === PASS: 3-bit rotate PASS: 5-bit rotate PASS: 7-bit rotate PASS: 10-bit rotate PASS: 13-bit rotate === Comprehensive Negative Shift Tests === rotate_left(341, -1) = 426 rotate_left(341, -2) = 213 rotate_left(341, -3) = 362 rotate_left(341, -4) = 181 rotate_left(341, -5) = 346 rotate_left(341, -6) = 173 rotate_left(341, -7) = 342 rotate_left(341, -8) = 171 rotate_left(341, -9) = 341 rotate_left(341, -10) = 426 rotate_left(341, -11) = 213 rotate_left(341, -12) = 362 rotate_left(341, -13) = 181 rotate_left(341, -14) = 346 rotate_left(341, -15) = 173 rotate_left(341, -16) = 342 rotate_left(341, -17) = 171 rotate_left(341, -18) = 341 rotate_left(341, -19) = 426 rotate_left(341, -20) = 213 === Comprehensive Modulo Reduction Tests === rotate_left(341, 100) = 171 (reduced: 1) rotate_left(341, 1000) = 171 (reduced: 1) rotate_left(341, 10000) = 171 (reduced: 1) rotate_left(341, 50000) = 181 (reduced: 5) rotate_left(341, 1073741823) = 341 (reduced: 0) === Cross-Direction Equivalence Tests === rotate_left(341, -1) = 426, rotate_right(341, 1) = 426 rotate_left(341, -2) = 213, rotate_right(341, 2) = 213 rotate_left(341, -3) = 362, rotate_right(341, 3) = 362 rotate_left(341, -4) = 181, rotate_right(341, 4) = 181 rotate_left(341, -5) = 346, rotate_right(341, 5) = 346 rotate_left(341, -6) = 173, rotate_right(341, 6) = 173 rotate_left(341, -7) = 342, rotate_right(341, 7) = 342 rotate_left(341, -8) = 171, rotate_right(341, 8) = 171 rotate_left(341, -9) = 341, rotate_right(341, 9) = 341 rotate_left(341, -10) = 426, rotate_right(341, 10) = 426 rotate_left(341, -11) = 213, rotate_right(341, 11) = 213 rotate_left(341, -12) = 362, rotate_right(341, 12) = 362 rotate_left(341, -13) = 181, rotate_right(341, 13) = 181 rotate_left(341, -14) = 346, rotate_right(341, 14) = 346 rotate_left(341, -15) = 173, rotate_right(341, 15) = 173 rotate_left(341, -16) = 342, rotate_right(341, 16) = 342 rotate_left(341, -17) = 171, rotate_right(341, 17) = 171 rotate_left(341, -18) = 341, rotate_right(341, 18) = 341 rotate_left(341, -19) = 426, rotate_right(341, 19) = 426 rotate_left(341, -20) = 213, rotate_right(341, 20) = 213 === Large Bit Width Stress Tests === PASS: 37-bit large value PASS: 61-bit large value 37-bit rotate by 10000: result computed === Large Shifts on Small Bit Widths === PASS: 8-bit by 64 PASS: 8-bit by 65 PASS: 8-bit by 1000 PASS: 3-bit by 64 PASS: 3-bit by 1000 PASS: 16-bit by 65536 PASS: 16-bit by 65537 PASS: 8-bit by -64 PASS: 8-bit by -1000 Testing shift amounts much larger than bit widths: PASSED 🎉 ALL COMPREHENSIVE TESTS PASSED! 🎉 Both compile-time and runtime rotation implementations are correct! Tested bit widths: 3, 5, 6, 7, 8, 9, 10, 11, 12, 13, 16, 37, 61 Tested shift ranges: -50000 to +65537 Tested edge cases: zero, max values, power-of-2, boundary conditions ``` https://github.com/llvm/llvm-project/pull/160259 _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
