Skip to content

Commit 7ba4967

Browse files
committed
Consume libc math functions directly from the jiterpreter without wrappers
Implement more math opcodes and use the f32 functions as appropriate
1 parent e57bb02 commit 7ba4967

File tree

4 files changed

+147
-143
lines changed

4 files changed

+147
-143
lines changed

src/mono/mono/mini/interp/jiterpreter.c

Lines changed: 0 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -1193,87 +1193,6 @@ mono_jiterp_debug_count ()
11931193
return mono_debug_count();
11941194
}
11951195

1196-
EMSCRIPTEN_KEEPALIVE double
1197-
mono_jiterp_math_rem (double lhs, double rhs) {
1198-
return fmod(lhs, rhs);
1199-
}
1200-
1201-
EMSCRIPTEN_KEEPALIVE double
1202-
mono_jiterp_math_atan2 (double lhs, double rhs) {
1203-
return atan2(lhs, rhs);
1204-
}
1205-
1206-
EMSCRIPTEN_KEEPALIVE double
1207-
mono_jiterp_math_pow (double lhs, double rhs) {
1208-
return pow(lhs, rhs);
1209-
}
1210-
1211-
EMSCRIPTEN_KEEPALIVE double
1212-
mono_jiterp_math_acos (double value)
1213-
{
1214-
return acos(value);
1215-
}
1216-
1217-
EMSCRIPTEN_KEEPALIVE double
1218-
mono_jiterp_math_cos (double value)
1219-
{
1220-
return cos(value);
1221-
}
1222-
1223-
EMSCRIPTEN_KEEPALIVE double
1224-
mono_jiterp_math_asin (double value)
1225-
{
1226-
return asin(value);
1227-
}
1228-
1229-
EMSCRIPTEN_KEEPALIVE double
1230-
mono_jiterp_math_sin (double value)
1231-
{
1232-
return sin(value);
1233-
}
1234-
1235-
EMSCRIPTEN_KEEPALIVE double
1236-
mono_jiterp_math_atan (double value)
1237-
{
1238-
return atan(value);
1239-
}
1240-
1241-
EMSCRIPTEN_KEEPALIVE double
1242-
mono_jiterp_math_tan (double value)
1243-
{
1244-
return tan(value);
1245-
}
1246-
1247-
EMSCRIPTEN_KEEPALIVE double
1248-
mono_jiterp_math_exp (double value)
1249-
{
1250-
return exp(value);
1251-
}
1252-
1253-
EMSCRIPTEN_KEEPALIVE double
1254-
mono_jiterp_math_log (double value)
1255-
{
1256-
return log(value);
1257-
}
1258-
1259-
EMSCRIPTEN_KEEPALIVE double
1260-
mono_jiterp_math_log2 (double value)
1261-
{
1262-
return log2(value);
1263-
}
1264-
1265-
EMSCRIPTEN_KEEPALIVE double
1266-
mono_jiterp_math_log10 (double value)
1267-
{
1268-
return log10(value);
1269-
}
1270-
1271-
EMSCRIPTEN_KEEPALIVE double
1272-
mono_jiterp_math_fma (double x, double y, double z)
1273-
{
1274-
return fma(x, y, z);
1275-
}
1276-
12771196
EMSCRIPTEN_KEEPALIVE int
12781197
mono_jiterp_stelem_ref (
12791198
MonoArray *o, gint32 aindex, MonoObject *ref

src/mono/wasm/runtime/jiterpreter-trace-generator.ts

Lines changed: 23 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1062,19 +1062,11 @@ export function generate_wasm_body (
10621062

10631063
// LOCAL_VAR (ip [1], double) = fma (LOCAL_VAR (ip [2], double), LOCAL_VAR (ip [3], double), LOCAL_VAR (ip [4], double));
10641064
append_ldloc(builder, getArgU16(ip, 2), loadOp);
1065-
if (isF32)
1066-
builder.appendU8(WasmOpcode.f64_promote_f32);
10671065
append_ldloc(builder, getArgU16(ip, 3), loadOp);
1068-
if (isF32)
1069-
builder.appendU8(WasmOpcode.f64_promote_f32);
10701066
append_ldloc(builder, getArgU16(ip, 4), loadOp);
1071-
if (isF32)
1072-
builder.appendU8(WasmOpcode.f64_promote_f32);
10731067

1074-
builder.callImport("fma");
1068+
builder.callImport(isF32 ? "fmaf" : "fma");
10751069

1076-
if (isF32)
1077-
builder.appendU8(WasmOpcode.f32_demote_f64);
10781070
append_stloc_tail(builder, getArgU16(ip, 1), storeOp);
10791071
break;
10801072
}
@@ -2584,37 +2576,45 @@ const mathIntrinsicTable : { [opcode: number] : [isUnary: boolean, isF32: boolea
25842576
[MintOpcode.MINT_ABSF]: [true, true, WasmOpcode.f32_abs],
25852577

25862578
[MintOpcode.MINT_ACOS]: [true, false, "acos"],
2587-
[MintOpcode.MINT_ACOSF]: [true, true, "acos"],
2579+
[MintOpcode.MINT_ACOSF]: [true, true, "acosf"],
2580+
[MintOpcode.MINT_ACOSH]: [true, false, "acosh"],
2581+
[MintOpcode.MINT_ACOSHF]: [true, true, "acoshf"],
25882582
[MintOpcode.MINT_COS]: [true, false, "cos"],
2589-
[MintOpcode.MINT_COSF]: [true, true, "cos"],
2583+
[MintOpcode.MINT_COSF]: [true, true, "cosf"],
25902584
[MintOpcode.MINT_ASIN]: [true, false, "asin"],
2591-
[MintOpcode.MINT_ASINF]: [true, true, "asin"],
2585+
[MintOpcode.MINT_ASINF]: [true, true, "asinf"],
2586+
[MintOpcode.MINT_ASINH]: [true, false, "asinh"],
2587+
[MintOpcode.MINT_ASINHF]: [true, true, "asinhf"],
25922588
[MintOpcode.MINT_SIN]: [true, false, "sin"],
2593-
[MintOpcode.MINT_SINF]: [true, true, "sin"],
2589+
[MintOpcode.MINT_SINF]: [true, true, "sinf"],
25942590
[MintOpcode.MINT_ATAN]: [true, false, "atan"],
2595-
[MintOpcode.MINT_ATANF]: [true, true, "atan"],
2591+
[MintOpcode.MINT_ATANF]: [true, true, "atanf"],
2592+
[MintOpcode.MINT_ATANH]: [true, false, "atanh"],
2593+
[MintOpcode.MINT_ATANHF]: [true, true, "atanhf"],
25962594
[MintOpcode.MINT_TAN]: [true, false, "tan"],
2597-
[MintOpcode.MINT_TANF]: [true, true, "tan"],
2595+
[MintOpcode.MINT_TANF]: [true, true, "tanf"],
2596+
[MintOpcode.MINT_CBRT]: [true, false, "cbrt"],
2597+
[MintOpcode.MINT_CBRTF]: [true, true, "cbrtf"],
25982598
[MintOpcode.MINT_EXP]: [true, false, "exp"],
2599-
[MintOpcode.MINT_EXPF]: [true, true, "exp"],
2599+
[MintOpcode.MINT_EXPF]: [true, true, "expf"],
26002600
[MintOpcode.MINT_LOG]: [true, false, "log"],
2601-
[MintOpcode.MINT_LOGF]: [true, true, "log"],
2601+
[MintOpcode.MINT_LOGF]: [true, true, "logf"],
26022602
[MintOpcode.MINT_LOG2]: [true, false, "log2"],
2603-
[MintOpcode.MINT_LOG2F]: [true, true, "log2"],
2603+
[MintOpcode.MINT_LOG2F]: [true, true, "log2f"],
26042604
[MintOpcode.MINT_LOG10]: [true, false, "log10"],
2605-
[MintOpcode.MINT_LOG10F]: [true, true, "log10"],
2605+
[MintOpcode.MINT_LOG10F]: [true, true, "log10f"],
26062606

26072607
[MintOpcode.MINT_MIN]: [false, false, WasmOpcode.f64_min],
26082608
[MintOpcode.MINT_MINF]: [false, true, WasmOpcode.f32_min],
26092609
[MintOpcode.MINT_MAX]: [false, false, WasmOpcode.f64_max],
26102610
[MintOpcode.MINT_MAXF]: [false, true, WasmOpcode.f32_max],
26112611

26122612
[MintOpcode.MINT_ATAN2]: [false, false, "atan2"],
2613-
[MintOpcode.MINT_ATAN2F]: [false, true, "atan2"],
2613+
[MintOpcode.MINT_ATAN2F]: [false, true, "atan2f"],
26142614
[MintOpcode.MINT_POW]: [false, false, "pow"],
2615-
[MintOpcode.MINT_POWF]: [false, true, "pow"],
2616-
[MintOpcode.MINT_REM_R4]: [false, true, "rem"],
2617-
[MintOpcode.MINT_REM_R8]: [false, false, "rem"],
2615+
[MintOpcode.MINT_POWF]: [false, true, "powf"],
2616+
[MintOpcode.MINT_REM_R8]: [false, false, "fmod"],
2617+
[MintOpcode.MINT_REM_R4]: [false, true, "fmodf"],
26182618
};
26192619

26202620
function emit_math_intrinsic (builder: WasmBuilder, ip: MintOpcodePtr, opcode: MintOpcode) : boolean {
@@ -2644,29 +2644,19 @@ function emit_math_intrinsic (builder: WasmBuilder, ip: MintOpcodePtr, opcode: M
26442644
if (wasmOp) {
26452645
builder.appendU8(wasmOp);
26462646
} else if (name) {
2647-
if (isF32)
2648-
builder.appendU8(WasmOpcode.f64_promote_f32);
26492647
builder.callImport(name);
2650-
if (isF32)
2651-
builder.appendU8(WasmOpcode.f32_demote_f64);
26522648
} else
26532649
throw new Error("internal error");
26542650
append_stloc_tail(builder, destOffset, isF32 ? WasmOpcode.f32_store : WasmOpcode.f64_store);
26552651
return true;
26562652
} else {
26572653
append_ldloc(builder, srcOffset, isF32 ? WasmOpcode.f32_load : WasmOpcode.f64_load);
2658-
if (isF32 && name)
2659-
builder.appendU8(WasmOpcode.f64_promote_f32);
26602654
append_ldloc(builder, rhsOffset, isF32 ? WasmOpcode.f32_load : WasmOpcode.f64_load);
2661-
if (isF32 && name)
2662-
builder.appendU8(WasmOpcode.f64_promote_f32);
26632655

26642656
if (wasmOp) {
26652657
builder.appendU8(wasmOp);
26662658
} else if (name) {
26672659
builder.callImport(name);
2668-
if (isF32)
2669-
builder.appendU8(WasmOpcode.f32_demote_f64);
26702660
} else
26712661
throw new Error("internal error");
26722662

src/mono/wasm/runtime/jiterpreter.ts

Lines changed: 75 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -224,24 +224,51 @@ export let traceImports : Array<[string, string, Function]> | undefined;
224224

225225
export let _wrap_trace_function: Function;
226226

227-
const mathOps1 = [
228-
"asin",
229-
"acos",
230-
"atan",
231-
"cos",
232-
"exp",
233-
"log",
234-
"log2",
235-
"log10",
236-
"sin",
237-
"tan",
238-
];
239-
240-
const mathOps2 = [
241-
"rem",
242-
"atan2",
243-
"pow",
244-
];
227+
const mathOps1d = [
228+
"asin",
229+
"acos",
230+
"atan",
231+
"asinh",
232+
"acosh",
233+
"atanh",
234+
"cos",
235+
"sin",
236+
"tan",
237+
"cosh",
238+
"sinh",
239+
"tanh",
240+
"exp",
241+
"log",
242+
"log2",
243+
"log10",
244+
"cbrt",
245+
], mathOps2d = [
246+
"fmod",
247+
"atan2",
248+
"pow",
249+
], mathOps1f = [
250+
"asinf",
251+
"acosf",
252+
"atanf",
253+
"asinhf",
254+
"acoshf",
255+
"atanhf",
256+
"cosf",
257+
"sinf",
258+
"tanf",
259+
"coshf",
260+
"sinhf",
261+
"tanhf",
262+
"expf",
263+
"logf",
264+
"log2f",
265+
"log10f",
266+
"cbrtf",
267+
], mathOps2f = [
268+
"fmodf",
269+
"atan2f",
270+
"powf",
271+
];
245272

246273
function getTraceImports () {
247274
if (traceImports)
@@ -277,7 +304,8 @@ function getTraceImports () {
277304
importDef("cmpxchg_i32", getRawCwrap("mono_jiterp_cas_i32")),
278305
importDef("cmpxchg_i64", getRawCwrap("mono_jiterp_cas_i64")),
279306
importDef("stelem_ref", getRawCwrap("mono_jiterp_stelem_ref")),
280-
importDef("fma", getRawCwrap("mono_jiterp_math_fma")),
307+
importDef("fma", getRawCwrap("fma")),
308+
importDef("fmaf", getRawCwrap("fmaf")),
281309
];
282310

283311
if (instrumentedMethodNames.length > 0) {
@@ -288,15 +316,17 @@ function getTraceImports () {
288316
if (nullCheckValidation)
289317
traceImports.push(importDef("notnull", assert_not_null));
290318

291-
for (let i = 0; i < mathOps1.length; i++) {
292-
const mop = mathOps1[i];
293-
traceImports.push([mop, "mathop_d_d", getRawCwrap("mono_jiterp_math_" + mop)]);
294-
}
319+
const pushMathOps = (list: string[], type: string) => {
320+
for (let i = 0; i < list.length; i++) {
321+
const mop = list[i];
322+
traceImports!.push([mop, type, getRawCwrap(mop)]);
323+
}
324+
};
295325

296-
for (let i = 0; i < mathOps2.length; i++) {
297-
const mop = mathOps2[i];
298-
traceImports.push([mop, "mathop_dd_d", getRawCwrap("mono_jiterp_math_" + mop)]);
299-
}
326+
pushMathOps(mathOps1f, "mathop_f_f");
327+
pushMathOps(mathOps2f, "mathop_ff_f");
328+
pushMathOps(mathOps1d, "mathop_d_d");
329+
pushMathOps(mathOps2d, "mathop_dd_d");
300330

301331
return traceImports;
302332
}
@@ -411,6 +441,24 @@ function initialize_builder (builder: WasmBuilder) {
411441
"rhs": WasmValtype.f64,
412442
}, WasmValtype.f64, true
413443
);
444+
builder.defineType(
445+
"mathop_f_f", {
446+
"value": WasmValtype.f32,
447+
}, WasmValtype.f32, true
448+
);
449+
builder.defineType(
450+
"mathop_ff_f", {
451+
"lhs": WasmValtype.f32,
452+
"rhs": WasmValtype.f32,
453+
}, WasmValtype.f32, true
454+
);
455+
builder.defineType(
456+
"fmaf", {
457+
"x": WasmValtype.f32,
458+
"y": WasmValtype.f32,
459+
"z": WasmValtype.f32,
460+
}, WasmValtype.f32, true
461+
);
414462
builder.defineType(
415463
"fma", {
416464
"x": WasmValtype.f64,

0 commit comments

Comments
 (0)