How to replace the i64::from( vol as i32) part with something platform (32/64 bit) agnostic

that is what I am doing currently - mapping a f64 based on the actual range of the device.
But I am stuck at making it work with both 64/32 bit proof:
For example I have this for 32bit
 
let vol_db = i64::from((self.pvol(vol, 0x0000, 0xFFFF).log10() * 6000.0).floor() as i32)
+ self.params.max_db.0;
But want replace the i64::from( vol as i32) part with something platform (32/64 bit) agnostic
You already invited:

MarcoFalke

Upvotes from:

I don’t think this is something you would want to vary across 32/64 bit
Either the volume spans a 32 bit range, or a 64 bit range
The size of a pointer shouldn’t affect that... 32 bit systems can do 64 bit arithmetic too

Benny

Upvotes from:

 Matching the word size of the target platform is really annoying, and a lot of "32-bit" processors directly support 64-bit floats anyway. You should only do it if you really, really need to for performance.
Otherwise, if you need 64 bits for accuracy, use 64 bits everywhere.

Dmitry

Upvotes from:

On the C side you're stuck with c_long unfortunately
At least on the Rust side you have the opportunity to expose it consistently
i64 would support a c_long on any platform targetted by Rust; on 32-bit platforms you could have value >> 32 as c_long, and value as c_long on 64 bits
Or do it with 0.0 <= f64 < 1.0 and map to c_long as appropriate
 
Or if you want to keep fidelity with the underling ALSA lib, expose c_long on the Rust side and have the user deal with it

Aamir

Upvotes from:

You also could stick something in that would fail to compile if the two ever get out of sync. You can get a constant equal to the size by calling std::mem::size_of::<isize>() and likewise for c_long
 
Unless performance is critical, I'd do the math with the same amount of space always (i64 in this case, I guess)
I rather suspect that if you're calling audio libs, doing a little 64-bit math on a 32 bit platform is not going to be the bottleneck.

If you wanna answer this question please Login or Register