float128_util.c

Go to the documentation of this file.
00001 /****************************************************************************/
00002 /*                                                                          */
00003 /*  This file is part of QSopt_ex.                                          */
00004 /*                                                                          */
00005 /*  (c) Copyright 2006 by David Applegate, William Cook, Sanjeeb Dash,      */
00006 /*  and Daniel Espinoza                                                     */
00007 /*                                                                          */
00008 /*  Sanjeeb Dash ownership of copyright in QSopt_ex is derived from his     */
00009 /*  copyright in QSopt.                                                     */
00010 /*                                                                          */
00011 /*  This code may be used under the terms of the GNU General Public License */
00012 /*  (Version 2.1 or later) as published by the Free Software Foundation.    */
00013 /*                                                                          */
00014 /*  Alternatively, use is granted for research purposes only.               */
00015 /*                                                                          */
00016 /*  It is your choice of which of these two licenses you are operating      */
00017 /*  under.                                                                  */
00018 /*                                                                          */
00019 /*  We make no guarantees about the correctness or usefulness of this code. */
00020 /*                                                                          */
00021 /****************************************************************************/
00022 
00023 /* RCSINFO $Id: float128_util.c,v 1.2 2003/11/05 16:47:22 meven Exp $ */
00024 /****************************************************************************/
00025 /*                                                                          */
00026 /*  This file is part of CONCORDE                                           */
00027 /*                                                                          */
00028 /*  (c) Copyright 1995--1999 by David Applegate, Robert Bixby,              */
00029 /*  Vasek Chvatal, and William Cook                                         */
00030 /*                                                                          */
00031 /*  Permission is granted for academic research use.  For other uses,       */
00032 /*  contact the authors for licensing options.                              */
00033 /*                                                                          */
00034 /*  Use at your own risk.  We make no guarantees about the                  */
00035 /*  correctness or usefulness of this code.                                 */
00036 /*                                                                          */
00037 /****************************************************************************/
00038 
00039 /****************************************************************************/
00040 /*                                                                          */
00041 /*               MISCELLANEOUS UTILITY ROUTINES                             */
00042 /*                                                                          */
00043 /*                           TSP CODE                                       */
00044 /*                                                                          */
00045 /*  Written by:  Applegate, Bixby, Chvatal, and Cook                        */
00046 /*  Date: October 12, 1995                                                  */
00047 /*  Date: September 28, 1997                                                */
00048 /*                                                                          */
00049 /*    EXPORTED FUNCTIONS:                                                   */
00050 /*                                                                          */
00051 /*  unsigned int float128_ILLutil_nextprime (unsigned int x)                         */
00052 /*    FINDS the smallest positive prime >= x                                */
00053 /*                                                                          */
00054 /*  int float128_ILLutil_our_gcd (int a, int b)                                      */
00055 /*    COMPUTES gcd(a,b)                                                     */
00056 /*    -gcd(a,b) is always >= 0                                              */
00057 /*    -a and b can be negative, positive, or zero                           */
00058 /*                                                                          */
00059 /*  int float128_ILLutil_our_lcm (int a, int b)                                      */
00060 /*    COMPUTES lcm(a,b)                                                     */
00061 /*    -lcm(a,b) is always >= 0                                              */
00062 /*    -a and b can be negative, positive, or zero                           */
00063 /*                                                                          */
00064 /*  double float128_ILLutil_our_floor (double x)                                     */
00065 /*    REURNS the greatest integer no larger than x.                         */
00066 /*                                                                          */
00067 /*  double float128_ILLutil_our_ceil (double x)                                      */
00068 /*    REURNS the least integer no smaller than x.                           */
00069 /*                                                                          */
00070 /*  double float128_ILLutil_our_frac (double x)                                      */
00071 /*    REURNS the fractional part of x.                                      */
00072 /*                                                                          */
00073 /*  char *float128_ILLutil_strchr (const char *s, int c)                             */
00074 /*    RETURNS a pointer to the first occurrence of c in s, or NULL if c     */
00075 /*    does not occur in s                                                   */
00076 /*                                                                          */
00077 /* int float128_ILLutil_strcasecmp(const char *s1, const char *s2)                   */
00078 /*    RETURNS the string comparison, iqnoring the case of the letters.      */
00079 /*                                                                          */
00080 /* int float128_ILLutil_strncasecmp(const char *s1, const char *s2, size_t n)        */
00081 /*    RETURNS the string comparision, iqnoring case, looks at max n bytes.  */
00082 /*                                                                          */
00083 /*  char *float128_ILLutil_str(const char *s)                                        */
00084 /*    allocates and returns a copy of s                                     */
00085 /*                                                                          */
00086 /****************************************************************************/
00087 
00088 #include "machdefs.h"
00089 #include "except.h"
00090 #include "float128_util.h"
00091 #ifdef USEDMALLOC
00092 #include "dmalloc.h"
00093 #endif
00094 
00095 
00096 
00097 static int float128_isprime (
00098   unsigned int x);
00099 
00100 
00101 unsigned int float128_ILLutil_nextprime (
00102   unsigned int x)
00103 {
00104   if (x < 3)
00105     return 3;
00106   x |= 1;
00107   while (!float128_isprime (x))
00108     x += 2;
00109   return x;
00110 }
00111 
00112 static int float128_isprime (
00113   unsigned int p)
00114 {
00115   unsigned int i;
00116 
00117   if ((p & 1) == 0)
00118     return 0;
00119   for (i = 3; i * i <= p; i += 2)
00120   {
00121     if (p % i == 0)
00122       return 0;
00123   }
00124   return 1;
00125 }
00126 
00127 int float128_ILLutil_our_gcd (
00128   int a,
00129   int b)
00130 {
00131   int c;
00132 
00133   if (a < 0)
00134     a = -a;
00135   if (b < 0)
00136     b = -b;
00137   if (a > b)
00138     float128_ILL_SWAP (a, b, c);
00139 
00140   while (a)
00141   {
00142     c = b % a;
00143     b = a;
00144     a = c;
00145   }
00146   return b;
00147 }
00148 
00149 int float128_ILLutil_our_lcm (
00150   int a,
00151   int b)
00152 {
00153   int c;
00154 
00155   if (a < 0)
00156     a = -a;
00157   if (b < 0)
00158     b = -b;
00159 
00160   c = float128_ILLutil_our_gcd (a, b);
00161 
00162   return (a / c) * b;
00163 }
00164 
00165 double float128_ILLutil_our_floor (
00166   double x)
00167 {
00168   return floor (x);
00169 }
00170 
00171 double float128_ILLutil_our_ceil (
00172   double x)
00173 {
00174   return ceil (x);
00175 }
00176 
00177 double float128_ILLutil_our_frac (
00178   double x)
00179 {
00180   return x - floor (x);
00181 }
00182 
00183 double float128_ILLutil_norm_sqr (
00184   double *v,
00185   int len)
00186 {
00187   int i;
00188   double sum = 0.0;
00189 
00190   for (i = 0; i < len; i++)
00191     sum += v[i] * v[i];
00192   return sum;
00193 }
00194 
00195 int float128_ILLutil_our_log2 (
00196   int a)
00197 {
00198   int i = 0, j = 1;
00199 
00200   while (j < a)
00201   {
00202     j = j << 1;
00203     i++;
00204   }
00205   return i ? i : 1;
00206 }
00207 
00208 const char *float128_ILLutil_strchr (
00209   const char *s,
00210   int c)
00211 {
00212   while (*s)
00213   {
00214     if (*s == c)
00215       return s;
00216     s++;
00217   }
00218   return (char *) NULL;
00219 }
00220 
00221 int float128_ILLutil_strcasecmp (
00222   const char *s1,
00223   const char *s2)
00224 {
00225   return strcasecmp (s1, s2);
00226 }
00227 
00228 int float128_ILLutil_strncasecmp (
00229   const char *s1,
00230   const char *s2,
00231   size_t n)
00232 {
00233   return strncasecmp (s1, s2, n);
00234 }
00235 
00236 int float128_ILLutil_index (
00237   const char *list[],
00238   const char *name)
00239 {
00240   int i;
00241 
00242   for (i = 0; list[i] != NULL; i++)
00243   {
00244     if (!strcmp (name, list[i]))
00245     {
00246       return i;
00247     }
00248   }
00249   return -1;
00250 }
00251 
00252 int float128_ILLutil_array_index (
00253   char *list[],
00254   int n,
00255   const char *name)
00256 {
00257   int i;
00258 
00259   for (i = 0; i < n; i++)
00260   {
00261     if ((list[i] != NULL) && !strcmp (name, list[i]))
00262     {
00263       return i;
00264     }
00265   }
00266   return -1;
00267 }
00268 
00269 char *float128_ILLutil_str (
00270   const char *str)
00271 {
00272   int len;
00273   char *cpy = NULL;
00274 
00275   if (str != NULL)
00276   {
00277     len = strlen (str) + 1;
00278     ILL_SAFE_MALLOC_no_rval (cpy, len, char);
00279 
00280     strcpy (cpy, str);
00281   }
00282 CLEANUP:
00283   return cpy;
00284 }

Generated on Thu Mar 29 09:32:30 2012 for QSopt_ex by  doxygen 1.4.7