aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMike Pall <mike>2012-10-07 15:47:11 +0200
committerMike Pall <mike>2012-10-07 15:47:11 +0200
commit0561a5693884d76db5b75f7cc746478b325b311b (patch)
treebeef0d8674adf5b0f24a101e4e92613ee85e996e
parent0d62e2e1ab450a2d2d2291dc9da43606bd573bf7 (diff)
downloadluajit-0561a5693884d76db5b75f7cc746478b325b311b.tar.gz
luajit-0561a5693884d76db5b75f7cc746478b325b311b.tar.bz2
luajit-0561a5693884d76db5b75f7cc746478b325b311b.zip
From Lua 5.2: Add math.log(x, base).
-rw-r--r--src/lib_math.c17
-rw-r--r--src/lj_ffrecord.c22
-rw-r--r--src/lj_vm.h10
-rw-r--r--src/vm_arm.dasc23
-rw-r--r--src/vm_mips.dasc14
-rw-r--r--src/vm_ppc.dasc11
-rw-r--r--src/vm_ppcspe.dasc13
-rw-r--r--src/vm_x86.dasc30
8 files changed, 129 insertions, 11 deletions
diff --git a/src/lib_math.c b/src/lib_math.c
index a1d8a618..e4fc4c0f 100644
--- a/src/lib_math.c
+++ b/src/lib_math.c
@@ -33,7 +33,6 @@ LJLIB_ASM(math_sqrt) LJLIB_REC(math_unary IRFPM_SQRT)
33 lj_lib_checknum(L, 1); 33 lj_lib_checknum(L, 1);
34 return FFH_RETRY; 34 return FFH_RETRY;
35} 35}
36LJLIB_ASM_(math_log) LJLIB_REC(math_unary IRFPM_LOG)
37LJLIB_ASM_(math_log10) LJLIB_REC(math_unary IRFPM_LOG10) 36LJLIB_ASM_(math_log10) LJLIB_REC(math_unary IRFPM_LOG10)
38LJLIB_ASM_(math_exp) LJLIB_REC(math_unary IRFPM_EXP) 37LJLIB_ASM_(math_exp) LJLIB_REC(math_unary IRFPM_EXP)
39LJLIB_ASM_(math_sin) LJLIB_REC(math_unary IRFPM_SIN) 38LJLIB_ASM_(math_sin) LJLIB_REC(math_unary IRFPM_SIN)
@@ -48,6 +47,22 @@ LJLIB_ASM_(math_tanh) LJLIB_REC(math_htrig IRCALL_tanh)
48LJLIB_ASM_(math_frexp) 47LJLIB_ASM_(math_frexp)
49LJLIB_ASM_(math_modf) LJLIB_REC(.) 48LJLIB_ASM_(math_modf) LJLIB_REC(.)
50 49
50LJLIB_ASM(math_log) LJLIB_REC(math_log)
51{
52 double x = lj_lib_checknum(L, 1);
53 if (L->base+1 < L->top) {
54 double y = lj_lib_checknum(L, 2);
55#ifdef LUAJIT_NO_LOG2
56 x = log(x); y = 1.0 / log(y);
57#else
58 x = lj_vm_log2(x); y = 1.0 / lj_vm_log2(y);
59#endif
60 setnumV(L->base-1, x*y); /* Do NOT join the expression to x / y. */
61 return FFH_RES(1);
62 }
63 return FFH_RETRY;
64}
65
51LJLIB_PUSH(57.29577951308232) 66LJLIB_PUSH(57.29577951308232)
52LJLIB_ASM_(math_deg) LJLIB_REC(math_degrad) 67LJLIB_ASM_(math_deg) LJLIB_REC(math_degrad)
53 68
diff --git a/src/lj_ffrecord.c b/src/lj_ffrecord.c
index b9e0d8f9..270c10c5 100644
--- a/src/lj_ffrecord.c
+++ b/src/lj_ffrecord.c
@@ -447,6 +447,28 @@ static void LJ_FASTCALL recff_math_unary(jit_State *J, RecordFFData *rd)
447 J->base[0] = emitir(IRTN(IR_FPMATH), lj_ir_tonum(J, J->base[0]), rd->data); 447 J->base[0] = emitir(IRTN(IR_FPMATH), lj_ir_tonum(J, J->base[0]), rd->data);
448} 448}
449 449
450/* Record math.log. */
451static void LJ_FASTCALL recff_math_log(jit_State *J, RecordFFData *rd)
452{
453 TRef tr = lj_ir_tonum(J, J->base[0]);
454 if (J->base[1]) {
455#ifdef LUAJIT_NO_LOG2
456 uint32_t fpm = IRFPM_LOG;
457#else
458 uint32_t fpm = IRFPM_LOG2;
459#endif
460 TRef trb = lj_ir_tonum(J, J->base[1]);
461 tr = emitir(IRTN(IR_FPMATH), tr, fpm);
462 trb = emitir(IRTN(IR_FPMATH), trb, fpm);
463 trb = emitir(IRTN(IR_DIV), lj_ir_knum_one(J), trb);
464 tr = emitir(IRTN(IR_MUL), tr, trb);
465 } else {
466 tr = emitir(IRTN(IR_FPMATH), tr, IRFPM_LOG);
467 }
468 J->base[0] = tr;
469 UNUSED(rd);
470}
471
450/* Record math.atan2. */ 472/* Record math.atan2. */
451static void LJ_FASTCALL recff_math_atan2(jit_State *J, RecordFFData *rd) 473static void LJ_FASTCALL recff_math_atan2(jit_State *J, RecordFFData *rd)
452{ 474{
diff --git a/src/lj_vm.h b/src/lj_vm.h
index 4f9a10b8..813335e3 100644
--- a/src/lj_vm.h
+++ b/src/lj_vm.h
@@ -60,6 +60,11 @@ LJ_ASMF double lj_vm_floor_hf(double);
60LJ_ASMF double lj_vm_ceil_hf(double); 60LJ_ASMF double lj_vm_ceil_hf(double);
61#endif 61#endif
62#endif 62#endif
63#if defined(LUAJIT_NO_LOG2) || LJ_TARGET_X86ORX64
64LJ_ASMF double lj_vm_log2(double);
65#else
66#define lj_vm_log2 log2
67#endif
63 68
64#if LJ_HASJIT 69#if LJ_HASJIT
65#if LJ_TARGET_X86ORX64 70#if LJ_TARGET_X86ORX64
@@ -80,11 +85,6 @@ LJ_ASMF double lj_vm_trunc_hf(double);
80#endif 85#endif
81#endif 86#endif
82LJ_ASMF double lj_vm_powi(double, int32_t); 87LJ_ASMF double lj_vm_powi(double, int32_t);
83#ifdef LUAJIT_NO_LOG2
84LJ_ASMF double lj_vm_log2(double);
85#else
86#define lj_vm_log2 log2
87#endif
88#ifdef LUAJIT_NO_EXP2 88#ifdef LUAJIT_NO_EXP2
89LJ_ASMF double lj_vm_exp2(double); 89LJ_ASMF double lj_vm_exp2(double);
90#else 90#else
diff --git a/src/vm_arm.dasc b/src/vm_arm.dasc
index 31657b70..331a1b70 100644
--- a/src/vm_arm.dasc
+++ b/src/vm_arm.dasc
@@ -1468,7 +1468,28 @@ static void build_subroutines(BuildCtx *ctx)
1468 | math_extern sqrt 1468 | math_extern sqrt
1469 |.endif 1469 |.endif
1470 | 1470 |
1471 | math_extern log 1471 |.ffunc math_log
1472 |.if HFABI
1473 | ldr CARG2, [BASE, #4]
1474 | cmp NARGS8:RC, #8 // Need exactly 1 argument.
1475 | vldr d0, [BASE]
1476 | bne ->fff_fallback
1477 |.else
1478 | ldrd CARG12, [BASE]
1479 | cmp NARGS8:RC, #8 // Need exactly 1 argument.
1480 | bne ->fff_fallback
1481 |.endif
1482 | checktp CARG2, LJ_TISNUM
1483 | bhs ->fff_fallback
1484 | .IOS mov RA, BASE
1485 | bl extern log
1486 | .IOS mov BASE, RA
1487 |.if HFABI
1488 | b ->fff_resd
1489 |.else
1490 | b ->fff_restv
1491 |.endif
1492 |
1472 | math_extern log10 1493 | math_extern log10
1473 | math_extern exp 1494 | math_extern exp
1474 | math_extern sin 1495 | math_extern sin
diff --git a/src/vm_mips.dasc b/src/vm_mips.dasc
index fdf07343..719d9252 100644
--- a/src/vm_mips.dasc
+++ b/src/vm_mips.dasc
@@ -1486,7 +1486,19 @@ static void build_subroutines(BuildCtx *ctx)
1486 | math_round floor 1486 | math_round floor
1487 | math_round ceil 1487 | math_round ceil
1488 | 1488 |
1489 | math_extern log 1489 |.ffunc math_log
1490 | lw CARG3, HI(BASE)
1491 | li AT, 8
1492 | bne NARGS8:RC, AT, ->fff_fallback // Exactly 1 argument.
1493 |. load_got log
1494 | sltiu AT, CARG3, LJ_TISNUM
1495 | beqz AT, ->fff_fallback
1496 |. nop
1497 | call_extern
1498 |. ldc1 FARG1, 0(BASE)
1499 | b ->fff_resn
1500 |. nop
1501 |
1490 | math_extern log10 1502 | math_extern log10
1491 | math_extern exp 1503 | math_extern exp
1492 | math_extern sin 1504 | math_extern sin
diff --git a/src/vm_ppc.dasc b/src/vm_ppc.dasc
index 7c567aad..6dbfb90d 100644
--- a/src/vm_ppc.dasc
+++ b/src/vm_ppc.dasc
@@ -1804,7 +1804,16 @@ static void build_subroutines(BuildCtx *ctx)
1804 |.else 1804 |.else
1805 | math_extern sqrt 1805 | math_extern sqrt
1806 |.endif 1806 |.endif
1807 | math_extern log 1807 |
1808 |.ffunc math_log
1809 | cmplwi NARGS8:RC, 8
1810 | lwz CARG3, 0(BASE)
1811 | lfd FARG1, 0(BASE)
1812 | bne ->fff_fallback // Need exactly 1 argument.
1813 | checknum CARG3; bge ->fff_fallback
1814 | blex log
1815 | b ->fff_resn
1816 |
1808 | math_extern log10 1817 | math_extern log10
1809 | math_extern exp 1818 | math_extern exp
1810 | math_extern sin 1819 | math_extern sin
diff --git a/src/vm_ppcspe.dasc b/src/vm_ppcspe.dasc
index 0adbcd7e..2af06494 100644
--- a/src/vm_ppcspe.dasc
+++ b/src/vm_ppcspe.dasc
@@ -1426,7 +1426,18 @@ static void build_subroutines(BuildCtx *ctx)
1426 | math_round ceil 1426 | math_round ceil
1427 | 1427 |
1428 | math_extern sqrt 1428 | math_extern sqrt
1429 | math_extern log 1429 |
1430 |.ffunc math_log
1431 | cmplwi NARGS8:RC, 8
1432 | evldd CARG2, 0(BASE)
1433 | bne ->fff_fallback // Need exactly 1 argument.
1434 | checknum CARG2
1435 | evmergehi CARG1, CARG2, CARG2
1436 | checkfail ->fff_fallback
1437 | bl extern log
1438 | evmergelo CRET1, CRET1, CRET2
1439 | b ->fff_restv
1440 |
1430 | math_extern log10 1441 | math_extern log10
1431 | math_extern exp 1442 | math_extern exp
1432 | math_extern sin 1443 | math_extern sin
diff --git a/src/vm_x86.dasc b/src/vm_x86.dasc
index 67984c6c..75683225 100644
--- a/src/vm_x86.dasc
+++ b/src/vm_x86.dasc
@@ -2045,7 +2045,12 @@ static void build_subroutines(BuildCtx *ctx)
2045 |.else 2045 |.else
2046 |.ffunc_n math_sqrt; fsqrt; jmp ->fff_resn 2046 |.ffunc_n math_sqrt; fsqrt; jmp ->fff_resn
2047 |.endif 2047 |.endif
2048 |.ffunc_n math_log, fldln2; fyl2x; jmp ->fff_resn 2048 |
2049 |.ffunc math_log
2050 | cmp NARGS:RD, 1+1; jne ->fff_fallback // Exactly one argument.
2051 | cmp dword [BASE+4], LJ_TISNUM; jae ->fff_fallback
2052 | fldln2; fld qword [BASE]; fyl2x; jmp ->fff_resn
2053 |
2049 |.ffunc_n math_log10, fldlg2; fyl2x; jmp ->fff_resn 2054 |.ffunc_n math_log10, fldlg2; fyl2x; jmp ->fff_resn
2050 |.ffunc_n math_exp; call ->vm_exp_x87; jmp ->fff_resn 2055 |.ffunc_n math_exp; call ->vm_exp_x87; jmp ->fff_resn
2051 | 2056 |
@@ -3157,6 +3162,29 @@ static void build_subroutines(BuildCtx *ctx)
3157 | ret 3162 | ret
3158 |.endif 3163 |.endif
3159 | 3164 |
3165 |// FP log2(x). Called by math.log(x, base).
3166 |->vm_log2:
3167 |.if X64WIN
3168 | movsd qword [rsp+8], xmm0 // Use scratch area.
3169 | fld1
3170 | fld qword [rsp+8]
3171 | fyl2x
3172 | fstp qword [rsp+8]
3173 | movsd xmm0, qword [rsp+8]
3174 |.elif X64
3175 | movsd qword [rsp-8], xmm0 // Use red zone.
3176 | fld1
3177 | fld qword [rsp-8]
3178 | fyl2x
3179 | fstp qword [rsp-8]
3180 | movsd xmm0, qword [rsp-8]
3181 |.else
3182 | fld1
3183 | fld qword [esp+4]
3184 | fyl2x
3185 |.endif
3186 | ret
3187 |
3160 |// FP exponentiation e^x and 2^x. Called by math.exp fast function and 3188 |// FP exponentiation e^x and 2^x. Called by math.exp fast function and
3161 |// from JIT code. Arg/ret on x87 stack. No int/xmm regs modified. 3189 |// from JIT code. Arg/ret on x87 stack. No int/xmm regs modified.
3162 |// Caveat: needs 3 slots on x87 stack! 3190 |// Caveat: needs 3 slots on x87 stack!