|
12 | 12 | #![stable(feature = "rust1", since = "1.0.0")]
|
13 | 13 |
|
14 | 14 | use crate::convert::FloatToInt;
|
| 15 | +use crate::intrinsics::{self, const_eval_select}; |
15 | 16 | use crate::num::FpCategory;
|
16 | 17 | use crate::panic::const_assert;
|
17 |
| -use crate::{cfg_match, intrinsics, mem}; |
| 18 | +use crate::{cfg_match, mem}; |
18 | 19 |
|
19 | 20 | /// The radix or base of the internal representation of `f32`.
|
20 | 21 | /// Use [`f32::RADIX`] instead.
|
@@ -1510,48 +1511,98 @@ impl f32 {
|
1510 | 1511 | /// See [algebraic operators](primitive@f32#algebraic-operators) for more info.
|
1511 | 1512 | #[must_use = "method returns a new number and does not mutate the original value"]
|
1512 | 1513 | #[unstable(feature = "float_algebraic", issue = "136469")]
|
| 1514 | + #[rustc_const_unstable(feature = "float_algebraic", issue = "136469")] |
1513 | 1515 | #[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) |
1516 | 1527 | }
|
1517 | 1528 |
|
1518 | 1529 | /// Float subtraction that allows optimizations based on algebraic rules.
|
1519 | 1530 | ///
|
1520 | 1531 | /// See [algebraic operators](primitive@f32#algebraic-operators) for more info.
|
1521 | 1532 | #[must_use = "method returns a new number and does not mutate the original value"]
|
1522 | 1533 | #[unstable(feature = "float_algebraic", issue = "136469")]
|
| 1534 | + #[rustc_const_unstable(feature = "float_algebraic", issue = "136469")] |
1523 | 1535 | #[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) |
1526 | 1547 | }
|
1527 | 1548 |
|
1528 | 1549 | /// Float multiplication that allows optimizations based on algebraic rules.
|
1529 | 1550 | ///
|
1530 | 1551 | /// See [algebraic operators](primitive@f32#algebraic-operators) for more info.
|
1531 | 1552 | #[must_use = "method returns a new number and does not mutate the original value"]
|
1532 | 1553 | #[unstable(feature = "float_algebraic", issue = "136469")]
|
| 1554 | + #[rustc_const_unstable(feature = "float_algebraic", issue = "136469")] |
1533 | 1555 | #[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) |
1536 | 1567 | }
|
1537 | 1568 |
|
1538 | 1569 | /// Float division that allows optimizations based on algebraic rules.
|
1539 | 1570 | ///
|
1540 | 1571 | /// See [algebraic operators](primitive@f32#algebraic-operators) for more info.
|
1541 | 1572 | #[must_use = "method returns a new number and does not mutate the original value"]
|
1542 | 1573 | #[unstable(feature = "float_algebraic", issue = "136469")]
|
| 1574 | + #[rustc_const_unstable(feature = "float_algebraic", issue = "136469")] |
1543 | 1575 | #[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) |
1546 | 1587 | }
|
1547 | 1588 |
|
1548 | 1589 | /// Float remainder that allows optimizations based on algebraic rules.
|
1549 | 1590 | ///
|
1550 | 1591 | /// See [algebraic operators](primitive@f32#algebraic-operators) for more info.
|
1551 | 1592 | #[must_use = "method returns a new number and does not mutate the original value"]
|
1552 | 1593 | #[unstable(feature = "float_algebraic", issue = "136469")]
|
| 1594 | + #[rustc_const_unstable(feature = "float_algebraic", issue = "136469")] |
1553 | 1595 | #[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) |
1556 | 1607 | }
|
1557 | 1608 | }
|
0 commit comments