view liboctave/system/mach-info.cc @ 22197:e43d83253e28

refill multi-line macro definitions Use the Emacs C++ mode style for line continuation markers in multi-line macro definitions. * make_int.cc, __dsearchn__.cc, __magick_read__.cc, besselj.cc, bitfcns.cc, bsxfun.cc, cellfun.cc, data.cc, defun-dld.h, defun-int.h, defun.h, det.cc, error.h, find.cc, gcd.cc, graphics.cc, interpreter.h, jit-ir.h, jit-typeinfo.h, lookup.cc, ls-mat5.cc, max.cc, mexproto.h, mxarray.in.h, oct-stream.cc, ordschur.cc, pr-output.cc, profiler.h, psi.cc, regexp.cc, sparse-xdiv.cc, sparse-xpow.cc, tril.cc, txt-eng.h, utils.cc, variables.cc, variables.h, xdiv.cc, xpow.cc, __glpk__.cc, ov-base.cc, ov-base.h, ov-cell.cc, ov-ch-mat.cc, ov-classdef.cc, ov-complex.cc, ov-cx-mat.cc, ov-cx-sparse.cc, ov-float.cc, ov-float.h, ov-flt-complex.cc, ov-flt-cx-mat.cc, ov-flt-re-mat.cc, ov-int-traits.h, ov-lazy-idx.h, ov-perm.cc, ov-re-mat.cc, ov-re-sparse.cc, ov-scalar.cc, ov-scalar.h, ov-str-mat.cc, ov-type-conv.h, ov.cc, ov.h, op-class.cc, op-int-conv.cc, op-int.h, op-str-str.cc, ops.h, lex.ll, Array.cc, CMatrix.cc, CSparse.cc, MArray.cc, MArray.h, MDiagArray2.cc, MDiagArray2.h, MSparse.h, Sparse.cc, dMatrix.cc, dSparse.cc, fCMatrix.cc, fMatrix.cc, idx-vector.cc, f77-fcn.h, quit.h, bsxfun-decl.h, bsxfun-defs.cc, lo-specfun.cc, oct-convn.cc, oct-convn.h, oct-norm.cc, oct-norm.h, oct-rand.cc, Sparse-op-decls.h, Sparse-op-defs.h, mx-inlines.cc, mx-op-decl.h, mx-op-defs.h, mach-info.cc, oct-group.cc, oct-passwd.cc, oct-syscalls.cc, oct-time.cc, data-conv.cc, kpse.cc, lo-ieee.h, lo-macros.h, oct-cmplx.h, oct-glob.cc, oct-inttypes.cc, oct-inttypes.h, oct-locbuf.h, oct-sparse.h, url-transfer.cc, oct-conf-post.in.h, shared-fcns.h: Refill macro definitions.
author John W. Eaton <jwe@octave.org>
date Mon, 01 Aug 2016 12:40:18 -0400
parents c4ab2e54f100
children bac0d6f07a3e
line wrap: on
line source

/*

Copyright (C) 1996-2015 John W. Eaton

This file is part of Octave.

Octave is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation; either version 3 of the License, or (at your
option) any later version.

Octave is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
for more details.

You should have received a copy of the GNU General Public License
along with Octave; see the file COPYING.  If not, see
<http://www.gnu.org/licenses/>.

*/

#if defined (HAVE_CONFIG_H)
#  include "config.h"
#endif

#include "f77-fcn.h"
#include "lo-error.h"
#include "mach-info.h"
#include "singleton-cleanup.h"

extern "C"
{
  double F77_FUNC (d1mach, D1MACH) (const octave_idx_type&);
}

namespace octave
{
  mach_info *mach_info::instance = 0;

  union equiv
  {
    double d;
    int i[2];
  };

  struct
  float_params
  {
    mach_info::float_format fp_fmt;
    equiv fp_par[4];
  };

#define INIT_FLT_PAR(fp, fmt, sm1, sm2, lrg1, lrg2, rt1, rt2, dv1, dv2) \
  do                                                                    \
    {                                                                   \
      fp.fp_fmt = (fmt);                                                \
      fp.fp_par[0].i[0] = (sm1);  fp.fp_par[0].i[1] = (sm2);            \
      fp.fp_par[1].i[0] = (lrg1); fp.fp_par[1].i[1] = (lrg2);           \
      fp.fp_par[2].i[0] = (rt1);  fp.fp_par[2].i[1] = (rt2);            \
      fp.fp_par[3].i[0] = (dv1);  fp.fp_par[3].i[1] = (dv2);            \
    }                                                                   \
  while (0)

  static int
  equiv_compare (const equiv *std, const equiv *v, int len)
  {
    int i;
    for (i = 0; i < len; i++)
      if (v[i].i[0] != std[i].i[0] || v[i].i[1] != std[i].i[1])
        return 0;
    return 1;
  }

  static mach_info::float_format
  get_float_format (void)
  {
    mach_info::float_format retval = mach_info::flt_fmt_unknown;

    float_params fp[5];

    INIT_FLT_PAR (fp[0], mach_info::flt_fmt_ieee_big_endian,
                     1048576,  0,
                  2146435071, -1,
                  1017118720,  0,
                  1018167296,  0);

    INIT_FLT_PAR (fp[1], mach_info::flt_fmt_ieee_little_endian,
                   0,    1048576,
                  -1, 2146435071,
                   0, 1017118720,
                   0, 1018167296);

    INIT_FLT_PAR (fp[4], mach_info::flt_fmt_unknown,
                  0, 0,
                  0, 0,
                  0, 0,
                  0, 0);

    equiv mach_fp_par[4];

    mach_fp_par[0].d = F77_FUNC (d1mach, D1MACH) (1);
    mach_fp_par[1].d = F77_FUNC (d1mach, D1MACH) (2);
    mach_fp_par[2].d = F77_FUNC (d1mach, D1MACH) (3);
    mach_fp_par[3].d = F77_FUNC (d1mach, D1MACH) (4);

    int i = 0;
    do
      {
        if (equiv_compare (fp[i].fp_par, mach_fp_par, 4))
          {
            retval = fp[i].fp_fmt;
            break;
          }
      }
    while (fp[++i].fp_fmt != mach_info::flt_fmt_unknown);

    return retval;
  }

  static bool
  ten_little_endians (void)
  {
    // Are we little or big endian?  From Harbison & Steele.

    union
    {
      long l;
      char c[sizeof (long)];
    } u;

    u.l = 1;

    return (u.c[sizeof (long) - 1] == 1);
  }

  mach_info::mach_info (void)
    : native_float_fmt (get_float_format ()),
      big_chief (ten_little_endians ()) { }

  bool
  mach_info::instance_ok (void)
  {
    bool retval = true;

    if (! instance)
      {
        instance = new mach_info ();

        if (instance)
          singleton_cleanup_list::add (cleanup_instance);
      }

    if (! instance)
      (*current_liboctave_error_handler)
        ("unable to create command history object!");

    return retval;
  }

  mach_info::float_format
  mach_info::native_float_format (void)
  {
    return (instance_ok ())
           ? instance->native_float_fmt : mach_info::flt_fmt_unknown;
  }

  bool
  mach_info::words_big_endian (void)
  {
    return (instance_ok ())
           ? instance->big_chief : false;
  }

  bool
  mach_info::words_little_endian (void)
  {
    return (instance_ok ())
           ? (! instance->big_chief) : false;
  }

  mach_info::float_format
  mach_info::string_to_float_format (const std::string& s)
  {
    mach_info::float_format retval = mach_info::flt_fmt_unknown;

    if (s == "native" || s == "n")
      retval = mach_info::native_float_format ();
    else if (s == "ieee-be" || s == "b")
      retval = mach_info::flt_fmt_ieee_big_endian;
    else if (s == "ieee-le" || s == "l")
      retval = mach_info::flt_fmt_ieee_little_endian;
    else if (s == "unknown")
      retval = mach_info::flt_fmt_unknown;
    else
      (*current_liboctave_error_handler) ("invalid architecture type specified");

    return retval;
  }

  std::string
  mach_info::float_format_as_string (float_format flt_fmt)
  {
    std::string retval = "unknown";

    switch (flt_fmt)
      {
      case flt_fmt_ieee_big_endian:
        retval = "ieee-be";
        break;

      case flt_fmt_ieee_little_endian:
        retval = "ieee-le";
        break;

      default:
        break;
      }

    return retval;
  }
}