From 4fab2bee9c914b95601d8c5ebd06834575e4ba6a Mon Sep 17 00:00:00 2001 From: Hunter Kvalevog Date: Sat, 3 Oct 2026 21:45:31 -0500 Subject: rust/asm: call rustc directly --- rust/asm/Cargo.lock | 32 -------------------------------- rust/asm/Cargo.toml | 9 --------- rust/asm/build.sh | 5 +++++ rust/asm/main.rs | 45 +++++++++++++++++++++++++++++++++++++++++++++ rust/asm/main_aarch64.S | 35 +++++++++++++++++++++++++++++++++++ rust/asm/src/main.rs | 45 --------------------------------------------- rust/asm/src/main_aarch64.S | 35 ----------------------------------- 7 files changed, 85 insertions(+), 121 deletions(-) delete mode 100644 rust/asm/Cargo.lock delete mode 100644 rust/asm/Cargo.toml create mode 100755 rust/asm/build.sh create mode 100644 rust/asm/main.rs create mode 100644 rust/asm/main_aarch64.S delete mode 100644 rust/asm/src/main.rs delete mode 100644 rust/asm/src/main_aarch64.S diff --git a/rust/asm/Cargo.lock b/rust/asm/Cargo.lock deleted file mode 100644 index 92ce097..0000000 --- a/rust/asm/Cargo.lock +++ /dev/null @@ -1,32 +0,0 @@ -# This file is automatically @generated by Cargo. -# It is not intended for manual editing. -version = 4 - -[[package]] -name = "cc" -version = "1.5.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f360145194ee8e21db5ee7f3fcd4fe52210864c75c985dae33218202c8bbe040" -dependencies = [ - "find-msvc-tools", - "shlex", -] - -[[package]] -name = "find-msvc-tools" -version = "0.1.14" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "aedcfb3409746eddb02b9e19ebda1c3394f759a152e48ee875a0844d1b955484" - -[[package]] -name = "rust-asm" -version = "0.1.0" -dependencies = [ - "cc", -] - -[[package]] -name = "shlex" -version = "2.0.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f8fadd59c855ef2080decdef8ff161eb6661b86933c9d82e5ba29dc602a55aba" diff --git a/rust/asm/Cargo.toml b/rust/asm/Cargo.toml deleted file mode 100644 index 87c91e7..0000000 --- a/rust/asm/Cargo.toml +++ /dev/null @@ -1,9 +0,0 @@ -[package] -name = "rust-asm" -version = "0.1.0" -edition = "2024" - -[dependencies] - -[build-dependencies] -cc = "*" diff --git a/rust/asm/build.sh b/rust/asm/build.sh new file mode 100755 index 0000000..0d51849 --- /dev/null +++ b/rust/asm/build.sh @@ -0,0 +1,5 @@ +#!/bin/sh +mkdir -p bin +cc -c main_aarch64.S -o bin/asm.o +ar rcs bin/libasm.a bin/asm.o +rustc --edition 2024 main.rs -L bin -l static=asm --out-dir bin diff --git a/rust/asm/main.rs b/rust/asm/main.rs new file mode 100644 index 0000000..7265deb --- /dev/null +++ b/rust/asm/main.rs @@ -0,0 +1,45 @@ +// ================================================================================================ +// Rust->assembly interop demo +// +// License: +// Copyright (c) 2026 Hunter Kvalevog +// +// Permission to use, copy, modify, and/or distribute this software for any +// purpose with or without fee is hereby granted. +// +// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES +// WITH REGARD TO THIS SOFTWARE. +// ================================================================================================ + +#[repr(C)] +struct Vec3 { + x: f32, + y: f32, + z: f32, +} + +impl Vec3 { + pub fn new(x: f32, y: f32, z: f32) -> Self { + Self { x, y, z } + } +} + +unsafe extern "C" { + fn add_u32(a: u32, b: u32) -> u32; + fn dot_f32v3(a: *const Vec3, b: *const Vec3) -> f32; +} + +fn test_add(a: u32, b: u32) { + let r = unsafe { add_u32(a, b) }; + println!("{a} + {b} = {r}"); +} + +fn test_dot(a: &Vec3, b: &Vec3) { + let r = unsafe { dot_f32v3(a, b) }; + println!("[{}, {}, {}] ⋅ [{}, {}, {}] = {}", a.x, a.y, a.z, b.x, b.y, b.z, r); +} + +fn main() { + test_add(1, 2); + test_dot(&Vec3::new(1.0, 2.0, 3.0), &Vec3::new(4.0, 5.0, 6.0)); +} diff --git a/rust/asm/main_aarch64.S b/rust/asm/main_aarch64.S new file mode 100644 index 0000000..191df8c --- /dev/null +++ b/rust/asm/main_aarch64.S @@ -0,0 +1,35 @@ +// ================================================================================================ +// Rust->assembly interop demo +// +// License: +// Copyright (c) 2026 Hunter Kvalevog +// +// Permission to use, copy, modify, and/or distribute this software for any +// purpose with or without fee is hereby granted. +// +// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES +// WITH REGARD TO THIS SOFTWARE. +// ================================================================================================ + +#include "../../_common/aarch64/common.S" + +TEXT_SEGMENT + +.global SYM(add_u32) +.global SYM(dot_f32v3) + +SYM(add_u32): + add w0, w0, w1 + ret + +SYM(dot_f32v3): + ldp s1, s2, [x0] // a.x, a.y + ldp s3, s4, [x1] // b.x, b.y + ldr s5, [x0, #8] // a.z + ldr s6, [x1, #8] // b.z + + fmul s0, s1, s3 // a.x * b.x + fmadd s0, s2, s4, s0 // + a.y * b.y + fmadd s0, s5, s6, s0 // + a.z * b.z + + ret diff --git a/rust/asm/src/main.rs b/rust/asm/src/main.rs deleted file mode 100644 index 7265deb..0000000 --- a/rust/asm/src/main.rs +++ /dev/null @@ -1,45 +0,0 @@ -// ================================================================================================ -// Rust->assembly interop demo -// -// License: -// Copyright (c) 2026 Hunter Kvalevog -// -// Permission to use, copy, modify, and/or distribute this software for any -// purpose with or without fee is hereby granted. -// -// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES -// WITH REGARD TO THIS SOFTWARE. -// ================================================================================================ - -#[repr(C)] -struct Vec3 { - x: f32, - y: f32, - z: f32, -} - -impl Vec3 { - pub fn new(x: f32, y: f32, z: f32) -> Self { - Self { x, y, z } - } -} - -unsafe extern "C" { - fn add_u32(a: u32, b: u32) -> u32; - fn dot_f32v3(a: *const Vec3, b: *const Vec3) -> f32; -} - -fn test_add(a: u32, b: u32) { - let r = unsafe { add_u32(a, b) }; - println!("{a} + {b} = {r}"); -} - -fn test_dot(a: &Vec3, b: &Vec3) { - let r = unsafe { dot_f32v3(a, b) }; - println!("[{}, {}, {}] ⋅ [{}, {}, {}] = {}", a.x, a.y, a.z, b.x, b.y, b.z, r); -} - -fn main() { - test_add(1, 2); - test_dot(&Vec3::new(1.0, 2.0, 3.0), &Vec3::new(4.0, 5.0, 6.0)); -} diff --git a/rust/asm/src/main_aarch64.S b/rust/asm/src/main_aarch64.S deleted file mode 100644 index 56403d5..0000000 --- a/rust/asm/src/main_aarch64.S +++ /dev/null @@ -1,35 +0,0 @@ -// ================================================================================================ -// Rust->assembly interop demo -// -// License: -// Copyright (c) 2026 Hunter Kvalevog -// -// Permission to use, copy, modify, and/or distribute this software for any -// purpose with or without fee is hereby granted. -// -// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES -// WITH REGARD TO THIS SOFTWARE. -// ================================================================================================ - -#include "../../../_common/aarch64/common.S" - -TEXT_SEGMENT - -.global SYM(add_u32) -.global SYM(dot_f32v3) - -SYM(add_u32): - add w0, w0, w1 - ret - -SYM(dot_f32v3): - ldp s1, s2, [x0] // a.x, a.y - ldp s3, s4, [x1] // b.x, b.y - ldr s5, [x0, #8] // a.z - ldr s6, [x1, #8] // b.z - - fmul s0, s1, s3 // a.x * b.x - fmadd s0, s2, s4, s0 // + a.y * b.y - fmadd s0, s5, s6, s0 // + a.z * b.z - - ret -- cgit v1.2.3