]>
Commit | Line | Data |
---|---|---|
1a4d82fc JJ |
1 | // Copyright 2014 The Rust Project Developers. See the COPYRIGHT |
2 | // file at the top-level directory of this distribution and at | |
3 | // http://rust-lang.org/COPYRIGHT. | |
4 | // | |
5 | // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | |
6 | // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | |
7 | // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | |
8 | // option. This file may not be copied, modified, or distributed | |
9 | // except according to those terms. | |
10 | // | |
11 | // | |
223e47cc LB |
12 | // This is a helper C program for generating required math constants |
13 | // | |
14 | // Should only be required when porting to a different target architecture | |
15 | // (or c compiler/libmath) | |
16 | // | |
17 | // Call with <rust machine type of c_float> <rust machine type of c_double> | |
18 | // and ensure that libcore/cmath.rs complies to the output | |
19 | // | |
20 | // Requires a printf that supports "%a" specifiers | |
21 | // | |
22 | ||
23 | #include <float.h> | |
24 | #include <math.h> | |
25 | #include <stdio.h> | |
26 | ||
1a4d82fc | 27 | // must match std::ctypes |
223e47cc LB |
28 | |
29 | #define C_FLT(x) (float)x | |
30 | #define C_DBL(x) (double)x | |
31 | ||
32 | int main(int argc, char** argv) { | |
33 | if (argc != 3) { | |
34 | fprintf(stderr, "%s <ctypes::c_float> <ctypes::c_double>\n", argv[0]); | |
35 | return 1; | |
36 | } | |
37 | char* c_flt = argv[1]; | |
38 | char* c_dbl = argv[2]; | |
39 | ||
40 | printf("mod c_float_math_consts {\n"); | |
41 | printf(" const pi: c_float = %a_%s;\n", C_FLT(M_PI), c_flt); | |
42 | printf(" const div_1_pi: c_float = %a_%s;\n", C_FLT(M_1_PI), c_flt); | |
43 | printf(" const div_2_pi: c_float = %a_%s;\n", C_FLT(M_2_PI), c_flt); | |
44 | printf(" const div_pi_2: c_float = %a_%s;\n", C_FLT(M_PI_2), c_flt); | |
45 | printf(" const div_pi_4: c_float = %a_%s;\n", C_FLT(M_PI_4), c_flt); | |
46 | printf(" const div_2_sqrtpi: c_float = %a_%s;\n", | |
47 | C_FLT(M_2_SQRTPI), c_flt); | |
48 | printf(" const e: c_float = %a_%s;\n", C_FLT(M_E), c_flt); | |
49 | printf(" const log2_e: c_float = %a_%s;\n", C_FLT(M_LOG2E), c_flt); | |
50 | printf(" const log10_e: c_float = %a_%s;\n", C_FLT(M_LOG10E), c_flt); | |
51 | printf(" const ln_2: c_float = %a_%s;\n", C_FLT(M_LN2), c_flt); | |
52 | printf(" const ln_10: c_float = %a_%s;\n", C_FLT(M_LN10), c_flt); | |
53 | printf(" const sqrt2: c_float = %a_%s;\n", C_FLT(M_SQRT2), c_flt); | |
54 | printf(" const div_1_sqrt2: c_float = %a_%s;\n", | |
55 | C_FLT(M_SQRT1_2), c_flt); | |
56 | printf("}\n\n"); | |
57 | ||
58 | printf("mod c_double_math_consts {\n"); | |
59 | printf(" const pi: c_double = %a_%s;\n", C_DBL(M_PI), c_dbl); | |
60 | printf(" const div_1_pi: c_double = %a_%s;\n", C_DBL(M_1_PI), c_dbl); | |
61 | printf(" const div_2_pi: c_double = %a_%s;\n", C_DBL(M_2_PI), c_dbl); | |
62 | printf(" const div_pi_2: c_double = %a_%s;\n", C_DBL(M_PI_2), c_dbl); | |
63 | printf(" const div_pi_4: c_double = %a_%s;\n", C_DBL(M_PI_4), c_dbl); | |
64 | printf(" const div_2_sqrtpi: c_double = %a_%s;\n", | |
65 | C_DBL(M_2_SQRTPI), c_dbl); | |
66 | printf(" const e: c_double = %a_%s;\n", C_DBL(M_E), c_dbl); | |
67 | printf(" const log2_e: c_double = %a_%s;\n", C_DBL(M_LOG2E), c_dbl); | |
68 | printf(" const log10_e: c_double = %a_%s;\n", C_DBL(M_LOG10E), c_dbl); | |
69 | printf(" const ln_2: c_double = %a_%s;\n", C_DBL(M_LN2), c_dbl); | |
70 | printf(" const ln_10: c_double = %a_%s;\n", C_DBL(M_LN10), c_dbl); | |
71 | printf(" const sqrt2: c_double = %a_%s;\n", C_DBL(M_SQRT2), c_dbl); | |
72 | printf(" const div_1_sqrt2: c_double = %a_%s;\n", | |
73 | C_DBL(M_SQRT1_2), c_dbl); | |
74 | printf("}\n\n"); | |
75 | ||
76 | printf("mod c_float_targ_consts {\n"); | |
77 | printf(" const radix: uint = %uu;\n", FLT_RADIX); | |
78 | printf(" const mantissa_digits: uint = %uu;\n", FLT_MANT_DIG); | |
79 | printf(" const digits: uint = %uu;\n", FLT_DIG); | |
80 | printf(" const min_exp: int = %i;\n", FLT_MIN_EXP); | |
81 | printf(" const max_exp: int = %i;\n", FLT_MAX_EXP); | |
82 | printf(" const min_10_exp: int = %i;\n", FLT_MIN_10_EXP); | |
83 | printf(" const max_10_exp: int = %i;\n", FLT_MAX_10_EXP); | |
84 | printf(" const min_value: c_float = %a_%s;\n", C_FLT(FLT_MIN), c_flt); | |
85 | printf(" const max_value: c_float = %a_%s;\n", C_FLT(FLT_MAX), c_flt); | |
86 | printf(" const epsilon: c_float = %a_%s;\n", C_FLT(FLT_EPSILON), c_flt); | |
87 | printf("}\n\n"); | |
88 | ||
89 | printf("mod c_double_targ_consts {\n"); | |
90 | printf(" const radix: uint = %uu;\n", FLT_RADIX); | |
91 | printf(" const mantissa_digits: uint = %uu;\n", DBL_MANT_DIG); | |
92 | printf(" const digits: uint = %uu;\n", DBL_DIG); | |
93 | printf(" const min_exp: int = %i;\n", DBL_MIN_EXP); | |
94 | printf(" const max_exp: int = %i;\n", DBL_MAX_EXP); | |
95 | printf(" const min_10_exp: int = %i;\n", DBL_MIN_10_EXP); | |
96 | printf(" const max_10_exp: int = %i;\n", DBL_MAX_10_EXP); | |
97 | printf(" const min_value: c_double = %a_%s;\n", C_DBL(DBL_MIN), c_dbl); | |
98 | printf(" const max_value: c_double = %a_%s;\n", C_DBL(DBL_MAX), c_dbl); | |
99 | printf(" const epsilon: c_double = %a_%s;\n", C_DBL(DBL_EPSILON), c_dbl); | |
100 | printf("}\n"); | |
101 | ||
102 | return 0; | |
103 | } |