简体   繁体   English

Lua数字和64位整数的并行实现策略

[英]Strategies for parallel implementation of Lua numbers and a 64bit integer

Lua by default uses a double precision floating point ( double ) type as its only numeric type. 默认情况下,Lua使用双精度浮点( double )类型作为唯一的数字类型。 That's nice and useful. 很好而且有用。 However, I'm working on software that expects to see 64bit integers, for which I don't get around using actual 64bit integers one way or another. 但是,我正在开发希望能看到64位整数的软件,为此,我不会以一种或另一种方式使用实际的64位整数。

The place where the integer type becomes relevant is for file sizes. 整数类型变得重要的地方是文件大小。 Although I don't truly expect to see file sizes beyond what Lua can represent with full "integer" precision using a double , I want to be prepared. 尽管我并不真正希望看到文件大小超出Lua可以使用double表示的完整“整数”精度,但我还是要做好准备。

What strategies can you recommend when using a 64bit integer type in parallel with the default numeric type of Lua? 将64位整数类型与Lua的默认数字类型并行使用时,您可以推荐什么策略? I don't really want to throw the default implementation overboard (and I'm not worried of its performance compared to integer arithmetics), but I need some way of representing 64bit integers up to their full precision without too much of a performance penalty. 我真的不想把默认实现抛诸脑后(而且我不担心它与整数算术相比的性能),但是我需要某种方式来表示64位整数,使其达到全精度,而不会造成太多性能损失。

My problem is that I'm unsure where to modify the behavior. 我的问题是我不确定在哪里修改行为。 Should I modify the syntax and extend the parser (numbers with appended LL or ULL come to mind, which to my knowledge doesn't exist in default Lua) or should I instead write my own C module and define a userdata type that represents the 64bit integer, along with library functions able to manipulate the values? 我应该修改语法并扩展解析器(想到的是带有附加的LL或ULL的数字,据我所知在默认的Lua中不存在)或我应该编写自己的C模块并定义一个表示64位的userdata类型整数,以及能够操纵值的库函数? ... ...

Note: yes, I am embedding Lua, so I am free to extend it whichever way I please. 注意:是的,我正在嵌入Lua,所以我可以随意扩展它。

As part of LuaJIT's port to ARM CPUs (which often have poor floating-point), LuaJIT implemented a "Dual-number VM", which allows it to switch between integers and floats dynamically as needed. 作为LuaJIT移植到ARM CPU(通常浮点数不高)的一部分,LuaJIT实现了“双数VM”,从而可以在整数之间切换并根据需要动态浮动。 You could use this yourself, just switch between 64-bit integers and doubles instead of 32-bit integers and floats. 您可以自己使用它,只需在64位整数和双精度数之间切换,而不是在32位整数和浮点数之间切换。

It's currently live in builds, so you may want to consider using LuaJIT as your Lua "interpreter." 它当前在构建中,因此您可能需要考虑使用LuaJIT作为Lua的“解释器”。 Or you could use it as a way to learn how to do this sort of thing. 或者,您可以将其用作学习如何进行此类操作的方法。

However, I do agree with Marcelo; 但是,我同意马塞洛的观点。 the 53-bit mantissa should be plenty. 53位尾数应该足够。 You shouldn't really need this for a good 10 years or so. 在大约10年的时间里,您真的不需要此功能。

I'd suggest storing your data outside of Lua and use some type of reference to retrieve it when calling your other libraries. 我建议将您的数据存储在Lua之外,并在调用其他库时使用某种类型的引用来检索它。 You can then push various results onto the Lua stack for the user the see, you can even retrieve the value as a string to be precise, but I would avoid modifying them in Lua and relying on the Lua values when calling your external library. 然后,您可以将各种结果推送到Lua堆栈上,以供用户查看,甚至可以精确地将值检索为字符串,但是我会避免在Lua中修改它们并在调用外部库时依赖Lua值。

If you're not going to need floating-point precision at any point in the program, you can just redefine LUA_NUMBER to __int64 (or whatever 64-bit int may be in your environment) in luaconf.h . 如果你不会需要浮点精度在程序中的任何一点,你可以重新定义LUA_NUMBER__int64 (或任何64位int可能是在您的环境)中luaconf.h

Otherwise, you can just bring in another library to handle your integers- for infinite precision, you can use a bignum library such as lhf's lbn . 否则,您可以引入另一个库来处理您的整数-为了获得无限精度,您可以使用bignum库,例如lhf的lbn

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM