|
| 1 | +(module |
| 2 | + ;; precision value p => 1/p! |
| 3 | + (global $precision f64 (f64.const 26.0)) |
| 4 | + |
| 5 | + ;; |
| 6 | + ;; adds two complex numbers |
| 7 | + ;; |
| 8 | + ;; @param $realA {f64} - the real part of the first number |
| 9 | + ;; @param $imagA {f64} - the imaginary part of the first number |
| 10 | + ;; @param $realB {f64} - the real part of the second number |
| 11 | + ;; @param $imagB {f64} - the imaginary part of the second number |
| 12 | + ;; |
| 13 | + ;; @returns {(f64,f64)} - the real and imaginary parts of the complex sum |
| 14 | + ;; |
| 15 | + (func (export "add") (param $realA f64) (param $imagA f64) (param $realB f64) (param $imagB f64) (result f64 f64) |
| 16 | + (f64.add (local.get $realA) (local.get $realB)) |
| 17 | + (f64.add (local.get $imagA) (local.get $imagB)) |
| 18 | + ) |
| 19 | + |
| 20 | + ;; |
| 21 | + ;; subtracts two complex numbers |
| 22 | + ;; |
| 23 | + ;; @param $realA {f64} - the real part of the first number |
| 24 | + ;; @param $imagA {f64} - the imaginary part of the first number |
| 25 | + ;; @param $realB {f64} - the real part of the second number |
| 26 | + ;; @param $imagB {f64} - the imaginary part of the second number |
| 27 | + ;; |
| 28 | + ;; @returns {(f64,f64)} - the real and imaginary parts of the complex difference |
| 29 | + ;; |
| 30 | + (func (export "sub") (param $realA f64) (param $imagA f64) (param $realB f64) (param $imagB f64) (result f64 f64) |
| 31 | + (f64.sub (local.get $realA) (local.get $realB)) |
| 32 | + (f64.sub (local.get $imagA) (local.get $imagB)) |
| 33 | + ) |
| 34 | + |
| 35 | + ;; |
| 36 | + ;; multiplicates two complex numbers |
| 37 | + ;; |
| 38 | + ;; @param $realA {f64} - the real part of the first number |
| 39 | + ;; @param $imagA {f64} - the imaginary part of the first number |
| 40 | + ;; @param $realB {f64} - the real part of the second number |
| 41 | + ;; @param $imagB {f64} - the imaginary part of the second number |
| 42 | + ;; |
| 43 | + ;; @returns {(f64,f64)} - the real and imaginary parts of the complex product |
| 44 | + ;; |
| 45 | + (func (export "mul") (param $realA f64) (param $imagA f64) (param $realB f64) (param $imagB f64) (result f64 f64) |
| 46 | + (f64.sub (f64.mul (local.get $realA) (local.get $realB)) |
| 47 | + (f64.mul (local.get $imagA) (local.get $imagB))) |
| 48 | + (f64.add (f64.mul (local.get $imagA) (local.get $realB)) |
| 49 | + (f64.mul (local.get $imagB) (local.get $realA))) |
| 50 | + ) |
| 51 | + |
| 52 | + ;; |
| 53 | + ;; divides two complex numbers |
| 54 | + ;; |
| 55 | + ;; @param $realA {f64} - the real part of the first number |
| 56 | + ;; @param $imagA {f64} - the imaginary part of the first number |
| 57 | + ;; @param $realB {f64} - the real part of the second number |
| 58 | + ;; @param $imagB {f64} - the imaginary part of the second number |
| 59 | + ;; |
| 60 | + ;; @returns {(f64,f64)} - the real and imaginary parts of the complex quotient |
| 61 | + ;; |
| 62 | + (func (export "div") (param $realA f64) (param $imagA f64) (param $realB f64) (param $imagB f64) (result f64 f64) |
| 63 | + (f64.div (f64.add (f64.mul (local.get $realA) (local.get $realB)) |
| 64 | + (f64.mul (local.get $imagA) (local.get $imagB))) |
| 65 | + (f64.add (f64.mul (local.get $realB) (local.get $realB)) |
| 66 | + (f64.mul (local.get $imagB) (local.get $imagB)))) |
| 67 | + (f64.div (f64.sub (f64.mul (local.get $imagA) (local.get $realB)) |
| 68 | + (f64.mul (local.get $realA) (local.get $imagB))) |
| 69 | + (f64.add (f64.mul (local.get $realB) (local.get $realB)) |
| 70 | + (f64.mul (local.get $imagB) (local.get $imagB)))) |
| 71 | + ) |
| 72 | + |
| 73 | + ;; |
| 74 | + ;; returns the absolute of a complex number |
| 75 | + ;; |
| 76 | + ;; @param $real {f64} - the real part of the number |
| 77 | + ;; @param $imag {f64} - the imaginary part of the number |
| 78 | + ;; |
| 79 | + ;; @returns {f64} - the absolute value of the number |
| 80 | + ;; |
| 81 | + (func (export "abs") (param $real f64) (param $imag f64) (result f64) |
| 82 | + (f64.sqrt (f64.add (f64.mul (local.get $real) (local.get $real)) |
| 83 | + (f64.mul (local.get $imag) (local.get $imag)))) |
| 84 | + ) |
| 85 | + |
| 86 | + ;; |
| 87 | + ;; returns the conjugate of a complex number |
| 88 | + ;; |
| 89 | + ;; @param $real {f64} - the real part of the number |
| 90 | + ;; @param $imag {f64} - the imaginary part of the number |
| 91 | + ;; |
| 92 | + ;; @returns {(f64,f64)} - the real and imaginary parts of the conjugate of the number |
| 93 | + ;; |
| 94 | + (func (export "conj") (param $real f64) (param $imag f64) (result f64 f64) |
| 95 | + (local.get $real) (f64.sub (f64.const 0.0) (local.get $imag)) |
| 96 | + ) |
| 97 | + |
| 98 | + ;; |
| 99 | + ;; exponential function |
| 100 | + ;; |
| 101 | + ;; @param $num {f64} - the number for which the exponent should be calculated |
| 102 | + ;; |
| 103 | + ;; @returns {f64} - the exponential e^num |
| 104 | + ;; |
| 105 | + (func $exp (param $num f64) (result f64) |
| 106 | + (local $product f64) |
| 107 | + (local $sum f64) |
| 108 | + (local $fac f64) |
| 109 | + (local $step f64) |
| 110 | + (local.set $sum (f64.add (f64.const 1.0) (local.get $num))) |
| 111 | + (local.set $product (local.get $num)) |
| 112 | + (local.set $step (f64.const 3.0)) |
| 113 | + (local.set $fac (f64.const 2.0)) |
| 114 | + (loop $series |
| 115 | + (local.set $product (f64.mul (local.get $product) (local.get $num))) |
| 116 | + (local.set $sum (f64.add (local.get $sum) (f64.div (local.get $product) (local.get $fac)))) |
| 117 | + (local.set $fac (f64.mul (local.get $fac) (local.get $step))) |
| 118 | + (local.set $step (f64.add (local.get $step) (f64.const 1.0))) |
| 119 | + (br_if $series (f64.lt (local.get $step) (global.get $precision)))) |
| 120 | + (local.get $sum) |
| 121 | + ) |
| 122 | + |
| 123 | + ;; |
| 124 | + ;; radial sine of a number |
| 125 | + ;; |
| 126 | + ;; @param $num {f64} - the number for which the sine should be calculated |
| 127 | + ;; |
| 128 | + ;; @returns {f64} - the sine of the number |
| 129 | + ;; |
| 130 | + (func $sin (param $num f64) (result f64) |
| 131 | + (local $sum f64) |
| 132 | + (local $square f64) |
| 133 | + (local $product f64) |
| 134 | + (local $fac f64) |
| 135 | + (local $step f64) |
| 136 | + (local.set $sum (local.get $num)) |
| 137 | + (local.set $square (f64.mul (local.get $num) (local.get $num))) |
| 138 | + (local.set $product (local.get $num)) |
| 139 | + (local.set $fac (f64.const 6.0)) |
| 140 | + (local.set $step (f64.const 4.0)) |
| 141 | + (loop $series |
| 142 | + (local.set $product (f64.mul (local.get $product) (local.get $square))) |
| 143 | + (local.set $sum (f64.add (local.get $sum) (f64.div |
| 144 | + (select (local.get $product) (f64.sub (f64.const 0) (local.get $product)) |
| 145 | + (i32.and (i32.trunc_f64_u (local.get $step)) (i32.const 2))) |
| 146 | + (local.get $fac)))) |
| 147 | + (local.set $fac (f64.mul (f64.mul (local.get $fac) (local.get $step)) |
| 148 | + (f64.add (local.get $step) (f64.const 1.0)))) |
| 149 | + (local.set $step (f64.add (local.get $step) (f64.const 2))) |
| 150 | + (br_if $series (f64.lt (local.get $step) (global.get $precision)))) |
| 151 | + (local.get $sum) |
| 152 | + ) |
| 153 | + |
| 154 | + ;; |
| 155 | + ;; radial cosine of a number |
| 156 | + ;; |
| 157 | + ;; @param $num {f64} - the number for which the cosine should be calculated |
| 158 | + ;; |
| 159 | + ;; @returns {f64} - the cosine of the number |
| 160 | + ;; |
| 161 | + (func $cos (param $num f64) (result f64) |
| 162 | + (local $sum f64) |
| 163 | + (local $square f64) |
| 164 | + (local $product f64) |
| 165 | + (local $fac f64) |
| 166 | + (local $step f64) |
| 167 | + (local.set $sum (f64.const 1)) |
| 168 | + (local.set $square (f64.mul (local.get $num) (local.get $num))) |
| 169 | + (local.set $product (f64.const 1)) |
| 170 | + (local.set $fac (f64.const 2.0)) |
| 171 | + (local.set $step (f64.const 3.0)) |
| 172 | + (loop $series |
| 173 | + (local.set $product (f64.mul (local.get $product) (local.get $square))) |
| 174 | + (local.set $sum (f64.add (local.get $sum) (f64.div |
| 175 | + (select (f64.sub (f64.const 0) (local.get $product)) (local.get $product) |
| 176 | + (i32.and (i32.trunc_f64_u (local.get $step)) (i32.const 2))) |
| 177 | + (local.get $fac)))) |
| 178 | + (local.set $fac (f64.mul (f64.mul (local.get $fac) (local.get $step)) |
| 179 | + (f64.add (local.get $step) (f64.const 1.0)))) |
| 180 | + (local.set $step (f64.add (local.get $step) (f64.const 2))) |
| 181 | + (br_if $series (f64.lt (local.get $step) (global.get $precision)))) |
| 182 | + (local.get $sum) |
| 183 | + ) |
| 184 | + |
| 185 | + ;; |
| 186 | + ;; returns the exponentiation of a complex number |
| 187 | + ;; |
| 188 | + ;; @param $real {f64} - the real part of the number |
| 189 | + ;; @param $imag {f64} - the imaginary part of the number |
| 190 | + ;; |
| 191 | + ;; @returns {(f64,f64}} - exponentiation of the complex number |
| 192 | + ;; |
| 193 | + (func (export "exp") (param $real f64) (param $imag f64) (result f64 f64) |
| 194 | + (local $expReal f64) |
| 195 | + (local.set $expReal (call $exp (local.get $real))) |
| 196 | + (f64.mul (local.get $expReal) (call $cos (local.get $imag))) |
| 197 | + (f64.mul (local.get $expReal) (call $sin (local.get $imag))) |
| 198 | + ) |
| 199 | +) |
0 commit comments