Skip to content

Commit 345c1e9

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

File tree

4 files changed

+248
-44
lines changed

4 files changed

+248
-44
lines changed

library/core/src/num/f128.rs

+62-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,98 @@ 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+
#[inline(always)]
1380+
fn rt_algebraic_add(lhs: f128, rhs: f128) -> f128 {
1381+
intrinsics::fadd_algebraic(lhs, rhs)
1382+
}
1383+
1384+
const_eval_select((self, rhs), const_algebraic_add, rt_algebraic_add)
13741385
}
13751386

13761387
/// Float subtraction that allows optimizations based on algebraic rules.
13771388
///
13781389
/// See [algebraic operators](primitive@f32#algebraic-operators) for more info.
13791390
#[must_use = "method returns a new number and does not mutate the original value"]
13801391
#[unstable(feature = "float_algebraic", issue = "136469")]
1392+
#[rustc_const_unstable(feature = "float_algebraic", issue = "136469")]
13811393
#[inline]
1382-
pub fn algebraic_sub(self, rhs: f128) -> f128 {
1383-
intrinsics::fsub_algebraic(self, rhs)
1394+
pub const fn algebraic_sub(self, rhs: f128) -> f128 {
1395+
const fn const_algebraic_sub(lhs: f128, rhs: f128) -> f128 {
1396+
lhs - rhs
1397+
}
1398+
1399+
#[inline(always)]
1400+
fn rt_algebraic_sub(lhs: f128, rhs: f128) -> f128 {
1401+
intrinsics::fsub_algebraic(lhs, rhs)
1402+
}
1403+
1404+
const_eval_select((self, rhs), const_algebraic_sub, rt_algebraic_sub)
13841405
}
13851406

13861407
/// Float multiplication that allows optimizations based on algebraic rules.
13871408
///
13881409
/// See [algebraic operators](primitive@f32#algebraic-operators) for more info.
13891410
#[must_use = "method returns a new number and does not mutate the original value"]
13901411
#[unstable(feature = "float_algebraic", issue = "136469")]
1412+
#[rustc_const_unstable(feature = "float_algebraic", issue = "136469")]
13911413
#[inline]
1392-
pub fn algebraic_mul(self, rhs: f128) -> f128 {
1393-
intrinsics::fmul_algebraic(self, rhs)
1414+
pub const fn algebraic_mul(self, rhs: f128) -> f128 {
1415+
const fn const_algebraic_mul(lhs: f128, rhs: f128) -> f128 {
1416+
lhs * rhs
1417+
}
1418+
1419+
#[inline(always)]
1420+
fn rt_algebraic_mul(lhs: f128, rhs: f128) -> f128 {
1421+
intrinsics::fmul_algebraic(lhs, rhs)
1422+
}
1423+
1424+
const_eval_select((self, rhs), const_algebraic_mul, rt_algebraic_mul)
13941425
}
13951426

13961427
/// Float division that allows optimizations based on algebraic rules.
13971428
///
13981429
/// See [algebraic operators](primitive@f32#algebraic-operators) for more info.
13991430
#[must_use = "method returns a new number and does not mutate the original value"]
14001431
#[unstable(feature = "float_algebraic", issue = "136469")]
1432+
#[rustc_const_unstable(feature = "float_algebraic", issue = "136469")]
14011433
#[inline]
1402-
pub fn algebraic_div(self, rhs: f128) -> f128 {
1403-
intrinsics::fdiv_algebraic(self, rhs)
1434+
pub const fn algebraic_div(self, rhs: f128) -> f128 {
1435+
const fn const_algebraic_div(lhs: f128, rhs: f128) -> f128 {
1436+
lhs / rhs
1437+
}
1438+
1439+
#[inline(always)]
1440+
fn rt_algebraic_div(lhs: f128, rhs: f128) -> f128 {
1441+
intrinsics::fdiv_algebraic(lhs, rhs)
1442+
}
1443+
1444+
const_eval_select((self, rhs), const_algebraic_div, rt_algebraic_div)
14041445
}
14051446

14061447
/// Float remainder that allows optimizations based on algebraic rules.
14071448
///
14081449
/// See [algebraic operators](primitive@f32#algebraic-operators) for more info.
14091450
#[must_use = "method returns a new number and does not mutate the original value"]
14101451
#[unstable(feature = "float_algebraic", issue = "136469")]
1452+
#[rustc_const_unstable(feature = "float_algebraic", issue = "136469")]
14111453
#[inline]
1412-
pub fn algebraic_rem(self, rhs: f128) -> f128 {
1413-
intrinsics::frem_algebraic(self, rhs)
1454+
pub const fn algebraic_rem(self, rhs: f128) -> f128 {
1455+
const fn const_algebraic_rem(lhs: f128, rhs: f128) -> f128 {
1456+
lhs % rhs
1457+
}
1458+
1459+
#[inline(always)]
1460+
fn rt_algebraic_rem(lhs: f128, rhs: f128) -> f128 {
1461+
intrinsics::frem_algebraic(lhs, rhs)
1462+
}
1463+
1464+
const_eval_select((self, rhs), const_algebraic_rem, rt_algebraic_rem)
14141465
}
14151466
}

library/core/src/num/f16.rs

+62-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,98 @@ 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+
#[inline(always)]
1356+
fn rt_algebraic_add(lhs: f16, rhs: f16) -> f16 {
1357+
intrinsics::fadd_algebraic(lhs, rhs)
1358+
}
1359+
1360+
const_eval_select((self, rhs), const_algebraic_add, rt_algebraic_add)
13501361
}
13511362

13521363
/// Float subtraction that allows optimizations based on algebraic rules.
13531364
///
13541365
/// See [algebraic operators](primitive@f32#algebraic-operators) for more info.
13551366
#[must_use = "method returns a new number and does not mutate the original value"]
13561367
#[unstable(feature = "float_algebraic", issue = "136469")]
1368+
#[rustc_const_unstable(feature = "float_algebraic", issue = "136469")]
13571369
#[inline]
1358-
pub fn algebraic_sub(self, rhs: f16) -> f16 {
1359-
intrinsics::fsub_algebraic(self, rhs)
1370+
pub const fn algebraic_sub(self, rhs: f16) -> f16 {
1371+
const fn const_algebraic_sub(lhs: f16, rhs: f16) -> f16 {
1372+
lhs - rhs
1373+
}
1374+
1375+
#[inline(always)]
1376+
fn rt_algebraic_sub(lhs: f16, rhs: f16) -> f16 {
1377+
intrinsics::fsub_algebraic(lhs, rhs)
1378+
}
1379+
1380+
const_eval_select((self, rhs), const_algebraic_sub, rt_algebraic_sub)
13601381
}
13611382

13621383
/// Float multiplication that allows optimizations based on algebraic rules.
13631384
///
13641385
/// See [algebraic operators](primitive@f32#algebraic-operators) for more info.
13651386
#[must_use = "method returns a new number and does not mutate the original value"]
13661387
#[unstable(feature = "float_algebraic", issue = "136469")]
1388+
#[rustc_const_unstable(feature = "float_algebraic", issue = "136469")]
13671389
#[inline]
1368-
pub fn algebraic_mul(self, rhs: f16) -> f16 {
1369-
intrinsics::fmul_algebraic(self, rhs)
1390+
pub const fn algebraic_mul(self, rhs: f16) -> f16 {
1391+
const fn const_algebraic_mul(lhs: f16, rhs: f16) -> f16 {
1392+
lhs * rhs
1393+
}
1394+
1395+
#[inline(always)]
1396+
fn rt_algebraic_mul(lhs: f16, rhs: f16) -> f16 {
1397+
intrinsics::fmul_algebraic(lhs, rhs)
1398+
}
1399+
1400+
const_eval_select((self, rhs), const_algebraic_mul, rt_algebraic_mul)
13701401
}
13711402

13721403
/// Float division that allows optimizations based on algebraic rules.
13731404
///
13741405
/// See [algebraic operators](primitive@f32#algebraic-operators) for more info.
13751406
#[must_use = "method returns a new number and does not mutate the original value"]
13761407
#[unstable(feature = "float_algebraic", issue = "136469")]
1408+
#[rustc_const_unstable(feature = "float_algebraic", issue = "136469")]
13771409
#[inline]
1378-
pub fn algebraic_div(self, rhs: f16) -> f16 {
1379-
intrinsics::fdiv_algebraic(self, rhs)
1410+
pub const fn algebraic_div(self, rhs: f16) -> f16 {
1411+
const fn const_algebraic_div(lhs: f16, rhs: f16) -> f16 {
1412+
lhs / rhs
1413+
}
1414+
1415+
#[inline(always)]
1416+
fn rt_algebraic_div(lhs: f16, rhs: f16) -> f16 {
1417+
intrinsics::fdiv_algebraic(lhs, rhs)
1418+
}
1419+
1420+
const_eval_select((self, rhs), const_algebraic_div, rt_algebraic_div)
13801421
}
13811422

13821423
/// Float remainder that allows optimizations based on algebraic rules.
13831424
///
13841425
/// See [algebraic operators](primitive@f32#algebraic-operators) for more info.
13851426
#[must_use = "method returns a new number and does not mutate the original value"]
13861427
#[unstable(feature = "float_algebraic", issue = "136469")]
1428+
#[rustc_const_unstable(feature = "float_algebraic", issue = "136469")]
13871429
#[inline]
1388-
pub fn algebraic_rem(self, rhs: f16) -> f16 {
1389-
intrinsics::frem_algebraic(self, rhs)
1430+
pub const fn algebraic_rem(self, rhs: f16) -> f16 {
1431+
const fn const_algebraic_rem(lhs: f16, rhs: f16) -> f16 {
1432+
lhs % rhs
1433+
}
1434+
1435+
#[inline(always)]
1436+
fn rt_algebraic_rem(lhs: f16, rhs: f16) -> f16 {
1437+
intrinsics::frem_algebraic(lhs, rhs)
1438+
}
1439+
1440+
const_eval_select((self, rhs), const_algebraic_rem, rt_algebraic_rem)
13901441
}
13911442
}

library/core/src/num/f32.rs

+62-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,98 @@ 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+
#[inline(always)]
1522+
fn rt_algebraic_add(lhs: f32, rhs: f32) -> f32 {
1523+
intrinsics::fadd_algebraic(lhs, rhs)
1524+
}
1525+
1526+
const_eval_select((self, rhs), const_algebraic_add, rt_algebraic_add)
15161527
}
15171528

15181529
/// Float subtraction that allows optimizations based on algebraic rules.
15191530
///
15201531
/// See [algebraic operators](primitive@f32#algebraic-operators) for more info.
15211532
#[must_use = "method returns a new number and does not mutate the original value"]
15221533
#[unstable(feature = "float_algebraic", issue = "136469")]
1534+
#[rustc_const_unstable(feature = "float_algebraic", issue = "136469")]
15231535
#[inline]
1524-
pub fn algebraic_sub(self, rhs: f32) -> f32 {
1525-
intrinsics::fsub_algebraic(self, rhs)
1536+
pub const fn algebraic_sub(self, rhs: f32) -> f32 {
1537+
const fn const_algebraic_sub(lhs: f32, rhs: f32) -> f32 {
1538+
lhs - rhs
1539+
}
1540+
1541+
#[inline(always)]
1542+
fn rt_algebraic_sub(lhs: f32, rhs: f32) -> f32 {
1543+
intrinsics::fsub_algebraic(lhs, rhs)
1544+
}
1545+
1546+
const_eval_select((self, rhs), const_algebraic_sub, rt_algebraic_sub)
15261547
}
15271548

15281549
/// Float multiplication that allows optimizations based on algebraic rules.
15291550
///
15301551
/// See [algebraic operators](primitive@f32#algebraic-operators) for more info.
15311552
#[must_use = "method returns a new number and does not mutate the original value"]
15321553
#[unstable(feature = "float_algebraic", issue = "136469")]
1554+
#[rustc_const_unstable(feature = "float_algebraic", issue = "136469")]
15331555
#[inline]
1534-
pub fn algebraic_mul(self, rhs: f32) -> f32 {
1535-
intrinsics::fmul_algebraic(self, rhs)
1556+
pub const fn algebraic_mul(self, rhs: f32) -> f32 {
1557+
const fn const_algebraic_mul(lhs: f32, rhs: f32) -> f32 {
1558+
lhs * rhs
1559+
}
1560+
1561+
#[inline(always)]
1562+
fn rt_algebraic_mul(lhs: f32, rhs: f32) -> f32 {
1563+
intrinsics::fmul_algebraic(lhs, rhs)
1564+
}
1565+
1566+
const_eval_select((self, rhs), const_algebraic_mul, rt_algebraic_mul)
15361567
}
15371568

15381569
/// Float division that allows optimizations based on algebraic rules.
15391570
///
15401571
/// See [algebraic operators](primitive@f32#algebraic-operators) for more info.
15411572
#[must_use = "method returns a new number and does not mutate the original value"]
15421573
#[unstable(feature = "float_algebraic", issue = "136469")]
1574+
#[rustc_const_unstable(feature = "float_algebraic", issue = "136469")]
15431575
#[inline]
1544-
pub fn algebraic_div(self, rhs: f32) -> f32 {
1545-
intrinsics::fdiv_algebraic(self, rhs)
1576+
pub const fn algebraic_div(self, rhs: f32) -> f32 {
1577+
const fn const_algebraic_div(lhs: f32, rhs: f32) -> f32 {
1578+
lhs / rhs
1579+
}
1580+
1581+
#[inline(always)]
1582+
fn rt_algebraic_div(lhs: f32, rhs: f32) -> f32 {
1583+
intrinsics::fdiv_algebraic(lhs, rhs)
1584+
}
1585+
1586+
const_eval_select((self, rhs), const_algebraic_div, rt_algebraic_div)
15461587
}
15471588

15481589
/// Float remainder that allows optimizations based on algebraic rules.
15491590
///
15501591
/// See [algebraic operators](primitive@f32#algebraic-operators) for more info.
15511592
#[must_use = "method returns a new number and does not mutate the original value"]
15521593
#[unstable(feature = "float_algebraic", issue = "136469")]
1594+
#[rustc_const_unstable(feature = "float_algebraic", issue = "136469")]
15531595
#[inline]
1554-
pub fn algebraic_rem(self, rhs: f32) -> f32 {
1555-
intrinsics::frem_algebraic(self, rhs)
1596+
pub const fn algebraic_rem(self, rhs: f32) -> f32 {
1597+
const fn const_algebraic_rem(lhs: f32, rhs: f32) -> f32 {
1598+
lhs % rhs
1599+
}
1600+
1601+
#[inline(always)]
1602+
fn rt_algebraic_rem(lhs: f32, rhs: f32) -> f32 {
1603+
intrinsics::frem_algebraic(lhs, rhs)
1604+
}
1605+
1606+
const_eval_select((self, rhs), const_algebraic_rem, rt_algebraic_rem)
15561607
}
15571608
}

0 commit comments

Comments
 (0)