Skip to content

Commit 72d0d8c

Browse files
committed
---
yaml --- r: 7031 b: refs/heads/master c: a16acc0 h: refs/heads/master i: 7029: d2242ae 7027: 6beec10 7023: 4619d30 v: v3
1 parent 74cf6cc commit 72d0d8c

File tree

5 files changed

+217
-24
lines changed

5 files changed

+217
-24
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
---
2-
refs/heads/master: 6284190ef9918e05cb9147a2a81100ddcb06fea8
2+
refs/heads/master: a16acc0c105c438a5cf1500eb4c4a5c4388c2dca

trunk/src/etc/cmathconsts.c

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
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 = %u;\n", FLT_RADIX);
67+
printf(" const mantissa_digits: uint = %u;\n", FLT_MANT_DIG);
68+
printf(" const digits: uint = %u;\n", FLT_DIG);
69+
printf(" const min_exp: uint = %i;\n", FLT_MIN_EXP);
70+
printf(" const max_exp: uint = %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 = %u;\n", FLT_RADIX);
80+
printf(" const mantissa_digits: uint = %u;\n", DBL_MANT_DIG);
81+
printf(" const digits: uint = %u;\n", DBL_DIG);
82+
printf(" const min_exp: uint = %i;\n", DBL_MIN_EXP);
83+
printf(" const max_exp: uint = %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+
}

trunk/src/libcore/cmath.rs

Lines changed: 102 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
1-
export c_double;
21
export c_float;
2+
export c_double;
3+
4+
// FIXME export c_float_math_consts;
5+
// FIXME export c_double_math_consts;
6+
7+
export c_float_targ_consts;
8+
export c_double_targ_consts;
39

410
import ctypes::c_int;
511
import ctypes::c_float;
@@ -143,6 +149,101 @@ native mod c_float {
143149
#[link_name="truncf"] pure fn trunc(n: c_float) -> c_float;
144150
}
145151

152+
// PORT check these by running src/etc/machconsts.c for your architecture
153+
154+
// FIXME obtain machine float/math constants automatically
155+
156+
mod c_float_targ_consts {
157+
const radix: uint = 2;
158+
const mantissa_digits: uint = 24;
159+
const digits: uint = 6;
160+
const min_exp: uint = -125;
161+
const max_exp: uint = 128;
162+
const min_10_exp: int = -37;
163+
const max_10_exp: int = 38;
164+
// FIXME this is wrong! replace with hexadecimal (%a) constants below
165+
const min_value: f32 = 1.175494e-38_f32;
166+
const max_value: f32 = 3.402823e+38_f32;
167+
const epsilon: f32 = 0.000000_f32;
168+
}
169+
170+
mod c_double_targ_consts {
171+
const radix: uint = 2;
172+
const mantissa_digits: uint = 53;
173+
const digits: uint = 15;
174+
const min_exp: uint = -1021;
175+
const max_exp: uint = 1024;
176+
const min_10_exp: int = -307;
177+
const max_10_exp: int = 308;
178+
// FIXME this is wrong! replace with hexadecimal (%a) constants below
179+
const min_value: f64 = 2.225074e-308_f64;
180+
const max_value: f64 = 1.797693e+308_f64;
181+
const epsilon: f64 = 2.220446e-16_f64;
182+
}
183+
184+
/*
185+
186+
FIXME use these once they can be parsed
187+
188+
mod c_float_math_consts {
189+
const pi: c_float = 0x1.921fb6p+1_f32;
190+
const div_1_pi: c_float = 0x1.45f306p-2_f32;
191+
const div_2_pi: c_float = 0x1.45f306p-1_f32;
192+
const div_pi_2: c_float = 0x1.921fb6p+0_f32;
193+
const div_pi_4: c_float = 0x1.921fb6p-1_f32;
194+
const div_2_sqrtpi: c_float = 0x1.20dd76p+0_f32;
195+
const e: c_float = 0x1.5bf0a8p+1_f32;
196+
const log2_e: c_float = 0x1.715476p+0_f32;
197+
const log10_e: c_float = 0x1.bcb7b2p-2_f32;
198+
const ln_2: c_float = 0x1.62e43p-1_f32;
199+
const ln_10: c_float = 0x1.26bb1cp+1_f32;
200+
const sqrt2: c_float = 0x1.6a09e6p+0_f32;
201+
const div_1_sqrt2: c_float = 0x1.6a09e6p-1_f32;
202+
}
203+
204+
mod c_double_math_consts {
205+
const pi: c_double = 0x1.921fb54442d18p+1_f64;
206+
const div_1_pi: c_double = 0x1.45f306dc9c883p-2_f64;
207+
const div_2_pi: c_double = 0x1.45f306dc9c883p-1_f64;
208+
const div_pi_2: c_double = 0x1.921fb54442d18p+0_f64;
209+
const div_pi_4: c_double = 0x1.921fb54442d18p-1_f64;
210+
const div_2_sqrtpi: c_double = 0x1.20dd750429b6dp+0_f64;
211+
const e: c_double = 0x1.5bf0a8b145769p+1_f64;
212+
const log2_e: c_double = 0x1.71547652b82fep+0_f64;
213+
const log10_e: c_double = 0x1.bcb7b1526e50ep-2_f64;
214+
const ln_2: c_double = 0x1.62e42fefa39efp-1_f64;
215+
const ln_10: c_double = 0x1.26bb1bbb55516p+1_f64;
216+
const sqrt2: c_double = 0x1.6a09e667f3bcdp+0_f64;
217+
const div_1_sqrt2: c_double = 0x1.6a09e667f3bcdp-1_f64;
218+
}
219+
220+
mod c_float_targ_consts {
221+
const radix: uint = 2;
222+
const mantissa_digits: uint = 24;
223+
const digits: uint = 6;
224+
const min_exp: uint = -125;
225+
const max_exp: uint = 128;
226+
const min_10_exp: int = -37;
227+
const max_10_exp: int = 38;
228+
const min_value: c_float = 0x1p-126_f32;
229+
const max_value: c_float = 0x1.fffffep+127_f32;
230+
const epsilon: c_float = 0x1p-23_f32;
231+
}
232+
233+
mod c_double_targ_consts {
234+
const radix: uint = 2;
235+
const mantissa_digits: uint = 53;
236+
const digits: uint = 15;
237+
const min_exp: uint = -1021;
238+
const max_exp: uint = 1024;
239+
const min_10_exp: int = -307;
240+
const max_10_exp: int = 308;
241+
const min_value: c_double = 0x1p-1022_f64;
242+
const max_value: c_double = 0x1.fffffffffffffp+1023_f64;
243+
const epsilon: c_double = 0x1p-52_f64;
244+
}
245+
*/
246+
146247
//
147248
// Local Variables:
148249
// mode: rust

trunk/src/libcore/f32.rs

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,33 +7,13 @@ Floating point operations and constants for `f32`
77
// PORT
88

99
import cmath::c_float::*;
10+
import cmath::c_float_targ_consts::*;
1011

1112
type t = f32;
1213

13-
1414
// These are not defined inside consts:: for consistency with
1515
// the integer types
1616

17-
// PORT check per architecture
18-
19-
// FIXME obtain these in a different way
20-
21-
const radix: uint = 2u;
22-
23-
const mantissa_digits: uint = 24u;
24-
const digits: uint = 6u;
25-
26-
const epsilon: f32 = 1.19209290e-07_f32;
27-
28-
const min_value: f32 = 1.17549435e-38_f32;
29-
const max_value: f32 = 3.40282347e+38_f32;
30-
31-
const min_exp: int = -125;
32-
const max_exp: int = 128;
33-
34-
const min_10_exp: int = -37;
35-
const max_10_exp: int = 38;
36-
3717
/* Const: NaN */
3818
const NaN: f32 = 0.0_f32/0.0_f32;
3919

@@ -150,6 +130,8 @@ pure fn is_finite(x: f32) -> bool {
150130
/* Module: consts */
151131
mod consts {
152132

133+
// FIXME replace with mathematical constants from cmath
134+
153135
/*
154136
Const: pi
155137
@@ -242,6 +224,14 @@ mod consts {
242224
const ln_10: f32 = 2.30258509299404568401799145468436421_f32;
243225
}
244226

227+
#[cfg(target_os="linux")]
228+
#[cfg(target_os="macos")]
229+
#[cfg(target_os="win32")]
230+
pure fn logarithm(n: f32, b: f32) -> f32 {
231+
ret log2(n) / log2(b);
232+
}
233+
234+
#[cfg(target_os="freebsd")]
245235
pure fn logarithm(n: f32, b: f32) -> f32 {
246236
ret ln(n) / ln(b);
247237
}

trunk/src/libcore/f64.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ Floating point operations and constants for `f64`
77
// PORT
88

99
import cmath::c_double::*;
10+
import cmath::c_double_targ_consts::*;
1011

1112
type t = f64;
1213

13-
1414
// These are not defined inside consts:: for consistency with
1515
// the integer types
1616

@@ -147,6 +147,8 @@ pure fn is_finite(x: f64) -> bool {
147147
/* Module: consts */
148148
mod consts {
149149

150+
// FIXME replace with mathematical constants from cmath
151+
150152
/*
151153
Const: pi
152154
@@ -239,6 +241,14 @@ mod consts {
239241
const ln_10: f64 = 2.30258509299404568401799145468436421_f64;
240242
}
241243

244+
#[cfg(target_os="linux")]
245+
#[cfg(target_os="macos")]
246+
#[cfg(target_os="win32")]
247+
pure fn logarithm(n: f64, b: f64) -> f64 {
248+
ret log2(n) / log2(b);
249+
}
250+
251+
#[cfg(target_os="freebsd")]
242252
pure fn logarithm(n: f64, b: f64) -> f64 {
243253
ret ln(n) / ln(b);
244254
}

0 commit comments

Comments
 (0)