trey floating point format is not standard

This commit is contained in:
Quaternions 2024-12-31 01:44:28 -08:00
parent 43ee18a2ca
commit 9003f88a9d

View File

@ -1,5 +1,55 @@
use binrw::{binrw,BinReaderExt,io::TakeSeekExt};
// whatever this is
fn trey_float(f:f32)->f32{
let bits=f.to_bits();
let s=bits&1!=0;
let e=((bits>>1)&((1<<8)-1)) as i32;
let m=(bits>>(1+8))&((1<<23)-1);
if e==255{
if m==0{
if s{
f32::NEG_INFINITY
}else{
f32::INFINITY
}
}else if m==1{
f32::NAN
}else{
// this is supposed to be QNAN but idk how to say it in rust
f32::NAN
}
}else if e==0{
if s{-(m as f32)*2.0f32.powi(-149)}else{(m as f32)*2.0f32.powi(-149)}
}else{
if s{-(m as f32/2.0f32.powi(23)+1.0)*2.0f32.powi(e-127)}else{(m as f32/2.0f32.powi(23)+1.0)*2.0f32.powi(e-127)}
}
}
fn trey_double(f:f64)->f64{
let bits=f.to_bits();
let s=bits&1!=0;
let e=((bits>>1)&((1<<11)-1)) as i32;
let m=(bits>>(1+11))&((1<<52)-1);
if e==2047{
if m==0{
if s{
f64::NEG_INFINITY
}else{
f64::INFINITY
}
}else if m==1{
f64::NAN
}else{
// this is supposed to be QNAN but idk how to say it in rust
f64::NAN
}
}else if e==0{
if s{-(m as f64)*2.0f64.powi(-1074)}else{(m as f64)*2.0f64.powi(-1074)}
}else{
if s{-(m as f64/2.0f64.powi(52)+1.0)*2.0f64.powi(e-1023)}else{(m as f64/2.0f64.powi(52)+1.0)*2.0f64.powi(e-1023)}
}
}
#[binrw]
#[brw(little)]
#[derive(Clone,Copy,Debug)]
@ -20,14 +70,19 @@ impl Into<bool> for Bool{
#[binrw]
#[brw(little)]
pub struct Vector2{
#[br(map=trey_float)]
pub x:f32,
#[br(map=trey_float)]
pub y:f32,
}
#[binrw]
#[brw(little)]
pub struct Vector3{
#[br(map=trey_float)]
pub x:f32,
#[br(map=trey_float)]
pub y:f32,
#[br(map=trey_float)]
pub z:f32,
}
@ -41,6 +96,7 @@ pub struct InputEvent{
#[binrw]
#[brw(little)]
pub struct TimedInputEvent{
#[br(map=trey_double)]
pub time:f64,
pub event:InputEvent,
}
@ -58,6 +114,7 @@ pub struct OutputEvent{
#[binrw]
#[brw(little)]
pub struct TimedOutputEvent{
#[br(map=trey_double)]
pub time:f64,
pub event:OutputEvent,
}
@ -72,6 +129,7 @@ pub struct SoundEvent{
#[binrw]
#[brw(little)]
pub struct TimedSoundEvent{
#[br(map=trey_double)]
pub time:f64,
pub event:SoundEvent,
}
@ -92,6 +150,7 @@ pub struct WorldEventButton{
#[binrw]
#[brw(little)]
pub struct WorldEventSetTime{
#[br(map=trey_double)]
pub time:f64,
#[brw(magic=b"data")]
__:(),
@ -118,6 +177,7 @@ pub enum WorldEvent{
#[binrw]
#[brw(little)]
pub struct TimedWorldEvent{
#[br(map=trey_double)]
pub time:f64,
pub event:WorldEvent,
}
@ -131,6 +191,7 @@ pub struct GravityEvent{
#[binrw]
#[brw(little)]
pub struct TimedGravityEvent{
#[br(map=trey_double)]
pub time:f64,
pub event:GravityEvent,
}
@ -164,6 +225,7 @@ pub struct RunEvent{
#[binrw]
#[brw(little)]
pub struct TimedRunEvent{
#[br(map=trey_double)]
pub time:f64,
pub event:RunEvent,
}
@ -178,6 +240,7 @@ pub struct CameraEvent{
#[binrw]
#[brw(little)]
pub struct TimedCameraEvent{
#[br(map=trey_double)]
pub time:f64,
pub event:CameraEvent,
}
@ -187,11 +250,13 @@ pub struct TimedCameraEvent{
#[brw(little)]
pub struct SettingEvent{
pub setting_id:u32,
#[br(map=trey_double)]
pub value:f64,
}
#[binrw]
#[brw(little)]
pub struct TimedSettingEvent{
#[br(map=trey_double)]
pub time:f64,
pub event:SettingEvent,
}
@ -291,6 +356,7 @@ pub struct BlockId(#[br(map=|i:u32|i-1)]u32);
#[brw(little)]
#[derive(Debug,Clone,Copy)]
pub struct TimedBlockId{
#[br(map=trey_double)]
pub time:f64,
pub block_id:BlockId,
}