| [ Web Proxy ] |
| Viewing: https://developer.mozilla.org/en-US/docs/WebAssembly/Reference/Numeric/floor | [Back] [Original] |
Get to know MDN better
The floor numeric instruction is used for getting the value of a number rounded down to the next integer.
floor differs from trunc when used on negative numbers floor will round down in those cases while trunc will round up.
(module
(import "console" "log" (func $log (param f32)))
(func $main
f32.const -2.7 ;; load a number onto the stack
f32.floor ;; round down
call $log ;; log the result
)
(start $main)
)
const url = "{%wasm-url%}";
await WebAssembly.instantiateStreaming(fetch(url), { console });
value_type.floor
value_typeThe type of value the instruction is being run on. The following types support floor:
f32f64v128 interpretations:
f32x4f64x2floorThe floor instruction. Must always be included after the value_type and a period (.).
[input] -> [output]
For a non-SIMD floor, these will be basic numeric values such as 14.3 or 3.0.
For a SIMD floor, these will be v128 value interpretations, for example f32x4 1.9 2.5 0.5 12.1. Each lane of the output pushed to the stack is the rounded down value of the corresponding lane in the input value.
| Instruction | Binary format | Example text => binary |
|---|---|---|
f32.floor |
0x8e |
f32.floor => 0x8e |
f64.floor |
0x9c |
f64.floor => 0x9c |
f32x4.floor |
0xfd 104:u32 |
f32x4.floor => 0xfd 0x68 |
f64x2.floor |
0xfd 117:u32 |
f64x2.floor => 0xfd 0x75 |
floor exampleIn this example, we demonstrate running floor on a SIMD value and outputting one of the lane values of the result.
In our script, we grab a reference to a <p> element that we will output our result to, then define an object for import into Wasm containing a single function that writes a value to the output <p>. We then compile and instantiate our Wasm module using the WebAssembly.instantiateStreaming() method, importing the object in the process.
<p></p>
const outputElem = document.querySelector("p");
const obj = {
output(val) {
outputElem.textContent += val;
},
};
WebAssembly.instantiateStreaming(fetch("{%wasm-url%}"), {
obj,
});
In our Wasm module, we first import the JavaScript output() function, making sure to declare that it has an f64 parameter. We then declare a SIMD f64x2 value and use f64x2.floor to round each lane down to the nearest integer. Finally we extract the value stored in lane 0 of the output value using the extract_lane instruction, and output it to the DOM by calling the imported output() function.
(module
;; Import output function
(import "obj" "output" (func $output (param f64)))
(func $main
;; load a SIMD value onto the stack
v128.const f64x2 3.9 2000.1
f64x2.floor ;; Round each value down
f64x2.extract_lane 0 ;; Extract a value from the result
call $output
)
(start $main)
)
The output is as follows:
3 is output because this is the result of rounding down lane 0 of the input value (3.9) to the nearest integer.
| Specification |
|---|
| WebAssembly Core Specification # numeric-instructions%E2%91%A8 |
This page was last modified on Jul 24, 2026 by MDN contributors.
Your blueprint for a better internet.
Portions of this content are 19982026 by individual mozilla.org contributors. Content available under a Creative Commons license.
| Web Proxy Viewer | New URL | Original Page |