是否有具有非IEEE C/C ++浮点格式的现代平台? [英] Are there any modern platforms with non-IEEE C/C++ float formats?

查看:89
本文介绍了是否有具有非IEEE C/C ++浮点格式的现代平台?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写视频游戏 Humm and Strumm ,该游戏的游戏引擎中需要网络组件.我可以轻松地处理字节序方面的差异,但是在尝试处理可能的float内存格式时遇到了麻烦.我知道现代计算机都具有标准整数格式,但是我听说它们可能并不都对浮点整数使用IEEE标准.这是真的吗?

I am writing a video game, Humm and Strumm, which requires a network component in its game engine. I can deal with differences in endianness easily, but I have hit a wall in attempting to deal with possible float memory formats. I know that modern computers have all a standard integer format, but I have heard that they may not all use the IEEE standard for floating-point integers. Is this true?

虽然我可以将其作为字符串输出到每个数据包中,但是无论平台如何,我仍然必须转换为每个客户端的众所周知的格式".标准的printf()atod()是不够的.

While certainly I could just output it as a character string into each packet, I would still have to convert to a "well-known format" of each client, regardless of the platform. The standard printf() and atod() would be inadequate.

请注意,因为此游戏是将在GNU/Linux,* BSD和Microsoft Windows上运行的免费/开源软件程序,所以我不能使用任何专有解决方案,也不能使用任何单平台解决方案.

Please note, because this game is a Free/Open Source Software program that will run on GNU/Linux, *BSD, and Microsoft Windows, I cannot use any proprietary solutions, nor any single-platform solutions.

干杯,
帕特里克

Cheers,
Patrick

推荐答案

我认为可以肯定地说,每个平台都有IEE-754规范的实现,您可以依靠它,即使它们都实现了IEE-754规范.相同的规范不能保证每个平台都具有完全相同的实现,具有相同的FP控制标志集,进行相同的优化或实现相同的非标准扩展.这使得浮点确定性非常难以控制,并且在这种情况下很难使用(在这种情况下,您将通过网络传达FP值).

I think it is safe to assume that each platform has an implementation of the IEE-754 spec that you can rely on, however, even if they all implement the same spec there is no guarantee that each platform has the exact same implementation, has the same FP control flags set, does the same optimizations, or implements the same non-standard extensions. This makes floating point determinism very hard to control and somewhat unreliable to use for this kind of thing (where you'll be communicating FP values over the network).

有关此的更多信息;阅读 http://gafferongames.com/networking-for-game-programmers /floating-point-determinism/

For more information on that; read http://gafferongames.com/networking-for-game-programmers/floating-point-determinism/

要解决的另一个问题是处理没有浮点运算单元的客户.在大多数情况下,这些将是低端CPU,控制台或嵌入式设备.如果要针对他们,请确保考虑到这一点.可以进行FP仿真,但在这些设备上往往很慢,因此您必须不停地进行定点计算.但是,建议您编写精巧的类以将浮点和定点计算抽象到相同的代码,这听起来像是计划.但在大多数设备上却不是.在处理定点值时,它不允许您挤出最大的精度和性能.

Another problem to tackle is handling clients that don't have a floating point unit; most of the time these will be low-end CPUs, consoles or embedded devices. Make sure to take this into account if you want to target them. FP emulation can be done but tends to be very slow on these devices so you'll have to get a hang of doing fixed point calculations. Be advised though, writing elaborate classes to abstract floating point and fixed point calculations to the same code sounds like a plan; but on most devices isn't. It doesn't allow you to squeeze out the maximum precision and performance when dealing with fixed point values.

另一个问题是处理浮点值的字节序,因为您不能只交换字节并再次在浮点寄存器中堆栈'm(字节可能具有不同的含义,请参见

Yet another problem is handling the endianness of the floating point values because you cannot just swap bytes and stack 'm in a floating point register again (the bytes might get a different meaning, see http://www.dmh2000.com/cpp/dswap.shtml on that).

我的建议是将浮点数转换为定点中间值,并在需要时进行字节序校正,然后进行传输.另外,不要假设在不同机器上进行两次浮点计算会得出相同的结果;他们没有.但是,除IEEE-754之外的浮点实现很少见.例如,GPU倾向于使用定点,但是如今由于它们不想处理被零除的异常,但它们将具有IEEE-754的子集,但它们将具有适合于半浮点数的扩展. 16位.

My advice would be to convert the floats to fixed point intermediate values, do an endian correction if needed and transmit that. Also, don't assume that two floating point calculations on different machines will yield the same results; they don't. However, floating point implementations other than IEEE-754 are rare. For example GPUs tended to use fixed point, but are more likely to have a subset of IEEE-754 these days because they don't want to deal with division-by-zero exceptions but they will have extensions for half-floats that fit in 16 bits.

还要意识到,那里已经有图书馆为您解决了这个问题(在游戏环境中发送低级数据格式). RakNet是这样的一个库,特别是它的BitStream类旨在将这些数据可靠地发送到不同的平台,同时将开销降至最低.例如,RakNet遇到了很多麻烦,不浪费任何带宽来发送字符串或向量.

Also realize that there are libraries out there that have already solved this problem (sending low-level data formats in a gaming context) for you. One such library is RakNet, specifically it's BitStream class is designed to send these kinds of data reliably to different platforms while keeping the overhead to a minimum; for example RakNet goes through quite some trouble not to waste any bandwidth on sending strings or vectors.

这篇关于是否有具有非IEEE C/C ++浮点格式的现代平台?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆