Menu

- Learn
- Documentation
  - Language Reference
  - Library Reference
  - Command-line Reference
  - Feature Overview
  - Articles
- Downloads
- Packages
- Community
  - Blog
  - Orgs using D
  - Twitter
  - Calendar
  - Forums
  - IRC
  - Community Discord
  - Wiki
  - GitHub
  - Issues
  - Get involved
  - Contributors
  - Foundation
  - Security Team
  - Donate
  - Sponsors
- Resources
  - Tour
  - Books
  - Tutorials
  - Tools
  - Editors
  - IDEs
  - run.dlang.io
  - Visual D
  - Acknowledgments
  - D Style
  - Glossary
  - Sitemap

Search
Report a bug
If you spot a problem with this page, click here to create a Bugzilla issue.
Improve this page
Quickly fork, edit online, and submit a pull request for this page. Requires a signed-in GitHub account. This works well for small changes. If you'd like to make larger changes you may want to consider using a local clone.

# std.math.hardware

This is a submodule of std.math.
It contains hardware support for floating point numbers.
License:
Boost License 1.0.
Authors:
Walter Bright, Don Clugston, Conversion of CEPHES math library to D by Iain Buclaw and David Nadlinger

Source std/math/hardware.d

struct `IeeeFlags`;
  IEEE exception status flags ('sticky bits')
  These flags indicate that an exceptional floating-point condition has occurred. They indicate that a NaN or an infinity has been generated, that a result is inexact, or that a signalling NaN has been encountered. If floating-point exceptions are enabled (unmasked), a hardware exception will be generated instead of setting these flags.
  Examples:

      import std.math.traits : isNaN;

      static void func() {
          int a = 10 * 10;
      }
      pragma(inline, false) static void blockopt(ref real x) {}
      real a = 3.5;
      // Set all the flags to zero
      resetIeeeFlags();
      assert(!ieeeFlags.divByZero);
      blockopt(a); // avoid constant propagation by the optimizer
      // Perform a division by zero.
      a /= 0.0L;
      writeln(a); // real.infinity
      assert(ieeeFlags.divByZero);
      blockopt(a); // avoid constant propagation by the optimizer
      // Create a NaN
      a *= 0.0L;
      assert(ieeeFlags.invalid);
      assert(isNaN(a));

      // Check that calling func() has no effect on the
      // status flags.
      IeeeFlags f = ieeeFlags;
      func();
      writeln(ieeeFlags); // f

  const nothrow @nogc @property @safe bool `inexact`();
    The result cannot be represented exactly, so rounding occurred.

    Example x = sin(0.1);

  const nothrow @nogc @property @safe bool `underflow`();
    A zero was generated by underflow

    Example x = real.min*real.epsilon/2;

  const nothrow @nogc @property @safe bool `overflow`();
    An infinity was generated by overflow

    Example x = real.max*2;

  const nothrow @nogc @property @safe bool `divByZero`();
    An infinity was generated by division by zero

    Example x = 3/0.0;

  const nothrow @nogc @property @safe bool `invalid`();
    A machine NaN was generated.

    Example x = real.infinity * 0.0;

nothrow @nogc @trusted void `resetIeeeFlags`();
  Set all of the floating-point status flags to false.
  Examples:

      pragma(inline, false) static void blockopt(ref real x) {}
      resetIeeeFlags();
      real a = 3.5;
      blockopt(a); // avoid constant propagation by the optimizer
      a /= 0.0L;
      blockopt(a); // avoid constant propagation by the optimizer
      writeln(a); // real.infinity
      assert(ieeeFlags.divByZero);

      resetIeeeFlags();
      assert(!ieeeFlags.divByZero);

pure nothrow @nogc @property @trusted IeeeFlags `ieeeFlags`();
  Returns:
  snapshot of the current state of the floating-point status flags
  Examples:

      import std.math.traits : isNaN;

      pragma(inline, false) static void blockopt(ref real x) {}
      resetIeeeFlags();
      real a = 3.5;
      blockopt(a); // avoid constant propagation by the optimizer

      a /= 0.0L;
      writeln(a); // real.infinity
      assert(ieeeFlags.divByZero);
      blockopt(a); // avoid constant propagation by the optimizer

      a *= 0.0L;
      assert(isNaN(a));
      assert(ieeeFlags.invalid);

struct `FloatingPointControl`;
  Control the Floating point hardware
  Change the IEEE754 floating-point rounding mode and the floating-point hardware exceptions.
  By default, the rounding mode is roundToNearest and all hardware exceptions are disabled. For most applications, debugging is easier if the division by zero, overflow, and invalid operation exceptions are enabled. These three are combined into a severeExceptions value for convenience. Note in particular that if invalidException is enabled, a hardware trap will be generated whenever an uninitialized floating-point variable is used.
  All changes are temporary. The previous state is restored at the end of the scope.

  Example

      {
          FloatingPointControl fpctrl;

          // Enable hardware exceptions for division by zero, overflow to infinity,
          // invalid operations, and uninitialized floating-point variables.
          fpctrl.enableExceptions(FloatingPointControl.severeExceptions);

          // This will generate a hardware exception, if x is a
          // default-initialized floating point variable:
          real x; // Add `= 0` or even `= real.nan` to not throw the exception.
          real y = x * 3.0;

          // The exception is only thrown for default-uninitialized NaN-s.
          // NaN-s with other payload are valid:
          real z = y * real.nan; // ok

          // The set hardware exceptions and rounding modes will be disabled when
          // leaving this scope.
      }

  Examples:

      import std.math.rounding : lrint;

      FloatingPointControl fpctrl;

      fpctrl.rounding = FloatingPointControl.roundDown;
      writeln(lrint(1.5)); // 1.0

      fpctrl.rounding = FloatingPointControl.roundUp;
      writeln(lrint(1.4)); // 2.0

      fpctrl.rounding = FloatingPointControl.roundToNearest;
      writeln(lrint(1.5)); // 2.0

  alias `RoundingMode` = uint;
  `roundToNearest`
  `roundDown`
  `roundUp`
  `roundToZero`
  `roundingMask`
    IEEE rounding modes. The default mode is roundToNearest.
    roundingMask = A mask of all rounding modes.
  nothrow @nogc @property @trusted void `rounding`(RoundingMode `newMode`);
    Change the floating-point hardware rounding mode
    Changing the rounding mode in the middle of a function can interfere with optimizations of floating point expressions, as the optimizer assumes that the rounding mode does not change. It is best to change the rounding mode only at the beginning of the function, and keep it until the function returns. It is also best to add the line:

        pragma(inline, false);

    as the first line of the function so it will not get inlined.
    Parameters:

    RoundingMode `newMode` | the new rounding mode

  static pure nothrow @nogc @property @trusted RoundingMode `rounding`();
    Returns:
    the currently active rounding mode
  alias `ExceptionMask` = uint;
  `subnormalException`
  `inexactException`
  `underflowException`
  `overflowException`
  `divByZeroException`
  `invalidException`
  `severeExceptions`
  `allExceptions`
    IEEE hardware exceptions. By default, all exceptions are masked (disabled).
    severeExceptions = The overflow, division by zero, and invalid exceptions.
  static pure nothrow @nogc @property @safe bool `hasExceptionTraps`();
    Returns:
    true if the current FPU supports exception trapping
  nothrow @nogc @trusted void `enableExceptions`(ExceptionMask `exceptions`);
    Enable (unmask) specific hardware exceptions. Multiple exceptions may be ORed together.
  nothrow @nogc @trusted void `disableExceptions`(ExceptionMask `exceptions`);
    Disable (mask) specific hardware exceptions. Multiple exceptions may be ORed together.
  static pure nothrow @nogc @property @trusted ExceptionMask `enabledExceptions`();
    Returns:
    the exceptions which are currently enabled (unmasked)

Copyright © 1999-2023 by the D Language Foundation | Page generated by Ddoc on Sun Jan 1 02:21:02 2023