klamath-rs/src/lib.rs

87 lines
2.7 KiB
Rust
Raw Normal View History

2021-12-18 21:05:00 -08:00
extern crate byteorder;
pub mod basic;
pub mod record;
pub mod records;
pub mod elements;
pub mod library;
2024-12-21 09:50:19 -08:00
2024-12-21 15:42:50 -08:00
use byteorder::{ByteOrder, BigEndian};
use std::mem::size_of;
2024-12-21 13:56:51 -08:00
2024-12-21 15:42:50 -08:00
macro_rules! impl_i16be {
( $tt:ty, $arr:ident, $size:ident ) => {
{
let sl = unsafe { std::slice::from_raw_parts_mut($arr, $size) };
2024-12-21 13:56:51 -08:00
for xx in sl.iter_mut() {
2024-12-21 15:42:50 -08:00
if *xx < i16::MIN as $tt { return *xx }
if *xx > i16::MAX as $tt { return *xx }
let mut buf = [0; size_of::<$tt>()];
BigEndian::write_i16(&mut buf, *xx as i16);
*xx = <$tt>::from_le_bytes(buf);
2024-12-21 09:50:19 -08:00
}
2024-12-21 13:56:51 -08:00
0 as $tt
2024-12-21 09:50:19 -08:00
}
}
}
2024-12-21 15:42:50 -08:00
macro_rules! impl_i32be {
( $tt:ty, $arr:ident, $size:ident ) => {
{
let sl = unsafe { std::slice::from_raw_parts_mut($arr, $size) };
for xx in sl.iter_mut() {
if *xx < i32::MIN as $tt { return *xx }
if *xx > i32::MAX as $tt { return *xx }
2024-12-21 09:50:19 -08:00
2024-12-21 15:42:50 -08:00
let mut buf = [0; size_of::<$tt>()];
BigEndian::write_i32(&mut buf, *xx as i32);
*xx = <$tt>::from_le_bytes(buf);
2024-12-21 09:50:19 -08:00
}
2024-12-21 15:42:50 -08:00
0 as $tt
2024-12-21 09:50:19 -08:00
}
}
2024-12-21 15:42:50 -08:00
}
2024-12-21 09:50:19 -08:00
2024-12-21 15:42:50 -08:00
#[no_mangle]
pub extern "C" fn f64_to_i16(arr: *mut f64, size: usize) -> f64 { impl_i16be!(f64, arr, size) }
#[no_mangle]
pub extern "C" fn f64_to_i32(arr: *mut f64, size: usize) -> f64 { impl_i32be!(f64, arr, size) }
2024-12-21 09:50:19 -08:00
2024-12-21 15:42:50 -08:00
#[no_mangle]
pub extern "C" fn f32_to_i16(arr: *mut f32, size: usize) -> f32 { impl_i16be!(f32, arr, size) }
#[no_mangle]
pub extern "C" fn f32_to_i32(arr: *mut f32, size: usize) -> f32 { impl_i32be!(f32, arr, size) }
2024-12-21 09:50:19 -08:00
2024-12-21 15:42:50 -08:00
#[no_mangle]
pub extern "C" fn u64_to_i16(arr: *mut u64, size: usize) -> u64 { impl_i16be!(u64, arr, size) }
#[no_mangle]
pub extern "C" fn u64_to_i32(arr: *mut u64, size: usize) -> u64 { impl_i32be!(u64, arr, size) }
2024-12-21 09:50:19 -08:00
2024-12-21 15:42:50 -08:00
#[no_mangle]
pub extern "C" fn i64_to_i16(arr: *mut i64, size: usize) -> i64 { impl_i16be!(i64, arr, size) }
#[no_mangle]
pub extern "C" fn i64_to_i32(arr: *mut i64, size: usize) -> i64 { impl_i32be!(i64, arr, size) }
2024-12-21 09:50:19 -08:00
2024-12-21 15:42:50 -08:00
#[no_mangle]
pub extern "C" fn u32_to_i16(arr: *mut u32, size: usize) -> u32 { impl_i16be!(u32, arr, size) }
#[no_mangle]
pub extern "C" fn u32_to_i32(arr: *mut u32, size: usize) -> u32 { impl_i32be!(u32, arr, size) }
2024-12-21 09:50:19 -08:00
2024-12-21 15:42:50 -08:00
#[no_mangle]
pub extern "C" fn i32_to_i16(arr: *mut i32, size: usize) -> i32 { impl_i16be!(i32, arr, size) }
#[no_mangle]
pub extern "C" fn i32_to_i32(arr: *mut i32, size: usize) -> i32 { impl_i32be!(i32, arr, size) }
#[no_mangle]
pub extern "C" fn u16_to_i16(arr: *mut u16, size: usize) -> u16 { impl_i16be!(u16, arr, size) }
#[no_mangle]
pub extern "C" fn i16_to_i16(arr: *mut i16, size: usize) -> i16 { impl_i16be!(i16, arr, size) }
2024-12-21 09:50:19 -08:00