Skip to content

Commit 40cb9b3

Browse files
committed
Initial support for saturated arithmetic
The tinygo compiler would sometimes use these instructions making it very useful to have them.
1 parent 8479bb8 commit 40cb9b3

File tree

3 files changed

+53
-0
lines changed

3 files changed

+53
-0
lines changed

src/Interpreter/instructions.cpp

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1294,6 +1294,54 @@ bool i_instr_extension(Module *m, uint8_t opcode) {
12941294
return true;
12951295
}
12961296

1297+
/**
1298+
* 0xfc misc operations for saturated arithmetic and bulk memory/tables
1299+
*/
1300+
bool i_instr_sat_bulk(Module *m) {
1301+
uint32_t opcode = read_LEB_32(&m->pc_ptr);
1302+
switch (opcode) {
1303+
case 0x00: // i32.trunc_sat_s_f32
1304+
m->stack[m->sp].value.int32 = m->stack[m->sp].value.f32;
1305+
m->stack[m->sp].value_type = I32;
1306+
break;
1307+
case 0x01: // i32.trunc_sat_u_f32
1308+
m->stack[m->sp].value.uint32 = m->stack[m->sp].value.f32;
1309+
m->stack[m->sp].value_type = I32;
1310+
break;
1311+
case 0x02: // i32.trunc_sat_s_f64
1312+
m->stack[m->sp].value.int32 = m->stack[m->sp].value.f64;
1313+
m->stack[m->sp].value_type = I32;
1314+
break;
1315+
case 0x03: // i32.trunc_sat_u_f64
1316+
m->stack[m->sp].value.uint32 = m->stack[m->sp].value.f64;
1317+
m->stack[m->sp].value_type = I32;
1318+
break;
1319+
case 0x04: // i64.trunc_sat_s_f32
1320+
m->stack[m->sp].value.int64 = m->stack[m->sp].value.f32;
1321+
m->stack[m->sp].value_type = I64;
1322+
break;
1323+
case 0x05: // i64.trunc_sat_u_f32
1324+
m->stack[m->sp].value.uint64 = m->stack[m->sp].value.f32;
1325+
m->stack[m->sp].value_type = I64;
1326+
break;
1327+
case 0x06: // i64.trunc_sat_s_f64
1328+
m->stack[m->sp].value.int64 = m->stack[m->sp].value.f64;
1329+
m->stack[m->sp].value_type = I64;
1330+
break;
1331+
case 0x07: // i64.trunc_sat_u_f32
1332+
m->stack[m->sp].value.uint64 = m->stack[m->sp].value.f64;
1333+
m->stack[m->sp].value_type = I64;
1334+
break;
1335+
case 0x08 ... 0x11:
1336+
FATAL("Bulk memory/table operations currently not supported!\n");
1337+
break;
1338+
default:
1339+
return false;
1340+
}
1341+
1342+
return true;
1343+
}
1344+
12971345
/**
12981346
* 0xe0 ... 0xe3 callback operations
12991347
*/

src/Interpreter/instructions.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,4 +74,6 @@ bool i_instr_conversion(Module *m, uint8_t opcode);
7474

7575
bool i_instr_extension(Module *m, uint8_t opcode);
7676

77+
bool i_instr_sat_bulk(Module *m);
78+
7779
bool i_instr_callback(Module *m, uint8_t opcode);

src/Interpreter/interpreter.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -440,6 +440,9 @@ bool Interpreter::interpret(Module *m, bool waiting) {
440440
case 0xc0 ... 0xc4:
441441
success &= i_instr_extension(m, opcode);
442442
continue;
443+
// saturated arithmetic + bulk operations
444+
case 0xfc:
445+
success &= i_instr_sat_bulk(m);
443446

444447
// callback operations
445448
case 0xe0 ... 0xe3:

0 commit comments

Comments
 (0)