File: IEEEFP.cpp

package info (click to toggle)
freemat 4.2%2Bdfsg1-6
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 142,116 kB
  • sloc: ansic: 126,788; cpp: 62,015; python: 2,080; perl: 1,255; sh: 1,146; yacc: 1,019; lex: 239; makefile: 107
file content (461 lines) | stat: -rw-r--r-- 10,105 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
/*
 * Copyright (c) 2002-2006 Samit Basu
 *
 * This program 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 2 of the License, or
 * (at your option) any later version.
 *
 * This program 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 this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */
#include "IEEEFP.hpp"
#include <cstdio>

static bool endianDetected = false;
static bool bigEndian = false;

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

void CheckBigEndian() {
  u.l = 1;
  endianDetected = true;
  bigEndian = (u.c[sizeof(long) - 1] == 1);
  
}

bool IsInfinite(float t) {
  union {
    float f;
    unsigned int i;
  } u;             
  u.f = t;
  if (((u.i & 0x7f800000) == 0x7f800000) && ((u.i & 0x007fffff) == 0))
    return true;
  return false;
}

bool IsInfinite(double t) {
  union
  {
    double d;
    unsigned int i[2];
  } u;
  u.d = t;
  if (!endianDetected) 
    CheckBigEndian();
  if (!bigEndian) {
    if( ((u.i[1] & 0x7ff00000) == 0x7ff00000)
	&& (((u.i[1] & 0x000fffff) == 0) && (u.i[0] == 0)))
      return true;
  } else {
    if( ((u.i[0] & 0x7ff00000) == 0x7ff00000)
	&& (((u.i[0] & 0x000fffff) == 0) && (u.i[1] == 0)))
      return true;
  }
  return false;

}

bool IsInfinite(int8) {return false;}
bool IsInfinite(int16) {return false;}
bool IsInfinite(int32) {return false;}
bool IsInfinite(int64) {return false;}
bool IsInfinite(uint8) {return false;}
bool IsInfinite(uint16) {return false;}
bool IsInfinite(uint32) {return false;}
bool IsInfinite(uint64) {return false;}

bool IsFinite(int8) {return true;}
bool IsFinite(int16) {return true;}
bool IsFinite(int32) {return true;}
bool IsFinite(int64) {return true;}
bool IsFinite(uint8) {return true;}
bool IsFinite(uint16) {return true;}
bool IsFinite(uint32) {return true;}
bool IsFinite(uint64) {return true;}

bool IsNaN(int32 t) {
  return false;
}

bool IsNaN(int64 t) {
  return false;
}

bool IsNaN(uint32 t) {
  return false;
}

bool IsNaN(uint64 t) {
  return false;
}

bool IsNaN(float t) {
  union {
    float f;
    unsigned int i;
  } u;
  u.f = t;
  if (((u.i & 0x7f800000) == 0x7f800000) && ((u.i & 0x007fffff) != 0))
    return true; 
  return false;
}

bool IsNaN(double t) {
  union
  {
    double d;
    unsigned int i[2];
  } u;
  u.d = t;

  if (!endianDetected) 
    CheckBigEndian();
  if (!bigEndian) {
    if( ((u.i[1] & 0x7ff00000) == 0x7ff00000)
	&& (((u.i[1] & 0x000fffff) != 0) || (u.i[0] != 0)))
      return true;
  } else {
    if( ((u.i[0] & 0x7ff00000) == 0x7ff00000)
	&& (((u.i[0] & 0x000fffff) != 0) || (u.i[1] != 0)))
      return true;
  }
  return false;
}

bool IsFinite(float t) {
  return (!(IsNaN(t) || IsInfinite(t)));
}

bool IsFinite(double t) {
  return (!(IsNaN(t) || IsInfinite(t)));
}

double NaN() {
  union {
    float f;
    unsigned int i;
  } u;
  u.i = 0x7fC00000;
  return u.f;
}

double Inf() {
  union {
    float f;
    unsigned int i;
  } u;
  u.i = 0x7f800000;
  return u.f;
}

QString ToHexString(float t) {
  union {
    float f;
    unsigned int i;
  } u;
  if (IsNaN(t))
    return QString("fff80000");
  u.f = t;
  return QString("%1").arg(uint(u.i),int(8),int(16),QChar('0'));
}

QString ToHexString(double t) {
  union {
    double f;
    unsigned int i[2];
  } u;
  if (IsNaN(t))
    return QString("fff8000000000000");
  u.f = t;
  if (!endianDetected) 
    CheckBigEndian();
  if (!bigEndian) 
    return QString("%1%2").arg(uint(u.i[1]),int(8),int(16),QChar('0')).arg(uint(u.i[0]),int(8),int(16),QChar('0'));
  else
    return QString("%1%2").arg(uint(u.i[0]),int(8),int(16),QChar('0')).arg(uint(u.i[1]),int(8),int(16),QChar('0'));
}


#if defined(_MSC_VER )

#include <math.h>

extern "C" { 
__declspec( dllexport ) double rint (double x)
{
  __asm{
	  fld x
	  frndint
	  fstp x
  };
  return x;
}

__declspec( dllexport ) float rintf( float x ) { return rint(x); }

//VC doesn't define log1p so we borrow the definition from libm (mingw)
__declspec( dllexport ) double log1p(double x){

	const double limit = 0.29;
	const double one = 1.0;

	__asm{
	/* BASED ON log1p.S from mingw
	 * Written by J.T. Conklin <jtc@netbsd.org>.
	 * Public domain.
	 * Removed header file dependency for use in libmingwex.a by
	 *   Danny Smith <dannysmith@users.sourceforge.net>
	 */

			/* The fyl2xp1 can only be used for values in
			   -1 + sqrt(2) / 2 <= x <= 1 - sqrt(2) / 2
			   0.29 is a safe value.
			 */
			fldln2
			fld     x //  [esp+4]
			fxam
			fnstsw	ax
			fld        st
			sahf
			jc        l3        // in case x is NaN or �Inf

	l4:      
			fabs
			fcomp        limit
			fnstsw	ax
			sahf
			jc        l2
			fadd        one
			fyl2x
			fstp x
			jmp l5

	l2:      
			fyl2xp1
			fstp x
			jmp l5

	l3:      
			jp        l4        // in case x is �Inf
			fstp        st(1)
			fstp        st(1)
	l5:
	};
return x;
}

const long double LOGE2L  = 6.9314718055994530941723E-1L;
const long double LOG2EL  = 1.4426950408889634073599E0L;
/* BASED ON log1p.S from mingw
 * Written 2005 by Gregory W. Chicares <chicares@cox.net>.
 * Adapted to double by Danny Smith <dannysmith@users.sourceforge.net>. 
 * Public domain.
 */
__declspec( dllexport ) double expm1 (double x)
{
  if (fabs(x) < LOGE2L)
    {
      x *= LOG2EL;
	  __asm{ 
		  fld x
		  f2xm1 
		  fstp x
	  };

      return x;
    }
  else
    return exp(x) - 1.0;
}

__declspec( dllexport ) float nextafterf (float x, float y)
{
	union
	{
		float f;
		unsigned int i;
	} u;
	if (_isnan (y) || _isnan (x))
		return x + y;
	if (x == y )
		/* nextafter (0.0, -O.0) should return -0.0.  */
		return y;
	u.f = x; 
	if (x == 0.0F)
	{
		u.i = 1;
		return y > 0.0F ? u.f : -u.f;
	}
	if (((x > 0.0F) ^ (y > x)) == 0)
		u.i++;
	else
		u.i--;
	return u.f;
}

//TODO: implement actual floating point log1pf, expm1f
__declspec( dllexport ) float log1pf (float x){
    return log1p( x );
}
__declspec( dllexport ) float expm1f (float x){
    return expm1( x );
}

//Based on asinhf routine  from cygwin
/* asinh(x) = copysign(log(fabs(x) + sqrt(x * x + 1.0)), x) */
__declspec( dllexport ) float asinhf(float x)
{
  float z;
  if (!_finite (x))
    return x;
  z = fabsf (x);

  /* Use log1p to avoid cancellation with small x. Put
     x * x in denom, so overflow is harmless. 
     asinh(x) = log1p (x + sqrt (x * x + 1.0) - 1.0)
              = log1p (x + x * x / (sqrt (x * x + 1.0) + 1.0))  */

  z = log1p (z + z * z / (sqrt (z * z + 1.0) + 1.0));

  return ( x > 0.0 ? z : -z);
}

//Based on asinh routine  from cygwin
/* asinh(x) = copysign(log(fabs(x) + sqrt(x * x + 1.0)), x) */
__declspec( dllexport ) double asinh(double x)
{
  double z;
  if (!_finite (x))
    return x;
  z = fabs (x);

  /* Use log1p to avoid cancellation with small x. Put
     x * x in denom, so overflow is harmless. 
     asinh(x) = log1p (x + sqrt (x * x + 1.0) - 1.0)
              = log1p (x + x * x / (sqrt (x * x + 1.0) + 1.0))  */

  z = log1p (z + z * z / (sqrt (z * z + 1.0) + 1.0));

  return ( x > 0.0 ? z : -z);
}

//Based on routine  from cygwin
/* acosh(x) = log (x + sqrt(x * x - 1)) */
__declspec( dllexport ) double acosh (double x)
{
  if (_isnan (x)) 
    return x;

  if (x < 1.0)
    {
      errno = EDOM;
      return NaN();
    }

  if (x > 4294967296.f)
    /*  Avoid overflow (and unnecessary calculation when
        sqrt (x * x - 1) == x). GCC optimizes by replacing
        the long double M_LN2 const with a fldln2 insn.  */ 
    return log (x) + 6.9314718055994530941723E-1L;

  /* Since  x >= 1, the arg to log will always be greater than
     the fyl2xp1 limit (approx 0.29) so just use logl. */ 
  return log (x + sqrt((x + 1.0) * (x - 1.0)));
}

//Based on routine  from cygwin
/* acosh(x) = log (x + sqrt(x * x - 1)) */
__declspec( dllexport ) float acoshf (float x)
{
  if (_isnan (x)) 
    return x;
  if (x < 1.0f)
    {
      errno = EDOM;
      return NaN();
    }

 if (x > 4294967296.f)
    /*  Avoid overflow (and unnecessary calculation when
        sqrt (x * x - 1) == x). GCC optimizes by replacing
        the long double M_LN2 const with a fldln2 insn.  */ 
    return log (x) + 6.9314718055994530941723E-1L;

  /* Since  x >= 1, the arg to log will always be greater than
     the fyl2xp1 limit (approx 0.29) so just use logl. */ 
  return log (x + sqrt((x + 1.0) * (x - 1.0)));
}

//Based on routine  from cygwin
/* atanh (x) = 0.5 * log ((1.0 + x)/(1.0 - x)) */
__declspec( dllexport ) double atanh(double x)
{
  double z;
  if( _isnan (x))
    return x;
  z = fabs (x);
  if (z == 1.0)
    {
      errno  = ERANGE;
      return (x > 0 ? Inf() : -Inf());
    }
  if (z > 1.0)
    {
      errno = EDOM;
      return NaN();
    }
  /* Rearrange formula to avoid precision loss for small x.

  atanh(x) = 0.5 * log ((1.0 + x)/(1.0 - x))
           = 0.5 * log1p ((1.0 + x)/(1.0 - x) - 1.0)
           = 0.5 * log1p ((1.0 + x - 1.0 + x) /(1.0 - x)) 
           = 0.5 * log1p ((2.0 * x ) / (1.0 - x))  */
  z = 0.5 * log1p ((z + z) / (1.0 - z));
  return x >= 0 ? z : -z;
}

//Based on routine  from cygwin
/* atanh (x) = 0.5 * log ((1.0 + x)/(1.0 - x)) */
__declspec( dllexport ) float atanhf (float x)
{
  float z;
  if( _isnan (x))
    return x;
  z = fabsf (x);
  if (z == 1.0)
    {
      errno  = ERANGE;
      return (x > 0 ? Inf() : -Inf());
    }
  if ( z > 1.0)
    {
      errno = EDOM;
      return NaN();
    }
  /* Rearrange formula to avoid precision loss for small x.

  atanh(x) = 0.5 * log ((1.0 + x)/(1.0 - x))
           = 0.5 * log1p ((1.0 + x)/(1.0 - x) - 1.0)
           = 0.5 * log1p ((1.0 + x - 1.0 + x) /(1.0 - x)) 
           = 0.5 * log1p ((2.0 * x ) / (1.0 - x))  */
  z = 0.5 * log1p ((z + z) / (1.0 - z));
  return x >= 0 ? z : -z;
}


}


#endif