Skip to content

Commit

Permalink
Support for int128, uint128 and float128 types
Browse files Browse the repository at this point in the history
These types are only available in some architectures and C compilers,
such as x86_64 with a recent GCC/Clang.
  • Loading branch information
edubart committed Sep 23, 2020
1 parent 71f6b5f commit abde6de
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 60 deletions.
42 changes: 24 additions & 18 deletions docs/pages/overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -471,26 +471,32 @@ print(a,b) --outputs: 1234 1.000000

The following table shows Nelua primitive numeric types and is related type in C:

| Type | C Type | Suffixes |
|-------------------|-----------------|---------------------|
| `integer` | `int64_t` | `_i` `_integer` |
| `uinteger` | `unt64_t` | `_u` `_uinteger` |
| `number` | `double` | `_n` `_number` |
| `byte` | `uint8_t` | `_b` `_byte` |
| `isize` | `intptr_t` | `_is` `_isize` |
| `int8` | `int8_t` | `_i8` `_int8` |
| `int16` | `int16_t` | `_i16` `_int16` |
| `int32` | `int32_t` | `_i32` `_int32` |
| `int64` | `int64_t` | `_i64` `_int64` |
| `usize` | `uintptr_t` | `_us` `_usize` |
| `uint8` | `uint8_t` | `_u8` `_uint8` |
| `uint16` | `uint16_t` | `_u16` `_uint16` |
| `uint32` | `uint32_t` | `_u32` `_uint32` |
| `uint64` | `uint64_t` | `_u64` `_uint64` |
| `float32` | `float` | `_f32` `_float32` |
| `float64` | `double` | `_f64` `_float64` |
| Type | C Type | Suffixes |
|-------------------|---------------------|---------------------|
| `integer` | `int64_t` | `_i` `_integer` |
| `uinteger` | `unt64_t` | `_u` `_uinteger` |
| `number` | `double` | `_n` `_number` |
| `byte` | `uint8_t` | `_b` `_byte` |
| `isize` | `intptr_t` | `_is` `_isize` |
| `int8` | `int8_t` | `_i8` `_int8` |
| `int16` | `int16_t` | `_i16` `_int16` |
| `int32` | `int32_t` | `_i32` `_int32` |
| `int64` | `int64_t` | `_i64` `_int64` |
| `int128`* | `__int128` | `_i128` `_int128` |
| `usize` | `uintptr_t` | `_us` `_usize` |
| `uint8` | `uint8_t` | `_u8` `_uint8` |
| `uint16` | `uint16_t` | `_u16` `_uint16` |
| `uint32` | `uint32_t` | `_u32` `_uint32` |
| `uint64` | `uint64_t` | `_u64` `_uint64` |
| `uint128`* | `unsigned __int128` | `_u128` `_uint128` |
| `float32` | `float` | `_f32` `_float32` |
| `float64` | `double` | `_f64` `_float64` |
| `float128`* | `_Float128` | `_f128` `_float128` |
{: .table.table-bordered.table-striped.table-sm}

*\* Only supported by some C compilers and architectures.*
{:.text-muted}

The types `isize` and `usize` types are usually 32 bits wide on 32-bit systems,
and 64 bits wide on 64-bit systems.

Expand Down
3 changes: 3 additions & 0 deletions nelua/cdefs.lua
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,16 @@ cdefs.primitive_ctypes = {
nlint16 = 'int16_t',
nlint32 = 'int32_t',
nlint64 = 'int64_t',
nlint128 = '__int128',
nlusize = 'uintptr_t',
nluint8 = 'uint8_t',
nluint16 = 'uint16_t',
nluint32 = 'uint32_t',
nluint64 = 'uint64_t',
nluint128 = 'unsigned __int128',
nlfloat32 = 'float',
nlfloat64 = 'double',
nlfloat128 = '_Float128',
nlboolean = 'bool',
nlcstring = 'char*',
nlpointer = 'void*',
Expand Down
37 changes: 6 additions & 31 deletions nelua/typedefs.lua
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,17 @@ primtypes.int8 = types.IntegralType('int8', 1)
primtypes.int16 = types.IntegralType('int16', 2)
primtypes.int32 = types.IntegralType('int32', 4)
primtypes.int64 = types.IntegralType('int64', 8)
primtypes.int128 = types.IntegralType('int128', 16)
primtypes.isize = types.IntegralType('isize', cpusize)
primtypes.uint8 = types.IntegralType('uint8', 1, true)
primtypes.uint16 = types.IntegralType('uint16', 2, true)
primtypes.uint32 = types.IntegralType('uint32', 4, true)
primtypes.uint64 = types.IntegralType('uint64', 8, true)
primtypes.uint128 = types.IntegralType('uint128', 16, true)
primtypes.usize = types.IntegralType('usize', cpusize, true)
primtypes.float32 = types.FloatType('float32', 4, 9)
primtypes.float64 = types.FloatType('float64', 8, 17)
primtypes.float128 = types.FloatType('float128', 16, 36)
primtypes.byte = primtypes.uint8

-- Types for C compatibility.
Expand Down Expand Up @@ -76,37 +79,6 @@ primtypes.stringview = types.StringViewType('stringview')
primtypes.any = types.AnyType('any', 2*cpusize)
primtypes.varanys = types.VaranysType('varanys')

-- List of signed types.
typedefs.integral_signed_types = {
primtypes.int8,
primtypes.int16,
primtypes.int32,
primtypes.int64,
primtypes.isize,
primtypes.cschar,
primtypes.cshort,
primtypes.cint,
primtypes.clong,
primtypes.clonglong,
primtypes.cptrdiff,
primtypes.cchar
}

-- List of unsigned types.
typedefs.unsigned_types = {
primtypes.uint8,
primtypes.uint16,
primtypes.uint32,
primtypes.uint64,
primtypes.usize,
primtypes.cuchar,
primtypes.cushort,
primtypes.cuint,
primtypes.culong,
primtypes.culonglong,
primtypes.csize,
}

-- Map of literal suffixes for arithmetic types.
typedefs.number_literal_types = {
_i = primtypes.integer, _integer = primtypes.integer,
Expand All @@ -118,13 +90,16 @@ typedefs.number_literal_types = {
_i16 = primtypes.int16, _int16 = primtypes.int16,
_i32 = primtypes.int32, _int32 = primtypes.int32,
_i64 = primtypes.int64, _int64 = primtypes.int64,
_i128 = primtypes.int128, _int128 = primtypes.int128,
_us = primtypes.usize, _usize = primtypes.usize,
_u8 = primtypes.uint8, _uint8 = primtypes.uint8,
_u16 = primtypes.uint16, _uint16 = primtypes.uint16,
_u32 = primtypes.uint32, _uint32 = primtypes.uint32,
_u64 = primtypes.uint64, _uint64 = primtypes.uint64,
_u128 = primtypes.uint128, _uint128 = primtypes.uint128,
_f32 = primtypes.float32, _float32 = primtypes.float32,
_f64 = primtypes.float64, _float64 = primtypes.float64,
_f128 = primtypes.float128, _float128 = primtypes.float128,

_cchar = primtypes.cchar,
_cschar = primtypes.cschar,
Expand Down
11 changes: 2 additions & 9 deletions nelua/types.lua
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,12 @@ Type.shape = shaper.shape {
is_int16 = shaper.optional_boolean,
is_int32 = shaper.optional_boolean,
is_int64 = shaper.optional_boolean,
is_int128 = shaper.optional_boolean,
is_uint8 = shaper.optional_boolean,
is_uint16 = shaper.optional_boolean,
is_uint32 = shaper.optional_boolean,
is_uint64 = shaper.optional_boolean,
is_uint128 = shaper.optional_boolean,

-- Booleans for checking the underlying type (primitive types).
is_any = shaper.optional_boolean,
Expand Down Expand Up @@ -830,15 +832,6 @@ IntegralType.shape = shaper.fork_shape(Type.shape, {
min = shaper.arithmetic, max = shaper.arithmetic,
-- Signess of the integral type.
is_signed = shaper.optional_boolean, is_unsigned = shaper.optional_boolean,
-- Boolean to know the exactly underlying integral type.
is_uint64 = shaper.optional_boolean,
is_uint32 = shaper.optional_boolean,
is_uint16 = shaper.optional_boolean,
is_uint8 = shaper.optional_boolean,
is_int64 = shaper.optional_boolean,
is_int32 = shaper.optional_boolean,
is_int16 = shaper.optional_boolean,
is_int8 = shaper.optional_boolean,
})

function IntegralType:_init(name, size, is_unsigned)
Expand Down
4 changes: 2 additions & 2 deletions nelua/utils/bn.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
-- to mix operation between different integers ranges at compile time,
-- and to do error checking on invalid large integers.

-- BN is actually a bint class created with 128bits and with some extensions.
local bn = require 'nelua.thirdparty.bint'(128)
-- BN is actually a bint class created with 192bits and with some extensions.
local bn = require 'nelua.thirdparty.bint'(192)

-- This is used to check if a table is a 'bn'.
bn._bn = true
Expand Down

0 comments on commit abde6de

Please sign in to comment.