Back to Content

neg: Wasm text instruction

files/en-us/webassembly/reference/numeric/neg/index.md

latest868 B
Original Source

The neg instructions, short for negate, are used to negate a number. That is, turn a positive number into a negative number and a negative number into a positive number.

{{InteractiveExample("Wat Demo: neg", "tabbed-standard")}}

wat
(module
  (import "console" "log" (func $log (param f32)))
  (func $main

    f32.const 10 ;; load a number onto the stack
    f32.neg ;; negate the values
    call $log ;; log the result

  )
  (start $main)
)
js
const url = "{%wasm-url%}";
await WebAssembly.instantiateStreaming(fetch(url), { console });

Syntax

wat
;; load a number onto the stack
f32.const 2.7

;; negate
f32.neg

;; the top item on the stack will now be -2.7
InstructionBinary opcode
f32.neg0x8c
f64.neg0x9a