Skip to content

Commit

Permalink
Fix fn-impl
Browse files Browse the repository at this point in the history
  • Loading branch information
byeongkeunahn committed Oct 17, 2024
1 parent db2528e commit 32cf026
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 18 deletions.
37 changes: 25 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -627,8 +627,7 @@ basm/src/solution.rs를 다음과 같이 수정합니다.
use alloc::{format, string::String, vec::Vec};
use basm::serialization::Pair;
use basm_macro::{basm_export, basm_import};
pub fn main() {
}
pub fn main() {}

basm_import! {
fn guess(b: String) -> Pair::<i32, i32>;
Expand All @@ -640,24 +639,26 @@ static mut N: i32 = 0;
#[basm_export]
fn init(_t: i32, n: i32) {
unsafe {
ALL.clear();
(*core::ptr::addr_of_mut!(ALL)).clear();
N = n;
let pow10_n = 10i32.pow(n as u32);
'outer: for i in 0..pow10_n {
let mut digits = [false; 10];
let mut j = i;
for _ in 0..n {
let d = (j % 10) as usize;
if digits[d] { continue 'outer; }
if digits[d] {
continue 'outer;
}
digits[d] = true;
j /= 10;
}
ALL.push(i);
(*core::ptr::addr_of_mut!(ALL)).push(i);
}
}
}

fn check(mut x: i32, mut y: i32) -> Pair::<i32, i32> {
fn check(mut x: i32, mut y: i32) -> Pair<i32, i32> {
let mut digits_x = [false; 10];
let mut digits_y = [false; 10];
let mut strikes = 0;
Expand All @@ -666,9 +667,15 @@ fn check(mut x: i32, mut y: i32) -> Pair::<i32, i32> {
for _ in 0..N {
let d_x = (x % 10) as usize;
let d_y = (y % 10) as usize;
if d_x == d_y { strikes += 1; }
if digits_x[d_y] { balls += 1; }
if digits_y[d_x] { balls += 1; }
if d_x == d_y {
strikes += 1;
}
if digits_x[d_y] {
balls += 1;
}
if digits_y[d_x] {
balls += 1;
}
digits_x[d_x] = true;
digits_y[d_y] = true;
x /= 10;
Expand All @@ -680,13 +687,19 @@ fn check(mut x: i32, mut y: i32) -> Pair::<i32, i32> {

#[basm_export]
fn game() {
let mut all = unsafe { ALL.clone() };
let mut all = unsafe { (*core::ptr::addr_of!(ALL)).clone() };
let n = unsafe { N };
loop {
let query = all[0];
let query_str = if n == 3 { format!("{:03}", query) } else { format!("{:04}", query) };
let query_str = if n == 3 {
format!("{:03}", query)
} else {
format!("{:04}", query)
};
let out = guess(query_str);
if out == Pair(n, 0) { break; }
if out == Pair(n, 0) {
break;
}
let mut all_new = Vec::new();
for x in all {
if check(x, query) == out {
Expand Down
6 changes: 4 additions & 2 deletions basm-macro/src/export.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ pub fn export_impl(_attr: TokenStream, item: TokenStream) -> TokenStream {
let out = quote! {
#[allow(clippy::ptr_arg)]
#itemfn
#[allow(non_snake_case)]
mod #basm_export_mod {
#[allow(non_snake_case)]
mod #internals {
pub static mut SER_VEC: alloc::vec::Vec::<u8> = alloc::vec::Vec::<u8>::new();

Expand All @@ -51,14 +53,14 @@ pub fn export_impl(_attr: TokenStream, item: TokenStream) -> TokenStream {
pub unsafe extern "C" fn free() { SER_VEC.clear() }

#[cfg(target_arch = "x86_64")]
#[no_mangle]
#[unsafe(no_mangle)]
#[inline(never)]
unsafe extern "win64" fn #basm_export(ptr_serialized: usize) -> usize {
super::basm_export_impl(ptr_serialized)
}

#[cfg(not(target_arch = "x86_64"))]
#[no_mangle]
#[unsafe(no_mangle)]
#[inline(never)]
unsafe extern "C" fn #basm_export(ptr_serialized: usize) -> usize {
super::basm_export_impl(ptr_serialized)
Expand Down
10 changes: 6 additions & 4 deletions basm-macro/src/import.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ fn import_impl_single(sig: &Signature) -> TokenStream {
}
};
let out = quote! {
#[allow(non_snake_case)]
mod #basm_import_mod {
#[allow(non_snake_case)]
mod #internals {
pub static mut SER_VEC: alloc::vec::Vec::<u8> = alloc::vec::Vec::<u8>::new();
pub static mut PTR_FN: usize = 0;
Expand All @@ -57,12 +59,12 @@ fn import_impl_single(sig: &Signature) -> TokenStream {
pub unsafe extern "C" fn free() { SER_VEC.clear() }

#[cfg(target_arch = "x86_64")]
#[no_mangle]
#[unsafe(no_mangle)]
#[inline(never)]
pub unsafe extern "win64" fn #basm_import(ptr_fn: usize) { PTR_FN = ptr_fn; }

#[cfg(not(target_arch = "x86_64"))]
#[no_mangle]
#[unsafe(no_mangle)]
#[inline(never)]
pub unsafe extern "C" fn #basm_import(ptr_fn: usize) { PTR_FN = ptr_fn; }
}
Expand All @@ -79,8 +81,8 @@ fn import_impl_single(sig: &Signature) -> TokenStream {
let ptr_serialized = basm_std::serialization::call_import(#internals::PTR_FN, #internals::SER_VEC.as_ptr() as usize);

let mut buf: &'static [u8] = basm_std::serialization::eat(ptr_serialized);
type return_type = #return_type;
let out = return_type::de(&mut buf);
type ReturnType = #return_type;
let out = ReturnType::de(&mut buf);
let ptr_free_remote = usize::de(&mut buf);
assert!(buf.is_empty());

Expand Down

0 comments on commit 32cf026

Please sign in to comment.