|
| 1 | +// This is a helper C program for generating required math constants |
| 2 | +// |
| 3 | +// Should only be required when porting to a different target architecture |
| 4 | +// (or c compiler/libmath) |
| 5 | +// |
| 6 | +// Call with <rust machine type of c_float> <rust machine type of c_double> |
| 7 | +// and ensure that libcore/cmath.rs complies to the output |
| 8 | +// |
| 9 | +// Requires a printf that supports "%a" specifiers |
| 10 | +// |
| 11 | + |
| 12 | +#include <float.h> |
| 13 | +#include <math.h> |
| 14 | +#include <stdio.h> |
| 15 | + |
| 16 | +// must match core::ctypes |
| 17 | + |
| 18 | +#define C_FLT(x) (float)x |
| 19 | +#define C_DBL(x) (double)x |
| 20 | + |
| 21 | +int main(int argc, char** argv) { |
| 22 | + if (argc != 3) { |
| 23 | + fprintf(stderr, "%s <ctypes::c_float> <ctypes::c_double>\n", argv[0]); |
| 24 | + return 1; |
| 25 | + } |
| 26 | + char* c_flt = argv[1]; |
| 27 | + char* c_dbl = argv[2]; |
| 28 | + |
| 29 | + printf("mod c_float_math_consts {\n"); |
| 30 | + printf(" const pi: c_float = %a_%s;\n", C_FLT(M_PI), c_flt); |
| 31 | + printf(" const div_1_pi: c_float = %a_%s;\n", C_FLT(M_1_PI), c_flt); |
| 32 | + printf(" const div_2_pi: c_float = %a_%s;\n", C_FLT(M_2_PI), c_flt); |
| 33 | + printf(" const div_pi_2: c_float = %a_%s;\n", C_FLT(M_PI_2), c_flt); |
| 34 | + printf(" const div_pi_4: c_float = %a_%s;\n", C_FLT(M_PI_4), c_flt); |
| 35 | + printf(" const div_2_sqrtpi: c_float = %a_%s;\n", |
| 36 | + C_FLT(M_2_SQRTPI), c_flt); |
| 37 | + printf(" const e: c_float = %a_%s;\n", C_FLT(M_E), c_flt); |
| 38 | + printf(" const log2_e: c_float = %a_%s;\n", C_FLT(M_LOG2E), c_flt); |
| 39 | + printf(" const log10_e: c_float = %a_%s;\n", C_FLT(M_LOG10E), c_flt); |
| 40 | + printf(" const ln_2: c_float = %a_%s;\n", C_FLT(M_LN2), c_flt); |
| 41 | + printf(" const ln_10: c_float = %a_%s;\n", C_FLT(M_LN10), c_flt); |
| 42 | + printf(" const sqrt2: c_float = %a_%s;\n", C_FLT(M_SQRT2), c_flt); |
| 43 | + printf(" const div_1_sqrt2: c_float = %a_%s;\n", |
| 44 | + C_FLT(M_SQRT1_2), c_flt); |
| 45 | + printf("}\n\n"); |
| 46 | + |
| 47 | + printf("mod c_double_math_consts {\n"); |
| 48 | + printf(" const pi: c_double = %a_%s;\n", C_DBL(M_PI), c_dbl); |
| 49 | + printf(" const div_1_pi: c_double = %a_%s;\n", C_DBL(M_1_PI), c_dbl); |
| 50 | + printf(" const div_2_pi: c_double = %a_%s;\n", C_DBL(M_2_PI), c_dbl); |
| 51 | + printf(" const div_pi_2: c_double = %a_%s;\n", C_DBL(M_PI_2), c_dbl); |
| 52 | + printf(" const div_pi_4: c_double = %a_%s;\n", C_DBL(M_PI_4), c_dbl); |
| 53 | + printf(" const div_2_sqrtpi: c_double = %a_%s;\n", |
| 54 | + C_DBL(M_2_SQRTPI), c_dbl); |
| 55 | + printf(" const e: c_double = %a_%s;\n", C_DBL(M_E), c_dbl); |
| 56 | + printf(" const log2_e: c_double = %a_%s;\n", C_DBL(M_LOG2E), c_dbl); |
| 57 | + printf(" const log10_e: c_double = %a_%s;\n", C_DBL(M_LOG10E), c_dbl); |
| 58 | + printf(" const ln_2: c_double = %a_%s;\n", C_DBL(M_LN2), c_dbl); |
| 59 | + printf(" const ln_10: c_double = %a_%s;\n", C_DBL(M_LN10), c_dbl); |
| 60 | + printf(" const sqrt2: c_double = %a_%s;\n", C_DBL(M_SQRT2), c_dbl); |
| 61 | + printf(" const div_1_sqrt2: c_double = %a_%s;\n", |
| 62 | + C_DBL(M_SQRT1_2), c_dbl); |
| 63 | + printf("}\n\n"); |
| 64 | + |
| 65 | + printf("mod c_float_targ_consts {\n"); |
| 66 | + printf(" const radix: uint = %uu;\n", FLT_RADIX); |
| 67 | + printf(" const mantissa_digits: uint = %uu;\n", FLT_MANT_DIG); |
| 68 | + printf(" const digits: uint = %uu;\n", FLT_DIG); |
| 69 | + printf(" const min_exp: int = %i;\n", FLT_MIN_EXP); |
| 70 | + printf(" const max_exp: int = %i;\n", FLT_MAX_EXP); |
| 71 | + printf(" const min_10_exp: int = %i;\n", FLT_MIN_10_EXP); |
| 72 | + printf(" const max_10_exp: int = %i;\n", FLT_MAX_10_EXP); |
| 73 | + printf(" const min_value: c_float = %a_%s;\n", C_FLT(FLT_MIN), c_flt); |
| 74 | + printf(" const max_value: c_float = %a_%s;\n", C_FLT(FLT_MAX), c_flt); |
| 75 | + printf(" const epsilon: c_float = %a_%s;\n", C_FLT(FLT_EPSILON), c_flt); |
| 76 | + printf("}\n\n"); |
| 77 | + |
| 78 | + printf("mod c_double_targ_consts {\n"); |
| 79 | + printf(" const radix: uint = %uu;\n", FLT_RADIX); |
| 80 | + printf(" const mantissa_digits: uint = %uu;\n", DBL_MANT_DIG); |
| 81 | + printf(" const digits: uint = %uu;\n", DBL_DIG); |
| 82 | + printf(" const min_exp: int = %i;\n", DBL_MIN_EXP); |
| 83 | + printf(" const max_exp: int = %i;\n", DBL_MAX_EXP); |
| 84 | + printf(" const min_10_exp: int = %i;\n", DBL_MIN_10_EXP); |
| 85 | + printf(" const max_10_exp: int = %i;\n", DBL_MAX_10_EXP); |
| 86 | + printf(" const min_value: c_double = %a_%s;\n", C_DBL(DBL_MIN), c_dbl); |
| 87 | + printf(" const max_value: c_double = %a_%s;\n", C_DBL(DBL_MAX), c_dbl); |
| 88 | + printf(" const epsilon: c_double = %a_%s;\n", C_DBL(DBL_EPSILON), c_dbl); |
| 89 | + printf("}\n"); |
| 90 | + |
| 91 | + return 0; |
| 92 | +} |
0 commit comments