Skip to content
This repository was archived by the owner on Aug 31, 2021. It is now read-only.

Commit d54ed87

Browse files
committed
[[ Bug 20329 ]] Add natural int and float types
This patch adds 'natural' int and float types. These types are the same width as the natural bit width of the processor.
1 parent cd2fdcf commit d54ed87

6 files changed

Lines changed: 101 additions & 0 deletions

File tree

docs/guides/LiveCode Builder Language Reference.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -456,6 +456,10 @@ There are the standard machine types (defined in the foreign module):
456456
- Int64/SInt64 and UInt64 map to 64-bit integers
457457
- IntSize/SIntSize and UIntSize map to the integer size needed to hold a memory size
458458
- IntPtr/SIntPtr and UIntPtr map to the integer size needed to hold a pointer
459+
- NaturalSInt and NaturalUInt map to 32-bit integers on 32-bit processors and
460+
64-bit integers on 64-bit processors
461+
- NaturalFloat maps to the 32-bit float type on 32-bit processors and the 64-bit
462+
float (double) type on 64-bit processors
459463

460464
There are the standard C primitive types (defined in the foreign module)
461465

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
---
2+
version: 9.0.0-dp-9
3+
---
4+
# LiveCode Builder Standard Library
5+
## Foreign function interface
6+
7+
* Natural integer types NaturalSInt and NaturalUInt have been added to the
8+
foreign module. These map to 32-bit or 64-bit integers, depending on the bitness
9+
of the processor.
10+
11+
* A natural float type NaturalFloat has been added to the foreign module. This
12+
maps to float on 32-bit processors and double on 64-bit processors.
13+

libfoundation/include/foundation.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1644,6 +1644,13 @@ MC_DLLEXPORT extern MCTypeInfoRef kMCSIntPtrTypeInfo;
16441644
MC_DLLEXPORT MCTypeInfoRef MCForeignUIntPtrTypeInfo(void) ATTRIBUTE_PURE;
16451645
MC_DLLEXPORT MCTypeInfoRef MCForeignSIntPtrTypeInfo(void) ATTRIBUTE_PURE;
16461646

1647+
MC_DLLEXPORT extern MCTypeInfoRef kMCNaturalUIntTypeInfo;
1648+
MC_DLLEXPORT extern MCTypeInfoRef kMCNaturalSIntTypeInfo;
1649+
MC_DLLEXPORT extern MCTypeInfoRef kMCNaturalFloatTypeInfo;
1650+
MC_DLLEXPORT MCTypeInfoRef MCForeignNaturalUIntTypeInfo(void) ATTRIBUTE_PURE;
1651+
MC_DLLEXPORT MCTypeInfoRef MCForeignNaturalSIntTypeInfo(void) ATTRIBUTE_PURE;
1652+
MC_DLLEXPORT MCTypeInfoRef MCForeignNaturalFloatTypeInfo(void) ATTRIBUTE_PURE;
1653+
16471654
MC_DLLEXPORT extern MCTypeInfoRef kMCCBoolTypeInfo;
16481655
MC_DLLEXPORT MCTypeInfoRef MCForeignCBoolTypeInfo(void) ATTRIBUTE_PURE;
16491656

libfoundation/src/foundation-foreign.cpp

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,12 @@ MC_DLLEXPORT_DEF MCTypeInfoRef kMCSIntPtrTypeInfo;
6161
* provide an element of safety against nullptr vs non-nullptr. */
6262
MC_DLLEXPORT_DEF MCTypeInfoRef kMCPointerTypeInfo;
6363

64+
/* The natural int and float types are 32-bit wide on 32-bit processors and
65+
* 64-bit wide on 64-bit processors. */
66+
MC_DLLEXPORT_DEF MCTypeInfoRef kMCNaturalUIntTypeInfo;
67+
MC_DLLEXPORT_DEF MCTypeInfoRef kMCNaturalSIntTypeInfo;
68+
MC_DLLEXPORT_DEF MCTypeInfoRef kMCNaturalFloatTypeInfo;
69+
6470
/* C Types
6571
*
6672
* These types represent the C types which are relative to the compiler,
@@ -109,6 +115,10 @@ MC_DLLEXPORT_DEF MCTypeInfoRef MCForeignSIntSizeTypeInfo() { return kMCSIntSizeT
109115
MC_DLLEXPORT_DEF MCTypeInfoRef MCForeignUIntPtrTypeInfo() { return kMCUIntPtrTypeInfo; }
110116
MC_DLLEXPORT_DEF MCTypeInfoRef MCForeignSIntPtrTypeInfo() { return kMCSIntPtrTypeInfo; }
111117

118+
MC_DLLEXPORT_DEF MCTypeInfoRef MCForeignNaturalUIntTypeInfo() { return kMCNaturalUIntTypeInfo; }
119+
MC_DLLEXPORT_DEF MCTypeInfoRef MCForeignNaturalSIntTypeInfo() { return kMCNaturalSIntTypeInfo; }
120+
MC_DLLEXPORT_DEF MCTypeInfoRef MCForeignNaturalFloatTypeInfo() { return kMCNaturalFloatTypeInfo; }
121+
112122
/**/
113123

114124
MC_DLLEXPORT_DEF MCTypeInfoRef MCForeignCBoolTypeInfo() { return kMCCBoolTypeInfo; }
@@ -131,12 +141,30 @@ MC_DLLEXPORT_DEF MCTypeInfoRef MCForeignSIntTypeInfo() { return kMCSIntTypeInfo;
131141

132142
////////////////////////////////////////////////////////////////////////////////
133143

144+
#if defined(__32_BIT__)
145+
typedef uint32_t natural_uint_t;
146+
typedef int32_t natural_sint_t;
147+
typedef float natural_float_t;
148+
#elif defined(__64_BIT__)
149+
typedef uint64_t natural_uint_t;
150+
typedef int64_t natural_sint_t;
151+
typedef float natural_float_t;
152+
#else
153+
#error Bitness of target not defined
154+
#endif
155+
134156
static_assert(sizeof(int) == 4,
135157
"Assumption that int is 4 bytes in size not valid");
136158

137159
static_assert(sizeof(bool) == 1,
138160
"Assumption that bool is 1 byte in size not valid");
139161

162+
static_assert(sizeof(uintptr_t) == sizeof(natural_uint_t),
163+
"Assumption that natural_uint_t is the same width as uintptr_t not valid");
164+
165+
static_assert(sizeof(intptr_t) == sizeof(natural_sint_t),
166+
"Assumption that natural_uint_t is the same width as uintptr_t not valid");
167+
140168
template <typename CType, typename Enable = void>
141169
struct compute_primitive_type
142170
{
@@ -318,6 +346,33 @@ struct sintptr_type_desc_t: public integral_type_desc_t<intptr_t> {
318346
static constexpr auto is_promotable = false;
319347
};
320348

349+
/**/
350+
351+
struct naturaluint_type_desc_t: public integral_type_desc_t<natural_uint_t> {
352+
static constexpr MCTypeInfoRef& type_info() { return kMCNaturalUIntTypeInfo; }
353+
static constexpr auto describe_format = "<foreign natural unsigned integer %zu>";
354+
static constexpr auto is_promotable = false;
355+
};
356+
357+
struct naturalsint_type_desc_t: public integral_type_desc_t<natural_sint_t> {
358+
static constexpr MCTypeInfoRef& type_info() { return kMCNaturalSIntTypeInfo; }
359+
static constexpr auto describe_format = "<foreign natural signed integer %zd>";
360+
static constexpr auto is_promotable = false;
361+
};
362+
363+
#if defined(__32_BIT__)
364+
struct naturalfloat_type_desc_t: public float_type_desc_t {
365+
static constexpr MCTypeInfoRef& type_info() { return kMCNaturalFloatTypeInfo; }
366+
static constexpr auto describe_format = "<foreign natural float %lg>";
367+
};
368+
#elif defined(__64_BIT__)
369+
struct naturalfloat_type_desc_t: public double_type_desc_t {
370+
static constexpr MCTypeInfoRef& type_info() { return kMCNaturalFloatTypeInfo; }
371+
static constexpr auto describe_format = "<foreign natural float %lg>";
372+
};
373+
#endif
374+
375+
321376
/**/
322377

323378
struct cbool_type_desc_t: public bool_type_desc_t
@@ -1044,6 +1099,9 @@ bool __MCForeignValueInitialize(void)
10441099
DescriptorBuilder<uintptr_type_desc_t>::create("__builtin__.uintptr") &&
10451100
DescriptorBuilder<sintptr_type_desc_t>::create("__builtin__.sintptr") &&
10461101
DescriptorBuilder<pointer_type_desc_t>::create("__builtin__.pointer") &&
1102+
DescriptorBuilder<naturaluint_type_desc_t>::create("__builtin__.naturaluint") &&
1103+
DescriptorBuilder<naturalsint_type_desc_t>::create("__builtin__.naturalsint") &&
1104+
DescriptorBuilder<naturalfloat_type_desc_t>::create("__builtin__.naturalfloat") &&
10471105
DescriptorBuilder<cbool_type_desc_t>::create("__builtin__.cbool") &&
10481106
DescriptorBuilder<cuchar_type_desc_t>::create("__builtin__.cuchar") &&
10491107
DescriptorBuilder<cschar_type_desc_t>::create("__builtin__.cschar") &&
@@ -1088,6 +1146,9 @@ void __MCForeignValueFinalize(void)
10881146
MCValueRelease(kMCUIntPtrTypeInfo);
10891147
MCValueRelease(kMCSIntPtrTypeInfo);
10901148
MCValueRelease(kMCPointerTypeInfo);
1149+
MCValueRelease(kMCNaturalUIntTypeInfo);
1150+
MCValueRelease(kMCNaturalSIntTypeInfo);
1151+
MCValueRelease(kMCNaturalFloatTypeInfo);
10911152

10921153
MCValueRelease(kMCCCharTypeInfo);
10931154
MCValueRelease(kMCCUCharTypeInfo);

libfoundation/test/test_foreign.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,15 @@ TEST(foreign, Double)
279279
test_numeric<double>(kMCDoubleTypeInfo, DBL_MIN, DBL_MAX, MCHashDouble, "<foreign double %lg>", MCNumberCreateWithReal);
280280
}
281281

282+
TEST(foreign, NaturalFloat)
283+
{
284+
#ifdef __32_BIT__
285+
test_numeric<float>(kMCNaturalFloatTypeInfo, FLT_MIN, FLT_MAX, MCHashDouble, "<foreign natural float %lg>", MCNumberCreateWithReal);
286+
#else
287+
test_numeric<double>(kMCNaturalFloatTypeInfo, DBL_MIN, DBL_MAX, MCHashDouble, "<foreign natural float %lg>", MCNumberCreateWithReal);
288+
#endif
289+
}
290+
282291
/* Integral Type Tests */
283292

284293
template<typename T>
@@ -401,6 +410,9 @@ TEST_INTEGRAL(SIntSize, ssize_t, "signed size %zd")
401410
TEST_INTEGRAL(UIntPtr, uintptr_t, "unsigned intptr %zu")
402411
TEST_INTEGRAL(SIntPtr, intptr_t, "signed intptr %zd")
403412

413+
TEST_INTEGRAL(NaturalUInt, uintptr_t, "natural unsigned integer %zu")
414+
TEST_INTEGRAL(NaturalSInt, intptr_t, "natural signed integer %zd")
415+
404416
TEST_INTEGRAL(CChar, char, "c char '%c'");
405417
TEST_INTEGRAL(CUChar, unsigned char, "c unsigned char %u")
406418
TEST_INTEGRAL(CSChar, signed char, "c signed char %d")

libscript/src/foreign.lcb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@ public foreign type SIntPtr binds to "MCForeignSIntPtrTypeInfo"
5151

5252
public type IntPtr is SIntPtr
5353

54+
public foreign type NaturalUInt binds to "MCForeignNaturalUIntTypeInfo"
55+
public foreign type NaturalSInt binds to "MCForeignNaturalSIntTypeInfo"
56+
public foreign type NaturalFloat binds to "MCForeignNaturalFloatTypeInfo"
57+
5458
----------------------------------------------------------------
5559
-- C types
5660
----------------------------------------------------------------

0 commit comments

Comments
 (0)