Skip to content

Commit 671b71d

Browse files
huonwgraydon
authored andcommitted
---
yaml --- r: 55219 b: refs/heads/snap-stage3 c: 1eb5efc h: refs/heads/master i: 55217: 66ae112 55215: 07e8d30 v: v3
1 parent f85ab49 commit 671b71d

File tree

7 files changed

+690
-190
lines changed

7 files changed

+690
-190
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
refs/heads/master: 5f13e9ccc2e3328d4cd8ca49f84e6840dd998346
33
refs/heads/snap-stage1: e33de59e47c5076a89eadeb38f4934f58a3618a6
4-
refs/heads/snap-stage3: ed81e3353ea8d92b61669dc8d8692d5e9b01a8f4
4+
refs/heads/snap-stage3: 1eb5efc5e25303bd72ce18ee3ecf82b109e2f67f
55
refs/heads/try: 8eb2bab100b42f0ba751552d8eff00eb2134c55a
66
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105
77
refs/heads/ndm: f3868061cd7988080c30d6d5bf352a5a5fe2460b
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
#!/usr/bin/env python
2+
# xfail-license
3+
4+
# This creates the tables used for distributions implemented using the
5+
# ziggurat algorithm in `core::rand::distributions;`. They are
6+
# (basically) the tables as used in the ZIGNOR variant (Doornik 2005).
7+
# They are changed rarely, so the generated file should be checked in
8+
# to git.
9+
#
10+
# It creates 3 tables: X as in the paper, F which is f(x_i), and
11+
# F_DIFF which is f(x_i) - f(x_{i-1}). The latter two are just cached
12+
# values which is not done in that paper (but is done in other
13+
# variants). Note that the adZigR table is unnecessary because of
14+
# algebra.
15+
#
16+
# It is designed to be compatible with Python 2 and 3.
17+
18+
from math import exp, sqrt, log, floor
19+
import random
20+
21+
# The order should match the return value of `tables`
22+
TABLE_NAMES = ['X', 'F', 'F_DIFF']
23+
24+
# The actual length of the table is 1 more, to stop
25+
# index-out-of-bounds errors. This should match the bitwise operation
26+
# to find `i` in `zigurrat` in `libstd/rand/mod.rs`. Also the *_R and
27+
# *_V constants below depend on this value.
28+
TABLE_LEN = 256
29+
30+
# equivalent to `zigNorInit` in Doornik2005, but generalised to any
31+
# distribution. r = dR, v = dV, f = probability density function,
32+
# f_inv = inverse of f
33+
def tables(r, v, f, f_inv):
34+
# compute the x_i
35+
xvec = [0]*(TABLE_LEN+1)
36+
37+
xvec[0] = v / f(r)
38+
xvec[1] = r
39+
40+
for i in range(2, TABLE_LEN):
41+
last = xvec[i-1]
42+
xvec[i] = f_inv(v / last + f(last))
43+
44+
# cache the f's
45+
fvec = [0]*(TABLE_LEN+1)
46+
fdiff = [0]*(TABLE_LEN+1)
47+
for i in range(TABLE_LEN+1):
48+
fvec[i] = f(xvec[i])
49+
if i > 0:
50+
fdiff[i] = fvec[i] - fvec[i-1]
51+
52+
return xvec, fvec, fdiff
53+
54+
# Distributions
55+
# N(0, 1)
56+
def norm_f(x):
57+
return exp(-x*x/2.0)
58+
def norm_f_inv(y):
59+
return sqrt(-2.0*log(y))
60+
61+
NORM_R = 3.6541528853610088
62+
NORM_V = 0.00492867323399
63+
64+
NORM = tables(NORM_R, NORM_V,
65+
norm_f, norm_f_inv)
66+
67+
# Exp(1)
68+
def exp_f(x):
69+
return exp(-x)
70+
def exp_f_inv(y):
71+
return -log(y)
72+
73+
EXP_R = 7.69711747013104972
74+
EXP_V = 0.0039496598225815571993
75+
76+
EXP = tables(EXP_R, EXP_V,
77+
exp_f, exp_f_inv)
78+
79+
80+
# Output the tables/constants/types
81+
82+
def render_static(name, type, value):
83+
# no space or
84+
return 'pub static %s: %s =%s;\n' % (name, type, value)
85+
86+
# static `name`: [`type`, .. `len(values)`] =
87+
# [values[0], ..., values[3],
88+
# values[4], ..., values[7],
89+
# ... ];
90+
def render_table(name, values):
91+
rows = []
92+
# 4 values on each row
93+
for i in range(0, len(values), 4):
94+
row = values[i:i+4]
95+
rows.append(', '.join('%.18f' % f for f in row))
96+
97+
rendered = '\n [%s]' % ',\n '.join(rows)
98+
return render_static(name, '[f64, .. %d]' % len(values), rendered)
99+
100+
101+
with open('ziggurat_tables.rs', 'w') as f:
102+
f.write('''// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
103+
// file at the top-level directory of this distribution and at
104+
// http://rust-lang.org/COPYRIGHT.
105+
//
106+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
107+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
108+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
109+
// option. This file may not be copied, modified, or distributed
110+
// except according to those terms.
111+
112+
// Tables for distributions which are sampled using the ziggurat
113+
// algorithm. Autogenerated by `ziggurat_tables.py`.
114+
115+
pub type ZigTable = &\'static [f64, .. %d];
116+
''' % (TABLE_LEN + 1))
117+
for name, tables, r in [('NORM', NORM, NORM_R),
118+
('EXP', EXP, EXP_R)]:
119+
f.write(render_static('ZIG_%s_R' % name, 'f64', ' %.18f' % r))
120+
for (tabname, table) in zip(TABLE_NAMES, tables):
121+
f.write(render_table('ZIG_%s_%s' % (name, tabname), table))

branches/snap-stage3/src/libcore/libc.rs

Lines changed: 2 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,6 @@ pub use libc::funcs::posix88::unistd::*;
104104

105105
pub use libc::funcs::posix01::stat_::*;
106106
pub use libc::funcs::posix01::unistd::*;
107-
pub use libc::funcs::posix01::glob::*;
108107
pub use libc::funcs::posix08::unistd::*;
109108

110109
pub use libc::funcs::bsd44::*;
@@ -211,21 +210,7 @@ pub mod types {
211210
#[cfg(target_os = "android")]
212211
pub mod os {
213212
pub mod common {
214-
pub mod posix01 {
215-
use libc::types::common::c95::{c_void};
216-
use libc::types::os::arch::c95::{c_char, size_t};
217-
pub struct glob_t {
218-
gl_pathc: size_t,
219-
gl_pathv: **c_char,
220-
gl_offs: size_t,
221-
222-
__unused1: *c_void,
223-
__unused2: *c_void,
224-
__unused3: *c_void,
225-
__unused4: *c_void,
226-
__unused5: *c_void,
227-
}
228-
}
213+
pub mod posix01 {}
229214
}
230215

231216
#[cfg(target_arch = "x86")]
@@ -384,25 +369,7 @@ pub mod types {
384369
#[cfg(target_os = "freebsd")]
385370
pub mod os {
386371
pub mod common {
387-
pub mod posix01 {
388-
use libc::types::common::c95::{c_void};
389-
use libc::types::os::arch::c95::{c_char, c_int, size_t};
390-
pub struct glob_t {
391-
gl_pathc: size_t,
392-
__unused1: size_t,
393-
gl_offs: size_t,
394-
__unused2: c_int,
395-
gl_pathv: **c_char,
396-
397-
__unused3: *c_void,
398-
399-
__unused4: *c_void,
400-
__unused5: *c_void,
401-
__unused6: *c_void,
402-
__unused7: *c_void,
403-
__unused8: *c_void,
404-
}
405-
}
372+
pub mod posix01 {}
406373
}
407374

408375
#[cfg(target_arch = "x86_64")]
@@ -604,23 +571,6 @@ pub mod types {
604571
pub mod os {
605572
pub mod common {
606573
pub mod posix01 {
607-
use libc::types::common::c95::{c_void};
608-
use libc::types::os::arch::c95::{c_char, c_int, size_t};
609-
pub struct glob_t {
610-
gl_pathc: size_t,
611-
__unused1: c_int,
612-
gl_offs: size_t,
613-
__unused2: c_int,
614-
gl_pathv: **c_char,
615-
616-
__unused3: *c_void,
617-
618-
__unused4: *c_void,
619-
__unused5: *c_void,
620-
__unused6: *c_void,
621-
__unused7: *c_void,
622-
__unused8: *c_void,
623-
}
624574
}
625575
}
626576

@@ -927,18 +877,6 @@ pub mod consts {
927877
}
928878
pub mod posix01 {
929879
pub static SIGTRAP : int = 5;
930-
931-
pub static GLOB_ERR : int = 1 << 0;
932-
pub static GLOB_MARK : int = 1 << 1;
933-
pub static GLOB_NOSORT : int = 1 << 2;
934-
pub static GLOB_DOOFFS : int = 1 << 3;
935-
pub static GLOB_NOCHECK : int = 1 << 4;
936-
pub static GLOB_APPEND : int = 1 << 5;
937-
pub static GLOB_NOESCAPE : int = 1 << 6;
938-
939-
pub static GLOB_NOSPACE : int = 1;
940-
pub static GLOB_ABORTED : int = 2;
941-
pub static GLOB_NOMATCH : int = 3;
942880
}
943881
pub mod posix08 {
944882
}
@@ -1018,18 +956,6 @@ pub mod consts {
1018956
}
1019957
pub mod posix01 {
1020958
pub static SIGTRAP : int = 5;
1021-
1022-
pub static GLOB_APPEND : int = 0x0001;
1023-
pub static GLOB_DOOFFS : int = 0x0002;
1024-
pub static GLOB_ERR : int = 0x0004;
1025-
pub static GLOB_MARK : int = 0x0008;
1026-
pub static GLOB_NOCHECK : int = 0x0010;
1027-
pub static GLOB_NOSORT : int = 0x0020;
1028-
pub static GLOB_NOESCAPE : int = 0x2000;
1029-
1030-
pub static GLOB_NOSPACE : int = -1;
1031-
pub static GLOB_ABORTED : int = -2;
1032-
pub static GLOB_NOMATCH : int = -3;
1033959
}
1034960
pub mod posix08 {
1035961
}
@@ -1110,18 +1036,6 @@ pub mod consts {
11101036
}
11111037
pub mod posix01 {
11121038
pub static SIGTRAP : int = 5;
1113-
1114-
pub static GLOB_APPEND : int = 0x0001;
1115-
pub static GLOB_DOOFFS : int = 0x0002;
1116-
pub static GLOB_ERR : int = 0x0004;
1117-
pub static GLOB_MARK : int = 0x0008;
1118-
pub static GLOB_NOCHECK : int = 0x0010;
1119-
pub static GLOB_NOSORT : int = 0x0020;
1120-
pub static GLOB_NOESCAPE : int = 0x2000;
1121-
1122-
pub static GLOB_NOSPACE : int = -1;
1123-
pub static GLOB_ABORTED : int = -2;
1124-
pub static GLOB_NOMATCH : int = -3;
11251039
}
11261040
pub mod posix08 {
11271041
}
@@ -1692,21 +1606,6 @@ pub mod funcs {
16921606
-> pid_t;
16931607
}
16941608
}
1695-
1696-
#[nolink]
1697-
#[abi = "cdecl"]
1698-
pub mod glob {
1699-
use libc::types::common::c95::{c_void};
1700-
use libc::types::os::arch::c95::{c_char, c_int};
1701-
use libc::types::os::common::posix01::{glob_t};
1702-
1703-
pub extern {
1704-
unsafe fn glob(pattern: *c_char, flags: c_int,
1705-
errfunc: *c_void, // XXX callback
1706-
pglob: *mut glob_t);
1707-
unsafe fn globfree(pglob: *mut glob_t);
1708-
}
1709-
}
17101609
}
17111610

17121611
#[cfg(target_os = "win32")]
@@ -1716,9 +1615,6 @@ pub mod funcs {
17161615

17171616
pub mod unistd {
17181617
}
1719-
1720-
pub mod glob {
1721-
}
17221618
}
17231619

17241620

branches/snap-stage3/src/libcore/os.rs

Lines changed: 0 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ use ptr;
3838
use str;
3939
use task;
4040
use uint;
41-
use unstable::finally::Finally;
4241
use vec;
4342

4443
pub use libc::fclose;
@@ -1184,88 +1183,6 @@ pub fn set_args(new_args: ~[~str]) {
11841183
}
11851184
}
11861185
1187-
// FIXME #6100 we should really use an internal implementation of this - using
1188-
// the POSIX glob functions isn't portable to windows, probably has slight
1189-
// inconsistencies even where it is implemented, and makes extending
1190-
// functionality a lot more difficult
1191-
// FIXME #6101 also provide a non-allocating version - each_glob or so?
1192-
/// Returns a vector of Path objects that match the given glob pattern
1193-
#[cfg(target_os = "linux")]
1194-
#[cfg(target_os = "android")]
1195-
#[cfg(target_os = "freebsd")]
1196-
#[cfg(target_os = "macos")]
1197-
pub fn glob(pattern: &str) -> ~[Path] {
1198-
#[cfg(target_os = "linux")]
1199-
#[cfg(target_os = "android")]
1200-
fn default_glob_t () -> libc::glob_t {
1201-
libc::glob_t {
1202-
gl_pathc: 0,
1203-
gl_pathv: ptr::null(),
1204-
gl_offs: 0,
1205-
__unused1: ptr::null(),
1206-
__unused2: ptr::null(),
1207-
__unused3: ptr::null(),
1208-
__unused4: ptr::null(),
1209-
__unused5: ptr::null(),
1210-
}
1211-
}
1212-
1213-
#[cfg(target_os = "freebsd")]
1214-
fn default_glob_t () -> libc::glob_t {
1215-
libc::glob_t {
1216-
gl_pathc: 0,
1217-
__unused1: 0,
1218-
gl_offs: 0,
1219-
__unused2: 0,
1220-
gl_pathv: ptr::null(),
1221-
__unused3: ptr::null(),
1222-
__unused4: ptr::null(),
1223-
__unused5: ptr::null(),
1224-
__unused6: ptr::null(),
1225-
__unused7: ptr::null(),
1226-
__unused8: ptr::null(),
1227-
}
1228-
}
1229-
1230-
#[cfg(target_os = "macos")]
1231-
fn default_glob_t () -> libc::glob_t {
1232-
libc::glob_t {
1233-
gl_pathc: 0,
1234-
__unused1: 0,
1235-
gl_offs: 0,
1236-
__unused2: 0,
1237-
gl_pathv: ptr::null(),
1238-
__unused3: ptr::null(),
1239-
__unused4: ptr::null(),
1240-
__unused5: ptr::null(),
1241-
__unused6: ptr::null(),
1242-
__unused7: ptr::null(),
1243-
__unused8: ptr::null(),
1244-
}
1245-
}
1246-
1247-
let mut g = default_glob_t();
1248-
do str::as_c_str(pattern) |c_pattern| {
1249-
unsafe { libc::glob(c_pattern, 0, ptr::null(), &mut g) }
1250-
};
1251-
do(|| {
1252-
let paths = unsafe {
1253-
vec::raw::from_buf_raw(g.gl_pathv, g.gl_pathc as uint)
1254-
};
1255-
do paths.map |&c_str| {
1256-
Path(unsafe { str::raw::from_c_str(c_str) })
1257-
}
1258-
}).finally {
1259-
unsafe { libc::globfree(&mut g) };
1260-
}
1261-
}
1262-
1263-
/// Returns a vector of Path objects that match the given glob pattern
1264-
#[cfg(target_os = "win32")]
1265-
pub fn glob(pattern: &str) -> ~[Path] {
1266-
fail!(~"glob() is unimplemented on Windows")
1267-
}
1268-
12691186
#[cfg(target_os = "macos")]
12701187
extern {
12711188
// These functions are in crt_externs.h.

0 commit comments

Comments
 (0)