mod.rs 642 Bytes
Newer Older
afischer@lsv.uni-saarland.de's avatar
lib  
afischer@lsv.uni-saarland.de committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33

use std::sync::atomic::{AtomicU64 as StdAtomicU64, Ordering};
use std::mem::transmute;

#[derive(Debug)]
pub struct AtomicF64 {
    inner: StdAtomicU64,
}

impl AtomicF64 {
    pub fn new(val: f64) -> AtomicF64 {
        AtomicF64 { inner: StdAtomicU64::new(f64_to_u64(val)) }
    }

    #[inline]
    pub fn get(&self) -> f64 {
        u64_to_f64(self.inner.load(Ordering::Relaxed))
    }

    #[inline]
    pub fn set(&self, val: f64) {
        self.inner.store(f64_to_u64(val), Ordering::Relaxed)
    }
}

fn u64_to_f64(val: u64) -> f64 {
    unsafe { transmute(val) }
}

fn f64_to_u64(val: f64) -> u64 {
    unsafe { transmute(val) }
}