home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / octa21fs.zip / octave / octave-2.1.23 / liboctave / mach-info.cc < prev    next >
C/C++ Source or Header  |  2000-01-15  |  5KB  |  257 lines

  1. /*
  2.  
  3. Copyright (C) 1996, 1997 John W. Eaton
  4.  
  5. This file is part of Octave.
  6.  
  7. Octave is free software; you can redistribute it and/or modify it
  8. under the terms of the GNU General Public License as published by the
  9. Free Software Foundation; either version 2, or (at your option) any
  10. later version.
  11.  
  12. Octave is distributed in the hope that it will be useful, but WITHOUT
  13. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14. FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  15. for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with Octave; see the file COPYING.  If not, write to the Free
  19. Software Foundation, 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  20.  
  21. */
  22.  
  23. #ifdef HAVE_CONFIG_H
  24. #include <config.h>
  25. #endif
  26.  
  27. #include "f77-fcn.h"
  28. #include "lo-error.h"
  29. #include "mach-info.h"
  30.  
  31. extern "C"
  32. {
  33.   double F77_FCN (d1mach, D1MACH) (const int&);
  34. }
  35.  
  36. oct_mach_info *oct_mach_info::instance = 0;
  37.  
  38. union equiv
  39. {
  40.   double d;
  41.   int i[2];
  42. };
  43.  
  44. struct
  45. float_params
  46. {
  47.   oct_mach_info::float_format fp_fmt;
  48.   equiv fp_par[4];
  49. };
  50.  
  51. #define INIT_FLT_PAR(fp, fmt, sm1, sm2, lrg1, lrg2, rt1, rt2, dv1, dv2) \
  52.   do \
  53.     { \
  54.       fp.fp_fmt = (fmt); \
  55.       fp.fp_par[0].i[0] = (sm1);  fp.fp_par[0].i[1] = (sm2); \
  56.       fp.fp_par[1].i[0] = (lrg1); fp.fp_par[1].i[1] = (lrg2); \
  57.       fp.fp_par[2].i[0] = (rt1);  fp.fp_par[2].i[1] = (rt2); \
  58.       fp.fp_par[3].i[0] = (dv1);  fp.fp_par[3].i[1] = (dv2); \
  59.     } \
  60.   while (0)
  61.  
  62. static int
  63. equiv_compare (const equiv *std, const equiv *v, int len)
  64. {
  65.   int i;
  66.   for (i = 0; i < len; i++)
  67.     if (v[i].i[0] != std[i].i[0] || v[i].i[1] != std[i].i[1])
  68.       return 0;
  69.   return 1;
  70. }
  71.  
  72. void
  73. oct_mach_info::init_float_format (void) const
  74. {
  75.   float_params fp[5];
  76.  
  77.   INIT_FLT_PAR (fp[0], oct_mach_info::ieee_big_endian,
  78.            1048576,  0,
  79.         2146435071, -1,
  80.         1017118720,  0,
  81.         1018167296,  0);
  82.  
  83.   INIT_FLT_PAR (fp[1], oct_mach_info::ieee_little_endian,
  84.          0,    1048576,
  85.         -1, 2146435071,
  86.          0, 1017118720,
  87.          0, 1018167296);
  88.  
  89.   INIT_FLT_PAR (fp[2], oct_mach_info::vax_d,
  90.            128,  0,
  91.         -32769, -1,
  92.           9344,  0,
  93.           9344,  0);
  94.  
  95.   INIT_FLT_PAR (fp[3], oct_mach_info::vax_g,
  96.             16,  0,
  97.         -32769, -1,
  98.          15552,  0,
  99.          15552,  0);
  100.  
  101.   INIT_FLT_PAR (fp[4], oct_mach_info::unknown,
  102.         0, 0,
  103.         0, 0,
  104.         0, 0,
  105.         0, 0);
  106.  
  107.   equiv mach_fp_par[4];
  108.  
  109.   mach_fp_par[0].d = F77_FCN (d1mach, D1MACH) (1);
  110.   mach_fp_par[1].d = F77_FCN (d1mach, D1MACH) (2);
  111.   mach_fp_par[2].d = F77_FCN (d1mach, D1MACH) (3);
  112.   mach_fp_par[3].d = F77_FCN (d1mach, D1MACH) (4);
  113.  
  114.   int i = 0;
  115.   do
  116.     {
  117.       if (equiv_compare (fp[i].fp_par, mach_fp_par, 4))
  118.     {
  119.       native_float_fmt = fp[i].fp_fmt;
  120.       break;
  121.     }
  122.     }
  123.   while (fp[++i].fp_fmt != oct_mach_info::unknown);
  124. }
  125.  
  126. void
  127. oct_mach_info::ten_little_endians (void) const
  128. {
  129.   // Are we little or big endian?  From Harbison & Steele.
  130.  
  131.   union
  132.   {
  133.     long l;
  134.     char c[sizeof (long)];
  135.   } u;
  136.  
  137.   u.l = 1;
  138.  
  139.   big_chief = (u.c[sizeof (long) - 1] == 1);
  140. }
  141.  
  142. oct_mach_info::oct_mach_info (void)
  143. {
  144.   init_float_format ();
  145.   ten_little_endians ();
  146. }
  147.  
  148. bool
  149. oct_mach_info::instance_ok (void)
  150. {
  151.   bool retval = true;
  152.  
  153.   if (! instance)
  154.     instance = new oct_mach_info ();
  155.  
  156.   if (! instance)
  157.     {
  158.       (*current_liboctave_error_handler)
  159.     ("unable to create command history object!");
  160.  
  161.       retval = false;
  162.     }
  163.  
  164.   return retval;
  165. }
  166.  
  167. oct_mach_info::float_format
  168. oct_mach_info::native_float_format (void)
  169. {
  170.   return (instance_ok ())
  171.     ? instance->native_float_fmt : oct_mach_info::unknown;
  172. }
  173.  
  174. bool
  175. oct_mach_info::words_big_endian (void)
  176. {
  177.   return (instance_ok ())
  178.     ? instance->big_chief : false;
  179. }
  180.  
  181. bool
  182. oct_mach_info::words_little_endian (void)
  183. {
  184.   return (instance_ok ())
  185.     ? (! instance->big_chief) : false;
  186. }
  187.  
  188. oct_mach_info::float_format
  189. oct_mach_info::string_to_float_format (const string& s)
  190. {
  191.   oct_mach_info::float_format retval = oct_mach_info::unknown;
  192.  
  193.   if (s == "native" || s == "n")
  194.     retval = oct_mach_info::native;
  195.   else if (s == "ieee-be" || s == "b")
  196.     retval = oct_mach_info::ieee_big_endian;
  197.   else if (s == "ieee-le" || s == "l")
  198.     retval = oct_mach_info::ieee_little_endian;
  199.   else if (s == "vaxd" || s == "d")
  200.     retval = oct_mach_info::vax_d;
  201.   else if (s == "vaxg" || s == "g")
  202.     retval = oct_mach_info::vax_g;
  203.   else if (s == "cray" || s == "c")
  204.     retval = oct_mach_info::cray;
  205.   else if (s == "unknown")
  206.     retval = oct_mach_info::unknown;
  207.   else
  208.     (*current_liboctave_error_handler)
  209.       ("invalid architecture type specified");
  210.  
  211.   return retval;
  212. }
  213.  
  214. string
  215. oct_mach_info::float_format_as_string (float_format flt_fmt)
  216. {
  217.   string retval = "unknown";
  218.  
  219.   switch (flt_fmt)
  220.     {
  221.     case native:
  222.       retval = "native";
  223.       break;
  224.  
  225.     case ieee_big_endian:
  226.       retval = "ieee_big_endian";
  227.       break;
  228.  
  229.     case ieee_little_endian:
  230.       retval = "ieee_little_endian";
  231.       break;
  232.  
  233.     case vax_d:
  234.       retval = "vax_d_float";
  235.       break;
  236.  
  237.     case vax_g:
  238.       retval = "vax_g_float";
  239.       break;
  240.  
  241.     case cray:
  242.       retval = "cray";
  243.       break;
  244.  
  245.     default:
  246.       break;
  247.     }
  248.  
  249.   return retval;
  250. }
  251.  
  252. /*
  253. ;;; Local Variables: ***
  254. ;;; mode: C++ ***
  255. ;;; End: ***
  256. */
  257.