forked from mdn/content
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added wasm conversion instructions (mdn#17103)
* Added wasm conversion instructions * Specify extend type * fix trunc example * Better explanation of wasm wrap * Fix demote doc mistake
- Loading branch information
1 parent
e0671a9
commit fe523b0
Showing
9 changed files
with
274 additions
and
4 deletions.
There are no files selected for viewing
38 changes: 38 additions & 0 deletions
38
files/en-us/webassembly/reference/numeric/convert/index.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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` | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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` | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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` | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
files/en-us/webassembly/reference/numeric/promote/index.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
37
files/en-us/webassembly/reference/numeric/reinterpret/index.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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` | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
files/en-us/webassembly/reference/numeric/truncate_float_to_int/index.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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` | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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` | |