Skip to content

Commit ccbac15

Browse files
authored
Define non-trapping float-to-int conversions. (WebAssembly#1089)
* Define non-trapping float-to-int conversions. This also introduces the concept of prefix bytes, and defines the "numeric" prefix byte, used for encodin the new conversion instructions. * Add feature markers. * Add the feature marker in more places. * Rename "numeric" to "misc". See WebAssembly/nontrapping-float-to-int-conversions#5. * Rename opcodes. See WebAssembly/spec#884 (comment).
1 parent d4e7f96 commit ccbac15

2 files changed

Lines changed: 44 additions & 7 deletions

File tree

BinaryEncoding.md

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,20 @@ Note: Currently, the only sizes used are `varint7`, `varint32` and `varint64`.
5757

5858
## Instruction Opcodes
5959

60-
In the MVP, the opcodes of [instructions](Semantics.md) are all encoded in a
61-
single byte since there are fewer than 256 opcodes. Future features like
62-
[SIMD][future simd] and [atomics][future threads]
63-
will bring the total count above 256 and so an extension scheme will be
64-
necessary, designating one or more single-byte values as prefixes for multi-byte
65-
opcodes.
60+
The opcodes for many common instructions are encoded in a single byte.
61+
62+
The opcodes for some families of instructions are encoded as a *prefix byte*
63+
followed by an LEB128 value. Prefix byte values are allocated starting at `0xfe`
64+
and counting downwards.
65+
66+
### Prefix Bytes
67+
68+
| Opcode | Name | Description |
69+
| ------ | -------- | ----------- |
70+
| `0xff` | reserved | Reserved for unknown future language evolution :milky_way: |
71+
| `0xfe` | threads | Expected to be used for [atomics :unicorn:][future threads] |
72+
| `0xfd` | simd | Expected to be used for [SIMD :unicorn:][future simd] |
73+
| `0xfc` | misc | Miscellaneous operations :bowling: |
6674

6775
## Language Types
6876

@@ -802,6 +810,14 @@ for [future :unicorn:][future multiple tables] use and must be 0 in the MVP.
802810
| `f64.convert_s/i64` | `0xb9` | | |
803811
| `f64.convert_u/i64` | `0xba` | | |
804812
| `f64.promote/f32` | `0xbb` | | |
813+
| `i32.trunc_sat_f32_s` | `0xfc` `0x00` | | :bowling: saturating form of `i32.trunc_f32_s` |
814+
| `i32.trunc_sat_f32_u` | `0xfc` `0x01` | | :bowling: saturating form of `i32.trunc_f32_u` |
815+
| `i32.trunc_sat_f64_s` | `0xfc` `0x02` | | :bowling: saturating form of `i32.trunc_f64_s` |
816+
| `i32.trunc_sat_f64_u` | `0xfc` `0x03` | | :bowling: saturating form of `i32.trunc_f64_u` |
817+
| `i64.trunc_sat_f32_s` | `0xfc` `0x04` | | :bowling: saturating form of `i64.trunc_f32_s` |
818+
| `i64.trunc_sat_f32_u` | `0xfc` `0x05` | | :bowling: saturating form of `i64.trunc_f32_u` |
819+
| `i64.trunc_sat_f64_s` | `0xfc` `0x06` | | :bowling: saturating form of `i64.trunc_f64_s` |
820+
| `i64.trunc_sat_f64_u` | `0xfc` `0x07` | | :bowling: saturating form of `i64.trunc_f64_u` |
805821

806822
## Reinterpretations ([described here](Semantics.md#datatype-conversions-truncations-reinterpretations-promotions-and-demotions))
807823

Semantics.md

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,14 @@ detailed in this document.
3535

3636
[:unicorn:][future general] = Planned [future][future general] feature
3737

38+
## Post-MVP Features
39+
40+
Some features were added post-MVP. These are indicated with the following symbols:
41+
42+
| Symbol | Feature |
43+
| ---------- | ------- |
44+
| :bowling: | Saturating float to int conversions |
45+
3846
## Traps
3947

4048
Some operators may *trap* under some conditions, as noted below. In the MVP,
@@ -640,6 +648,14 @@ is NaN, and *ordered* otherwise.
640648
* `f64.convert_u/i32`: convert an unsigned 32-bit integer to a 64-bit float
641649
* `f64.convert_u/i64`: convert an unsigned 64-bit integer to a 64-bit float
642650
* `f64.reinterpret/i64`: reinterpret the bits of a 64-bit integer as a 64-bit float
651+
* `i32.trunc_sat_f32_s`: :bowling: truncate a 32-bit float to a signed 32-bit integer with saturation
652+
* `i32.trunc_sat_f64_s`: :bowling: truncate a 64-bit float to a signed 32-bit integer with saturation
653+
* `i32.trunc_sat_f32_u`: :bowling: truncate a 32-bit float to an unsigned 32-bit integer with saturation
654+
* `i32.trunc_sat_f64_u`: :bowling: truncate a 64-bit float to an unsigned 32-bit integer with saturation
655+
* `i64.trunc_sat_f32_s`: :bowling: truncate a 32-bit float to a signed 64-bit integer with saturation
656+
* `i64.trunc_sat_f64_s`: :bowling: truncate a 64-bit float to a signed 64-bit integer with saturation
657+
* `i64.trunc_sat_f32_u`: :bowling: truncate a 32-bit float to an unsigned 64-bit integer with saturation
658+
* `i64.trunc_sat_f64_u`: :bowling: truncate a 64-bit float to an unsigned 64-bit integer with saturation
643659

644660
Wrapping and extension of integer values always succeed.
645661
Promotion and demotion of floating point values always succeed.
@@ -665,7 +681,12 @@ round-to-nearest ties-to-even rounding.
665681

666682
Truncation from floating point to integer where IEEE 754-2008 would specify an
667683
invalid operator exception (e.g. when the floating point value is NaN or
668-
outside the range which rounds to an integer in range) traps.
684+
outside the range which rounds to an integer in range) is handled as follows:
685+
- For instructions with no exceptional behavior specified, a trap is produced.
686+
- :bowling: For instructions containing `_sat`, no trap is produced, and:
687+
- If the floating-point value is positive, the maximum integer value is returned.
688+
- If the floating-point value is negative, the minimum integer value is returned.
689+
- If the floating-point value is NaN, zero is returned.
669690

670691
## Type-parametric operators
671692

0 commit comments

Comments
 (0)