File: dtoa1.c

package info (click to toggle)
libamplsolver 0~20190702-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,184 kB
  • sloc: ansic: 40,472; asm: 76; sh: 72; fortran: 51; makefile: 16
file content (150 lines) | stat: -rw-r--r-- 3,352 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
/*******************************************************************
Copyright (C) 2016 AMPL Optimization, Inc.; written by David M. Gay.

Permission to use, copy, modify, and distribute this software and its
documentation for any purpose and without fee is hereby granted,
provided that the above copyright notice appear in all copies and that
both that the copyright notice and this permission notice and warranty
disclaimer appear in supporting documentation.

The author and AMPL Optimization, Inc. disclaim all warranties with
regard to this software, including all implied warranties of
merchantability and fitness.  In no event shall the author be liable
for any special, indirect or consequential damages or any damages
whatsoever resulting from loss of use, data or profits, whether in an
action of contract, negligence or other tortious action, arising out
of or in connection with the use or performance of this software.
*******************************************************************/

#ifndef No_dtoa /*{{*/
#ifdef __cplusplus
#include "memory.h"
#endif
#include "arith.h"
#include "math.h"
#undef  __MATH_H__
#define __MATH_H__

#include "stdlib.h"	/* A recent (2008) version of cygwin gratuitously
			   #includes stdlib.h.  To prevent confusion, any
			   such #include must come before the next line. */
#define strtod strtod_ASL
#define No_leftright
#ifdef ALLOW_OPENMP /*{*/
#define MULTIPLE_THREADS
#include <omp.h>

/* This assumes use of separate ASL* values for each thread, so we */
/* can ignore ACQUIRE_DTOA_LOCK(n) and FREE_DTOA_LOCK(n) for n >= 2. */

#define dtoa_get_threadno omp_get_thread_num
 static omp_lock_t Locks[2];

 void
init_dtoa_locks(void)
{
	int i;
	static int need_init = 1;

	if (need_init) {
		for(i = 0; i < 2; ++i)
			omp_init_lock(&Locks[i]);
		need_init = 0;
		}
	}

 void
ACQUIRE_DTOA_LOCK(unsigned int n)
{
	if (n < 2)
		omp_set_lock(&Locks[n]);
	}

 void
FREE_DTOA_LOCK(unsigned int n)
{
	if (n < 2)
		omp_unset_lock(&Locks[n]);
	}
#endif /*}*/

#ifndef MALLOC /*{{*/
#define MALLOC dtoamalloc
#include "dtoa.c" /* >= 19991215 */

#include "stdio1.h"

#ifdef __cplusplus
extern "C" {
#endif
#ifndef Stderr
extern FILE *Stderr;
#endif

 void *
dtoamalloc(size_t x)
{
	void *rv = malloc(x);
	if (!rv) {
		fprintf(Stderr, "\nmalloc failure in dtoa!\n");
		exit(63);
		}
	return rv;
	}
#ifdef __cplusplus
	}
#endif
#else /*}{MALLOC*/
#include "dtoa.c"
#endif /*}}MALLOC*/

#else /*}{*/

#ifdef __cplusplus
extern "C" {
#endif

extern char *ecvt(double value, int ndigit, int *decpt, int *sign);
extern char *fcvt(double value, int ndigit, int *decpt, int *sign);

 char *
dtoa_r(double d, int mode, int ndigits, int *decpt, int *sign, char **rve, char *s0, size_t s0len)
{
	char *s, *s1, *s2;

	if (!d) {
		*decpt = 1;
		*s0 = "0";
		if (rve)
			*rve = s0 + 1;
		return s0;
		}
	if (mode > 3)
		mode = 2 + (mode & 1);
	if (mode == 3)
		s = fcvt(d, ndigits, decpt, sign);
	else {
		if (mode <= 1)
			ndigits = 15;
		s = ecvt(d, ndigits, decpt, sign);
		}
	for(s1 = s; *s1; ++s1);
	if (s0len <= s1 - s)
		return 0;
	s2 = s0;
	if ((*s2 = *(s1 = s))) {
		while((*++s2 = *++s1));
		while(--s1 > s && *s1 == '0')
			--s2;
		*s2 = 0;
		}
	if (rve)
		*rve = s2;
	return s0;
	}
#ifdef __cplusplus
}
#endif
#endif /*}}*/
/* 20070913:  dtoa.c INFNAN_CHECK := default */
/* 20180411:  sync with /netlib/fp */