Skip to content

moving math to core #1293

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 82 additions & 0 deletions src/libcore/cmath.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import ctypes::c_int;

#[link_name = "m"]
#[abi = "cdecl"]
native mod f64 {

// Alpabetically sorted by link_name

pure fn acos(n: f64) -> f64;
pure fn asin(n: f64) -> f64;
pure fn atan(n: f64) -> f64;
pure fn atan2(a: f64, b: f64) -> f64;
pure fn ceil(n: f64) -> f64;
pure fn cos(n: f64) -> f64;
pure fn cosh(n: f64) -> f64;
pure fn exp(n: f64) -> f64;
#[link_name="fabs"] pure fn abs(n: f64) -> f64;
pure fn floor(n: f64) -> f64;
pure fn fmod(x: f64, y: f64) -> f64;
pure fn frexp(n: f64, &value: c_int) -> f64;
pure fn ldexp(x: f64, n: c_int) -> f64;
#[link_name="log"] pure fn ln(n: f64) -> f64;
#[link_name="log1p"] pure fn ln1p(n: f64) -> f64;
pure fn log10(n: f64) -> f64;
pure fn log2(n: f64) -> f64;
pure fn modf(n: f64, iptr: *f64) -> f64;
pure fn pow(n: f64, e: f64) -> f64;
pure fn rint(n: f64) -> f64;
pure fn round(n: f64) -> f64;
pure fn sin(n: f64) -> f64;
pure fn sinh(n: f64) -> f64;
pure fn sqrt(n: f64) -> f64;
pure fn tan(n: f64) -> f64;
pure fn tanh(n: f64) -> f64;
pure fn trunc(n: f64) -> f64;
}

#[link_name = "m"]
#[abi = "cdecl"]
native mod f32 {

// Alpabetically sorted by link_name

#[link_name="acosf"] pure fn acos(n: f32) -> f32;
#[link_name="asinf"] pure fn asin(n: f32) -> f32;
#[link_name="atanf"] pure fn atan(n: f32) -> f32;
#[link_name="atan2f"] pure fn atan2(a: f32, b: f32) -> f32;
#[link_name="ceilf"] pure fn ceil(n: f32) -> f32;
#[link_name="cosf"] pure fn cos(n: f32) -> f32;
#[link_name="coshf"] pure fn cosh(n: f32) -> f32;
#[link_name="expf"] pure fn exp(n: f32) -> f32;
#[link_name="fabsf"] pure fn abs(n: f32) -> f32;
#[link_name="floorf"] pure fn floor(n: f32) -> f32;
#[link_name="frexpf"] pure fn frexp(n: f64, &value: c_int) -> f32;
#[link_name="fmodf"] pure fn fmod(x: f32, y: f32) -> f32;
#[link_name="ldexpf"] pure fn ldexp(x: f32, n: c_int) -> f32;
#[link_name="logf"] pure fn ln(n: f32) -> f32;
#[link_name="log1p"] pure fn ln1p(n: f64) -> f64;
#[link_name="log2f"] pure fn log2(n: f32) -> f32;
#[link_name="log10f"] pure fn log10(n: f32) -> f32;
#[link_name="modff"] pure fn modf(n: f32, iptr: *f32) -> f32;
#[link_name="powf"] pure fn pow(n: f32, e: f32) -> f32;
#[link_name="rintf"] pure fn rint(n: f32) -> f32;
#[link_name="roundf"] pure fn round(n: f32) -> f32;
#[link_name="sinf"] pure fn sin(n: f32) -> f32;
#[link_name="sinhf"] pure fn sinh(n: f32) -> f32;
#[link_name="sqrtf"] pure fn sqrt(n: f32) -> f32;
#[link_name="tanf"] pure fn tan(n: f32) -> f32;
#[link_name="tanhf"] pure fn tanh(n: f32) -> f32;
#[link_name="truncf"] pure fn trunc(n: f32) -> f32;
}

//
// Local Variables:
// mode: rust
// fill-column: 78;
// indent-tabs-mode: nil
// c-basic-offset: 4
// buffer-file-coding-system: utf-8-unix
// End:
//

9 changes: 7 additions & 2 deletions src/libcore/core.rc
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,19 @@
#[license = "BSD"];
#[crate_type = "lib"];

export box, char, float, int, str, ptr, uint, u8, u32, u64, vec, bool;
export box, char, float, f32, f64, int, str, ptr;
export uint, u8, u32, u64, vec, bool;
export either, option, result;
export ctypes, sys, unsafe, comm, task;
export ctypes, mtypes, sys, unsafe, comm, task;
export extfmt;

// Built-in-type support modules

mod box;
mod char;
mod float;
mod f32;
mod f64;
mod int;
mod str;
mod ptr;
Expand All @@ -38,6 +41,8 @@ mod result;
// Runtime and language-primitive support

mod ctypes;
mod mtypes;
mod cmath;
mod sys;
mod unsafe;
mod comm;
Expand Down
30 changes: 0 additions & 30 deletions src/libcore/ctypes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,36 +72,6 @@ when interoperating with C void pointers can help in documentation.
*/
type void = int;

// machine type equivalents of rust int, uint, float

/*
Type: m_int

FIXME: What C type does this represent?
*/
#[cfg(target_arch="x86")]
type m_int = i32;
#[cfg(target_arch="x86_64")]
type m_int = i64;

/*
Type: m_uint

FIXME: What C type does this represent?
*/
#[cfg(target_arch="x86")]
type m_uint = u32;
#[cfg(target_arch="x86_64")]
type m_uint = u64;

// This *must* match with "import m_float = fXX" in std::math per arch
/*
Type: m_float

FIXME: What C type does this represent?
*/
type m_float = f64;

/*
Type: size_t

Expand Down
125 changes: 125 additions & 0 deletions src/libcore/f32.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@

/*
Module: f32

Floating point operations and constants for `f32`

This exposes the same operations as `math`, just for `f32` even though
they do not show up in the docs right now!
*/

import cmath::f32::*;

export
acos, asin, atan, atan2, ceil, cos, cosh, exp, abs, floor, fmod,
frexp, ldexp, ln, ln1p, log10, log2, modf, rint, round, pow, sin,
sinh, sqrt, tan, tanh, trunc, t;

export consts;

type t = f32;

/* Module: consts */
mod consts {

/*
Const: pi

Archimedes' constant
*/
const pi: f32 = 3.14159265358979323846264338327950288f32;

/*
Const: frac_pi_2

pi/2.0
*/
const frac_pi_2: f32 = 1.57079632679489661923132169163975144f32;

/*
Const: frac_pi_4

pi/4.0
*/
const frac_pi_4: f32 = 0.785398163397448309615660845819875721f32;

/*
Const: frac_1_pi

1.0/pi
*/
const frac_1_pi: f32 = 0.318309886183790671537767526745028724f32;

/*
Const: frac_2_pi

2.0/pi
*/
const frac_2_pi: f32 = 0.636619772367581343075535053490057448f32;

/*
Const: frac_2_sqrtpi

2.0/sqrt(pi)
*/
const frac_2_sqrtpi: f32 = 1.12837916709551257389615890312154517f32;

/*
Const: sqrt2

sqrt(2.0)
*/
const sqrt2: f32 = 1.41421356237309504880168872420969808f32;

/*
Const: frac_1_sqrt2

1.0/sqrt(2.0)
*/
const frac_1_sqrt2: f32 = 0.707106781186547524400844362104849039f32;

/*
Const: e

Euler's number
*/
const e: f32 = 2.71828182845904523536028747135266250f32;

/*
Const: log2_e

log2(e)
*/
const log2_e: f32 = 1.44269504088896340735992468100189214f32;

/*
Const: log10_e

log10(e)
*/
const log10_e: f32 = 0.434294481903251827651128918916605082f32;

/*
Const: ln_2

ln(2.0)
*/
const ln_2: f32 = 0.693147180559945309417232121458176568f32;

/*
Const: ln_10

ln(10.0)
*/
const ln_10: f32 = 2.30258509299404568401799145468436421f32;
}

//
// Local Variables:
// mode: rust
// fill-column: 78;
// indent-tabs-mode: nil
// c-basic-offset: 4
// buffer-file-coding-system: utf-8-unix
// End:
//
Loading