⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 slamch.c

📁 InsightToolkit-1.4.0(有大量的优化算法程序)
💻 C
📖 第 1 页 / 共 2 页
字号:
#include "f2c.h"
#include "netlib.h"
#include <stdio.h>

static void slamc1_(integer *beta, integer *t, logical *rnd, logical *ieee1);
static void slamc2_(integer *beta, integer *t, logical *rnd, real *eps,
                    integer *emin, real *rmin, integer *emax, real *rmax);
static real slamc3_(real *a, real *b);
static void slamc4_(integer *emin, real *start, integer *base);
static void slamc5_(integer *beta, integer *p, integer *emin, logical *ieee, integer *emax, real *rmax);

real slamch_(const char *cmach)
{
/*  -- LAPACK auxiliary routine (version 2.0) --
       Univ. of Tennessee, Univ. of California Berkeley, NAG Ltd.,
       Courant Institute, Argonne National Lab, and Rice University
       October 31, 1992

    Purpose
    =======

    SLAMCH determines single precision machine parameters.

    Arguments
    =========

    CMACH   (input) CHARACTER*1
            Specifies the value to be returned by SLAMCH:
            = 'E' or 'e',   SLAMCH := eps
            = 'S' or 's ,   SLAMCH := sfmin
            = 'B' or 'b',   SLAMCH := base
            = 'P' or 'p',   SLAMCH := eps*base
            = 'N' or 'n',   SLAMCH := t
            = 'R' or 'r',   SLAMCH := rnd
            = 'M' or 'm',   SLAMCH := emin
            = 'U' or 'u',   SLAMCH := rmin
            = 'L' or 'l',   SLAMCH := emax
            = 'O' or 'o',   SLAMCH := rmax

            where

            eps   = relative machine precision
            sfmin = safe minimum, such that 1/sfmin does not overflow
            base  = base of the machine
            prec  = eps*base
            t     = number of (base) digits in the mantissa
            rnd   = 1.0 when rounding occurs in addition, 0.0 otherwise
            emin  = minimum exponent before (gradual) underflow
            rmin  = underflow threshold - base**(emin-1)
            emax  = largest exponent before overflow
            rmax  = overflow threshold  - (base**emax)*(1-eps)

   =====================================================================
*/

    /* Initialized data */
    static logical first = TRUE_;
    /* System generated locals */
    integer i__1;
    /* Local variables */
    static real base;
    static integer beta;
    static real emin, prec, emax;
    static integer imin, imax;
    static logical lrnd;
    static real rmin, rmax, t, rmach;
    static real small, sfmin;
    static integer it;
    static real rnd, eps;

    if (first) {
        first = FALSE_;
        slamc2_(&beta, &it, &lrnd, &eps, &imin, &rmin, &imax, &rmax);
        base = (real) beta;
        t = (real) it;
        if (lrnd) {
            rnd = 1.f;
            i__1 = 1 - it;
            eps = pow_ri(&base, &i__1) / 2;
        } else {
            rnd = 0.f;
            i__1 = 1 - it;
            eps = pow_ri(&base, &i__1);
        }
        prec = eps * base;
        emin = (real) imin;
        emax = (real) imax;
        sfmin = rmin;
        small = 1.f / rmax;
        if (small >= sfmin) {

/*           Use SMALL plus a bit, to avoid the possibility of rounding
             causing overflow when computing  1/sfmin.
*/

            sfmin = small * (eps + 1.f);
        }
    }

    if (lsame_(cmach, "E")) {
        rmach = eps;
    } else if (lsame_(cmach, "S")) {
        rmach = sfmin;
    } else if (lsame_(cmach, "B")) {
        rmach = base;
    } else if (lsame_(cmach, "P")) {
        rmach = prec;
    } else if (lsame_(cmach, "N")) {
        rmach = t;
    } else if (lsame_(cmach, "R")) {
        rmach = rnd;
    } else if (lsame_(cmach, "M")) {
        rmach = emin;
    } else if (lsame_(cmach, "U")) {
        rmach = rmin;
    } else if (lsame_(cmach, "L")) {
        rmach = emax;
    } else if (lsame_(cmach, "O")) {
        rmach = rmax;
    }

    return rmach;

} /* slamch_ */

/* Subroutine */ void slamc1_(integer *beta, integer *t, logical *rnd, logical *ieee1)
{
/*  -- LAPACK auxiliary routine (version 2.0) --
       Univ. of Tennessee, Univ. of California Berkeley, NAG Ltd.,
       Courant Institute, Argonne National Lab, and Rice University
       October 31, 1992

    Purpose
    =======

    SLAMC1 determines the machine parameters given by BETA, T, RND, and
    IEEE1.

    Arguments
    =========

    BETA    (output) INTEGER
            The base of the machine.

    T       (output) INTEGER
            The number of ( BETA ) digits in the mantissa.

    RND     (output) LOGICAL
            Specifies whether proper rounding  ( RND = .TRUE. )  or
            chopping  ( RND = .FALSE. )  occurs in addition. This may not

            be a reliable guide to the way in which the machine performs
            its arithmetic.

    IEEE1   (output) LOGICAL
            Specifies whether rounding appears to be done in the IEEE
            'round to nearest' style.

    Further Details
    ===============

    The routine is based on the routine  ENVRON  by Malcolm and
    incorporates suggestions by Gentleman and Marovich. See

       Malcolm M. A. (1972) Algorithms to reveal properties of
          floating-point arithmetic. Comms. of the ACM, 15, 949-951.

       Gentleman W. M. and Marovich S. B. (1974) More on algorithms
          that reveal properties of floating point arithmetic units.
          Comms. of the ACM, 17, 276-277.

   =====================================================================
*/

    /* Initialized data */
    static logical first = TRUE_;
    /* System generated locals */
    real r__1, r__2;
    /* Local variables */
    static logical lrnd;
    static real a, b, c, f;
    static integer lbeta;
    static real savec;
    static logical lieee1;
    static real t1, t2;
    static integer lt;
    static real one = 1.f;

    if (first) {
        first = FALSE_;

/*        LBETA,  LIEEE1,  LT and  LRND  are the  local values  of  BETA,
          IEEE1, T and RND.

          Throughout this routine  we use the function  SLAMC3  to ensure
          that relevant values are  stored and not held in registers, or
          are not affected by optimizers.

          Compute  a = 2.0**m  with the  smallest positive integer m such
          that
               fl( a + 1.0 ) = a.
*/

        a = c = one;

        while (c == one) {
            a *= 2;
            c = slamc3_(&a, &one);
            r__1 = -a;
            c = slamc3_(&c, &r__1);
        }

/*        Now compute  b = 2.0**m  with the smallest positive integer m
          such that
             fl( a + b ) .gt. a.
*/

        b = one;
        c = slamc3_(&a, &b);

/* The next two lines of code were replaced by Ian Scott from the original line
  > while (c==a) {
  During a optimised build under MSVC, the compiler was using the value of
  C still in a register in while loop test. This is an 80-bit value rather than
  the 64 bit value it uses after saving and loading from memory.
  So the 80 bit precision value was having 1 added, making it a different number
  and so not executing the loop.
  The call to slamc3_ in the loop condition forces the value to 64-bit precision
  as during the previous calculation.
*/
        r__1 = -a;
        while (slamc3_(&c, &r__1) == 0.f) {
            b *= 2;
            c = slamc3_(&a, &b);
        }

/*        Now compute the base.  a and c  are neighbouring floating point
          numbers  in the  interval  ( beta**t, beta**( t + 1 ) )  and so
          their difference is beta. Adding 0.25 to c is to ensure that it
          is truncated to beta and not ( beta - 1 ).
*/

        savec = c;
        r__1 = -a;
        c = slamc3_(&c, &r__1);
        lbeta = (integer)(c + 0.25f);

/*        Now determine whether rounding or chopping occurs,  by adding a
          bit  less  than  beta/2  and a  bit  more  than  beta/2  to a.
*/

        b = (real) lbeta;
        r__1 = b / 2;
        r__2 = -b / 100;
        f = slamc3_(&r__1, &r__2);
        c = slamc3_(&f, &a);
        if (c == a) {
            lrnd = TRUE_;
        } else {
            lrnd = FALSE_;
        }
        r__1 = b / 2;
        r__2 = b / 100;
        f = slamc3_(&r__1, &r__2);
        c = slamc3_(&f, &a);
        if (lrnd && c == a) {
            lrnd = FALSE_;
        }

/*        Try and decide whether rounding is done in the  IEEE  'round to
          nearest' style. B/2 is half a unit in the last place of the two
          numbers A and SAVEC. Furthermore, A is even, i.e. has last bit
          zero, and SAVEC is odd. Thus adding B/2 to A should not  change
          A, but adding B/2 to SAVEC should change SAVEC.
*/

        r__1 = b / 2;
        t1 = slamc3_(&r__1, &a);
        r__1 = b / 2;
        t2 = slamc3_(&r__1, &savec);
        lieee1 = t1 == a && t2 > savec && lrnd;

/*        Now find  the  mantissa, t.  It should  be the  integer part of
          log to the base beta of a,  however it is safer to determine t
          by powering.  So we find t as the smallest positive integer for
          which

             fl( beta**t + 1.0 ) = 1.0. */

        lt = 0;
        a = c = one;

        while (c == one) {
            ++lt;
            a *= lbeta;
            c = slamc3_(&a, &one);
            r__1 = -a;
            c = slamc3_(&c, &r__1);
        }
    }

    *beta = lbeta;
    *t = lt;
    *rnd = lrnd;
    *ieee1 = lieee1;
} /* slamc1_ */

/* Subroutine */ void slamc2_(integer *beta, integer *t, logical *rnd,
                              real *eps, integer *emin, real *rmin,
                              integer *emax, real *rmax)
{
/*  -- LAPACK auxiliary routine (version 2.0) --
       Univ. of Tennessee, Univ. of California Berkeley, NAG Ltd.,
       Courant Institute, Argonne National Lab, and Rice University
       October 31, 1992


    Purpose
    =======

    SLAMC2 determines the machine parameters specified in its argument
    list.

    Arguments
    =========

    BETA    (output) INTEGER
            The base of the machine.

    T       (output) INTEGER
            The number of ( BETA ) digits in the mantissa.

    RND     (output) LOGICAL
            Specifies whether proper rounding  ( RND = .TRUE. )  or
            chopping  ( RND = .FALSE. )  occurs in addition. This may not

            be a reliable guide to the way in which the machine performs

            its arithmetic.

    EPS     (output) REAL
            The smallest positive number such that

               fl( 1.0 - EPS ) .LT. 1.0,

            where fl denotes the computed value.

    EMIN    (output) INTEGER
            The minimum exponent before (gradual) underflow occurs.

    RMIN    (output) REAL
            The smallest normalized number for the machine, given by
            BASE**( EMIN - 1 ), where  BASE  is the floating point value

            of BETA.

    EMAX    (output) INTEGER
            The maximum exponent before overflow occurs.

    RMAX    (output) REAL
            The largest positive number for the machine, given by
            BASE**EMAX * ( 1 - EPS ), where  BASE  is the floating point

            value of BETA.

    Further Details
    ===============

    The computation of  EPS  is based on a routine PARANOIA by
    W. Kahan of the University of California at Berkeley.

   =====================================================================
*/

    /* Initialized data */
    static logical first = TRUE_;
    static logical iwarn = FALSE_;
    /* System generated locals */
    integer i__1;
    real r__1, r__2;
    /* Local variables */
    static logical ieee;
    static real half = 0.5f;
    static logical lrnd;
    static real leps, zero = 0.f, a, b, c;
    static integer i, lbeta;
    static real rbase;
    static integer lemin, lemax, gnmin;
    static real small;
    static integer gpmin;
    static real third, lrmin, lrmax, sixth;
    static logical lieee1;
    static integer lt, ngnmin, ngpmin;
    static real one = 1.f;

    if (first) {
        first = FALSE_;

/*        LBETA, LT, LRND, LEPS, LEMIN and LRMIN  are the local values of
          BETA, T, RND, EPS, EMIN and RMIN.

          Throughout this routine  we use the function  SLAMC3  to ensure

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -