<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
<head>
  <meta name="generator" content=
  "HTML Tidy for OS/2 (vers 1 September 2005), see www.w3.org">
<style type="text/css">
      del { font-weight:bold;background-color:#ffa0a0 }
      ins { font-weight:bold;background-color:#a0ffa0 }
      blockquote {
      display: block;
      font-family: "Times New Roman";
      background-color:lightgray;
      margin-top: 1em;
      margin-bottom: 1em;
      margin-left: 40px;
      margin-right: 40px
      }
</style>

  <title>N2xxx: intmax_t and math functions</title>
</head>

<body>
  <p><br>
  <!-- Who are the authors... -->
   <b>Submitter:</b>Fred J. Tydeman<br>
  <!-- What is the date of submission. yyyy-mm-dd -->
   <b>Submission Date:</b> 2020-mm-dd<br>
  <b>Document:</b> WG14 N2xxx<br>
  <b>Title:</b> N2xxx: intmax_t and math functions<br>
  <b>Reference Documents:</b> N2478, N2525</p>

  <p>Summary</p>Due to issues raised by the WG14 committee with
  [u]intmax_t, several of the CFP related functions ([u]fromfp[x],
  compoundn, pown, rootn) should be changed to not use [u]intmax_t.

  <p>The WG14 consideration of removing [u]intmax_t, sparked the
  CFP discussion. We concluded that changing the interfaces to not
  use [u]intmax_t would be better anyway (even if [u]intmax_t were
  not removed). Introducing type [u]intmax_t into an expression via
  a function return type could have unpredictable negative
  performance implications.</p>

  <p>In general, replace [u]intmax_t with the floating type used as
  the parameter.</p>

  <p>Change</p>

  <blockquote>
    7.12.9.10 The fromfp and ufromfp functions
  </blockquote>

  <blockquote>
    Synopsis
  </blockquote>

  <blockquote>
    <pre>
<code>
1 #include <stdint.h>
  #include <math.h>
  intmax_t fromfp(double x, int round, unsigned int width);
  intmax_t fromfpf(float x, int round, unsigned int width);
  intmax_t fromfpl(long double x, int round, unsigned int width);
  uintmax_t ufromfp(double x, int round, unsigned int width);
  uintmax_t ufromfpf(float x, int round, unsigned int width);
  uintmax_t ufromfpl(long double x, int round, unsigned int width);
  #ifdef __STDC_IEC_60559_DFP__
  intmax_t fromfpd32(_Decimal32 x, int round, unsigned int width);
  intmax_t fromfpd64(_Decimal64 x, int round, unsigned int width);
  intmax_t fromfpd128(_Decimal128 x, int round, unsigned int width);
  uintmax_t ufromfpd32(_Decimal32 x, int round, unsigned int width);
  uintmax_t ufromfpd64(_Decimal64 x, int round, unsigned int width);
  uintmax_t ufromfpd128(_Decimal128 x, int round, unsigned int width);
  #endif
</code>
    
</pre>
  </blockquote>

  <blockquote>
    Description
  </blockquote>

  <blockquote>
    <p>2 The <b>fromfp</b> and <b>ufromfp</b> functions round x,
    using the math rounding direction indicated by <b>round</b>, to
    a signed or unsigned integer, respectively, of <b>width</b>
    bits, and return the result value in the integer type
    designated by <b>intmax_t</b> or <b>uintmax_t</b>,
    respectively. If the value of the <b>round</b> argument is not
    equal to the value of a math rounding direction macro, the
    direction of rounding is unspecified. If the value of
    <b>width</b> exceeds the width of the function type, the
    rounding is to the full width of the function type. The
    <b>fromfp</b> and <b>ufromfp</b> functions do not raise the
    "inexact" floating-point exception. If x is infinite or NaN or
    rounds to an integral value that is outside the range of any
    supported integer type of the specified width, or if width is
    zero, the functions return an unspecified value and a domain
    error occurs.</p>
  </blockquote>

  <blockquote>
    3 Returns
  </blockquote>

  <blockquote>
    <p>The <b>fromfp</b> and <b>ufromfp</b> functions return the
    rounded integer value.</p>
  </blockquote>

  <blockquote>
    <p>4 EXAMPLE Upward rounding of double x to type int, without
    raising the "inexact" floating-point exception, is achieved
    by</p>
    <pre>
<code>
(int)fromfp(x, FP_INT_UPWARD, INT_WIDTH)
</code>
    
</pre>
  </blockquote>

  <p>to</p>

  <blockquote>
    7.12.9.10 The fromfp and ufromfp functions
  </blockquote>

  <blockquote>
    Synopsis
  </blockquote>

  <blockquote>
    <pre>
<code>
1 #include <stdint.h>
  #include <math.h>
  <del>intmax_t</del> <ins>double</ins> fromfp(double x, int round, unsigned int width);
  <del>intmax_t</del> <ins>float</ins> fromfpf(float x, int round, unsigned int width);
  <del>intmax_t</del> <ins>long double</ins> fromfpl(long double x, int round, unsigned int width);
  <del>uintmax_t</del> <ins>double</ins> ufromfp(double x, int round, unsigned int width);
  <del>uintmax_t</del> <ins>float</ins> ufromfpf(float x, int round, unsigned int width);
  <del>uintmax_t</del> <ins>long double</ins> ufromfpl(long double x, int round, unsigned int width);
  #ifdef __STDC_IEC_60559_DFP__
  <del>intmax_t</del> <ins>_Decimal32</ins> fromfpd32(_Decimal32 x, int round, unsigned int width);
  <del>intmax_t</del> <ins>_Decimal64</ins> fromfpd64(_Decimal64 x, int round, unsigned int width);
  <del>intmax_t</del> <ins>_Decimal128</ins> fromfpd128(_Decimal128 x, int round, unsigned int width);
  <del>uintmax_t</del> <ins>_Decimal32</ins> ufromfpd32(_Decimal32 x, int round, unsigned int width);
  <del>uintmax_t</del> <ins>_Decimal64</ins> ufromfpd64(_Decimal64 x, int round, unsigned int width);
  <del>uintmax_t</del> <ins>_Decimal128</ins> ufromfpd128(_Decimal128 x, int round, unsigned int width);
  #endif
</code>
    
</pre>
  </blockquote>

  <blockquote>
    Description
  </blockquote>

  <blockquote>
    <p>2 The <b>fromfp</b> and <b>ufromfp</b> functions round x,
    using the math rounding direction indicated by <b>round</b>, to
    a signed or unsigned integer, respectively. <del>, of
    <b>width</b> bits, and return the result value in the integer
    type designated by <b>intmax_t</b> or <b>uintmax_t</b>,
    respectively.</del> <ins>If <b>width</b> is nonzero and the
    resulting integer is within the range</ins></p>
    <pre>
<ins>   [-2**(width-1), 2**(width-1) - 1], for signed
   [0, 2**width - 1], for unsigned
</ins>
</pre>

    <p><ins>the functions return the integer value (represented in
    floating type). Otherwise, if <b>width</b> is zero or <b>x</b>
    does not round to an integer within the range, the functions
    return a NaN (if available), else an infinity (if available),
    else a HUGE_VAL, of the type of the <b>x</b> argument, and a
    domain error occurs.</ins> If the value of the <b>round</b>
    argument is not equal to the value of a math rounding direction
    macro <ins>(7.12)</ins>, the direction of rounding is
    unspecified. If the value of <b>width</b> exceeds the width of
    the function type, the rounding is to the full width of the
    function type. The <b>fromfp</b> and <b>ufromfp</b> functions
    do not raise the "inexact" floating-point exception. <del>If x
    is infinite or NaN or rounds to an integral value that is
    outside the range of any supported integer type of the
    specified width, or if width is zero, the functions return an
    unspecified value and a domain error occurs.</del></p>
  </blockquote>

  <blockquote>
    3 Returns
  </blockquote>

  <blockquote>
    <p>The <b>fromfp</b> and <b>ufromfp</b> functions return the
    rounded integer value.</p>
  </blockquote>

  <blockquote>
    <p>4 EXAMPLE Upward rounding of double x to type int, without
    raising the "inexact" floating-point exception<ins>; unsigned
    wrapping is not performed in the second call</ins>, is achieved
    by</p>
    <pre>
<code>
(int)fromfp(x, FP_INT_UPWARD, INT_WIDTH)
<ins>ufromfp(-3.0, FP_INT_UPWARD, UINT_WIDTH) /* domain error */</ins>
</code>
    
</pre>
  </blockquote>

  <p>Also, in F.10.6.10:</p>

  <p>Change:</p>

  <blockquote>
    <p>1 The fromfp and ufromfp functions raise the "invalid"
    floating-point exception and return an unspecified value if the
    floating-point argument x is infinite or NaN or rounds to an
    integral value that is outside the range of any supported
    integer type of the specified width.</p>
  </blockquote>

  <p>to:</p>

  <blockquote>
    <p>1 The fromfp and ufromfp functions raise the "invalid"
    floating-point exception and return <del>an unspecified
    value</del> <ins>a NaN</ins> if the <ins>argument <b>width</b>
    is zero or if the</ins> floating-point argument x is infinite
    or NaN or rounds to an integer value that is outside the range
    <del>of any supported integer type of the specified width</del>
    <ins>determined by the argument width (see 7.12.9.2)</ins>.</p>
  </blockquote>
</body>
</html>