rename a_pid_fuzzy_equ to a_fuzzy_equ
[liba.git] / javascript / src / a.cpp
blob31b26342132e689f0df59dc49a6eb2aa09466ab7
1 #include "a/a.h"
2 #include <emscripten/bind.h>
3 #if defined(__has_feature) && __has_feature(address_sanitizer)
4 #include <sanitizer/lsan_interface.h>
5 #endif /* -fsanitize=address */
7 namespace
10 static emscripten::val js_concat(emscripten::val x)
12 emscripten::val val = emscripten::val::array();
13 return val["concat"].call<emscripten::val>("apply", val, x);
16 static a_float *js_array_num_get(emscripten::val const &x, a_float *p, a_size n)
18 a_size length = x["length"].as<a_size>();
19 n = (n < length ? length : n) * sizeof(a_float);
20 p = a_cast_s(a_float *, a_alloc(p, n));
21 for (a_size i = 0; i < length; ++i)
23 p[i] = x[i].as<a_float>();
25 return p;
28 static emscripten::val js_array_num_new(a_float const *p, a_size n)
30 return emscripten::val(emscripten::typed_memory_view(n, p));
33 static a_u32 hash_bkdr(std::string const &str, a_u32 val)
35 return a_hash_bkdr_(str.data(), str.length(), val);
38 static a_u32 hash_sdbm(std::string const &str, a_u32 val)
40 return a_hash_sdbm_(str.data(), str.length(), val);
43 } /* namespace */
45 #include "a/crc.h"
47 struct crc8
49 a_u8 table[0x100];
50 std::vector<a_byte> _pack;
51 A_INLINE emscripten::val get_table() const
53 return emscripten::val(emscripten::typed_memory_view(0x100, table));
55 A_INLINE a_u8 operator()(std::string const &block, a_u8 value = 0)
57 return a_crc8(table, block.data(), block.length(), value);
59 A_INLINE emscripten::val pack(std::string const &block, a_u8 value = 0)
61 size_t n = block.length();
62 size_t m = block.length() + sizeof(value);
63 if (_pack.size() < m) { _pack.resize(m); }
64 std::copy(block.data(), block.data() + n, _pack.data());
65 *(_pack.data() + n) = a_crc8(table, block.data(), n, value);
66 return emscripten::val(emscripten::typed_memory_view(m, _pack.data()));
68 A_INLINE crc8 *gen(a_u8 poly, bool reversed = false)
70 reversed ? a_crc8l_init(table, poly) : a_crc8m_init(table, poly);
71 return this;
73 A_INLINE crc8(a_u8 poly, bool reversed = false)
75 gen(poly, reversed);
79 struct crc16
81 a_u16 table[0x100];
82 std::vector<a_byte> _pack;
83 a_u16 (*eval)(a_u16 const[0x100], void const *, a_size, a_u16);
84 A_INLINE emscripten::val get_table() const
86 return emscripten::val(emscripten::typed_memory_view(0x100, table));
88 A_INLINE a_u16 operator()(std::string const &block, a_u16 value = 0)
90 return eval(table, block.data(), block.length(), value);
92 A_INLINE emscripten::val pack(std::string const &block, a_u16 value = 0)
94 size_t n = block.length();
95 size_t m = block.length() + sizeof(value);
96 if (_pack.size() < m) { _pack.resize(m); }
97 std::copy(block.data(), block.data() + n, _pack.data());
98 value = eval(table, block.data(), n, value);
99 a_byte *p = _pack.data() + n;
100 eval == a_crc16m ? a_u16_setb(p, value) : a_u16_setl(p, value);
101 return emscripten::val(emscripten::typed_memory_view(m, _pack.data()));
103 A_INLINE crc16 *gen(a_u16 poly, bool reversed = false)
105 if (reversed)
107 a_crc16l_init(table, poly);
108 eval = a_crc16l;
110 else
112 a_crc16m_init(table, poly);
113 eval = a_crc16m;
115 return this;
117 A_INLINE crc16(a_u16 poly, bool reversed = false)
119 gen(poly, reversed);
123 struct crc32
125 a_u32 table[0x100];
126 std::vector<a_byte> _pack;
127 a_u32 (*eval)(a_u32 const[0x100], void const *, a_size, a_u32);
128 A_INLINE emscripten::val get_table() const
130 return emscripten::val(emscripten::typed_memory_view(0x100, table));
132 A_INLINE a_u32 operator()(std::string const &block, a_u32 value = 0)
134 return eval(table, block.data(), block.length(), value);
136 A_INLINE emscripten::val pack(std::string const &block, a_u32 value = 0)
138 size_t n = block.length();
139 size_t m = block.length() + sizeof(value);
140 if (_pack.size() < m) { _pack.resize(m); }
141 std::copy(block.data(), block.data() + n, _pack.data());
142 value = eval(table, block.data(), n, value);
143 a_byte *p = _pack.data() + n;
144 eval == a_crc32m ? a_u32_setb(p, value) : a_u32_setl(p, value);
145 return emscripten::val(emscripten::typed_memory_view(m, _pack.data()));
147 A_INLINE crc32 *gen(a_u32 poly, bool reversed = false)
149 if (reversed)
151 a_crc32l_init(table, poly);
152 eval = a_crc32l;
154 else
156 a_crc32m_init(table, poly);
157 eval = a_crc32m;
159 return this;
161 A_INLINE crc32(a_u32 poly, bool reversed = false)
163 gen(poly, reversed);
167 #if defined(WASM_BIGINT)
168 struct crc64
170 a_u64 table[0x100];
171 std::vector<a_byte> _pack;
172 a_u64 (*eval)(a_u64 const[0x100], void const *, a_size, a_u64);
173 A_INLINE emscripten::val get_table() const
175 return emscripten::val(emscripten::typed_memory_view(0x100, table));
177 A_INLINE emscripten::val operator()(std::string const &block, a_u64 value = 0)
179 return emscripten::val(eval(table, block.data(), block.length(), value));
181 A_INLINE emscripten::val pack(std::string const &block, a_u64 value = 0)
183 size_t n = block.length();
184 size_t m = block.length() + sizeof(value);
185 if (_pack.size() < m) { _pack.resize(m); }
186 std::copy(block.data(), block.data() + n, _pack.data());
187 value = eval(table, block.data(), n, value);
188 a_byte *p = _pack.data() + n;
189 eval == a_crc64m ? a_u64_setb(p, value) : a_u64_setl(p, value);
190 return emscripten::val(emscripten::typed_memory_view(m, _pack.data()));
192 A_INLINE crc64 *gen(a_u64 poly, bool reversed = false)
194 if (reversed)
196 a_crc64l_init(table, poly);
197 eval = a_crc64l;
199 else
201 a_crc64m_init(table, poly);
202 eval = a_crc64m;
204 return this;
206 A_INLINE crc64(a_u64 poly, bool reversed = false)
208 gen(poly, reversed);
211 #endif /* WASM_BIGINT */
213 #include "a/hpf.h"
215 struct hpf: public a_hpf
217 A_INLINE hpf(a_float fc, a_float ts)
219 alpha = A_HPF_GEN(fc, ts);
220 output = 0;
221 input = 0;
223 A_INLINE hpf(a_float _alpha)
225 alpha = _alpha;
226 output = 0;
227 input = 0;
229 A_INLINE hpf *gen(a_float fc, a_float ts)
231 a_hpf::gen(fc, ts);
232 return this;
234 A_INLINE a_float operator()(a_float x)
236 return a_hpf::operator()(x);
238 A_INLINE hpf *zero()
240 a_hpf::zero();
241 return this;
245 #include "a/lpf.h"
247 struct lpf: public a_lpf
249 A_INLINE lpf(a_float fc, a_float ts)
251 alpha = A_LPF_GEN(fc, ts);
252 output = 0;
254 A_INLINE lpf(a_float _alpha)
256 alpha = _alpha;
257 output = 0;
259 A_INLINE lpf *gen(a_float fc, a_float ts)
261 a_lpf::gen(fc, ts);
262 return this;
264 A_INLINE a_float operator()(a_float x)
266 return a_lpf::operator()(x);
268 A_INLINE lpf *zero()
270 a_lpf::zero();
271 return this;
275 #include "a/mf.h"
277 struct mf
279 static unsigned int const NUL;
280 static a_float gauss(a_float x, a_float sigma, a_float c)
282 return a_mf_gauss(x, sigma, c);
284 static unsigned int const GAUSS;
285 static a_float gauss2(a_float x, a_float sigma1, a_float c1, a_float sigma2, a_float c2)
287 return a_mf_gauss2(x, sigma1, c1, sigma2, c2);
289 static unsigned int const GAUSS2;
290 static a_float gbell(a_float x, a_float a, a_float b, a_float c)
292 return a_mf_gbell(x, a, b, c);
294 static unsigned int const GBELL;
295 static a_float sig(a_float x, a_float a, a_float c)
297 return a_mf_sig(x, a, c);
299 static unsigned int const SIG;
300 static a_float dsig(a_float x, a_float a1, a_float c1, a_float a2, a_float c2)
302 return a_mf_dsig(x, a1, c1, a2, c2);
304 static unsigned int const DSIG;
305 static a_float psig(a_float x, a_float a1, a_float c1, a_float a2, a_float c2)
307 return a_mf_psig(x, a1, c1, a2, c2);
309 static unsigned int const PSIG;
310 static a_float trap(a_float x, a_float a, a_float b, a_float c, a_float d)
312 return a_mf_trap(x, a, b, c, d);
314 static unsigned int const TRAP;
315 static a_float tri(a_float x, a_float a, a_float b, a_float c)
317 return a_mf_tri(x, a, b, c);
319 static unsigned int const TRI;
320 static a_float lins(a_float x, a_float a, a_float b)
322 return a_mf_lins(x, a, b);
324 static unsigned int const LINS;
325 static a_float linz(a_float x, a_float a, a_float b)
327 return a_mf_linz(x, a, b);
329 static unsigned int const LINZ;
330 static a_float s(a_float x, a_float a, a_float b)
332 return a_mf_s(x, a, b);
334 static unsigned int const S;
335 static a_float z(a_float x, a_float a, a_float b)
337 return a_mf_z(x, a, b);
339 static unsigned int const Z;
340 static a_float pi(a_float x, a_float a, a_float b, a_float c, a_float d)
342 return a_mf_pi(x, a, b, c, d);
344 static unsigned int const PI;
346 unsigned int const mf::NUL = A_MF_NUL;
347 unsigned int const mf::GAUSS = A_MF_GAUSS;
348 unsigned int const mf::GAUSS2 = A_MF_GAUSS2;
349 unsigned int const mf::GBELL = A_MF_GBELL;
350 unsigned int const mf::SIG = A_MF_SIG;
351 unsigned int const mf::DSIG = A_MF_DSIG;
352 unsigned int const mf::PSIG = A_MF_PSIG;
353 unsigned int const mf::TRAP = A_MF_TRAP;
354 unsigned int const mf::TRI = A_MF_TRI;
355 unsigned int const mf::LINS = A_MF_LINS;
356 unsigned int const mf::LINZ = A_MF_LINZ;
357 unsigned int const mf::S = A_MF_S;
358 unsigned int const mf::Z = A_MF_Z;
359 unsigned int const mf::PI = A_MF_PI;
361 #include "a/pid.h"
363 struct pid: public a_pid
365 A_INLINE pid *kpid(a_float _kp, a_float _ki, a_float _kd)
367 a_pid::kpid(_kp, _ki, _kd);
368 return this;
370 A_INLINE a_float run(a_float set, a_float _fdb)
372 return a_pid::run(set, _fdb);
374 A_INLINE a_float pos(a_float set, a_float _fdb)
376 return a_pid::pos(set, _fdb);
378 A_INLINE a_float inc(a_float set, a_float _fdb)
380 return a_pid::inc(set, _fdb);
382 A_INLINE pid *zero()
384 a_pid::zero();
385 return this;
387 A_INLINE pid()
389 kp = 1;
390 ki = 0;
391 kd = 0;
392 summax = +A_FLOAT_INF;
393 summin = -A_FLOAT_INF;
394 outmax = +A_FLOAT_INF;
395 outmin = -A_FLOAT_INF;
396 a_pid_init(this);
400 #include "a/pid_fuzzy.h"
402 struct pid_fuzzy: public a_pid_fuzzy
404 A_INLINE pid_fuzzy *rule(emscripten::val const &_me,
405 emscripten::val const &_mec,
406 emscripten::val const &_mkp,
407 emscripten::val const &_mki,
408 emscripten::val const &_mkd)
410 union
412 a_float const *p;
413 a_float *o;
414 } u;
415 order = _me["length"].as<unsigned int>();
416 u.p = me;
417 emscripten::val val = js_concat(_me);
418 me = js_array_num_get(val, u.o, 0);
419 val.delete_(val);
420 u.p = mec;
421 val = js_concat(_mec);
422 mec = js_array_num_get(val, u.o, 0);
423 val.delete_(val);
424 u.p = mkp;
425 val = js_concat(_mkp);
426 mkp = js_array_num_get(val, u.o, 0);
427 val.delete_(val);
428 u.p = mki;
429 val = js_concat(_mki);
430 mki = js_array_num_get(val, u.o, 0);
431 val.delete_(val);
432 u.p = mkd;
433 val = js_concat(_mkd);
434 mkd = js_array_num_get(val, u.o, 0);
435 val.delete_(val);
436 return this;
438 A_INLINE pid_fuzzy *set_op(unsigned int _op)
440 a_pid_fuzzy::set_op(_op);
441 return this;
443 A_INLINE pid_fuzzy *set_block(unsigned int num)
445 void *ptr = a_alloc(a_pid_fuzzy_block(this), A_PID_FUZZY_BLOCK(num));
446 a_pid_fuzzy_set_block(this, ptr, num);
447 return this;
449 A_INLINE pid_fuzzy *kpid(a_float _kp, a_float _ki, a_float _kd)
451 a_pid_fuzzy::kpid(_kp, _ki, _kd);
452 return this;
454 A_INLINE a_float run(a_float set, a_float fdb)
456 return a_pid_fuzzy::run(set, fdb);
458 A_INLINE a_float pos(a_float set, a_float fdb)
460 return a_pid_fuzzy::pos(set, fdb);
462 A_INLINE a_float inc(a_float set, a_float fdb)
464 return a_pid_fuzzy::inc(set, fdb);
466 A_INLINE pid_fuzzy *zero()
468 a_pid_fuzzy::zero();
469 return this;
471 A_INLINE pid_fuzzy()
473 pid.kp = kp = 1;
474 pid.ki = ki = 0;
475 pid.kd = kd = 0;
476 pid.summax = +A_FLOAT_INF;
477 pid.summin = -A_FLOAT_INF;
478 pid.outmax = +A_FLOAT_INF;
479 pid.outmin = -A_FLOAT_INF;
480 me = nullptr;
481 mec = nullptr;
482 mkp = nullptr;
483 mki = nullptr;
484 mkd = nullptr;
485 idx = nullptr;
486 val = nullptr;
487 order = 0;
488 block = 0;
489 op = a_fuzzy_equ;
490 a_pid_fuzzy_init(this);
492 A_INLINE ~pid_fuzzy()
494 union
496 a_float const *p;
497 a_float *o;
498 } u;
499 a_alloc(a_pid_fuzzy_block(this), 0);
500 u.p = me;
501 a_alloc(u.o, 0);
502 u.p = mec;
503 a_alloc(u.o, 0);
504 u.p = mkp;
505 a_alloc(u.o, 0);
506 u.p = mki;
507 a_alloc(u.o, 0);
508 u.p = mkd;
509 a_alloc(u.o, 0);
511 A_INLINE unsigned int get_block() const { return block; }
512 A_INLINE a_float get_summax() const { return pid.summax; }
513 A_INLINE void set_summax(a_float summax) { pid.summax = summax; }
514 A_INLINE a_float get_summin() const { return pid.summin; }
515 A_INLINE void set_summin(a_float summin) { pid.summin = summin; }
516 A_INLINE a_float get_outmax() const { return pid.outmax; }
517 A_INLINE void set_outmax(a_float outmax) { pid.outmax = outmax; }
518 A_INLINE a_float get_outmin() const { return pid.outmin; }
519 A_INLINE void set_outmin(a_float outmin) { pid.outmin = outmin; }
520 A_INLINE a_float get_out() const { return pid.out; }
521 A_INLINE a_float get_fdb() const { return pid.fdb; }
522 A_INLINE a_float get_err() const { return pid.err; }
523 static unsigned int const CAP;
524 static unsigned int const CAP_ALGEBRA;
525 static unsigned int const CAP_BOUNDED;
526 static unsigned int const CUP;
527 static unsigned int const CUP_ALGEBRA;
528 static unsigned int const CUP_BOUNDED;
529 static unsigned int const EQU;
531 unsigned int const pid_fuzzy::CAP = A_PID_FUZZY_CAP;
532 unsigned int const pid_fuzzy::CAP_ALGEBRA = A_PID_FUZZY_CAP_ALGEBRA;
533 unsigned int const pid_fuzzy::CAP_BOUNDED = A_PID_FUZZY_CAP_BOUNDED;
534 unsigned int const pid_fuzzy::CUP = A_PID_FUZZY_CUP;
535 unsigned int const pid_fuzzy::CUP_ALGEBRA = A_PID_FUZZY_CUP_ALGEBRA;
536 unsigned int const pid_fuzzy::CUP_BOUNDED = A_PID_FUZZY_CUP_BOUNDED;
537 unsigned int const pid_fuzzy::EQU = A_PID_FUZZY_EQU;
539 #include "a/pid_neuro.h"
541 struct pid_neuro: public a_pid_neuro
543 A_INLINE pid_neuro *kpid(a_float _k, a_float kp, a_float ki, a_float kd)
545 a_pid_neuro::kpid(_k, kp, ki, kd);
546 return this;
548 A_INLINE pid_neuro *wpid(a_float _wp, a_float _wi, a_float _wd)
550 a_pid_neuro::wpid(_wp, _wi, _wd);
551 return this;
553 A_INLINE a_float run(a_float set, a_float fdb)
555 return a_pid_neuro::run(set, fdb);
557 A_INLINE a_float inc(a_float set, a_float fdb)
559 return a_pid_neuro::inc(set, fdb);
561 A_INLINE pid_neuro *zero()
563 a_pid_neuro::zero();
564 return this;
566 A_INLINE pid_neuro()
568 pid.summax = +A_FLOAT_INF;
569 pid.summin = -A_FLOAT_INF;
570 pid.outmax = +A_FLOAT_INF;
571 pid.outmin = -A_FLOAT_INF;
572 pid.kp = k = 1;
573 pid.ki = 0;
574 pid.kd = 0;
575 wp = A_FLOAT_C(0.1);
576 wi = A_FLOAT_C(0.1);
577 wd = A_FLOAT_C(0.1);
578 a_pid_neuro_init(this);
580 A_INLINE a_float get_kp() const { return pid.kp; }
581 A_INLINE void set_kp(a_float kp) { pid.kp = kp; }
582 A_INLINE a_float get_ki() const { return pid.ki; }
583 A_INLINE void set_ki(a_float ki) { pid.ki = ki; }
584 A_INLINE a_float get_kd() const { return pid.kd; }
585 A_INLINE void set_kd(a_float kd) { pid.kd = kd; }
586 A_INLINE a_float get_outmax() const { return pid.outmax; }
587 A_INLINE void set_outmax(a_float outmax) { pid.outmax = outmax; }
588 A_INLINE a_float get_outmin() const { return pid.outmin; }
589 A_INLINE void set_outmin(a_float outmin) { pid.outmin = outmin; }
590 A_INLINE a_float get_out() const { return pid.out; }
591 A_INLINE a_float get_fdb() const { return pid.fdb; }
592 A_INLINE a_float get_err() const { return pid.err; }
595 #include "a/tf.h"
597 struct tf: public a_tf
599 void set_num_(emscripten::val const &_num, a_float *num)
601 a_uint num_n = _num["length"].as<a_uint>();
602 a_float *p = js_array_num_get(_num, num, a_size_c(num_n) * 2);
603 a_tf_set_num(this, num_n, p, p + num_n);
605 void set_den_(emscripten::val const &_den, a_float *den)
607 a_uint den_n = _den["length"].as<a_uint>();
608 a_float *p = js_array_num_get(_den, den, a_size_c(den_n) * 2);
609 a_tf_set_den(this, den_n, p, p + den_n);
611 A_INLINE tf(emscripten::val const &num, emscripten::val const &den)
613 set_num_(num, nullptr);
614 set_den_(den, nullptr);
616 ~tf()
618 union
620 a_float const *p;
621 a_float *o;
622 } u;
623 u.p = num_p;
624 a_alloc(u.o, 0);
625 u.p = den_p;
626 a_alloc(u.o, 0);
628 A_INLINE emscripten::val get_input() const { return js_array_num_new(input, num_n); }
629 A_INLINE emscripten::val get_output() const { return js_array_num_new(output, den_n); }
630 A_INLINE emscripten::val get_num() const { return js_array_num_new(num_p, num_n); }
631 A_INLINE emscripten::val get_den() const { return js_array_num_new(den_p, den_n); }
632 A_INLINE tf *set_num(emscripten::val const &num)
634 union
636 a_float const *p;
637 a_float *o;
638 } u = {num_p};
639 set_num_(num, u.o);
640 return this;
642 A_INLINE tf *set_den(emscripten::val const &den)
644 union
646 a_float const *p;
647 a_float *o;
648 } u = {den_p};
649 set_den_(den, u.o);
650 return this;
652 A_INLINE a_float operator()(a_float x)
654 return a_tf::operator()(x);
656 A_INLINE tf *zero()
658 a_tf::zero();
659 return this;
663 #include "a/trajbell.h"
665 struct trajbell: public a_trajbell
667 A_INLINE a_float gen(a_float _jm, a_float _am, a_float _vm, a_float _p0, a_float _p1,
668 a_float _v0 = 0, a_float _v1 = 0)
670 return a_trajbell::gen(_jm, _am, _vm, _p0, _p1, _v0, _v1);
672 A_INLINE a_float pos(a_float dt) { return a_trajbell::pos(dt); }
673 A_INLINE a_float vel(a_float dt) { return a_trajbell::vel(dt); }
674 A_INLINE a_float acc(a_float dt) { return a_trajbell::acc(dt); }
675 A_INLINE a_float jer(a_float dt) { return a_trajbell::jer(dt); }
678 #include "a/trajpoly3.h"
680 struct trajpoly3: public a_trajpoly3
682 A_INLINE trajpoly3(a_float ts, a_float p0, a_float p1,
683 a_float v0 = 0, a_float v1 = 0)
685 a_trajpoly3_gen(this, ts, p0, p1, v0, v1);
687 A_INLINE a_float pos(a_float dt) { return a_trajpoly3::pos(dt); }
688 A_INLINE a_float vel(a_float dt) { return a_trajpoly3::vel(dt); }
689 A_INLINE a_float acc(a_float dt) { return a_trajpoly3::acc(dt); }
690 A_INLINE emscripten::val get_p() const { return js_array_num_new(p, A_LEN(p)); }
691 A_INLINE emscripten::val get_v() const { return js_array_num_new(v, A_LEN(v)); }
692 A_INLINE emscripten::val get_a() const { return js_array_num_new(a, A_LEN(a)); }
695 #include "a/trajpoly5.h"
697 struct trajpoly5: public a_trajpoly5
699 A_INLINE trajpoly5(a_float ts, a_float p0, a_float p1,
700 a_float v0 = 0, a_float v1 = 0,
701 a_float a0 = 0, a_float a1 = 0)
703 a_trajpoly5_gen(this, ts, p0, p1, v0, v1, a0, a1);
705 A_INLINE a_float pos(a_float dt) { return a_trajpoly5::pos(dt); }
706 A_INLINE a_float vel(a_float dt) { return a_trajpoly5::vel(dt); }
707 A_INLINE a_float acc(a_float dt) { return a_trajpoly5::acc(dt); }
708 A_INLINE emscripten::val get_p() const { return js_array_num_new(p, A_LEN(p)); }
709 A_INLINE emscripten::val get_v() const { return js_array_num_new(v, A_LEN(v)); }
710 A_INLINE emscripten::val get_a() const { return js_array_num_new(a, A_LEN(a)); }
713 #include "a/trajpoly7.h"
715 struct trajpoly7: public a_trajpoly7
717 A_INLINE trajpoly7(a_float ts, a_float p0, a_float p1,
718 a_float v0 = 0, a_float v1 = 0,
719 a_float a0 = 0, a_float a1 = 0,
720 a_float j0 = 0, a_float j1 = 0)
722 a_trajpoly7_gen(this, ts, p0, p1, v0, v1, a0, a1, j0, j1);
724 A_INLINE a_float pos(a_float dt) { return a_trajpoly7::pos(dt); }
725 A_INLINE a_float vel(a_float dt) { return a_trajpoly7::vel(dt); }
726 A_INLINE a_float acc(a_float dt) { return a_trajpoly7::acc(dt); }
727 A_INLINE a_float jer(a_float dt) { return a_trajpoly7::jer(dt); }
728 A_INLINE emscripten::val get_p() const { return js_array_num_new(p, A_LEN(p)); }
729 A_INLINE emscripten::val get_v() const { return js_array_num_new(v, A_LEN(v)); }
730 A_INLINE emscripten::val get_a() const { return js_array_num_new(a, A_LEN(a)); }
731 A_INLINE emscripten::val get_j() const { return js_array_num_new(j, A_LEN(j)); }
734 #include "a/trajtrap.h"
736 struct trajtrap: public a_trajtrap
738 A_INLINE a_float gen(a_float vm, a_float _ac, a_float _de, a_float _p0, a_float _p1,
739 a_float _v0 = 0, a_float _v1 = 0)
741 return a_trajtrap::gen(vm, _ac, _de, _p0, _p1, _v0, _v1);
743 A_INLINE a_float pos(a_float dt) { return a_trajtrap::pos(dt); }
744 A_INLINE a_float vel(a_float dt) { return a_trajtrap::vel(dt); }
745 A_INLINE a_float acc(a_float dt) { return a_trajtrap::acc(dt); }
748 #include "a/version.h"
749 #undef a_version_check
751 struct version: public a_version
753 A_INLINE a_int cmp(version const &ver) const
755 return a_version::cmp(ver);
757 A_INLINE a_bool operator<(version const &ver) const
759 return a_version::operator<(ver);
761 A_INLINE a_bool operator>(version const &ver) const
763 return a_version::operator>(ver);
765 A_INLINE a_bool operator<=(version const &ver) const
767 return a_version::operator<=(ver);
769 A_INLINE a_bool operator>=(version const &ver) const
771 return a_version::operator>=(ver);
773 A_INLINE a_bool operator==(version const &ver) const
775 return a_version::operator==(ver);
777 A_INLINE a_bool operator!=(version const &ver) const
779 return a_version::operator!=(ver);
781 A_INLINE version *parse(std::string const &ver)
783 a_version::parse(ver.c_str());
784 return this;
786 A_INLINE std::string toString()
788 std::string s = std::to_string(major) + "." +
789 std::to_string(minor) + "." +
790 std::to_string(third);
791 if (extra)
793 s += "." + std::to_string(extra);
795 return s;
797 A_INLINE version(unsigned int _major = 0,
798 unsigned int _minor = 0,
799 unsigned int _third = 0,
800 unsigned int _extra = 0)
802 major = _major;
803 minor = _minor;
804 third = _third;
805 extra = _extra;
809 #include "a/math.h"
810 #if __has_warning("-Wglobal-constructors")
811 #pragma GCC diagnostic ignored "-Wglobal-constructors"
812 #endif /* -Wglobal-constructors */
813 EMSCRIPTEN_BINDINGS(liba) // NOLINT
815 emscripten::function("isqrt", a_u32_sqrt);
816 emscripten::function("rsqrt", a_f64_rsqrt);
817 emscripten::function("hash_bkdr", hash_bkdr);
818 emscripten::function("hash_sdbm", hash_sdbm);
819 emscripten::class_<mf>("mf")
820 .class_property("NUL", &mf::NUL)
821 .class_function("gauss", &mf::gauss)
822 .class_property("GAUSS", &mf::GAUSS)
823 .class_function("gauss2", &mf::gauss2)
824 .class_property("GAUSS2", &mf::GAUSS2)
825 .class_function("gbell", &mf::gbell)
826 .class_property("GBELL", &mf::GBELL)
827 .class_function("sig", &mf::sig)
828 .class_property("SIG", &mf::SIG)
829 .class_function("dsig", &mf::dsig)
830 .class_property("DSIG", &mf::DSIG)
831 .class_function("psig", &mf::psig)
832 .class_property("PSIG", &mf::PSIG)
833 .class_function("trap", &mf::trap)
834 .class_property("TRAP", &mf::TRAP)
835 .class_function("tri", &mf::tri)
836 .class_property("TRI", &mf::TRI)
837 .class_function("lins", &mf::lins)
838 .class_property("LINS", &mf::LINS)
839 .class_function("linz", &mf::linz)
840 .class_property("LINZ", &mf::LINZ)
841 .class_function("s", &mf::s)
842 .class_property("S", &mf::S)
843 .class_function("z", &mf::z)
844 .class_property("Z", &mf::Z)
845 .class_function("pi", &mf::pi)
846 .class_property("PI", &mf::PI);
847 emscripten::class_<crc8>("crc8")
848 .constructor<a_u8>()
849 .constructor<a_u8, bool>()
850 .property("table", &crc8::get_table)
851 .function("eval", &crc8::operator())
852 .function("pack", &crc8::pack)
853 .function("gen", &crc8::gen, emscripten::allow_raw_pointers());
854 emscripten::class_<crc16>("crc16")
855 .constructor<a_u16>()
856 .constructor<a_u16, bool>()
857 .property("table", &crc16::get_table)
858 .function("eval", &crc16::operator())
859 .function("pack", &crc16::pack)
860 .function("gen", &crc16::gen, emscripten::allow_raw_pointers());
861 emscripten::class_<crc32>("crc32")
862 .constructor<a_u32>()
863 .constructor<a_u32, bool>()
864 .property("table", &crc32::get_table)
865 .function("eval", &crc32::operator())
866 .function("pack", &crc32::pack)
867 .function("gen", &crc32::gen, emscripten::allow_raw_pointers());
868 #if defined(WASM_BIGINT)
869 emscripten::class_<crc64>("crc64")
870 .constructor<a_u64>()
871 .constructor<a_u64, bool>()
872 .property("table", &crc64::get_table)
873 .function("eval", &crc64::operator())
874 .function("pack", &crc64::pack)
875 .function("gen", &crc64::gen, emscripten::allow_raw_pointers());
876 #endif /* WASM_BIGINT */
877 emscripten::class_<hpf>("hpf")
878 .constructor<a_float>()
879 .constructor<a_float, a_float>()
880 .function("iter", &hpf::operator())
881 .function("gen", &hpf::gen, emscripten::allow_raw_pointers())
882 .function("zero", &hpf::zero, emscripten::allow_raw_pointers())
883 .property<a_float const, void>("alpha", &hpf::alpha)
884 .property<a_float const, void>("output", &hpf::output)
885 .property<a_float const, void>("input", &hpf::input);
886 emscripten::class_<lpf>("lpf")
887 .constructor<a_float>()
888 .constructor<a_float, a_float>()
889 .function("iter", &lpf::operator())
890 .function("gen", &lpf::gen, emscripten::allow_raw_pointers())
891 .function("zero", &lpf::zero, emscripten::allow_raw_pointers())
892 .property<a_float const, void>("alpha", &lpf::alpha)
893 .property<a_float const, void>("output", &lpf::output);
894 emscripten::class_<pid>("pid")
895 .constructor<>()
896 .function("kpid", &pid::kpid, emscripten::allow_raw_pointers())
897 .function("zero", &pid::zero, emscripten::allow_raw_pointers())
898 .function("run", &pid::run)
899 .function("pos", &pid::pos)
900 .function("inc", &pid::inc)
901 .property<a_float, void>("kp", &pid::kp)
902 .property<a_float, void>("ki", &pid::ki)
903 .property<a_float, void>("kd", &pid::kd)
904 .property<a_float, void>("summax", &pid::summax)
905 .property<a_float, void>("summin", &pid::summin)
906 .property<a_float, void>("outmax", &pid::outmax)
907 .property<a_float, void>("outmin", &pid::outmin)
908 .property<a_float const, void>("out", &pid::out)
909 .property<a_float const, void>("fdb", &pid::fdb)
910 .property<a_float const, void>("err", &pid::err);
911 emscripten::class_<pid_fuzzy>("pid_fuzzy")
912 .constructor<>()
913 .function("op", &pid_fuzzy::set_op, emscripten::allow_raw_pointers())
914 .function("rule", &pid_fuzzy::rule, emscripten::allow_raw_pointers())
915 .function("set_block", &pid_fuzzy::set_block, emscripten::allow_raw_pointers())
916 .function("kpid", &pid_fuzzy::kpid, emscripten::allow_raw_pointers())
917 .function("zero", &pid_fuzzy::zero, emscripten::allow_raw_pointers())
918 .function("run", &pid_fuzzy::run)
919 .function("pos", &pid_fuzzy::pos)
920 .function("inc", &pid_fuzzy::inc)
921 .class_property("CAP", &pid_fuzzy::CAP)
922 .class_property("CAP_ALGEBRA", &pid_fuzzy::CAP_ALGEBRA)
923 .class_property("CAP_BOUNDED", &pid_fuzzy::CAP_BOUNDED)
924 .class_property("CUP", &pid_fuzzy::CUP)
925 .class_property("CUP_ALGEBRA", &pid_fuzzy::CUP_ALGEBRA)
926 .class_property("CUP_BOUNDED", &pid_fuzzy::CUP_BOUNDED)
927 .class_property("EQU", &pid_fuzzy::EQU)
928 .property<a_float, void>("kp", &pid_fuzzy::kp)
929 .property<a_float, void>("ki", &pid_fuzzy::ki)
930 .property<a_float, void>("kd", &pid_fuzzy::kd)
931 .property("summax", &pid_fuzzy::get_summax, &pid_fuzzy::set_summax)
932 .property("summin", &pid_fuzzy::get_summin, &pid_fuzzy::set_summin)
933 .property("outmax", &pid_fuzzy::get_outmax, &pid_fuzzy::set_outmax)
934 .property("outmin", &pid_fuzzy::get_outmin, &pid_fuzzy::set_outmin)
935 .property("out", &pid_fuzzy::get_out)
936 .property("fdb", &pid_fuzzy::get_fdb)
937 .property("err", &pid_fuzzy::get_err)
938 .property<unsigned int const, void>("order", &pid_fuzzy::order)
939 .property("block", &pid_fuzzy::get_block, &pid_fuzzy::set_block);
940 emscripten::class_<pid_neuro>("pid_neuro")
941 .constructor<>()
942 .function("kpid", &pid_neuro::kpid, emscripten::allow_raw_pointers())
943 .function("wpid", &pid_neuro::wpid, emscripten::allow_raw_pointers())
944 .function("zero", &pid_neuro::zero, emscripten::allow_raw_pointers())
945 .function("run", &pid_neuro::run)
946 .function("inc", &pid_neuro::inc)
947 .property<a_float, void>("k", &pid_neuro::k)
948 .property("kp", &pid_neuro::get_kp, &pid_neuro::set_kp)
949 .property("ki", &pid_neuro::get_ki, &pid_neuro::set_ki)
950 .property("kd", &pid_neuro::get_kd, &pid_neuro::set_kd)
951 .property<a_float, void>("wp", &pid_neuro::wp)
952 .property<a_float, void>("wi", &pid_neuro::wi)
953 .property<a_float, void>("wd", &pid_neuro::wd)
954 .property("outmax", &pid_neuro::get_outmax, &pid_neuro::set_outmax)
955 .property("outmin", &pid_neuro::get_outmin, &pid_neuro::set_outmin)
956 .property("out", &pid_neuro::get_out)
957 .property("fdb", &pid_neuro::get_fdb)
958 .property("err", &pid_neuro::get_err)
959 .property<a_float const, void>("ec", &pid_neuro::ec);
960 emscripten::class_<tf>("tf")
961 .constructor<emscripten::val, emscripten::val>()
962 .function("set_num", &tf::set_num, emscripten::allow_raw_pointers())
963 .function("set_den", &tf::set_den, emscripten::allow_raw_pointers())
964 .function("zero", &tf::zero, emscripten::allow_raw_pointers())
965 .function("iter", &tf::operator())
966 .property("input", &tf::get_input)
967 .property("output", &tf::get_output)
968 .property("num", &tf::get_num, &tf::set_num)
969 .property("den", &tf::get_den, &tf::set_den);
970 emscripten::class_<trajbell>("trajbell")
971 .constructor<>()
972 .function("gen", &trajbell::gen)
973 .function("pos", &trajbell::pos)
974 .function("vel", &trajbell::vel)
975 .function("acc", &trajbell::acc)
976 .function("jer", &trajbell::jer)
977 .property<a_float const, void>("t", &trajbell::t)
978 .property<a_float const, void>("tv", &trajbell::tv)
979 .property<a_float const, void>("ta", &trajbell::ta)
980 .property<a_float const, void>("td", &trajbell::td)
981 .property<a_float const, void>("taj", &trajbell::taj)
982 .property<a_float const, void>("tdj", &trajbell::tdj)
983 .property<a_float const, void>("p0", &trajbell::p0)
984 .property<a_float const, void>("p1", &trajbell::p1)
985 .property<a_float const, void>("v0", &trajbell::v0)
986 .property<a_float const, void>("v1", &trajbell::v1)
987 .property<a_float const, void>("vm", &trajbell::vm)
988 .property<a_float const, void>("jm", &trajbell::jm)
989 .property<a_float const, void>("am", &trajbell::am)
990 .property<a_float const, void>("dm", &trajbell::dm);
991 emscripten::class_<trajpoly3>("trajpoly3")
992 .constructor<a_float, a_float, a_float>()
993 .constructor<a_float, a_float, a_float, a_float, a_float>()
994 .function("pos", &trajpoly3::pos)
995 .function("vel", &trajpoly3::vel)
996 .function("acc", &trajpoly3::acc)
997 .property("p", &trajpoly3::get_p)
998 .property("v", &trajpoly3::get_v)
999 .property("a", &trajpoly3::get_a);
1000 emscripten::class_<trajpoly5>("trajpoly5")
1001 .constructor<a_float, a_float, a_float>()
1002 .constructor<a_float, a_float, a_float, a_float, a_float>()
1003 .constructor<a_float, a_float, a_float, a_float, a_float, a_float, a_float>()
1004 .function("pos", &trajpoly5::pos)
1005 .function("vel", &trajpoly5::vel)
1006 .function("acc", &trajpoly5::acc)
1007 .property("p", &trajpoly5::get_p)
1008 .property("v", &trajpoly5::get_v)
1009 .property("a", &trajpoly5::get_a);
1010 emscripten::class_<trajpoly7>("trajpoly7")
1011 .constructor<a_float, a_float, a_float>()
1012 .constructor<a_float, a_float, a_float, a_float, a_float>()
1013 .constructor<a_float, a_float, a_float, a_float, a_float, a_float, a_float>()
1014 .constructor<a_float, a_float, a_float, a_float, a_float, a_float, a_float, a_float, a_float>()
1015 .function("pos", &trajpoly7::pos)
1016 .function("vel", &trajpoly7::vel)
1017 .function("acc", &trajpoly7::acc)
1018 .function("jer", &trajpoly7::jer)
1019 .property("p", &trajpoly7::get_p)
1020 .property("v", &trajpoly7::get_v)
1021 .property("a", &trajpoly7::get_a)
1022 .property("j", &trajpoly7::get_j);
1023 emscripten::class_<trajtrap>("trajtrap")
1024 .constructor<>()
1025 .function("gen", &trajtrap::gen)
1026 .function("pos", &trajtrap::pos)
1027 .function("vel", &trajtrap::vel)
1028 .function("acc", &trajtrap::acc)
1029 .property<a_float const, void>("t", &trajtrap::t)
1030 .property<a_float const, void>("p0", &trajtrap::p0)
1031 .property<a_float const, void>("p1", &trajtrap::p1)
1032 .property<a_float const, void>("v0", &trajtrap::v0)
1033 .property<a_float const, void>("v1", &trajtrap::v1)
1034 .property<a_float const, void>("vc", &trajtrap::vc)
1035 .property<a_float const, void>("ta", &trajtrap::ta)
1036 .property<a_float const, void>("td", &trajtrap::td)
1037 .property<a_float const, void>("pa", &trajtrap::pa)
1038 .property<a_float const, void>("pd", &trajtrap::pd)
1039 .property<a_float const, void>("ac", &trajtrap::ac)
1040 .property<a_float const, void>("de", &trajtrap::de);
1041 emscripten::class_<version>("version")
1042 .constructor<>()
1043 .constructor<a_uint>()
1044 .constructor<a_uint, a_uint>()
1045 .constructor<a_uint, a_uint, a_uint>()
1046 .constructor<a_uint, a_uint, a_uint, a_uint>()
1047 .property<a_uint, void>("major", &a_version::major)
1048 .property<a_uint, void>("minor", &a_version::minor)
1049 .property<a_uint, void>("third", &a_version::third)
1050 .property<a_uint, void>("extra", &a_version::extra)
1051 .function("toString", &version::toString)
1052 .function("parse", &version::parse, emscripten::allow_raw_pointers())
1053 .function("cmp", &version::cmp)
1054 .function("lt", &version::operator<)
1055 .function("gt", &version::operator>)
1056 .function("le", &version::operator<=)
1057 .function("ge", &version::operator>=)
1058 .function("eq", &version::operator==)
1059 .function("ne", &version::operator!=)
1060 .class_function("check", &a_version_check)
1061 .class_property("MAJOR", &a_version::MAJOR)
1062 .class_property("MINOR", &a_version::MINOR)
1063 .class_property("PATCH", &a_version::PATCH)
1064 .class_property("TWEAK", &a_version::TWEAK);
1065 emscripten::constant("VERSION", std::string(A_VERSION));
1066 #if defined(__has_feature) && __has_feature(address_sanitizer)
1067 emscripten::function("do_leak_check", &__lsan_do_recoverable_leak_check);
1068 #endif /* -fsanitize=address */