1/*
2 * Copyright (C) Bull S.A. 2001
3 * Copyright (c) International Business Machines  Corp., 2001
4 *
5 *   This program is free software;  you can redistribute it and/or modify
6 *   it under the terms of the GNU General Public License as published by
7 *   the Free Software Foundation; either version 2 of the License, or
8 *   (at your option) any later version.
9 *
10 *   This program is distributed in the hope that it will be useful,
11 *   but WITHOUT ANY WARRANTY;  without even the implied warranty of
12 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
13 *   the GNU General Public License for more details.
14 *
15 *   You should have received a copy of the GNU General Public License
16 *   along with this program;  if not, write to the Free Software
17 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20/******************************************************************************/
21/*                                                                            */
22/* Dec-03-2001  Created: Jacky Malcles & Jean Noel Cordenner                  */
23/*              These tests are adapted from AIX float PVT tests.             */
24/*                                                                            */
25/******************************************************************************/
26#include 	<float.h>
27#include 	<stdio.h>
28#include 	<stdlib.h>
29#include 	<string.h>
30#include 	<errno.h>
31#include        <limits.h>
32#include        <unistd.h>
33#include        <fcntl.h>
34#include        <errno.h>
35#include        <sys/signal.h>
36#include        <math.h>
37
38/******************************************************************
39 *
40 *	genldexp
41 *
42 * generate input and output file  for the ldexp function
43 * double x multiplied by 2 raised to the power y
44 *
45 *
46 */
47
48static int create_Result_file(void)
49{
50
51	int i, nbVal, tabInpi[20000];
52	double tabR[20000], tabInpd[20000];
53	char *F_name;
54	char *F_namini;
55	char *F_namind;
56	int fp, fpi, fpd;
57
58	F_name = "ldexp_out.ref";
59	F_namini = "ildexp_inp.ref";
60	F_namind = "ldexp_inp.ref";
61	nbVal = 20000;
62
63	fpi = open(F_namini, O_RDONLY, 0777);
64	fpd = open(F_namind, O_RDONLY, 0777);
65
66	if (!fpi || !fpd) {
67		printf("error opening file");
68		close(fpi);
69		close(fpd);
70		return -1;
71	} else {
72		for (i = 0; i < nbVal; i++) {
73			read(fpi, &(tabInpi[i]), sizeof(int));
74			read(fpd, &(tabInpd[i]), sizeof(double));
75			tabR[i] = ldexp(tabInpd[i], tabInpi[i]);
76		}
77		close(fpi);
78		close(fpd);
79
80		fp = open(F_name, O_RDWR | O_CREAT | O_TRUNC, 0777);
81		if (!fp) {
82			printf("error opening file");
83			close(fp);
84			return -1;
85		} else {
86			for (i = 0; i < nbVal; i++) {
87				write(fp, &tabR[i], sizeof(double));
88			}
89
90			close(fp);
91			return 0;
92		}
93	}
94}
95
96/*********************************************************************
97 *
98 *	create input data file
99 *
100 *	the format of the data is double x   int y
101 */
102
103static int create_Data_file(void)
104{
105	int i, nbVal;
106	double tabDD[20000], tabDI[20000], Inc;
107	char *F_named, *F_namei;
108	int fp, fpi;
109
110	F_named = "ldexp_inp.ref";
111	F_namei = "ildexp_inp.ref";
112	nbVal = 20000;
113
114	Inc = exp(1) / 10;
115
116	for (i = 0; i < (nbVal); i++) {
117		tabDD[i] = (Inc * i) + Inc;
118		tabDI[i] = nbVal - i;
119	}
120
121	fp = open(F_named, O_RDWR | O_CREAT | O_TRUNC, 0777);
122	fpi = open(F_namei, O_RDWR | O_CREAT | O_TRUNC, 0777);
123	if (!fp || !fpi) {
124		printf("error opening file");
125		close(fp);
126		close(fpi);
127		return -1;
128	} else {
129		for (i = 0; i < nbVal; i++) {
130			write(fp, &tabDD[i], sizeof(double));
131			write(fpi, &tabDI[i], sizeof(int));
132		}
133		close(fp);
134		close(fpi);
135		return 0;
136	}
137}
138
139int main(int argc, char *argv[])
140{
141
142	if (argc > 1) {
143		switch (atoi(argv[1])) {
144		case 1:
145			if (create_Data_file() == 0)
146				printf("Data file created\n");
147			else
148				printf("problem during %s data file creation\n",
149				       argv[0]);
150			break;
151
152		case 2:
153			if (create_Result_file() == 0)
154				printf("Result file created\n");
155			else
156				printf
157				    ("problem during %s result file creation\n",
158				     argv[0]);
159			break;
160		default:
161			printf("Bad arglist code for: '%s'\n", argv[0]);
162			return -1;
163			break;
164		}
165	} else {
166		if (create_Data_file() != 0)
167			printf("problem during %s data file creation\n",
168			       argv[0]);
169		if (create_Result_file() != 0)
170			printf("problem during %s result file creation\n",
171			       argv[0]);
172	}
173
174	return (0);
175
176}
177