00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
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 }