Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 6cb3d34

Browse files
committedJun 25, 2024
Add doctests to existing f16 and f128 functions
The symbols that these tests rely on are not available on all platforms and some ABIs are buggy, tests that rely on external functions are configured to only run on x86 (`f128`) or aarch64 (`f16`).
1 parent b0e0503 commit 6cb3d34

File tree

2 files changed

+68
-4
lines changed

2 files changed

+68
-4
lines changed
 

‎core/src/num/f128.rs

+34-2
Original file line numberDiff line numberDiff line change
@@ -221,8 +221,22 @@ impl f128 {
221221
pub const MAX_10_EXP: i32 = 4_932;
222222

223223
/// Returns `true` if this value is NaN.
224+
///
225+
/// ```
226+
/// #![feature(f128)]
227+
/// # // FIXME(f16_f128): remove when `unordtf2` is available
228+
/// # #[cfg(target_arch = "x86_64", target_os = "linux")] {
229+
///
230+
/// let nan = f128::NAN;
231+
/// let f = 7.0_f128;
232+
///
233+
/// assert!(nan.is_nan());
234+
/// assert!(!f.is_nan());
235+
/// # }
236+
/// ```
224237
#[inline]
225238
#[must_use]
239+
#[cfg(not(bootstrap))]
226240
#[unstable(feature = "f128", issue = "116909")]
227241
#[allow(clippy::eq_op)] // > if you intended to check if the operand is NaN, use `.is_nan()` instead :)
228242
pub const fn is_nan(self) -> bool {
@@ -234,7 +248,7 @@ impl f128 {
234248
/// meaning to the sign bit in case of a NaN, and as Rust doesn't guarantee that
235249
/// the bit pattern of NaNs are conserved over arithmetic operations, the result of
236250
/// `is_sign_positive` on a NaN might produce an unexpected result in some cases.
237-
/// See [explanation of NaN as a special value](f32) for more info.
251+
/// See [explanation of NaN as a special value](f128) for more info.
238252
///
239253
/// ```
240254
/// #![feature(f128)]
@@ -257,7 +271,7 @@ impl f128 {
257271
/// meaning to the sign bit in case of a NaN, and as Rust doesn't guarantee that
258272
/// the bit pattern of NaNs are conserved over arithmetic operations, the result of
259273
/// `is_sign_negative` on a NaN might produce an unexpected result in some cases.
260-
/// See [explanation of NaN as a special value](f32) for more info.
274+
/// See [explanation of NaN as a special value](f128) for more info.
261275
///
262276
/// ```
263277
/// #![feature(f128)]
@@ -287,6 +301,14 @@ impl f128 {
287301
///
288302
/// Note that this function is distinct from `as` casting, which attempts to
289303
/// preserve the *numeric* value, and not the bitwise value.
304+
///
305+
/// ```
306+
/// #![feature(f128)]
307+
///
308+
/// # // FIXME(f16_f128): enable this once const casting works
309+
/// # // assert_ne!((1f128).to_bits(), 1f128 as u128); // to_bits() is not casting!
310+
/// assert_eq!((12.5f128).to_bits(), 0x40029000000000000000000000000000);
311+
/// ```
290312
#[inline]
291313
#[unstable(feature = "f128", issue = "116909")]
292314
#[must_use = "this returns the result of the operation, without modifying the original"]
@@ -326,6 +348,16 @@ impl f128 {
326348
///
327349
/// Note that this function is distinct from `as` casting, which attempts to
328350
/// preserve the *numeric* value, and not the bitwise value.
351+
///
352+
/// ```
353+
/// #![feature(f128)]
354+
/// # // FIXME(f16_f128): remove when `eqtf2` is available
355+
/// # #[cfg(all(target_arch = "x86_64", target_os = "linux"))] {
356+
///
357+
/// let v = f128::from_bits(0x40029000000000000000000000000000);
358+
/// assert_eq!(v, 12.5);
359+
/// # }
360+
/// ```
329361
#[inline]
330362
#[must_use]
331363
#[unstable(feature = "f128", issue = "116909")]

‎core/src/num/f16.rs

+34-2
Original file line numberDiff line numberDiff line change
@@ -216,8 +216,21 @@ impl f16 {
216216
pub const MAX_10_EXP: i32 = 4;
217217

218218
/// Returns `true` if this value is NaN.
219+
///
220+
/// ```
221+
/// #![feature(f16)]
222+
/// # #[cfg(target_arch = "x86_64")] { // FIXME(f16_f128): remove when ABI bugs are fixed
223+
///
224+
/// let nan = f16::NAN;
225+
/// let f = 7.0_f16;
226+
///
227+
/// assert!(nan.is_nan());
228+
/// assert!(!f.is_nan());
229+
/// # }
230+
/// ```
219231
#[inline]
220232
#[must_use]
233+
#[cfg(not(bootstrap))]
221234
#[unstable(feature = "f16", issue = "116909")]
222235
#[allow(clippy::eq_op)] // > if you intended to check if the operand is NaN, use `.is_nan()` instead :)
223236
pub const fn is_nan(self) -> bool {
@@ -229,7 +242,7 @@ impl f16 {
229242
/// meaning to the sign bit in case of a NaN, and as Rust doesn't guarantee that
230243
/// the bit pattern of NaNs are conserved over arithmetic operations, the result of
231244
/// `is_sign_positive` on a NaN might produce an unexpected result in some cases.
232-
/// See [explanation of NaN as a special value](f32) for more info.
245+
/// See [explanation of NaN as a special value](f16) for more info.
233246
///
234247
/// ```
235248
/// #![feature(f16)]
@@ -252,7 +265,7 @@ impl f16 {
252265
/// meaning to the sign bit in case of a NaN, and as Rust doesn't guarantee that
253266
/// the bit pattern of NaNs are conserved over arithmetic operations, the result of
254267
/// `is_sign_negative` on a NaN might produce an unexpected result in some cases.
255-
/// See [explanation of NaN as a special value](f32) for more info.
268+
/// See [explanation of NaN as a special value](f16) for more info.
256269
///
257270
/// ```
258271
/// #![feature(f16)]
@@ -282,6 +295,16 @@ impl f16 {
282295
///
283296
/// Note that this function is distinct from `as` casting, which attempts to
284297
/// preserve the *numeric* value, and not the bitwise value.
298+
///
299+
/// ```
300+
/// #![feature(f16)]
301+
/// # #[cfg(target_arch = "aarch64")] { // FIXME(f16_F128): rust-lang/rust#123885
302+
///
303+
/// # // FIXME(f16_f128): enable this once const casting works
304+
/// # // assert_ne!((1f16).to_bits(), 1f16 as u128); // to_bits() is not casting!
305+
/// assert_eq!((12.5f16).to_bits(), 0x4a40);
306+
/// # }
307+
/// ```
285308
#[inline]
286309
#[unstable(feature = "f16", issue = "116909")]
287310
#[must_use = "this returns the result of the operation, without modifying the original"]
@@ -321,6 +344,15 @@ impl f16 {
321344
///
322345
/// Note that this function is distinct from `as` casting, which attempts to
323346
/// preserve the *numeric* value, and not the bitwise value.
347+
///
348+
/// ```
349+
/// #![feature(f16)]
350+
/// # #[cfg(target_arch = "aarch64")] { // FIXME(f16_F128): rust-lang/rust#123885
351+
///
352+
/// let v = f16::from_bits(0x4a40);
353+
/// assert_eq!(v, 12.5);
354+
/// # }
355+
/// ```
324356
#[inline]
325357
#[must_use]
326358
#[unstable(feature = "f16", issue = "116909")]

0 commit comments

Comments
 (0)
Failed to load comments.