Skip to content

Commit c61eb67

Browse files
committed
Make algebraic functions in 'f16', 'f32', 'f64', and 'f128' into 'const fn' items;
1 parent 1bc5618 commit c61eb67

File tree

4 files changed

+228
-44
lines changed

4 files changed

+228
-44
lines changed

library/core/src/num/f128.rs

+57-11
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,10 @@
1212
#![unstable(feature = "f128", issue = "116909")]
1313

1414
use crate::convert::FloatToInt;
15+
use crate::intrinsics::{self, const_eval_select};
16+
use crate::mem;
1517
use crate::num::FpCategory;
1618
use crate::panic::const_assert;
17-
use crate::{intrinsics, mem};
1819

1920
/// Basic mathematical constants.
2021
#[unstable(feature = "f128", issue = "116909")]
@@ -1368,48 +1369,93 @@ impl f128 {
13681369
/// See [algebraic operators](primitive@f32#algebraic-operators) for more info.
13691370
#[must_use = "method returns a new number and does not mutate the original value"]
13701371
#[unstable(feature = "float_algebraic", issue = "136469")]
1372+
#[rustc_const_unstable(feature = "float_algebraic", issue = "136469")]
13711373
#[inline]
1372-
pub fn algebraic_add(self, rhs: f128) -> f128 {
1373-
intrinsics::fadd_algebraic(self, rhs)
1374+
pub const fn algebraic_add(self, rhs: f128) -> f128 {
1375+
const fn const_algebraic_add(lhs: f128, rhs: f128) -> f128 {
1376+
lhs + rhs
1377+
}
1378+
1379+
fn rt_algebraic_add(lhs: f128, rhs: f128) -> f128 {
1380+
intrinsics::fadd_algebraic(lhs, rhs)
1381+
}
1382+
1383+
const_eval_select((self, rhs), const_algebraic_add, rt_algebraic_add)
13741384
}
13751385

13761386
/// Float subtraction that allows optimizations based on algebraic rules.
13771387
///
13781388
/// See [algebraic operators](primitive@f32#algebraic-operators) for more info.
13791389
#[must_use = "method returns a new number and does not mutate the original value"]
13801390
#[unstable(feature = "float_algebraic", issue = "136469")]
1391+
#[rustc_const_unstable(feature = "float_algebraic", issue = "136469")]
13811392
#[inline]
1382-
pub fn algebraic_sub(self, rhs: f128) -> f128 {
1383-
intrinsics::fsub_algebraic(self, rhs)
1393+
pub const fn algebraic_sub(self, rhs: f128) -> f128 {
1394+
const fn const_algebraic_sub(lhs: f128, rhs: f128) -> f128 {
1395+
lhs - rhs
1396+
}
1397+
1398+
fn rt_algebraic_sub(lhs: f128, rhs: f128) -> f128 {
1399+
intrinsics::fsub_algebraic(lhs, rhs)
1400+
}
1401+
1402+
const_eval_select((self, rhs), const_algebraic_sub, rt_algebraic_sub)
13841403
}
13851404

13861405
/// Float multiplication that allows optimizations based on algebraic rules.
13871406
///
13881407
/// See [algebraic operators](primitive@f32#algebraic-operators) for more info.
13891408
#[must_use = "method returns a new number and does not mutate the original value"]
13901409
#[unstable(feature = "float_algebraic", issue = "136469")]
1410+
#[rustc_const_unstable(feature = "float_algebraic", issue = "136469")]
13911411
#[inline]
1392-
pub fn algebraic_mul(self, rhs: f128) -> f128 {
1393-
intrinsics::fmul_algebraic(self, rhs)
1412+
pub const fn algebraic_mul(self, rhs: f128) -> f128 {
1413+
const fn const_algebraic_mul(lhs: f128, rhs: f128) -> f128 {
1414+
lhs * rhs
1415+
}
1416+
1417+
fn rt_algebraic_mul(lhs: f128, rhs: f128) -> f128 {
1418+
intrinsics::fmul_algebraic(lhs, rhs)
1419+
}
1420+
1421+
const_eval_select((self, rhs), const_algebraic_mul, rt_algebraic_mul)
13941422
}
13951423

13961424
/// Float division that allows optimizations based on algebraic rules.
13971425
///
13981426
/// See [algebraic operators](primitive@f32#algebraic-operators) for more info.
13991427
#[must_use = "method returns a new number and does not mutate the original value"]
14001428
#[unstable(feature = "float_algebraic", issue = "136469")]
1429+
#[rustc_const_unstable(feature = "float_algebraic", issue = "136469")]
14011430
#[inline]
1402-
pub fn algebraic_div(self, rhs: f128) -> f128 {
1403-
intrinsics::fdiv_algebraic(self, rhs)
1431+
pub const fn algebraic_div(self, rhs: f128) -> f128 {
1432+
const fn const_algebraic_div(lhs: f128, rhs: f128) -> f128 {
1433+
lhs / rhs
1434+
}
1435+
1436+
fn rt_algebraic_div(lhs: f128, rhs: f128) -> f128 {
1437+
intrinsics::fdiv_algebraic(lhs, rhs)
1438+
}
1439+
1440+
const_eval_select((self, rhs), const_algebraic_div, rt_algebraic_div)
14041441
}
14051442

14061443
/// Float remainder that allows optimizations based on algebraic rules.
14071444
///
14081445
/// See [algebraic operators](primitive@f32#algebraic-operators) for more info.
14091446
#[must_use = "method returns a new number and does not mutate the original value"]
14101447
#[unstable(feature = "float_algebraic", issue = "136469")]
1448+
#[rustc_const_unstable(feature = "float_algebraic", issue = "136469")]
14111449
#[inline]
1412-
pub fn algebraic_rem(self, rhs: f128) -> f128 {
1413-
intrinsics::frem_algebraic(self, rhs)
1450+
pub const fn algebraic_rem(self, rhs: f128) -> f128 {
1451+
const fn const_algebraic_rem(lhs: f128, rhs: f128) -> f128 {
1452+
lhs % rhs
1453+
}
1454+
1455+
fn rt_algebraic_rem(lhs: f128, rhs: f128) -> f128 {
1456+
intrinsics::frem_algebraic(lhs, rhs)
1457+
}
1458+
1459+
const_eval_select((self, rhs), const_algebraic_rem, rt_algebraic_rem)
14141460
}
14151461
}

library/core/src/num/f16.rs

+57-11
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,10 @@
1212
#![unstable(feature = "f16", issue = "116909")]
1313

1414
use crate::convert::FloatToInt;
15+
use crate::intrinsics::{self, const_eval_select};
16+
use crate::mem;
1517
use crate::num::FpCategory;
1618
use crate::panic::const_assert;
17-
use crate::{intrinsics, mem};
1819

1920
/// Basic mathematical constants.
2021
#[unstable(feature = "f16", issue = "116909")]
@@ -1344,48 +1345,93 @@ impl f16 {
13441345
/// See [algebraic operators](primitive@f32#algebraic-operators) for more info.
13451346
#[must_use = "method returns a new number and does not mutate the original value"]
13461347
#[unstable(feature = "float_algebraic", issue = "136469")]
1348+
#[rustc_const_unstable(feature = "float_algebraic", issue = "136469")]
13471349
#[inline]
1348-
pub fn algebraic_add(self, rhs: f16) -> f16 {
1349-
intrinsics::fadd_algebraic(self, rhs)
1350+
pub const fn algebraic_add(self, rhs: f16) -> f16 {
1351+
const fn const_algebraic_add(lhs: f16, rhs: f16) -> f16 {
1352+
lhs + rhs
1353+
}
1354+
1355+
fn rt_algebraic_add(lhs: f16, rhs: f16) -> f16 {
1356+
intrinsics::fadd_algebraic(lhs, rhs)
1357+
}
1358+
1359+
const_eval_select((self, rhs), const_algebraic_add, rt_algebraic_add)
13501360
}
13511361

13521362
/// Float subtraction that allows optimizations based on algebraic rules.
13531363
///
13541364
/// See [algebraic operators](primitive@f32#algebraic-operators) for more info.
13551365
#[must_use = "method returns a new number and does not mutate the original value"]
13561366
#[unstable(feature = "float_algebraic", issue = "136469")]
1367+
#[rustc_const_unstable(feature = "float_algebraic", issue = "136469")]
13571368
#[inline]
1358-
pub fn algebraic_sub(self, rhs: f16) -> f16 {
1359-
intrinsics::fsub_algebraic(self, rhs)
1369+
pub const fn algebraic_sub(self, rhs: f16) -> f16 {
1370+
const fn const_algebraic_sub(lhs: f16, rhs: f16) -> f16 {
1371+
lhs - rhs
1372+
}
1373+
1374+
fn rt_algebraic_sub(lhs: f16, rhs: f16) -> f16 {
1375+
intrinsics::fsub_algebraic(lhs, rhs)
1376+
}
1377+
1378+
const_eval_select((self, rhs), const_algebraic_sub, rt_algebraic_sub)
13601379
}
13611380

13621381
/// Float multiplication that allows optimizations based on algebraic rules.
13631382
///
13641383
/// See [algebraic operators](primitive@f32#algebraic-operators) for more info.
13651384
#[must_use = "method returns a new number and does not mutate the original value"]
13661385
#[unstable(feature = "float_algebraic", issue = "136469")]
1386+
#[rustc_const_unstable(feature = "float_algebraic", issue = "136469")]
13671387
#[inline]
1368-
pub fn algebraic_mul(self, rhs: f16) -> f16 {
1369-
intrinsics::fmul_algebraic(self, rhs)
1388+
pub const fn algebraic_mul(self, rhs: f16) -> f16 {
1389+
const fn const_algebraic_mul(lhs: f16, rhs: f16) -> f16 {
1390+
lhs * rhs
1391+
}
1392+
1393+
fn rt_algebraic_mul(lhs: f16, rhs: f16) -> f16 {
1394+
intrinsics::fmul_algebraic(lhs, rhs)
1395+
}
1396+
1397+
const_eval_select((self, rhs), const_algebraic_mul, rt_algebraic_mul)
13701398
}
13711399

13721400
/// Float division that allows optimizations based on algebraic rules.
13731401
///
13741402
/// See [algebraic operators](primitive@f32#algebraic-operators) for more info.
13751403
#[must_use = "method returns a new number and does not mutate the original value"]
13761404
#[unstable(feature = "float_algebraic", issue = "136469")]
1405+
#[rustc_const_unstable(feature = "float_algebraic", issue = "136469")]
13771406
#[inline]
1378-
pub fn algebraic_div(self, rhs: f16) -> f16 {
1379-
intrinsics::fdiv_algebraic(self, rhs)
1407+
pub const fn algebraic_div(self, rhs: f16) -> f16 {
1408+
const fn const_algebraic_div(lhs: f16, rhs: f16) -> f16 {
1409+
lhs / rhs
1410+
}
1411+
1412+
fn rt_algebraic_div(lhs: f16, rhs: f16) -> f16 {
1413+
intrinsics::fdiv_algebraic(lhs, rhs)
1414+
}
1415+
1416+
const_eval_select((self, rhs), const_algebraic_div, rt_algebraic_div)
13801417
}
13811418

13821419
/// Float remainder that allows optimizations based on algebraic rules.
13831420
///
13841421
/// See [algebraic operators](primitive@f32#algebraic-operators) for more info.
13851422
#[must_use = "method returns a new number and does not mutate the original value"]
13861423
#[unstable(feature = "float_algebraic", issue = "136469")]
1424+
#[rustc_const_unstable(feature = "float_algebraic", issue = "136469")]
13871425
#[inline]
1388-
pub fn algebraic_rem(self, rhs: f16) -> f16 {
1389-
intrinsics::frem_algebraic(self, rhs)
1426+
pub const fn algebraic_rem(self, rhs: f16) -> f16 {
1427+
const fn const_algebraic_rem(lhs: f16, rhs: f16) -> f16 {
1428+
lhs % rhs
1429+
}
1430+
1431+
fn rt_algebraic_rem(lhs: f16, rhs: f16) -> f16 {
1432+
intrinsics::frem_algebraic(lhs, rhs)
1433+
}
1434+
1435+
const_eval_select((self, rhs), const_algebraic_rem, rt_algebraic_rem)
13901436
}
13911437
}

library/core/src/num/f32.rs

+57-11
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,10 @@
1212
#![stable(feature = "rust1", since = "1.0.0")]
1313

1414
use crate::convert::FloatToInt;
15+
use crate::intrinsics::{self, const_eval_select};
1516
use crate::num::FpCategory;
1617
use crate::panic::const_assert;
17-
use crate::{cfg_match, intrinsics, mem};
18+
use crate::{cfg_match, mem};
1819

1920
/// The radix or base of the internal representation of `f32`.
2021
/// Use [`f32::RADIX`] instead.
@@ -1510,48 +1511,93 @@ impl f32 {
15101511
/// See [algebraic operators](primitive@f32#algebraic-operators) for more info.
15111512
#[must_use = "method returns a new number and does not mutate the original value"]
15121513
#[unstable(feature = "float_algebraic", issue = "136469")]
1514+
#[rustc_const_unstable(feature = "float_algebraic", issue = "136469")]
15131515
#[inline]
1514-
pub fn algebraic_add(self, rhs: f32) -> f32 {
1515-
intrinsics::fadd_algebraic(self, rhs)
1516+
pub const fn algebraic_add(self, rhs: f32) -> f32 {
1517+
const fn const_algebraic_add(lhs: f32, rhs: f32) -> f32 {
1518+
lhs + rhs
1519+
}
1520+
1521+
fn rt_algebraic_add(lhs: f32, rhs: f32) -> f32 {
1522+
intrinsics::fadd_algebraic(lhs, rhs)
1523+
}
1524+
1525+
const_eval_select((self, rhs), const_algebraic_add, rt_algebraic_add)
15161526
}
15171527

15181528
/// Float subtraction that allows optimizations based on algebraic rules.
15191529
///
15201530
/// See [algebraic operators](primitive@f32#algebraic-operators) for more info.
15211531
#[must_use = "method returns a new number and does not mutate the original value"]
15221532
#[unstable(feature = "float_algebraic", issue = "136469")]
1533+
#[rustc_const_unstable(feature = "float_algebraic", issue = "136469")]
15231534
#[inline]
1524-
pub fn algebraic_sub(self, rhs: f32) -> f32 {
1525-
intrinsics::fsub_algebraic(self, rhs)
1535+
pub const fn algebraic_sub(self, rhs: f32) -> f32 {
1536+
const fn const_algebraic_sub(lhs: f32, rhs: f32) -> f32 {
1537+
lhs - rhs
1538+
}
1539+
1540+
fn rt_algebraic_sub(lhs: f32, rhs: f32) -> f32 {
1541+
intrinsics::fsub_algebraic(lhs, rhs)
1542+
}
1543+
1544+
const_eval_select((self, rhs), const_algebraic_sub, rt_algebraic_sub)
15261545
}
15271546

15281547
/// Float multiplication that allows optimizations based on algebraic rules.
15291548
///
15301549
/// See [algebraic operators](primitive@f32#algebraic-operators) for more info.
15311550
#[must_use = "method returns a new number and does not mutate the original value"]
15321551
#[unstable(feature = "float_algebraic", issue = "136469")]
1552+
#[rustc_const_unstable(feature = "float_algebraic", issue = "136469")]
15331553
#[inline]
1534-
pub fn algebraic_mul(self, rhs: f32) -> f32 {
1535-
intrinsics::fmul_algebraic(self, rhs)
1554+
pub const fn algebraic_mul(self, rhs: f32) -> f32 {
1555+
const fn const_algebraic_mul(lhs: f32, rhs: f32) -> f32 {
1556+
lhs * rhs
1557+
}
1558+
1559+
fn rt_algebraic_mul(lhs: f32, rhs: f32) -> f32 {
1560+
intrinsics::fmul_algebraic(lhs, rhs)
1561+
}
1562+
1563+
const_eval_select((self, rhs), const_algebraic_mul, rt_algebraic_mul)
15361564
}
15371565

15381566
/// Float division that allows optimizations based on algebraic rules.
15391567
///
15401568
/// See [algebraic operators](primitive@f32#algebraic-operators) for more info.
15411569
#[must_use = "method returns a new number and does not mutate the original value"]
15421570
#[unstable(feature = "float_algebraic", issue = "136469")]
1571+
#[rustc_const_unstable(feature = "float_algebraic", issue = "136469")]
15431572
#[inline]
1544-
pub fn algebraic_div(self, rhs: f32) -> f32 {
1545-
intrinsics::fdiv_algebraic(self, rhs)
1573+
pub const fn algebraic_div(self, rhs: f32) -> f32 {
1574+
const fn const_algebraic_div(lhs: f32, rhs: f32) -> f32 {
1575+
lhs / rhs
1576+
}
1577+
1578+
fn rt_algebraic_div(lhs: f32, rhs: f32) -> f32 {
1579+
intrinsics::fdiv_algebraic(lhs, rhs)
1580+
}
1581+
1582+
const_eval_select((self, rhs), const_algebraic_div, rt_algebraic_div)
15461583
}
15471584

15481585
/// Float remainder that allows optimizations based on algebraic rules.
15491586
///
15501587
/// See [algebraic operators](primitive@f32#algebraic-operators) for more info.
15511588
#[must_use = "method returns a new number and does not mutate the original value"]
15521589
#[unstable(feature = "float_algebraic", issue = "136469")]
1590+
#[rustc_const_unstable(feature = "float_algebraic", issue = "136469")]
15531591
#[inline]
1554-
pub fn algebraic_rem(self, rhs: f32) -> f32 {
1555-
intrinsics::frem_algebraic(self, rhs)
1592+
pub const fn algebraic_rem(self, rhs: f32) -> f32 {
1593+
const fn const_algebraic_rem(lhs: f32, rhs: f32) -> f32 {
1594+
lhs % rhs
1595+
}
1596+
1597+
fn rt_algebraic_rem(lhs: f32, rhs: f32) -> f32 {
1598+
intrinsics::frem_algebraic(lhs, rhs)
1599+
}
1600+
1601+
const_eval_select((self, rhs), const_algebraic_rem, rt_algebraic_rem)
15561602
}
15571603
}

0 commit comments

Comments
 (0)