1use wasm_bindgen::prelude::*;
2
3
4#[derive(Debug)]
5
6pub enum Error {
7 Invalid(&'static str),
8}
9
10pub type Result<T> = std::result::Result<T, Error>;
11
12
13#[wasm_bindgen]
14pub fn heavy_calc(x: f64) -> f64 {
15 let mut sum = 0.0f64;
16 for i in 0..1_000_000 {
17 let a = 2.0 * (x + i as f64);
18 sum += 0.5 * a.sin();
19 }
20 sum
21}
22
23#[wasm_bindgen]
24pub fn local_time(cycles: f64, freq_hz: f64) -> f64 {
25 cycles / freq_hz
26}
27
28#[wasm_bindgen]
29pub fn local_energy(cycles: f64, freq_hz: f64, kappa: f64) -> f64 {
30 kappa * freq_hz.powi(2) * cycles
31}
32
33#[wasm_bindgen]
34pub fn optimal_frequency(alpha: f64, beta: f64, e_local_j: f64, kappa: f64, t_ref_s: f64) -> f64 {
35 let denom = 2.0 * beta * kappa * t_ref_s;
36 (alpha * e_local_j / denom).cbrt()
37}
38
39#[wasm_bindgen]
40pub fn snr_linear(p_watt: f64, channel_gain: f64, noise_watt: f64) -> f64 {
41 p_watt * channel_gain / noise_watt
42}
43
44#[wasm_bindgen]
45pub fn link_rate_bps(bandwidth_hz: f64, snr_linear_val: f64, subchannels_v: f64) -> f64 {
46 (bandwidth_hz / subchannels_v) * (1.0 + snr_linear_val).log2()
47}
48
49#[wasm_bindgen]
50pub fn link_rate_simplified_bps(bandwidth_hz: f64, spectral_eff_bps_per_hz: f64) -> f64 {
51 bandwidth_hz * spectral_eff_bps_per_hz
52}
53
54#[wasm_bindgen]
55pub fn tx_time_s(data_bits: f64, rate_bps: f64) -> f64 {
56 data_bits / rate_bps
57}
58
59#[wasm_bindgen]
60pub fn tx_energy_j(p_watt: f64, t_tx_s: f64) -> f64 {
61 p_watt * t_tx_s
62}
63
64#[wasm_bindgen]
65pub fn remote_compute_time_s(cycles: f64, dest_freq_hz: f64) -> f64 {
66 cycles / dest_freq_hz
67}
68
69#[wasm_bindgen]
70pub fn efficiency_score(alpha: f64, beta: f64, t_ref_s: f64, t_off_s: f64, e_local_j: f64, e_off_j: f64) -> f64 {
71 let time_term = (t_ref_s - t_off_s) / t_ref_s;
72 let energy_term = (e_local_j - e_off_j) / e_local_j;
73 let q = alpha * time_term + beta * energy_term;
74 q.clamp(-1.0, 1.0)
75}
76
77#[wasm_bindgen]
78pub fn power_for_target_rate_w(
79 target_rate_bps: f64,
80 bandwidth_hz: f64,
81 subchannels_v: f64,
82 channel_gain: f64,
83 noise_watt: f64
84) -> f64 {
85 let exp_arg = target_rate_bps / (bandwidth_hz / subchannels_v);
86 let snr_req = 2f64.powf(exp_arg) - 1.0;
87 (noise_watt / channel_gain) * snr_req.max(0.0)
88}
89
90#[wasm_bindgen]
91pub fn total_offload_time_s(data_bits: f64, rate_bps: f64, cycles: f64, dest_freq_hz: f64) -> f64 {
92 tx_time_s(data_bits, rate_bps) + remote_compute_time_s(cycles, dest_freq_hz)
93}
94
95#[wasm_bindgen]
96pub fn total_offload_energy_j(p_watt: f64, data_bits: f64, rate_bps: f64) -> f64 {
97 let ttx = tx_time_s(data_bits, rate_bps);
98 tx_energy_j(p_watt, ttx)
99}