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}; 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] #[binrw]
#[brw(little)] #[brw(little)]
#[derive(Clone,Copy,Debug)] #[derive(Clone,Copy,Debug)]
@ -20,14 +70,19 @@ impl Into<bool> for Bool{
#[binrw] #[binrw]
#[brw(little)] #[brw(little)]
pub struct Vector2{ pub struct Vector2{
#[br(map=trey_float)]
pub x:f32, pub x:f32,
#[br(map=trey_float)]
pub y:f32, pub y:f32,
} }
#[binrw] #[binrw]
#[brw(little)] #[brw(little)]
pub struct Vector3{ pub struct Vector3{
#[br(map=trey_float)]
pub x:f32, pub x:f32,
#[br(map=trey_float)]
pub y:f32, pub y:f32,
#[br(map=trey_float)]
pub z:f32, pub z:f32,
} }
@ -41,6 +96,7 @@ pub struct InputEvent{
#[binrw] #[binrw]
#[brw(little)] #[brw(little)]
pub struct TimedInputEvent{ pub struct TimedInputEvent{
#[br(map=trey_double)]
pub time:f64, pub time:f64,
pub event:InputEvent, pub event:InputEvent,
} }
@ -58,6 +114,7 @@ pub struct OutputEvent{
#[binrw] #[binrw]
#[brw(little)] #[brw(little)]
pub struct TimedOutputEvent{ pub struct TimedOutputEvent{
#[br(map=trey_double)]
pub time:f64, pub time:f64,
pub event:OutputEvent, pub event:OutputEvent,
} }
@ -72,6 +129,7 @@ pub struct SoundEvent{
#[binrw] #[binrw]
#[brw(little)] #[brw(little)]
pub struct TimedSoundEvent{ pub struct TimedSoundEvent{
#[br(map=trey_double)]
pub time:f64, pub time:f64,
pub event:SoundEvent, pub event:SoundEvent,
} }
@ -92,6 +150,7 @@ pub struct WorldEventButton{
#[binrw] #[binrw]
#[brw(little)] #[brw(little)]
pub struct WorldEventSetTime{ pub struct WorldEventSetTime{
#[br(map=trey_double)]
pub time:f64, pub time:f64,
#[brw(magic=b"data")] #[brw(magic=b"data")]
__:(), __:(),
@ -118,6 +177,7 @@ pub enum WorldEvent{
#[binrw] #[binrw]
#[brw(little)] #[brw(little)]
pub struct TimedWorldEvent{ pub struct TimedWorldEvent{
#[br(map=trey_double)]
pub time:f64, pub time:f64,
pub event:WorldEvent, pub event:WorldEvent,
} }
@ -131,6 +191,7 @@ pub struct GravityEvent{
#[binrw] #[binrw]
#[brw(little)] #[brw(little)]
pub struct TimedGravityEvent{ pub struct TimedGravityEvent{
#[br(map=trey_double)]
pub time:f64, pub time:f64,
pub event:GravityEvent, pub event:GravityEvent,
} }
@ -164,6 +225,7 @@ pub struct RunEvent{
#[binrw] #[binrw]
#[brw(little)] #[brw(little)]
pub struct TimedRunEvent{ pub struct TimedRunEvent{
#[br(map=trey_double)]
pub time:f64, pub time:f64,
pub event:RunEvent, pub event:RunEvent,
} }
@ -178,6 +240,7 @@ pub struct CameraEvent{
#[binrw] #[binrw]
#[brw(little)] #[brw(little)]
pub struct TimedCameraEvent{ pub struct TimedCameraEvent{
#[br(map=trey_double)]
pub time:f64, pub time:f64,
pub event:CameraEvent, pub event:CameraEvent,
} }
@ -187,11 +250,13 @@ pub struct TimedCameraEvent{
#[brw(little)] #[brw(little)]
pub struct SettingEvent{ pub struct SettingEvent{
pub setting_id:u32, pub setting_id:u32,
#[br(map=trey_double)]
pub value:f64, pub value:f64,
} }
#[binrw] #[binrw]
#[brw(little)] #[brw(little)]
pub struct TimedSettingEvent{ pub struct TimedSettingEvent{
#[br(map=trey_double)]
pub time:f64, pub time:f64,
pub event:SettingEvent, pub event:SettingEvent,
} }
@ -291,6 +356,7 @@ pub struct BlockId(#[br(map=|i:u32|i-1)]u32);
#[brw(little)] #[brw(little)]
#[derive(Debug,Clone,Copy)] #[derive(Debug,Clone,Copy)]
pub struct TimedBlockId{ pub struct TimedBlockId{
#[br(map=trey_double)]
pub time:f64, pub time:f64,
pub block_id:BlockId, pub block_id:BlockId,
} }