Skip to content

Commit

Permalink
Added wasm conversion instructions (mdn#17103)
Browse files Browse the repository at this point in the history
* Added wasm conversion instructions

* Specify extend type

* fix trunc example

* Better explanation of wasm wrap

* Fix demote doc mistake
  • Loading branch information
MendyBerger authored Jun 27, 2022
1 parent e0671a9 commit fe523b0
Show file tree
Hide file tree
Showing 9 changed files with 274 additions and 4 deletions.
38 changes: 38 additions & 0 deletions files/en-us/webassembly/reference/numeric/convert/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
---
title: Convert
slug: WebAssembly/Reference/Numeric/Convert
tags:
- WebAssembly
- wasm
- Reference
- Numeric
- Conversion
---
{{WebAssemblySidebar}}

The **`convert`** instructions, are used for converting integer numbers to floating point numbers. There are signed and unsigned versions of this instruction.

{{EmbedInteractiveExample("pages/wat/convert.html", "tabbed-taller")}}

## Syntax

```wasm
;; push an i32 onto the stack
i32.const 10
;; convert from signed i32 to f32
f32.convert_i32_s
;; the top item on the stack will now be the value 10 of type f32
```

| Instruction | Binary opcode |
| ------------------- | ------------- |
| `f32.convert_i32_s` | `0xb2` |
| `f32.convert_i32_u` | `0xb3` |
| `f32.convert_i64_s` | `0xb4` |
| `f32.convert_i64_u` | `0xb5` |
| `f64.convert_i32_s` | `0xb7` |
| `f64.convert_i32_u` | `0xb8` |
| `f64.convert_i64_s` | `0xb9` |
| `f64.convert_i64_u` | `0xba` |
31 changes: 31 additions & 0 deletions files/en-us/webassembly/reference/numeric/demote/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
---
title: Demote
slug: WebAssembly/Reference/Numeric/Demote
tags:
- WebAssembly
- wasm
- Reference
- Numeric
- Conversion
---
{{WebAssemblySidebar}}

The **`demote`** instruction, is used to convert (demote) numbers of type `f64` to type `f32`.

{{EmbedInteractiveExample("pages/wat/demote.html", "tabbed-taller")}}

## Syntax

```wasm
;; push an f64 onto the stack
f64.const 10.5
;; demote from f64 to f32
f32.demote_f64
;; the top item on the stack will now be the value 10.5 of type f32
```

| Instruction | Binary opcode |
| ---------------- | ------------- |
| `f32.demote_f64` | `0xb6` |
34 changes: 34 additions & 0 deletions files/en-us/webassembly/reference/numeric/extend/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
---
title: Extend
slug: WebAssembly/Reference/Numeric/Extend
tags:
- WebAssembly
- wasm
- Reference
- Numeric
- Conversion
---
{{WebAssemblySidebar}}


The **`extend`** instructions, are used to convert (extend) numbers of type `i32` to type `i64`. There are signed and unsigned versions of this instruction.


{{EmbedInteractiveExample("pages/wat/extend.html", "tabbed-taller")}}

## Syntax

```wasm
;; push an i32 onto the stack
i32.const 10
;; sign-extend from i32 to i64
i64.extend_i32_s
;; the top item on the stack will now be the value 10 of type i64
```

| Instruction | Binary opcode |
| ------------------ | ------------- |
| `i64.extend_i32_s` | `0xac` |
| `i64.extend_i32_u` | `0xad` |
25 changes: 24 additions & 1 deletion files/en-us/webassembly/reference/numeric/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,29 @@ WebAssembly numeric instructions.
- [`Remainder`](/en-US/docs/WebAssembly/Reference/Numeric/Remainder)
- : Calculate the remainder left over when one integer is divided by another integer.

## Conversion

- [`Extend`](/en-US/docs/WebAssembly/Reference/Numeric/Extend)
- : Convert (extend) `i32` to `i64`.

- [`Wrap`](/en-US/docs/WebAssembly/Reference/Numeric/Wrap)
- : Convert (wrap) `i64` to `i32`.

- [`Promote`](/en-US/docs/WebAssembly/Reference/Numeric/Promote)
- : Convert (promote) `f32` to `f64`.

- [`Demote`](/en-US/docs/WebAssembly/Reference/Numeric/Demote)
- : Convert (demote) `f64` to `f32`.

- [`Convert`](/en-US/docs/WebAssembly/Reference/Numeric/Convert)
- : Convert integers to floating points.

- [`Truncate (float to int)`](/en-US/docs/WebAssembly/Reference/Numeric/Truncate_float_to_int)
- : Convert (truncate fractional part) floating points to integers.

- [`Reinterpret`](/en-US/docs/WebAssembly/Reference/Numeric/Reinterpret)
- : Reinterpret the bytes of integers as floating points and vice versa.

## Floating point specific instructions

- [`Min`](/en-US/docs/WebAssembly/Reference/Numeric/Min)
Expand All @@ -71,7 +94,7 @@ WebAssembly numeric instructions.
- [`Floor`](/en-US/docs/WebAssembly/Reference/Numeric/Floor)
- : Round down a number.

- [`Truncate`](/en-US/docs/WebAssembly/Reference/Numeric/Truncate)
- [`Truncate (float to float)`](/en-US/docs/WebAssembly/Reference/Numeric/Truncate_float_to_float)
- : Discard the fractional part of a number.

- [`Absolute`](/en-US/docs/WebAssembly/Reference/Numeric/Absolute)
Expand Down
31 changes: 31 additions & 0 deletions files/en-us/webassembly/reference/numeric/promote/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
---
title: Promote
slug: WebAssembly/Reference/Numeric/Promote
tags:
- WebAssembly
- wasm
- Reference
- Numeric
- Conversion
---
{{WebAssemblySidebar}}

The **`promote`** instruction, is used to convert (promote) numbers of type `f32` to type `f64`.

{{EmbedInteractiveExample("pages/wat/promote.html", "tabbed-taller")}}

## Syntax

```wasm
;; push an f32 onto the stack
f32.const 10.5
;; promote from f32 to f64
f64.promote_f32
;; the top item on the stack will now be the value 10.5 of type f64
```

| Instruction | Binary opcode |
| ----------------- | ------------- |
| `f64.promote_f32` | `0xbb` |
37 changes: 37 additions & 0 deletions files/en-us/webassembly/reference/numeric/reinterpret/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
---
title: Reinterpret
slug: WebAssembly/Reference/Numeric/Reinterpret
tags:
- WebAssembly
- wasm
- Reference
- Numeric
- Conversion
---
{{WebAssemblySidebar}}

The **`reinterpret`** instructions, are used to reinterpret the bits of a number as a different type.

{{EmbedInteractiveExample("pages/wat/reinterpret.html", "tabbed-taller")}}

## Syntax

```wasm
;; the value `10000000_00000000_00000000_00000000` in binary
;; maps to `-0` as a floating point and to `-2147483648` as an integer
;; push an f32 onto the stack
f32.const -0
;; reinterpret the bytes of the f32 as i32
i32.reinterpret_f32
;; the top item on the stack will now be the value -2147483648 of type i32
```

| Instruction | Binary opcode |
| --------------------- | ------------- |
| `i32.reinterpret_f32` | `0xbc` |
| `i64.reinterpret_f64` | `0xbd` |
| `f32.reinterpret_i32` | `0xbe` |
| `f64.reinterpret_i64` | `0xbf` |
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
title: Truncate
slug: WebAssembly/Reference/Numeric/Truncate
title: Truncate (float to float)
slug: WebAssembly/Reference/Numeric/Truncate_float_to_float
tags:
- WebAssembly
- wasm
Expand All @@ -14,7 +14,9 @@ The **`trunc`** instructions, short for *truncate*, are used for getting the val

**`trunc`** differs from **`floor`** when used on negative numbers, **`floor`** will round down in those cases while **`trunc`** will round up.

{{EmbedInteractiveExample("pages/wat/trunc.html", "tabbed-standard")}}
There's another [**`trunc`**](/en-US/docs/WebAssembly/Reference/Numeric/Truncate_float_to_int) instruction that truncates the fractional part of a floating point and converts it to an integer.

{{EmbedInteractiveExample("pages/wat/trunc_float_to_float.html", "tabbed-standard")}}

## Syntax

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
---
title: Truncate (float to int)
slug: WebAssembly/Reference/Numeric/Truncate_float_to_int
tags:
- WebAssembly
- wasm
- Reference
- Numeric
- Conversion
---
{{WebAssemblySidebar}}

The **`trunc`** instructions, are used for converting floating points to integers. It's named truncate since it truncates the fractional part of the number when doing the conversion. There are signed and unsigned versions of this instruction.

There's another [**`trunc`**](/en-US/docs/WebAssembly/Reference/Numeric/Truncate_float_to_float) instruction that truncates the fractional part of a floating point without converting it to and integer.

{{EmbedInteractiveExample("pages/wat/trunc_float_to_int.html", "tabbed-taller")}}


## Syntax

```wasm
;; push an f32 onto the stack
f32.const 10.5
;; convert from f32 to signed i32 rounding towards zero (.5 will be lost)
i32.trunc_f32_s
;; the top item on the stack will now be the value 10 of type f32
```

| Instruction | Binary opcode |
| ----------------- | ------------- |
| `i32.trunc_f32_s` | `0xa8` |
| `i32.trunc_f32_u` | `0xa9` |
| `i32.trunc_f64_s` | `0xaa` |
| `i32.trunc_f64_u` | `0xab` |
| `i64.trunc_f32_s` | `0xae` |
| `i64.trunc_f32_u` | `0xaf` |
| `i64.trunc_f64_s` | `0xb0` |
| `i64.trunc_f64_u` | `0xb1` |
33 changes: 33 additions & 0 deletions files/en-us/webassembly/reference/numeric/wrap/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
---
title: Wrap
slug: WebAssembly/Reference/Numeric/Wrap
tags:
- WebAssembly
- wasm
- Reference
- Numeric
- Something
---
{{WebAssemblySidebar}}

The **`wrap`** instruction, is used to convert numbers of type `i64` to type `i32`. If the number is larger than what an `i32` can hold this operation will wrap, resulting in a different number.

One can think of wrap either as reducing the value [mod](https://en.wikipedia.org/wiki/Modular_arithmetic) 2<sup>32</sup>, or as discarding the high 32 bits to produce a value containing just the low 32 bits.

{{EmbedInteractiveExample("pages/wat/wrap.html", "tabbed-taller")}}

## Syntax

```wasm
;; push an i64 onto the stack
i64.const 10
;; wrap from i64 to i32
i32.wrap_i64
;; the top item on the stack will now be the value 10 of type `i32`
```

| Instruction | Binary opcode |
| -------------- | ------------- |
| `i32.wrap_i64` | `0xa7` |

0 comments on commit fe523b0

Please sign in to comment.