Skip to content

Commit 1e8f501

Browse files
committed
Machine types are different from int/uint, etc (Issue #2187)
1 parent 5f904d2 commit 1e8f501

24 files changed

+200
-131
lines changed

src/libcore/comm.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ io::println(comm::recv(p));
2525
"];
2626

2727
import either::either;
28+
import libc::size_t;
2829

2930
export port::{};
3031
export chan::{};
@@ -66,7 +67,7 @@ enum chan<T: send> {
6667

6768
#[doc = "Constructs a port"]
6869
fn port<T: send>() -> port<T> {
69-
port_t(@port_ptr(rustrt::new_port(sys::size_of::<T>())))
70+
port_t(@port_ptr(rustrt::new_port(sys::size_of::<T>() as size_t)))
7071
}
7172

7273
impl methods<T: send> for port<T> {
@@ -109,7 +110,7 @@ resource port_ptr<T: send>(po: *rust_port) unsafe {
109110
rustrt::rust_port_end_detach(po);
110111

111112
// Drain the port so that all the still-enqueued items get dropped
112-
while rustrt::rust_port_size(po) > 0u {
113+
while rustrt::rust_port_size(po) > 0u as size_t {
113114
recv_::<T>(po);
114115
}
115116
rustrt::del_port(po);

src/libcore/f64.rs

+4
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,10 @@ pure fn ge(x: f64, y: f64) -> bool { ret x >= y; }
7676

7777
pure fn gt(x: f64, y: f64) -> bool { ret x > y; }
7878

79+
pure fn sqrt(x: f64) -> f64 {
80+
cmath::c_double::sqrt(x as libc::c_double) as f64
81+
}
82+
7983
#[doc = "
8084
Returns true if `x` is a positive number, including +0.0f640 and +Infinity.
8185
"]

src/libcore/float.rs

+86-20
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,57 @@ export j0, j1, jn, y0, y1, yn;
2727
import m_float = f64;
2828
import f64::*;
2929

30+
const NaN: float = 0.0/0.0;
31+
32+
const infinity: float = 1.0/0.0;
33+
34+
const neg_infinity: float = -1.0/0.0;
35+
36+
/* Module: consts */
37+
mod consts {
38+
39+
// FIXME replace with mathematical constants from cmath
40+
// (requires Issue #1433 to fix)
41+
#[doc = "Archimedes' constant"]
42+
const pi: float = 3.14159265358979323846264338327950288;
43+
44+
#[doc = "pi/2.0"]
45+
const frac_pi_2: float = 1.57079632679489661923132169163975144;
46+
47+
#[doc = "pi/4.0"]
48+
const frac_pi_4: float = 0.785398163397448309615660845819875721;
49+
50+
#[doc = "1.0/pi"]
51+
const frac_1_pi: float = 0.318309886183790671537767526745028724;
52+
53+
#[doc = "2.0/pi"]
54+
const frac_2_pi: float = 0.636619772367581343075535053490057448;
55+
56+
#[doc = "2.0/sqrt(pi)"]
57+
const frac_2_sqrtpi: float = 1.12837916709551257389615890312154517;
58+
59+
#[doc = "sqrt(2.0)"]
60+
const sqrt2: float = 1.41421356237309504880168872420969808;
61+
62+
#[doc = "1.0/sqrt(2.0)"]
63+
const frac_1_sqrt2: float = 0.707106781186547524400844362104849039;
64+
65+
#[doc = "Euler's number"]
66+
const e: float = 2.71828182845904523536028747135266250;
67+
68+
#[doc = "log2(e)"]
69+
const log2_e: float = 1.44269504088896340735992468100189214;
70+
71+
#[doc = "log10(e)"]
72+
const log10_e: float = 0.434294481903251827651128918916605082;
73+
74+
#[doc = "ln(2.0)"]
75+
const ln_2: float = 0.693147180559945309417232121458176568;
76+
77+
#[doc = "ln(10.0)"]
78+
const ln_10: float = 2.30258509299404568401799145468436421;
79+
}
80+
3081
/**
3182
* Section: String Conversions
3283
*/
@@ -178,11 +229,11 @@ where `n` is the floating-point number represented by `[num]`.
178229
"]
179230
fn from_str(num: str) -> option<float> {
180231
if num == "inf" {
181-
ret some(infinity);
232+
ret some(infinity as float);
182233
} else if num == "-inf" {
183-
ret some(neg_infinity);
234+
ret some(neg_infinity as float);
184235
} else if num == "NaN" {
185-
ret some(NaN);
236+
ret some(NaN as float);
186237
}
187238

188239
let mut pos = 0u; //Current byte position in the string.
@@ -322,25 +373,40 @@ Compute the exponentiation of an integer by another integer as a float
322373
`NaN` if both `x` and `pow` are `0u`, otherwise `x^pow`
323374
"]
324375
fn pow_with_uint(base: uint, pow: uint) -> float {
325-
if base == 0u {
326-
if pow == 0u {
327-
ret NaN;
328-
}
329-
ret 0.;
330-
}
331-
let mut my_pow = pow;
332-
let mut total = 1f;
333-
let mut multiplier = base as float;
334-
while (my_pow > 0u) {
335-
if my_pow % 2u == 1u {
336-
total = total * multiplier;
337-
}
338-
my_pow /= 2u;
339-
multiplier *= multiplier;
340-
}
341-
ret total;
376+
if base == 0u {
377+
if pow == 0u {
378+
ret NaN as float;
379+
}
380+
ret 0.;
381+
}
382+
let mut my_pow = pow;
383+
let mut total = 1f;
384+
let mut multiplier = base as float;
385+
while (my_pow > 0u) {
386+
if my_pow % 2u == 1u {
387+
total = total * multiplier;
388+
}
389+
my_pow /= 2u;
390+
multiplier *= multiplier;
391+
}
392+
ret total;
342393
}
343394

395+
fn is_positive(x: float) -> bool { f64::is_positive(x as f64) }
396+
fn is_negative(x: float) -> bool { f64::is_negative(x as f64) }
397+
fn is_nonpositive(x: float) -> bool { f64::is_nonpositive(x as f64) }
398+
fn is_nonnegative(x: float) -> bool { f64::is_nonnegative(x as f64) }
399+
fn is_zero(x: float) -> bool { f64::is_zero(x as f64) }
400+
fn is_infinite(x: float) -> bool { f64::is_infinite(x as f64) }
401+
fn is_finite(x: float) -> bool { f64::is_finite(x as f64) }
402+
fn is_NaN(x: float) -> bool { f64::is_NaN(x as f64) }
403+
404+
fn abs(x: float) -> float { f64::abs(x as f64) as float }
405+
fn sqrt(x: float) -> float { f64::sqrt(x as f64) as float }
406+
fn atan(x: float) -> float { f64::atan(x as f64) as float }
407+
fn sin(x: float) -> float { f64::sin(x as f64) as float }
408+
fn cos(x: float) -> float { f64::cos(x as f64) as float }
409+
fn tan(x: float) -> float { f64::tan(x as f64) as float }
344410

345411
#[test]
346412
fn test_from_str() {

src/libcore/io.rs

+9-8
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ Basic input/output
77
import result::result;
88

99
import dvec::{dvec, extensions};
10-
import libc::{c_int, c_uint, c_void, size_t, ssize_t};
10+
import libc::{c_int, c_long, c_uint, c_void, size_t, ssize_t};
1111
import libc::consts::os::posix88::*;
1212
import libc::consts::os::extra::*;
1313

@@ -196,17 +196,17 @@ impl of reader for *libc::FILE {
196196
let mut buf : [mut u8] = [mut];
197197
vec::reserve(buf, len);
198198
vec::as_mut_buf(buf) {|b|
199-
let read = libc::fread(b as *mut c_void, 1u,
200-
len, self);
201-
vec::unsafe::set_len(buf, read);
199+
let read = libc::fread(b as *mut c_void, 1u as size_t,
200+
len as size_t, self);
201+
vec::unsafe::set_len(buf, read as uint);
202202
}
203203
ret vec::from_mut(buf);
204204
}
205205
fn read_byte() -> int { ret libc::fgetc(self) as int; }
206206
fn unread_byte(byte: int) { libc::ungetc(byte as c_int, self); }
207207
fn eof() -> bool { ret libc::feof(self) != 0 as c_int; }
208208
fn seek(offset: int, whence: seek_style) {
209-
assert libc::fseek(self, offset, convert_whence(whence))
209+
assert libc::fseek(self, offset as c_long, convert_whence(whence))
210210
== 0 as c_int;
211211
}
212212
fn tell() -> uint { ret libc::ftell(self) as uint; }
@@ -332,7 +332,8 @@ impl <T: writer, C> of writer for {base: T, cleanup: C} {
332332
impl of writer for *libc::FILE {
333333
fn write(v: [const u8]/&) unsafe {
334334
vec::unpack_const_slice(v) {|vbuf, len|
335-
let nout = libc::fwrite(vbuf as *c_void, len, 1u, self);
335+
let nout = libc::fwrite(vbuf as *c_void, len as size_t,
336+
1u as size_t, self);
336337
if nout < 1 as size_t {
337338
#error("error writing buffer");
338339
log(error, os::last_os_error());
@@ -341,7 +342,7 @@ impl of writer for *libc::FILE {
341342
}
342343
}
343344
fn seek(offset: int, whence: seek_style) {
344-
assert libc::fseek(self, offset, convert_whence(whence))
345+
assert libc::fseek(self, offset as c_long, convert_whence(whence))
345346
== 0 as c_int;
346347
}
347348
fn tell() -> uint { libc::ftell(self) as uint }
@@ -362,7 +363,7 @@ impl of writer for fd_t {
362363
vec::unpack_const_slice(v) {|vbuf, len|
363364
while count < len {
364365
let vb = ptr::const_offset(vbuf, count) as *c_void;
365-
let nout = libc::write(self, vb, len);
366+
let nout = libc::write(self, vb, len as size_t);
366367
if nout < 0 as ssize_t {
367368
#error("error writing buffer");
368369
log(error, os::last_os_error());

src/libcore/libc.rs

+12-12
Original file line numberDiff line numberDiff line change
@@ -185,8 +185,8 @@ mod types {
185185
mod c99 {
186186
type c_longlong = i64;
187187
type c_ulonglong = u64;
188-
type intptr_t = i32;
189-
type uintptr_t = u32;
188+
type intptr_t = int;
189+
type uintptr_t = uint;
190190
}
191191
mod posix88 {
192192
type off_t = i32;
@@ -229,8 +229,8 @@ mod types {
229229
mod c99 {
230230
type c_longlong = i64;
231231
type c_ulonglong = u64;
232-
type intptr_t = i64;
233-
type uintptr_t = u64;
232+
type intptr_t = int;
233+
type uintptr_t = uint;
234234
}
235235
mod posix88 {
236236
type off_t = i64;
@@ -276,8 +276,8 @@ mod types {
276276
mod c99 {
277277
type c_longlong = i64;
278278
type c_ulonglong = u64;
279-
type intptr_t = i64;
280-
type uintptr_t = u64;
279+
type intptr_t = int;
280+
type uintptr_t = uint;
281281
}
282282
mod posix88 {
283283
type off_t = i64;
@@ -323,8 +323,8 @@ mod types {
323323
mod c99 {
324324
type c_longlong = i64;
325325
type c_ulonglong = u64;
326-
type intptr_t = i32;
327-
type uintptr_t = u32;
326+
type intptr_t = int;
327+
type uintptr_t = uint;
328328
}
329329
mod posix88 {
330330
type off_t = i32;
@@ -397,8 +397,8 @@ mod types {
397397
mod c99 {
398398
type c_longlong = i64;
399399
type c_ulonglong = u64;
400-
type intptr_t = i32;
401-
type uintptr_t = u32;
400+
type intptr_t = int;
401+
type uintptr_t = uint;
402402
}
403403
mod posix88 {
404404
type off_t = i64;
@@ -441,8 +441,8 @@ mod types {
441441
mod c99 {
442442
type c_longlong = i64;
443443
type c_ulonglong = u64;
444-
type intptr_t = i64;
445-
type uintptr_t = u64;
444+
type intptr_t = int;
445+
type uintptr_t = uint;
446446
}
447447
mod posix88 {
448448
type off_t = i64;

src/libcore/os.rs

+16-12
Original file line numberDiff line numberDiff line change
@@ -90,11 +90,11 @@ mod win32 {
9090
import libc::funcs::extra::kernel32::*;
9191
import libc::consts::os::extra::*;
9292

93-
let mut n = tmpbuf_sz;
93+
let mut n = tmpbuf_sz as dword;
9494
let mut res = none;
9595
let mut done = false;
9696
while !done {
97-
let buf = vec::to_mut(vec::from_elem(n, 0u16));
97+
let buf = vec::to_mut(vec::from_elem(n as uint, 0u16));
9898
vec::as_mut_buf(buf) {|b|
9999
let k : dword = f(b, tmpbuf_sz as dword);
100100
if k == (0 as dword) {
@@ -412,7 +412,7 @@ fn self_exe_path() -> option<path> {
412412
import libc::funcs::extra::kernel32::*;
413413
import win32::*;
414414
fill_utf16_buf_and_decode() {|buf, sz|
415-
GetModuleFileNameW(0u, buf, sz)
415+
GetModuleFileNameW(0u as dword, buf, sz)
416416
}
417417
}
418418

@@ -692,16 +692,19 @@ fn copy_file(from: path, to: path) -> bool {
692692
let mut ok = true;
693693
while !done {
694694
vec::as_mut_buf(buf) {|b|
695-
let nread = libc::fread(b as *mut c_void, 1u, bufsize, istream);
695+
let nread = libc::fread(b as *mut c_void, 1u as size_t,
696+
bufsize as size_t,
697+
istream);
696698
if nread > 0 as size_t {
697-
if libc::fwrite(b as *c_void, 1u, nread, ostream) != nread {
698-
ok = false;
699-
done = true;
700-
}
699+
if libc::fwrite(b as *c_void, 1u as size_t, nread,
700+
ostream) != nread {
701+
ok = false;
702+
done = true;
703+
}
701704
} else {
702-
done = true;
705+
done = true;
703706
}
704-
}
707+
}
705708
}
706709
fclose(istream);
707710
fclose(ostream);
@@ -988,8 +991,9 @@ mod tests {
988991
let s = "hello";
989992
let mut buf = vec::to_mut(str::bytes(s) + [0 as u8]);
990993
vec::as_mut_buf(buf) {|b|
991-
assert (libc::fwrite(b as *c_void, 1u, str::len(s) + 1u, ostream) ==
992-
buf.len())};
994+
assert (libc::fwrite(b as *c_void, 1u as size_t,
995+
(str::len(s) + 1u) as size_t, ostream)
996+
== buf.len() as size_t)};
993997
assert (libc::fclose(ostream) == (0u as c_int));
994998
let rs = os::copy_file(in, out);
995999
if (!os::path_exists(in)) {

src/libcore/ptr.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export buf_len;
1414
export position;
1515
export extensions;
1616

17-
import libc::c_void;
17+
import libc::{c_void, size_t};
1818

1919
#[nolink]
2020
#[abi = "cdecl"]
@@ -93,7 +93,7 @@ and destination may not overlap.
9393
#[inline(always)]
9494
unsafe fn memcpy<T>(dst: *T, src: *T, count: uint) {
9595
let n = count * sys::size_of::<T>();
96-
libc_::memcpy(dst as *c_void, src as *c_void, n);
96+
libc_::memcpy(dst as *c_void, src as *c_void, n as size_t);
9797
}
9898

9999
#[doc = "
@@ -105,7 +105,7 @@ and destination may overlap.
105105
#[inline(always)]
106106
unsafe fn memmove<T>(dst: *T, src: *T, count: uint) {
107107
let n = count * sys::size_of::<T>();
108-
libc_::memmove(dst as *c_void, src as *c_void, n);
108+
libc_::memmove(dst as *c_void, src as *c_void, n as size_t);
109109
}
110110

111111
#[doc = "Extension methods for pointers"]

src/libcore/str.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ for efficiency, but UTF-8 unsafe operations should be avoided. For
77
some heavy-duty uses, try std::rope.
88
"];
99

10+
import libc::size_t;
11+
1012
export
1113
// Creating a string
1214
from_bytes,
@@ -1596,7 +1598,7 @@ capacity, then no action is taken.
15961598
"]
15971599
fn reserve(&s: str, n: uint) {
15981600
if capacity(s) < n {
1599-
rustrt::str_reserve_shared(s, n);
1601+
rustrt::str_reserve_shared(s, n as size_t);
16001602
}
16011603
}
16021604

0 commit comments

Comments
 (0)