mirror of
https://github.com/CPunch/Cosmo.git
synced 2024-11-05 08:10:05 +00:00
replaced facttest.lua with fibtest.lua
Optimized the NaN box to be byte aligned, performance under the fibtest.lua script improved by ~2.5 seconds (~31 before ~28.5 after on cpunch's machine) also cleaned up some misc. comments
This commit is contained in:
parent
f8a062919f
commit
9aa7fa1381
11
examples/fibtest.lua
Normal file
11
examples/fibtest.lua
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
local function fib(num)
|
||||||
|
if num <= 1 then
|
||||||
|
return num
|
||||||
|
else
|
||||||
|
return fib(num-2) + fib(num-1)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
for (var i = 1; i < 40; i++) do
|
||||||
|
print("The fib number of " .. i .. " is " .. fib(i))
|
||||||
|
end
|
@ -1,13 +0,0 @@
|
|||||||
local function fact(num)
|
|
||||||
var total = 1
|
|
||||||
for (var i = num; i > 0; i = i - 1) do
|
|
||||||
total = total * i
|
|
||||||
end
|
|
||||||
return total
|
|
||||||
end
|
|
||||||
|
|
||||||
for (var x = 0; x < 1000; x=x+1) do
|
|
||||||
for (var z = 0; z < 100; z=z+1) do
|
|
||||||
print("The factorial of " .. x .. "." .. z .. " is " .. fact(z))
|
|
||||||
end
|
|
||||||
end
|
|
@ -6,9 +6,7 @@
|
|||||||
#include "cobj.h"
|
#include "cobj.h"
|
||||||
#include "cbaselib.h"
|
#include "cbaselib.h"
|
||||||
|
|
||||||
/*
|
// realloc wrapper
|
||||||
copy buffer to new larger buffer, and free the old buffer
|
|
||||||
*/
|
|
||||||
void *cosmoM_reallocate(CState* state, void *buf, size_t oldSize, size_t newSize) {
|
void *cosmoM_reallocate(CState* state, void *buf, size_t oldSize, size_t newSize) {
|
||||||
state->allocatedBytes += newSize - oldSize;
|
state->allocatedBytes += newSize - oldSize;
|
||||||
|
|
||||||
@ -30,7 +28,7 @@ void *cosmoM_reallocate(CState* state, void *buf, size_t oldSize, size_t newSize
|
|||||||
cosmoM_checkGarbage(state, 0);
|
cosmoM_checkGarbage(state, 0);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// otherwise just use realloc to do all the heavy lifting
|
// if NULL is passed, realloc() acts like malloc()
|
||||||
void *newBuf = realloc(buf, newSize);
|
void *newBuf = realloc(buf, newSize);
|
||||||
|
|
||||||
if (newBuf == NULL) {
|
if (newBuf == NULL) {
|
||||||
@ -247,7 +245,7 @@ COSMO_API void cosmoM_collectGarbage(CState *state) {
|
|||||||
// set our next GC event
|
// set our next GC event
|
||||||
cosmoM_updateThreshhold(state);
|
cosmoM_updateThreshhold(state);
|
||||||
|
|
||||||
state->freezeGC--; // we don't want to use cosmoM_unfreezeGC because that might trigger a GC event
|
state->freezeGC--; // we don't want to use cosmoM_unfreezeGC because that might trigger a GC event (if GC_STRESS is defined)
|
||||||
#ifdef GC_DEBUG
|
#ifdef GC_DEBUG
|
||||||
printf("-- GC end, reclaimed %ld bytes (started at %ld, ended at %ld), next garbage collection scheduled at %ld bytes\n",
|
printf("-- GC end, reclaimed %ld bytes (started at %ld, ended at %ld), next garbage collection scheduled at %ld bytes\n",
|
||||||
start - state->allocatedBytes, start, state->allocatedBytes, state->nextGC);
|
start - state->allocatedBytes, start, state->allocatedBytes, state->nextGC);
|
||||||
|
@ -360,7 +360,7 @@ static void alignStack(CParseState *pstate, int alignment) {
|
|||||||
pstate->compiler->pushedValues = alignment;
|
pstate->compiler->pushedValues = alignment;
|
||||||
}
|
}
|
||||||
|
|
||||||
// ================================================================ [PRATT'S PARSER] ================================================================
|
// ================================================================ [PARSER] ================================================================
|
||||||
|
|
||||||
static void number(CParseState *pstate, bool canAssign) {
|
static void number(CParseState *pstate, bool canAssign) {
|
||||||
cosmo_Number num = strtod(pstate->previous.start, NULL);
|
cosmo_Number num = strtod(pstate->previous.start, NULL);
|
||||||
|
26
src/cvalue.h
26
src/cvalue.h
@ -25,8 +25,6 @@ typedef double cosmo_Number;
|
|||||||
|
|
||||||
both are great resources :)
|
both are great resources :)
|
||||||
|
|
||||||
Performance notes: this can actually degrade performance, so only enable if you know what you're doing.
|
|
||||||
|
|
||||||
TL;DR: we can store payloads in the NaN value in the IEEE 754 standard.
|
TL;DR: we can store payloads in the NaN value in the IEEE 754 standard.
|
||||||
*/
|
*/
|
||||||
typedef union CValue {
|
typedef union CValue {
|
||||||
@ -34,28 +32,28 @@ typedef union CValue {
|
|||||||
cosmo_Number num;
|
cosmo_Number num;
|
||||||
} CValue;
|
} CValue;
|
||||||
|
|
||||||
#define MASK_TYPE ((uint64_t)0x7)
|
#define MASK_TYPE ((uint64_t)0x0007000000000000)
|
||||||
#define MASK_PAYLOAD ((uint64_t)0x0007fffffffffff8)
|
#define MASK_PAYLOAD ((uint64_t)0x0000ffffffffffff)
|
||||||
|
|
||||||
// 3 bits (low bits) are reserved for the type
|
// 3 bits (low bits) are reserved for the type
|
||||||
#define MAKE_PAYLOAD(x) (((uint64_t)(x) << 3) & MASK_PAYLOAD)
|
#define MAKE_PAYLOAD(x) ((uint64_t)(x) & MASK_PAYLOAD)
|
||||||
#define READ_PAYLOAD(x) (((x).data & MASK_PAYLOAD) >> 3)
|
#define READ_PAYLOAD(x) ((x).data & MASK_PAYLOAD)
|
||||||
|
|
||||||
// The bits that must be set to indicate a quiet NaN.
|
// The bits that must be set to indicate a quiet NaN.
|
||||||
#define MASK_QUIETNAN ((uint64_t)0x7ff8000000000000)
|
#define MASK_QUIETNAN ((uint64_t)0x7ff8000000000000)
|
||||||
|
|
||||||
#define GET_TYPE(x) \
|
#define GET_TYPE(x) \
|
||||||
((((x).data & MASK_QUIETNAN) == MASK_QUIETNAN) ? ((x).data & MASK_TYPE) : COSMO_TNUMBER)
|
((((x).data & MASK_QUIETNAN) == MASK_QUIETNAN) ? (((x).data & MASK_TYPE) >> 48) : COSMO_TNUMBER)
|
||||||
|
|
||||||
#define SIG_MASK (MASK_QUIETNAN | MASK_TYPE)
|
static const uint64_t SIG_MASK = (MASK_QUIETNAN | MASK_TYPE);
|
||||||
#define BOOL_SIG (MASK_QUIETNAN | COSMO_TBOOLEAN)
|
static const uint64_t BOOL_SIG = (MASK_QUIETNAN | ((uint64_t)(COSMO_TBOOLEAN) << 48));
|
||||||
#define OBJ_SIG (MASK_QUIETNAN | COSMO_TOBJ)
|
static const uint64_t OBJ_SIG = (MASK_QUIETNAN | ((uint64_t)(COSMO_TOBJ) << 48));
|
||||||
#define NIL_SIG (MASK_QUIETNAN | COSMO_TNIL)
|
static const uint64_t NIL_SIG = (MASK_QUIETNAN | ((uint64_t)(COSMO_TNIL) << 48));
|
||||||
|
|
||||||
#define cosmoV_newNumber(x) ((CValue){.num = x})
|
#define cosmoV_newNumber(x) ((CValue){.num = x})
|
||||||
#define cosmoV_newObj(x) ((CValue){.data = MASK_QUIETNAN | MAKE_PAYLOAD((uintptr_t)x) | COSMO_TOBJ})
|
#define cosmoV_newBoolean(x) ((CValue){.data = MAKE_PAYLOAD(x) | BOOL_SIG})
|
||||||
#define cosmoV_newBoolean(x) ((CValue){.data = MASK_QUIETNAN | MAKE_PAYLOAD(x) | COSMO_TBOOLEAN})
|
#define cosmoV_newObj(x) ((CValue){.data = MAKE_PAYLOAD((uintptr_t)x) | OBJ_SIG})
|
||||||
#define cosmoV_newNil() ((CValue){.data = MASK_QUIETNAN | COSMO_TNIL})
|
#define cosmoV_newNil() ((CValue){.data = NIL_SIG})
|
||||||
|
|
||||||
#define cosmoV_readNumber(x) ((x).num)
|
#define cosmoV_readNumber(x) ((x).num)
|
||||||
#define cosmoV_readBoolean(x) ((bool)READ_PAYLOAD(x))
|
#define cosmoV_readBoolean(x) ((bool)READ_PAYLOAD(x))
|
||||||
|
Loading…
Reference in New Issue
Block a user