当前位置:网站首页>[Lua] Int64 support

[Lua] Int64 support

2022-07-04 22:40:00 Rice shrimp

In the previous version ,lua Only by default double type , Although it can be modified luaconf.h The definition in lua number Change it to int64, But for int64 Type abandonment double Obviously not , Belong to attend to one thing and lose the other .

that , about 64bit There are many solutions for , The basic idea is to use 8bytes Of string / lightuserdata / userdata Modify the meta table to realize .

We In general use 8bytes Long string To represent a int64 . This can be the only key use , Without complex extensions , It belongs to a better way .

If you want to deal with it directly in the project 64bit Of timestamp. It needs to be right int64 Do mathematical operations . stay luajit in , I defined one userdata And overload its operator to complete . You can use  ffi.cast("int64_t",0)  To construct a 64bit Of 0. For this practice , Let's not talk about userdata The extra cost of , One problem with this is When 64bit Of cdata do table Of key When , Of the same value int64 It's not the same key .

therefore , There is a lighter way to solve int64 The question of support . That is in 64 A platform , We can use it lightuserdata No loss means a int64, By giving lightuserdata Set up metatable , We can take over its data operations . The only drawback is , Compare one int64 And ordinary lua number Is it equal ,lua You can't convert implicitly ( Greater than less than comparison is no problem ).

thus , It took Fengyun a little time to build a library . This library only provides an explicit api , That is to construct a int64 Numbers . It can be downloaded from lua number structure , Also support from a 8 Byte wide string with small header . What is actually stored in memory is a lightuserdata That is, a 64bit The pointer ( So this library is not applicable to 32 Bit platform ). It can also be done through C Interface  lua_pushlightuserdata  Let's take one 64bit Integer push on stack .

hold int64 Convert to normal lua number Borrowed # The operator .

GitHub - cloudwu/lua-int64

原网站

版权声明
本文为[Rice shrimp]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/185/202207042208072091.html