Skip to content

Instantly share code, notes, and snippets.

@osanshouo
Created September 15, 2020 14:32
Show Gist options
  • Save osanshouo/ee9f3582613c52defb45dbf766c9c383 to your computer and use it in GitHub Desktop.
Save osanshouo/ee9f3582613c52defb45dbf766c9c383 to your computer and use it in GitHub Desktop.
浮動小数点数の加算アルゴリズム
impl Add for F64 {
type Output = $float;
fn add(self, rhs: $float) -> $float {
let (a, b) = (self.to_bits(), rhs.to_bits());
let (sign_a, sign_b) = (a & F64::SIGNPART, b & F64::SIGNPART);
if sign_a == sign_b {
add_mags(a, b, sign_a)
} else {
sub_mags(a, b, sign_a)
}
}
}
pub(crate) fn add_mags(a: u64, b: u64, sign: u64) -> F64 {
let (exp_a, sig_a) = F64::exp_and_sig(a);
let (exp_b, sig_b) = F64::exp_and_sig(b);
let result = if exp_a == exp_b {
add_for_same_exp(exp_a, sig_a, sig_b, sign)
} else if exp_a > exp_b {
add_for_different_exp(exp_a, sig_a, sig_b, exp_a - exp_b, sign)
} else {
add_for_different_exp(exp_b, sig_b, sig_a, exp_b - exp_a, sign)
};
match result {
Ok((exp, alpha)) => round_pack_to_f64(sign, exp, alpha),
Err(z) => Self::from_bits(z),
}
}
fn add_for_same_exp(exp: i16, sig_a: u64, sig_b: u64, sign: u64) -> Result<(i16, u64), u64> {
if exp == 0 { // 非正規数またはゼロ同士の加算
Err((sig_a + sig_b) | sign)
} else if exp == 0x7ff { // 無限大または NaN
if (sig_a | sig_b) != 0 { // どちらかが NaN の場合, 出力も NaN.
Err(F64::NAN.to_bits())
} else {
Err(0x7ff0000000000000 | sign)
}
} else { // 正規数
// 仮数部の加算を実行
let alpha = (0x0020000000000000 + sig_a + sig_b) << 9;
Ok((exp, alpha))
}
}
fn add_for_different_exp(exp: i16, mut sig_a: u64, mut sig_b: u64, diff: i16, sign: u64) -> Result<(i16, u64), u64> {
sig_a <<= 9;
sig_b <<= 9;
if exp == 0x7ff {
if sig_a != 0 {
Err(F64::NAN.to_bits())
} else {
// a が無限大の場合
Err(0x7ff0000000000000 | sign)
}
} else {
sig_b = if exp == diff {
// b が非正規数の場合, 仮数部はそのまま alpha を与える.
sig_b << 1
} else {
// b が正規数の場合, 仮数部のケチ表現で省略された最上位ビットを補う.
sig_b + 0x2000000000000000
};
// a に桁を合わせる
sig_b = shift_right_jam64(sig_b, diff);
// 加算を実行
let alpha = 0x2000000000000000 + sig_a + sig_b;
if alpha < 0x4000000000000000 {
Ok((exp - 1, alpha << 1))
} else {
Ok((exp, alpha))
}
}
}
fn shift_right_jam64(sig: u64, diff: i16) -> u64 {
if diff < 63 {
sig >> diff | ((sig << (-diff & 63)) != 0) as u64
} else { (sig != 0) as u64 }
}
fn round_pack_to_f64(sign: u64, exp: i16, mut alpha: u64) -> F64 {
let exp = exp as u64 + 1;
if dbg!(exp) >= 2047 {
F64::from_bits(F64::INFINITY.to_bits() | sign)
} else {
alpha = alpha >> 10;
alpha &= F64::SIGNIFICAND;
F64::from_bits(sign | (exp << 52) | alpha)
}
}
本 gist は Berkeley SoftFloat 3 のC言語コードを部分的に Rust に移植したものです. オリジナルのライセンスはこちら:
License for Berkeley SoftFloat Release 3e
John R. Hauser
2018 January 20
The following applies to the whole of SoftFloat Release 3e as well as to
each source file individually.
Copyright 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018 The Regents of the
University of California. All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions, and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions, and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. Neither the name of the University nor the names of its contributors
may be used to endorse or promote products derived from this software
without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS "AS IS", AND ANY
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE
DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment