This repository was archived by the owner on Apr 28, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 102
Add newlib cbrtf #177
Closed
Closed
Add newlib cbrtf #177
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
322b8f7
Add newlib fdlibm
tesuji ce5dd47
WIP make functions pub(crate)
tesuji 80a730c
WIP do not always inline, let LLVM decide
tesuji 0b3ea35
Move src/math/cbrtf.rs -> src/musl/cbrtf.rs
tesuji b718f26
chore: Remove musl module
tesuji d153151
Add newlib cbrtf
tesuji e68ca24
WIP make const
tesuji File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,7 @@ | ||
/* origin: FreeBSD /usr/src/lib/msun/src/s_cbrtf.c */ | ||
/* | ||
/* sf_cbrt.c -- float version of s_cbrt.c. | ||
* Conversion to float by Ian Lance Taylor, Cygnus Support, [email protected]. | ||
* Debugged and optimized by Bruce D. Evans. | ||
*/ | ||
|
||
/* | ||
* ==================================================== | ||
* Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. | ||
|
@@ -12,65 +11,52 @@ | |
* software is freely granted, provided that this notice | ||
* is preserved. | ||
* ==================================================== | ||
* | ||
*/ | ||
/* cbrtf(x) | ||
* Return cube root of x | ||
*/ | ||
|
||
use core::f32; | ||
use super::fdlibm::{FLT_UWORD_IS_FINITE, FLT_UWORD_IS_SUBNORMAL, FLT_UWORD_IS_ZERO}; | ||
|
||
const B1: u32 = 709_958_130; /* B1 = (84+2/3-0.03306235651)*2**23 */ | ||
const B2: u32 = 642_849_266; /* B2 = (76+2/3-0.03306235651)*2**23 */ | ||
|
||
const B1: u32 = 709958130; /* B1 = (127-127.0/3-0.03306235651)*2**23 */ | ||
const B2: u32 = 642849266; /* B2 = (127-127.0/3-24/3-0.03306235651)*2**23 */ | ||
const C: f32 = 5.428_571_701_0e-01; /* 19/35 = 0x3f0a_f8b0 */ | ||
const D: f32 = -7.053_061_127_7e-01; /* -864/1225 = 0xbf34_8ef1 */ | ||
const E: f32 = 1.414_285_659_8e+00; /* 99/70 = 0x3fb5_0750 */ | ||
const F: f32 = 1.607_142_806_1e+00; /* 45/28 = 0x3fcd_b6db */ | ||
const G: f32 = 3.571_428_656_6e-01; /* 5/14 = 0x3eb6_db6e */ | ||
|
||
/// Cube root (f32) | ||
/// | ||
/// Computes the cube root of the argument. | ||
#[inline] | ||
#[cfg_attr(all(test, assert_no_panic), no_panic::no_panic)] | ||
pub fn cbrtf(x: f32) -> f32 { | ||
let x1p24 = f32::from_bits(0x4b800000); // 0x1p24f === 2 ^ 24 | ||
pub extern "C" fn cbrtf(x: f32) -> f32 { | ||
let hx: u32 = x.to_bits() & 0x7fff_ffff; /* |x| */ | ||
let sign: u32 = x.to_bits() & 0x8000_0000; /* sign = sign(x) */ | ||
|
||
let mut r: f64; | ||
let mut t: f64; | ||
let mut ui: u32 = x.to_bits(); | ||
let mut hx: u32 = ui & 0x7fffffff; | ||
|
||
if hx >= 0x7f800000 { | ||
/* cbrt(NaN,INF) is itself */ | ||
return x + x; | ||
if !FLT_UWORD_IS_FINITE(hx) { | ||
return x + x; /* cbrt(NaN,INF) is itself */ | ||
} | ||
if FLT_UWORD_IS_ZERO(hx) { | ||
return x; /* cbrt(0) is itself */ | ||
} | ||
|
||
let x = f32::from_bits(hx); /* x <- |x| */ | ||
/* rough cbrt to 5 bits */ | ||
if hx < 0x00800000 { | ||
/* zero or subnormal? */ | ||
if hx == 0 { | ||
return x; /* cbrt(+-0) is itself */ | ||
} | ||
ui = (x * x1p24).to_bits(); | ||
hx = ui & 0x7fffffff; | ||
hx = hx / 3 + B2; | ||
let mut t: f32 = if FLT_UWORD_IS_SUBNORMAL(hx) { | ||
/* subnormal number */ | ||
const X1P24: f32 = 16777216f32; // 2 ** 24 | ||
let high: u32 = (x * X1P24).to_bits(); | ||
f32::from_bits((high / 3).wrapping_add(B2)) | ||
} else { | ||
hx = hx / 3 + B1; | ||
} | ||
ui &= 0x80000000; | ||
ui |= hx; | ||
|
||
/* | ||
* First step Newton iteration (solving t*t-x/t == 0) to 16 bits. In | ||
* double precision so that its terms can be arranged for efficiency | ||
* without causing overflow or underflow. | ||
*/ | ||
t = f32::from_bits(ui) as f64; | ||
r = t * t * t; | ||
t = t * (x as f64 + x as f64 + r) / (x as f64 + r + r); | ||
f32::from_bits((hx / 3).wrapping_add(B1)) | ||
}; | ||
|
||
/* | ||
* Second step Newton iteration to 47 bits. In double precision for | ||
* efficiency and accuracy. | ||
*/ | ||
r = t * t * t; | ||
t = t * (x as f64 + x as f64 + r) / (x as f64 + r + r); | ||
/* new cbrt to 23 bits */ | ||
let r: f32 = t * t / x; | ||
let s: f32 = C + r * t; | ||
t *= G + F / (s + E + D / s); | ||
|
||
/* rounding to 24 bits is perfect in round-to-nearest mode */ | ||
t as f32 | ||
/* restore the sign bit */ | ||
f32::from_bits(t.to_bits() | sign) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.