Skip to content
This repository was archived by the owner on Feb 1, 2020. It is now read-only.

Commit 5d9fa4a

Browse files
author
Lukas Hermanns
committed
Added "sinh", "cosh", "tanh", "exp", "exp2", "ceil", and "floor" intrinsics to VM and "Core.xasm".
1 parent f809121 commit 5d9fa4a

File tree

7 files changed

+182
-0
lines changed

7 files changed

+182
-0
lines changed

include/xiexie/xvm.h

+7
Original file line numberDiff line numberDiff line change
@@ -325,11 +325,18 @@ typedef enum
325325
INSC_ASIN, // float ASin(float x)
326326
INSC_ACOS, // float ACos(float x)
327327
INSC_ATAN, // float ATan(float x)
328+
INSC_SINH, // float SinH(float x)
329+
INSC_COSH, // float CosH(float x)
330+
INSC_TANH, // float TanH(float x)
331+
INSC_EXP, // float Exp(float x)
332+
INSC_EXP2, // float Exp2(float x)
328333
INSC_POW, // float Pow(float base, float exp)
329334
INSC_LOG, // float Log(float x)
330335
INSC_LOG2, // float Log2(float x)
331336
INSC_LOG10, // float Log10(float x)
332337
INSC_SQRT, // float Sqrt(float x)
338+
INSC_CEIL, // float Ceil(float x)
339+
INSC_FLOOR, // float Floor(float x)
333340

334341
/* --- Other intrinsics --- */
335342
INSC_RAND_INT, // int RandInt() -> In range [0 .. MAX_INT]

library/Core.xasm

+49
Original file line numberDiff line numberDiff line change
@@ -1551,6 +1551,41 @@ CMath.Patan,F?x:
15511551
insc ATan
15521552
ret 1
15531553

1554+
; float Math.sinh(float x)
1555+
CMath.Psinh,F?x:
1556+
ldw $tr, ($lb) -4
1557+
push $tr
1558+
insc SinH
1559+
ret 1
1560+
1561+
; float Math.cosh(float x)
1562+
CMath.Pcosh,F?x:
1563+
ldw $tr, ($lb) -4
1564+
push $tr
1565+
insc CosH
1566+
ret 1
1567+
1568+
; float Math.tanh(float x)
1569+
CMath.Ptanh,F?x:
1570+
ldw $tr, ($lb) -4
1571+
push $tr
1572+
insc TanH
1573+
ret 1
1574+
1575+
; float Math.exp(float x)
1576+
CMath.Pexp,F?x:
1577+
ldw $tr, ($lb) -4
1578+
push $tr
1579+
insc Exp
1580+
ret 1
1581+
1582+
; float Math.exp2(float x)
1583+
CMath.Pexp2,F?x:
1584+
ldw $tr, ($lb) -4
1585+
push $tr
1586+
insc Exp2
1587+
ret 1
1588+
15541589
; float Math.pow(float b, float e)
15551590
CMath.Ppow,F?b,F?e:
15561591
ldw $tr, ($lb) -8
@@ -1588,6 +1623,20 @@ CMath.Psqrt,F?x:
15881623
insc Sqrt
15891624
ret 1
15901625

1626+
; float Math.ceil(float x)
1627+
CMath.Pceil,F?x:
1628+
ldw $tr, ($lb) -4
1629+
push $tr
1630+
insc Ceil
1631+
ret 1
1632+
1633+
; float Math.floor(float x)
1634+
CMath.Pfloor,F?x:
1635+
ldw $tr, ($lb) -4
1636+
push $tr
1637+
insc Floor
1638+
ret 1
1639+
15911640
; int Math.randInt()
15921641
CMath.PrandInt:
15931642
insc RandInt

library/Core.xx

+7
Original file line numberDiff line numberDiff line change
@@ -309,11 +309,18 @@ extern class Math {
309309
static float asin(float x)
310310
static float acos(float x)
311311
static float atan(float x)
312+
static float sinh(float x)
313+
static float cosh(float x)
314+
static float tanh(float x)
315+
static float exp(float x)
316+
static float exp2(float x)
312317
static float pow(float b, float e)
313318
static float log(float x)
314319
static float log2(float x)
315320
static float log10(float x)
316321
static float sqrt(float x)
322+
static float ceil(float x)
323+
static float floor(float x)
317324

318325
static int randInt()
319326
static float randFloat()

sources/Compiler/CodeGenerators/XASM/Core.xasm.h

+35
Original file line numberDiff line numberDiff line change
@@ -986,6 +986,31 @@
986986
"push $tr\n"
987987
"insc ATan\n"
988988
"ret 1\n"
989+
"CMath.Psinh,F?x:\n"
990+
"ldw $tr, ($lb) -4\n"
991+
"push $tr\n"
992+
"insc SinH\n"
993+
"ret 1\n"
994+
"CMath.Pcosh,F?x:\n"
995+
"ldw $tr, ($lb) -4\n"
996+
"push $tr\n"
997+
"insc CosH\n"
998+
"ret 1\n"
999+
"CMath.Ptanh,F?x:\n"
1000+
"ldw $tr, ($lb) -4\n"
1001+
"push $tr\n"
1002+
"insc TanH\n"
1003+
"ret 1\n"
1004+
"CMath.Pexp,F?x:\n"
1005+
"ldw $tr, ($lb) -4\n"
1006+
"push $tr\n"
1007+
"insc Exp\n"
1008+
"ret 1\n"
1009+
"CMath.Pexp2,F?x:\n"
1010+
"ldw $tr, ($lb) -4\n"
1011+
"push $tr\n"
1012+
"insc Exp2\n"
1013+
"ret 1\n"
9891014
"CMath.Ppow,F?b,F?e:\n"
9901015
"ldw $tr, ($lb) -8\n"
9911016
"push $tr\n"
@@ -1013,6 +1038,16 @@
10131038
"push $tr\n"
10141039
"insc Sqrt\n"
10151040
"ret 1\n"
1041+
"CMath.Pceil,F?x:\n"
1042+
"ldw $tr, ($lb) -4\n"
1043+
"push $tr\n"
1044+
"insc Ceil\n"
1045+
"ret 1\n"
1046+
"CMath.Pfloor,F?x:\n"
1047+
"ldw $tr, ($lb) -4\n"
1048+
"push $tr\n"
1049+
"insc Floor\n"
1050+
"ret 1\n"
10161051
"CMath.PrandInt:\n"
10171052
"insc RandInt\n"
10181053
"ret\n"

sources/Compiler/ContextAnalyzer/StdCodeFactory.cpp

+7
Original file line numberDiff line numberDiff line change
@@ -581,11 +581,18 @@ static std::unique_ptr<ClassDeclStmnt> GenMathClass()
581581
GenStaticProc(*ast, floatType, "asin", GenParam(floatType, "x"));
582582
GenStaticProc(*ast, floatType, "acos", GenParam(floatType, "x"));
583583
GenStaticProc(*ast, floatType, "atan", GenParam(floatType, "x"));
584+
GenStaticProc(*ast, floatType, "sinh", GenParam(floatType, "x"));
585+
GenStaticProc(*ast, floatType, "cosh", GenParam(floatType, "x"));
586+
GenStaticProc(*ast, floatType, "tanh", GenParam(floatType, "x"));
587+
GenStaticProc(*ast, floatType, "exp", GenParam(floatType, "x"));
588+
GenStaticProc(*ast, floatType, "exp2", GenParam(floatType, "x"));
584589
GenStaticProc(*ast, floatType, "pow", ( GenParam(floatType, "b"), GenParam(floatType, "e") ));
585590
GenStaticProc(*ast, floatType, "log", GenParam(floatType, "x"));
586591
GenStaticProc(*ast, floatType, "log2", GenParam(floatType, "x"));
587592
GenStaticProc(*ast, floatType, "log10", GenParam(floatType, "x"));
588593
GenStaticProc(*ast, floatType, "sqrt", GenParam(floatType, "x"));
594+
GenStaticProc(*ast, floatType, "ceil", GenParam(floatType, "x"));
595+
GenStaticProc(*ast, floatType, "floor", GenParam(floatType, "x"));
589596

590597
GenStaticProc(*ast, Int(), "randInt");
591598
GenStaticProc(*ast, floatType, "randFloat");

sources/VirtualMachine/xvm.c

+70
Original file line numberDiff line numberDiff line change
@@ -543,11 +543,18 @@ const char* xvm_intrinsic_get_ident(const xvm_intrinsic_id addr)
543543
case INSC_ASIN: return "ASin";
544544
case INSC_ACOS: return "ACos";
545545
case INSC_ATAN: return "ATan";
546+
case INSC_SINH: return "SinH";
547+
case INSC_COSH: return "CosH";
548+
case INSC_TANH: return "TanH";
549+
case INSC_EXP: return "Exp";
550+
case INSC_EXP2: return "Exp2";
546551
case INSC_POW: return "Pow";
547552
case INSC_LOG: return "Log";
548553
case INSC_LOG2: return "Log2";
549554
case INSC_LOG10: return "Log10";
550555
case INSC_SQRT: return "Sqrt";
556+
case INSC_CEIL: return "Ceil";
557+
case INSC_FLOOR: return "Floor";
551558

552559
/* --- Other intrinsics --- */
553560

@@ -2539,6 +2546,51 @@ static void _xvm_call_intrinsic(unsigned int intrsc_id, xvm_stack* const stack,
25392546
}
25402547
break;
25412548

2549+
// float SinH(float x)
2550+
case INSC_SINH:
2551+
{
2552+
int arg0 = _xvm_stack_pop(stack, reg_sp);
2553+
float result = sinhf(INT_TO_FLT_REINTERPRET(arg0));
2554+
*reg_ar = FLT_TO_INT_REINTERPRET(result);
2555+
}
2556+
break;
2557+
2558+
// float CosH(float x)
2559+
case INSC_COSH:
2560+
{
2561+
int arg0 = _xvm_stack_pop(stack, reg_sp);
2562+
float result = coshf(INT_TO_FLT_REINTERPRET(arg0));
2563+
*reg_ar = FLT_TO_INT_REINTERPRET(result);
2564+
}
2565+
break;
2566+
2567+
// float TanH(float x)
2568+
case INSC_TANH:
2569+
{
2570+
int arg0 = _xvm_stack_pop(stack, reg_sp);
2571+
float result = tanhf(INT_TO_FLT_REINTERPRET(arg0));
2572+
*reg_ar = FLT_TO_INT_REINTERPRET(result);
2573+
}
2574+
break;
2575+
2576+
// float Exp(float x)
2577+
case INSC_EXP:
2578+
{
2579+
int arg0 = _xvm_stack_pop(stack, reg_sp);
2580+
float result = expf(INT_TO_FLT_REINTERPRET(arg0));
2581+
*reg_ar = FLT_TO_INT_REINTERPRET(result);
2582+
}
2583+
break;
2584+
2585+
// float Exp2(float x)
2586+
case INSC_EXP2:
2587+
{
2588+
int arg0 = _xvm_stack_pop(stack, reg_sp);
2589+
float result = exp2f(INT_TO_FLT_REINTERPRET(arg0));
2590+
*reg_ar = FLT_TO_INT_REINTERPRET(result);
2591+
}
2592+
break;
2593+
25422594
// float Pow(float base, float exp)
25432595
case INSC_POW:
25442596
{
@@ -2585,6 +2637,24 @@ static void _xvm_call_intrinsic(unsigned int intrsc_id, xvm_stack* const stack,
25852637
}
25862638
break;
25872639

2640+
// float Ceil(float x)
2641+
case INSC_CEIL:
2642+
{
2643+
int arg0 = _xvm_stack_pop(stack, reg_sp);
2644+
float result = ceilf(INT_TO_FLT_REINTERPRET(arg0));
2645+
*reg_ar = FLT_TO_INT_REINTERPRET(result);
2646+
}
2647+
break;
2648+
2649+
// float Floor(float x)
2650+
case INSC_FLOOR:
2651+
{
2652+
int arg0 = _xvm_stack_pop(stack, reg_sp);
2653+
float result = floorf(INT_TO_FLT_REINTERPRET(arg0));
2654+
*reg_ar = FLT_TO_INT_REINTERPRET(result);
2655+
}
2656+
break;
2657+
25882658
/* --- Other intrinsics --- */
25892659

25902660
// int RandInt() -> In range [0 .. MAX_INT]

sources/VirtualMachine/xvm_wrapper.cpp

+7
Original file line numberDiff line numberDiff line change
@@ -313,11 +313,18 @@ Intrinsics::Intrinsics()
313313
{ "ASin", INSC_ASIN },
314314
{ "ACos", INSC_ACOS },
315315
{ "ATan", INSC_ATAN },
316+
{ "SinH", INSC_SINH },
317+
{ "CosH", INSC_COSH },
318+
{ "TanH", INSC_TANH },
319+
{ "Exp", INSC_EXP },
320+
{ "Exp2", INSC_EXP2 },
316321
{ "Pow", INSC_POW },
317322
{ "Log", INSC_LOG },
318323
{ "Log2", INSC_LOG2 },
319324
{ "Log10", INSC_LOG10 },
320325
{ "Sqrt", INSC_SQRT },
326+
{ "Ceil", INSC_CEIL },
327+
{ "Floor", INSC_FLOOR },
321328
{ "RandInt", INSC_RAND_INT },
322329
{ "RandFloat", INSC_RAND_FLOAT },
323330
{ "Time", INSC_TIME },

0 commit comments

Comments
 (0)