Project

General

Profile

Revision 0bca9d53

View differences:

include/simulink_math_fcn.c
1 1
#include "simulink_math_fcn.h"
2 2
#include <math.h>
3 3

  
4
/* function exp_scalar_real (x: real) returns (y: real) prototype C lib m; */
5
double exp_scalar_real (double x) {
6
  return exp(x);
7
}
8

  
9

  
10
/* function log_scalar_real (x: real) returns (y: real) prototype C lib m; */
11
double log_scalar_real (double x) {
12
  return log(x);
13
}
14
/* function _10u_scalar_real (x: real) returns (y: real) prototype C lib m; */
15
double _10u_scalar_real (double x) {
16
  return pow(10.,x);
17
}
18

  
19
/* function log10_scalar_real (x: real) returns (y: real) prototype C lib m; */
20
double log10_scalar_real (double x) {
21
  return log10(x);
22
}
23

  
24
/* function magnitude_2_scalar_real (x: real) returns (y: real) prototype C lib m; */
25
double magnitude_2_scalar_real (double x) {
26
  return pow(fabs(x), 2.);
27
}
28 4

  
29
/* function square_scalar_real (x: real) returns (y: real) prototype C lib m; */
30
double square_scalar_real (double x) {
31
  return pow(x, 2.);
32
}
33

  
34
/* function pow_scalar_real (x,y: real) returns (z: real) prototype C lib m; */
35
double pow_scalar_real (double x, double y) {
36
  return pow(x, y);
37
}
38

  
39
/* function conj_scalar_real (x: real) returns (y: real) prototype C lib m; */
40
double conj_scalar_real (double x) {
41
  return x; // identity for real
42
}
43

  
44
/* function reciprocal_scalar_real (x: real) returns (y: real) prototype C lib m; */
45
double reciprocal_scalar_real (double x) {
46
  return 1./x; 
47
}
48

  
49
/* function hypot_scalar_real (x,y: real) returns (z: real) prototype C lib m; */
50
double hypot_scalar_real (double x, double y) {
51
  return sqrt(x*x + y*y); 
52
}
53

  
54

  
55

  
56
/*
57
  mod function produces a result that is either zero or has the same sign as the divisor.
58
  rem function produces a result that is either zero or has the same sign as the dividend.
59
  mod(a,0) returns a
60
  rem(a,0) returns NaN.
61

  
62
function rem_scalar_real_int_int_int (x,y: int) returns (z: int) prototype C lib m;
63
function rem_scalar_real_double_double_double (x,y: double) returns (z: double) prototype C lib m;
64
function rem_scalar_real_double_int_double (x: double; y: int) returns (z: double) prototype C lib m;
65
function rem_scalar_real_int_double_double (x: int; y: double) returns (z: double) prototype C lib m;
66

  
67
function mod_scalar_real_int_int_int (x,y: int) returns (z: int) prototype C lib m;
68
function mod_scalar_real_double_double_double (x,y: double) returns (z: double) prototype C lib m;
69
function mod_scalar_real_double_int_double (x: double; y: int) returns (z: double) prototype C lib m;
70
function mod_scalar_real_int_double_double (x: int; y: double) returns (z: double) prototype C lib m;
71
*/
72

  
73
int rem_scalar_real_int_int_int (int x, int y) {
5
int rem_int (int x, int y) {
74 6
  return x%y;
75 7
}
76 8

  
77
int mod_scalar_real_int_int_int (int x, int y) {
9
int mod_int (int x, int y) {
78 10
  int tmp;
79 11
  if (y == 0) { return x; };
80 12
  tmp = x%y;
......
86 18
  }
87 19
}
88 20

  
89
double rem_scalar_real_double_double_double (double x, double y) {
21
double rem_real (double x, double y) {
90 22
  return fmod(x, y);
91 23
}
92 24

  
93
double mod_scalar_real_double_double_double (double x, double y) {
25
double mod_real (double x, double y) {
94 26
  double tmp = 0.;
95 27
  if (y == 0.) { return x; };
96 28
  tmp = fmod(x, y);
......
101 33
    return tmp;
102 34
  }
103 35
}
104

  
105
double rem_scalar_real_double_int_double (double x, int y) {
106
  return rem_scalar_real_double_double_double (x, (double)y);
107
}
108

  
109
double rem_scalar_real_int_double_double (int x, double y) {
110
  return rem_scalar_real_double_double_double ((double)x, y);
111
}
112

  
113

  
114
double mod_scalar_real_double_int_double (double x, int y) {
115
  return (mod_scalar_real_double_double_double (x, (double)y));
116
}
117

  
118
double mod_scalar_real_int_double_double (int x, double y) {
119
  return (mod_scalar_real_double_double_double ((double)x, y));
120
}
121

  
122
/* function transpose_scalar_real (x: real) returns (y: real) prototype C lib m; */
123

  
124
/* function hermitian_scalar_real (x: real) returns (y: real) prototype C lib m; */
include/simulink_math_fcn.lusi
1
(*
2
Mathematical functions in Simulink Math Function blocks
3

  
4
All these functions can be applied to scalar value. All but transpose and
5
hermitian can be also applied as element-wise operations on vector, matrices
6
inputs. transpose and hermitian are applied on vector and matrices as regular
7
(non element-wise) operations.
8

  
9
The Lustre library provides only scalar functions for all cases, and, in the future,
10
the matrix versions of them.
11

  
12
exp: 
13
log
14
10^u
15
log10
16
magnitude^2
17
square
18
pow
19
conj
20
reciprocal
21
hypot
22
rem
23
mod
24
transpose
25
hermitian
26

  
27
For the moment, we focus only on theoretical types: real, complex.
28
A future version can be specialized for concrete datatypes (single, double,
29
(u)intXX).
30

  
31
*)
32

  
33
-- open <math>
34
function fmod (x,y: real) returns (z: real) prototype C lib m;
35

  
36
function exp_scalar_real (x: real) returns (y: real) prototype C lib m;
37
function log_scalar_real (x: real) returns (y: real) prototype C lib m;
38
function _10u_scalar_real (x: real) returns (y: real) prototype C lib m;
39
function log10_scalar_real (x: real) returns (y: real) prototype C lib m;
40

  
41
-- complex modulus: |x|^2
42
function magnitude_2_scalar_real (x: real) returns (y: real) prototype C lib m;
43
function square_scalar_real (x: real) returns (y: real) prototype C lib m;
44
function pow_scalar_real (x,y: real) returns (z: real) prototype C lib m;
45
function conj_scalar_real (x: real) returns (y: real) prototype C lib m;
46
function reciprocal_scalar_real (x: real) returns (y: real) prototype C lib m;
47
function hypot_scalar_real (x,y: real) returns (z: real) prototype C lib m;
48

  
49
function rem_scalar_real_int_int_int (x,y: int) returns (z: int) prototype C lib m;
50
function rem_scalar_real_double_double_double (x,y: real) returns (z: real) prototype C lib m;
51
function rem_scalar_real_double_int_double (x: real; y: int) returns (z: real) prototype C lib m;
52
function rem_scalar_real_int_double_double (x: int; y: real) returns (z: real) prototype C lib m;
53

  
54
function mod_scalar_real_int_int_int (x,y: int) returns (z: int) prototype C lib m;
55
function mod_scalar_real_double_double_double (x,y: real) returns (z: real) prototype C lib m;
56
function mod_scalar_real_double_int_double (x: real; y: int) returns (z: real) prototype C lib m;
57
function mod_scalar_real_int_double_double (x: int; y: real) returns (z: real) prototype C lib m;
58

  
59
(*
60
-- function transpose_scalar_real (x: real) returns (y: real) prototype C lib m;
61
-- function hermitian_scalar_real (x: real) returns (y: real) prototype C lib m;
62
-- function exp_matrix_real (const i,j: int; x: real^i^j) returns (y: real^i^j) prototype C lib m;
63
*)
1
#open <lustrec_math>
2
function rem_int (x,y: int) returns (z: int) prototype C;
3
function rem_real (x,y: real) returns (z: real) prototype C;
4
function mod_int (x,y: int) returns (z: int) prototype C;
5
function mod_real (x,y: real) returns (z: real) prototype C;
src/backends/C/c_backend_makefile.ml
13 13
open LustreSpec
14 14
open Corelang
15 15

  
16
let pp_dep fmt (Dep(b,id,tops,stateful)) =
17
  Format.fprintf fmt "%b, %s, {%a}, %b"
18
    b id Printers.pp_prog tops stateful
19
  
20
let pp_deps fmt deps = Format.fprintf fmt "@[<v 0>%a@ @]" (Utils.fprintf_list ~sep:"@ ," pp_dep) deps
21

  
16 22
let header_has_code header =
17 23
  List.exists 
18 24
    (fun top -> 
......
39 45
    (fun accu (Dep (_, _, header, _)) -> Utils.list_union (header_libs header) accu) [] dep
40 46
    
41 47
let fprintf_dependencies fmt (dep: dep_t list) =
48
  (* Format.eprintf "Deps: %a@." pp_deps dep; *)
42 49
  let compiled_dep = compiled_dependencies dep in
43
  List.iter (fun s -> (* Format.eprintf "Adding dependency: %s@." s;  *)
50
  (* Format.eprintf "Compiled Deps: %a@." pp_deps compiled_dep; *)
51
 
52
  List.iter (fun s -> Format.eprintf "Adding dependency: %s@." s;  
44 53
    fprintf fmt "\t${GCC} -I${INC} -c %s@." s)
45 54
    (("${INC}/io_frontend.c"):: (* IO functions when a main function is computed *)
46 55
	(List.map 
src/compiler_common.ml
232 232
  | _ -> false
233 233

  
234 234

  
235
let import_dependencies prog =
235
let rec import_dependencies prog =
236 236
  Log.report ~level:1 (fun fmt -> fprintf fmt "@[<v 4>.. extracting dependencies");
237 237
  let dependencies = Corelang.get_dependencies prog in
238 238
  let deps =
......
240 240
    (fun (compilation_dep, type_env, clock_env) dep ->
241 241
      let (local, s) = Corelang.dependency_of_top dep in
242 242
      let basename = Options_management.name_dependency (local, s) in
243
      Log.report ~level:1 (fun fmt -> Format.fprintf fmt "@ Library %s" basename);
243
      Log.report ~level:1 (fun fmt -> Format.fprintf fmt "@ Library %s@ " basename);
244 244
      let lusic = Modules.import_dependency dep.top_decl_loc (local, s) in
245 245
      (*Log.report ~level:1 (fun fmt -> Format.fprintf fmt "");*)
246
      let lusic_deps, type_env', clock_env' = import_dependencies lusic.Lusic.contents in
247
      let type_env = Env.overwrite type_env type_env' in
248
      let clock_env = Env.overwrite clock_env clock_env' in
246 249
      let (lusi_type_env, lusi_clock_env) = get_envs_from_top_decls lusic.Lusic.contents in
247 250
      let is_stateful = List.exists is_stateful lusic.Lusic.contents in
248 251
      let new_dep = Dep (local, s, lusic.Lusic.contents, is_stateful ) in
249
      new_dep::compilation_dep,
252
      new_dep::lusic_deps@compilation_dep,
250 253
      Env.overwrite type_env lusi_type_env,
251 254
      Env.overwrite clock_env lusi_clock_env)
252 255
    ([], Basic_library.type_env, Basic_library.clock_env)

Also available in: Unified diff