Skip to content

Commit

Permalink
add cast and trasmute example
Browse files Browse the repository at this point in the history
  • Loading branch information
ZuseZ4 committed Jul 23, 2024
1 parent 148f25f commit 33a9ae3
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions integration/tests/examples/cast_and_transmute.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#![feature(autodiff)]

#[no_mangle]
#[autodiff(b_dot_local, Reverse, Duplicated, Active)]
fn dot_local(x: &usize) -> f64 {
// dereference x, and cast the usize back to the double ptr.
// Then load a double from the pointer and return sin(the value).
let x = unsafe { *x as *const f64 };
let x = unsafe { *x };
x.sin()
}

// now the same with std::mem::transmute
#[no_mangle]
#[autodiff(b_dot_local2, Reverse, Duplicated, Active)]
fn dot_local2(x: &usize) -> f64 {
// dereference x, and std::mem::transmute the usize back to the double ptr.
// Then load a double from the pointer and return sin(the value).
let x = unsafe { std::mem::transmute::<usize, *const f64>(*x) };
let x = unsafe { *x };
x.sin()
}

fn main() {
let x = 1.34;
let mut dx = 0.0;
let mut dx2 = 0.0;
let x1: usize = &x as *const f64 as usize;
let mut dx1: usize = &mut dx as *mut f64 as usize;
let mut dx2: usize = &mut dx2 as *mut f64 as usize;
b_dot_local(&x1, &mut dx1, 1.0);
b_dot_local2(&x1, &mut dx2, 1.0);
let dx1f = unsafe { *(dx1 as *const f64) };
let dx2f = unsafe { *(dx2 as *const f64) };
assert_eq!(dx1f, f64::cos(x));
assert_eq!(dx2f, f64::cos(x));
}

0 comments on commit 33a9ae3

Please sign in to comment.