[gccrs,COMMIT,2/2] intrinsic: Add tests for cttz and cttz_nonzero

Message ID 20260404022637.3094-2-gerris.rs@gmail.com
State New
Headers
Series [gccrs,COMMIT,1/2] intrinsic: Add cttz and cttz_nonzero intrinsics |

Checks

Context Check Description
linaro-tcwg-bot/tcwg_gcc_build--master-arm fail Patch failed to apply
linaro-tcwg-bot/tcwg_gcc_build--master-aarch64 fail Patch failed to apply

Commit Message

gerris.rs@gmail.com April 4, 2026, 2:26 a.m. UTC
  From: Mohamed Ali <mohmedali1462005@gmail.com>

gcc/testsuite/ChangeLog:

	* rust/compile/cttz.rs: New test.
	* rust/compile/cttz_nonzero.rs: New test.
	* rust/execute/torture/cttz.rs: New test.
	* rust/execute/torture/cttz_nonzero.rs: New test.

Signed-off-by: Mohamed Ali <mohmedali1462005@gmail.com>
---
This change was merged into the gccrs repository and is posted here for
upstream visibility and potential drive-by review, as requested by GCC
release managers.
Each commit email contains a link to its details on github from where you can
find the Pull-Request and associated discussions.


Commit on github: https://github.com/Rust-GCC/gccrs/commit/5c0e4d2c59c681f5f22d31173bf9c17202f60c80

The commit has been mentioned in the following pull-request(s):
 - https://github.com/Rust-GCC/gccrs/pull/4511

 gcc/testsuite/rust/compile/cttz.rs            | 17 ++++
 gcc/testsuite/rust/compile/cttz_nonzero.rs    | 19 ++++
 gcc/testsuite/rust/execute/torture/cttz.rs    | 97 +++++++++++++++++++
 .../rust/execute/torture/cttz_nonzero.rs      | 74 ++++++++++++++
 4 files changed, 207 insertions(+)
 create mode 100644 gcc/testsuite/rust/compile/cttz.rs
 create mode 100644 gcc/testsuite/rust/compile/cttz_nonzero.rs
 create mode 100644 gcc/testsuite/rust/execute/torture/cttz.rs
 create mode 100644 gcc/testsuite/rust/execute/torture/cttz_nonzero.rs
  

Patch

diff --git a/gcc/testsuite/rust/compile/cttz.rs b/gcc/testsuite/rust/compile/cttz.rs
new file mode 100644
index 000000000..b072167ac
--- /dev/null
+++ b/gcc/testsuite/rust/compile/cttz.rs
@@ -0,0 +1,17 @@ 
+// { dg-do compile }
+#![feature(intrinsics, lang_items, no_core)]
+#![no_core]
+
+#[lang = "sized"]
+pub trait Sized {}
+
+#[lang = "copy"]
+pub trait Copy {}
+
+extern "rust-intrinsic" {
+    pub fn cttz<T>(x: T) -> u32; // { dg-error "cttz intrinsics can only be used with basic integer types .got 'bool'." }
+}
+
+fn main() {
+    let _ = cttz(true);
+}
diff --git a/gcc/testsuite/rust/compile/cttz_nonzero.rs b/gcc/testsuite/rust/compile/cttz_nonzero.rs
new file mode 100644
index 000000000..210bd8bd2
--- /dev/null
+++ b/gcc/testsuite/rust/compile/cttz_nonzero.rs
@@ -0,0 +1,19 @@ 
+// { dg-do compile }
+#![feature(intrinsics, lang_items, no_core)]
+#![no_core]
+
+#[lang = "sized"]
+pub trait Sized {}
+
+#[lang = "copy"]
+pub trait Copy {}
+
+extern "rust-intrinsic" {
+    pub fn cttz_nonzero<T>(x: T) -> u32; // { dg-error "cttz intrinsics can only be used with basic integer types .got 'bool'." }
+}
+
+fn main() {
+    unsafe {
+        let _ = cttz_nonzero(true);
+    }
+}
diff --git a/gcc/testsuite/rust/execute/torture/cttz.rs b/gcc/testsuite/rust/execute/torture/cttz.rs
new file mode 100644
index 000000000..d9f4656de
--- /dev/null
+++ b/gcc/testsuite/rust/execute/torture/cttz.rs
@@ -0,0 +1,97 @@ 
+#![feature(no_core)]
+#![no_core]
+#![feature(intrinsics)]
+#![feature(lang_items)]
+
+#[lang = "sized"]
+pub trait Sized {}
+
+extern "rust-intrinsic" {
+    pub fn cttz<T>(x: T) -> u32;
+    pub fn abort() -> !;
+}
+
+fn main() -> i32 {
+    // cttz(0) must return bit_size per the Rust reference
+    if cttz(0u8) != 8 {
+        abort();
+    }
+    if cttz(1u8) != 0 {
+        abort();
+    }
+    if cttz(0xFFu8) != 0 {
+        abort();
+    }
+
+    if cttz(0u16) != 16 {
+        abort();
+    }
+    if cttz(1u16) != 0 {
+        abort();
+    }
+    if cttz(0xFFFFu16) != 0 {
+        abort();
+    }
+
+    if cttz(0u32) != 32 {
+        abort();
+    }
+    if cttz(1u32) != 0 {
+        abort();
+    }
+    if cttz(0xFFFFFFFFu32) != 0 {
+        abort();
+    }
+
+    if cttz(0u64) != 64 {
+        abort();
+    }
+    if cttz(1u64) != 0 {
+        abort();
+    }
+    if cttz(!0u64) != 0 {
+        abort();
+    }
+
+    if cttz(0i8) != 8 {
+        abort();
+    }
+    if cttz(1i8) != 0 {
+        abort();
+    }
+    if cttz(-1i8) != 0 {
+        abort();
+    }
+
+    if cttz(0i16) != 16 {
+        abort();
+    }
+    if cttz(1i16) != 0 {
+        abort();
+    }
+    if cttz(-1i16) != 0 {
+        abort();
+    }
+
+    if cttz(0i32) != 32 {
+        abort();
+    }
+    if cttz(1i32) != 0 {
+        abort();
+    }
+    if cttz(-1i32) != 0 {
+        abort();
+    }
+
+    if cttz(0i64) != 64 {
+        abort();
+    }
+    if cttz(1i64) != 0 {
+        abort();
+    }
+    if cttz(-1i64) != 0 {
+        abort();
+    }
+
+    0
+}
diff --git a/gcc/testsuite/rust/execute/torture/cttz_nonzero.rs b/gcc/testsuite/rust/execute/torture/cttz_nonzero.rs
new file mode 100644
index 000000000..749dffaf6
--- /dev/null
+++ b/gcc/testsuite/rust/execute/torture/cttz_nonzero.rs
@@ -0,0 +1,74 @@ 
+#![feature(no_core)]
+#![no_core]
+#![feature(intrinsics)]
+#![feature(lang_items)]
+
+#[lang = "sized"]
+pub trait Sized {}
+
+extern "rust-intrinsic" {
+    pub fn cttz_nonzero<T>(x: T) -> u32;
+    pub fn abort() -> !;
+}
+
+fn main() -> i32 {
+    unsafe {
+        if cttz_nonzero(1u8) != 0 {
+            abort();
+        }
+        if cttz_nonzero(0xFFu8) != 0 {
+            abort();
+        }
+
+        if cttz_nonzero(1u16) != 0 {
+            abort();
+        }
+        if cttz_nonzero(0xFFFFu16) != 0 {
+            abort();
+        }
+
+        if cttz_nonzero(1u32) != 0 {
+            abort();
+        }
+        if cttz_nonzero(0xFFFFFFFFu32) != 0 {
+            abort();
+        }
+
+        if cttz_nonzero(1u64) != 0 {
+            abort();
+        }
+        if cttz_nonzero(!0u64) != 0 {
+            abort();
+        }
+
+        if cttz_nonzero(1i8) != 0 {
+            abort();
+        }
+        if cttz_nonzero(-1i8) != 0 {
+            abort();
+        }
+
+        if cttz_nonzero(1i16) != 0 {
+            abort();
+        }
+        if cttz_nonzero(-1i16) != 0 {
+            abort();
+        }
+
+        if cttz_nonzero(1i32) != 0 {
+            abort();
+        }
+        if cttz_nonzero(-1i32) != 0 {
+            abort();
+        }
+
+        if cttz_nonzero(1i64) != 0 {
+            abort();
+        }
+        if cttz_nonzero(-1i64) != 0 {
+            abort();
+        }
+    }
+
+    0
+}