当前位置:网站首页>【lua】int64的支持
【lua】int64的支持
2022-07-04 22:17:00 【米莱虾】
之前的版本中,lua 只默认支持 double 类型,虽然可以通过修改 luaconf.h 里的定义把 lua number改为 int64,但为了 int64 类型放弃 double 显然不合适,属于顾此失彼。
那么,对于 64bit 的解决方案有很多,基本思路都是使用 8bytes 的string / lightuserdata / userdata修改元表来实现。
我们一般使用 8bytes 长的 string 来表示一个 int64 。这样既可以做唯一的 key 用,又不用做复杂的扩展,属于比较好的处理方式。
如果想要在项目中直接处理 64bit 的 timestamp。这就需要对int64做数学运算了。在 luajit 中,是定义了一个 userdata 并重载其运算符完成的。即可以用 ffi.cast("int64_t",0) 来构造一个 64bit 的0。对于这种做法,姑且不谈 userdata 的额外开销问题,这样做有一个问题就是当 64bit 的 cdata 做 table 的 key 的时候,相同值的 int64 并不是同一个 key 。
因此,有了一个更轻量的方式来解决 int64 支持的问题。那就是在 64 位平台上,我们完全可以用 lightuserdata 无损失的表示一个 int64,通过给 lightuserdata 设置 metatable ,我们可以接管它的数据运算。唯一不足的是,比较一个 int64 和普通的 lua number 是否相等时,lua 不能隐式的做转换(大于小于比较没有问题)。
由此,风云大神花了点时间构造了一个库。这个库只提供了一个显式的 api ,即构造一个 int64 数字。可以从 lua number 构造,也支持从一个 8 字节宽的小头的字符串来构造。实际在内存储存的是一个 lightuserdata 即一个 64bit 指针(所以这个库不适用于 32 位平台)。也可以通过 C 接口 lua_pushlightuserdata 来把一个 64bit 整数压入堆栈。
把 int64 转换为普通的 lua number 借用了 # 操作符。
边栏推荐
- 制作条形码的手机App推荐
- leetcode 72. Edit distance edit distance (medium)
- About stack area, heap area, global area, text constant area and program code area
- 华泰证券是国家认可的券商吗?开户安不安全?
- Li Kou 98: verify binary search tree
- Convolutional neural network model -- lenet network structure and code implementation
- The Sandbox 和数字好莱坞达成合作,通过人力资源开发加速创作者经济的发展
- Close system call analysis - Performance Optimization
- Ascendex launched Walken (WLKN) - an excellent and leading "walk to earn" game
- 抖音实战~评论数量同步更新
猜你喜欢
![[Yugong series] go teaching course 003-ide installation and basic use in July 2022](/img/65/b36ca239e06a67c01d1eb0b4648f71.png)
[Yugong series] go teaching course 003-ide installation and basic use in July 2022

The use of complex numbers in number theory and geometry - Cao Zexian

LOGO special training camp section I identification logo and Logo Design Ideas

Logo Camp d'entraînement section 3 techniques créatives initiales

Introduction and application of bigfilter global transaction anti duplication component

30余家机构联合发起数字藏品行业倡议,未来会如何前进?

Logo special training camp Section IV importance of font design

i. Mx6ull driver development | 24 - platform based driver model lights LED

都说软件测试很简单有手就行,但为何仍有这么多劝退的?

Zhiyang innovation signed a cooperation agreement with Huawei to jointly promote the sustainable development of shengteng AI industry
随机推荐
Logo Camp d'entraînement section 3 techniques créatives initiales
高中物理:直线运动
Energy momentum: how to achieve carbon neutralization in the power industry?
It is said that software testing is very simple, but why are there so many dissuasions?
质量体系建设之路的分分合合
MD5 tool class
关于栈区、堆区、全局区、文字常量区、程序代码区
Introducing QA into the software development lifecycle is the best practice that engineers should follow
卷积神经网络模型之——LeNet网络结构与代码实现
Locust性能测试 —— 环境搭建及使用
繁华落尽、物是人非:个人站长该何去何从
2022-07-04:以下go语言代码输出什么?A:true;B:false;C:编译错误。 package main import “fmt“ func main() { fmt.Pri
LOGO special training camp section I identification logo and Logo Design Ideas
How to manage 15million employees easily?
php短视频源码,点赞时会有大拇指动画飘起
如何实现轻松管理1500万员工?
LOGO特训营 第四节 字体设计的重要性
达梦数据凭什么被称为国产数据库“第一股”?
我在linux里面 通过调用odspcmd 查询数据库信息 怎么静默输出 就是只输出值 不要这个
PostgreSQLSQL高级技巧透视表