Commit | Line | Data |
---|---|---|
c906108c SS |
1 | /* varargs.c - |
2 | * (Added as part of fix for bug 15306 - "call" to varargs functions fails) | |
3 | * This program is intended to let me try out "call" to varargs functions | |
4 | * with varying numbers of declared args and various argument types. | |
5 | * - RT 9/27/95 | |
6 | */ | |
7 | ||
8 | #include <stdio.h> | |
9 | #include <stdarg.h> | |
10 | ||
dedad4e3 PA |
11 | #include "../lib/unbuffer_output.c" |
12 | ||
c906108c SS |
13 | int find_max1(int, ...); |
14 | int find_max2(int, int, ...); | |
15 | double find_max_double(int, double, ...); | |
16 | ||
17 | char ch; | |
18 | unsigned char uc; | |
19 | short s; | |
20 | unsigned short us; | |
21 | int a,b,c,d; | |
22 | int max_val; | |
085dd6e6 | 23 | long long ll; |
c906108c SS |
24 | float fa,fb,fc,fd; |
25 | double da,db,dc,dd; | |
26 | double dmax_val; | |
27 | ||
6680506e YQ |
28 | #ifdef TEST_COMPLEX |
29 | extern float crealf (float _Complex); | |
30 | extern double creal (double _Complex); | |
31 | extern long double creall (long double _Complex); | |
32 | ||
33 | float _Complex fc1 = 1.0F + 1.0iF; | |
34 | float _Complex fc2 = 2.0F + 2.0iF; | |
35 | float _Complex fc3 = 3.0F + 3.0iF; | |
36 | float _Complex fc4 = 4.0F + 4.0iF; | |
37 | ||
38 | double _Complex dc1 = 1.0 + 1.0i; | |
39 | double _Complex dc2 = 2.0 + 2.0i; | |
40 | double _Complex dc3 = 3.0 + 3.0i; | |
41 | double _Complex dc4 = 4.0 + 4.0i; | |
42 | ||
43 | long double _Complex ldc1 = 1.0L + 1.0Li; | |
44 | long double _Complex ldc2 = 2.0L + 2.0Li; | |
45 | long double _Complex ldc3 = 3.0L + 3.0Li; | |
46 | long double _Complex ldc4 = 4.0L + 4.0Li; | |
47 | ||
48 | #endif | |
49 | ||
dedad4e3 PA |
50 | int |
51 | test (void) | |
52 | { | |
c906108c SS |
53 | c = -1; |
54 | uc = 1; | |
55 | s = -2; | |
56 | us = 2; | |
57 | a = 1; | |
58 | b = 60; | |
59 | max_val = find_max1(1, 60); | |
60 | max_val = find_max1(a, b); | |
61 | a = 3; | |
62 | b = 1; | |
63 | c = 4; | |
64 | d = 2; | |
65 | max_val = find_max1(3, 1, 4, 2); | |
66 | max_val = find_max2(a, b, c, d); | |
67 | da = 3.0; | |
68 | db = 1.0; | |
69 | dc = 4.0; | |
70 | dd = 2.0; | |
71 | dmax_val = find_max_double(3, 1.0, 4.0, 2.0); | |
72 | dmax_val = find_max_double(a, db, dc, dd); | |
085dd6e6 JM |
73 | |
74 | return 0; | |
c906108c SS |
75 | } |
76 | ||
dedad4e3 PA |
77 | int |
78 | main (void) | |
79 | { | |
80 | gdb_unbuffer_output (); | |
81 | test (); | |
82 | } | |
83 | ||
c906108c SS |
84 | /* Integer varargs, 1 declared arg */ |
85 | ||
86 | int find_max1(int num_vals, ...) { | |
87 | int max_val = 0; | |
88 | int x; | |
89 | int i; | |
90 | va_list argp; | |
91 | va_start(argp, num_vals); | |
92 | printf("find_max(%d,", num_vals); | |
93 | for (i = 0; i < num_vals; i++) { | |
94 | x = va_arg(argp, int); | |
95 | if (max_val < x) max_val = x; | |
96 | if (i < num_vals - 1) | |
97 | printf(" %d,", x); | |
98 | else | |
99 | printf(" %d)", x); | |
100 | } | |
101 | printf(" returns %d\n", max_val); | |
102 | return max_val; | |
103 | } | |
104 | ||
105 | /* Integer varargs, 2 declared args */ | |
106 | ||
107 | int find_max2(int num_vals, int first_val, ...) { | |
108 | int max_val = 0; | |
109 | int x; | |
110 | int i; | |
111 | va_list argp; | |
112 | va_start(argp, first_val); | |
113 | x = first_val; | |
114 | if (max_val < x) max_val = x; | |
115 | printf("find_max(%d, %d", num_vals, first_val); | |
116 | for (i = 1; i < num_vals; i++) { | |
117 | x = va_arg(argp, int); | |
118 | if (max_val < x) max_val = x; | |
119 | printf(", %d", x); | |
120 | } | |
121 | printf(") returns %d\n", max_val); | |
122 | return max_val; | |
123 | } | |
124 | ||
125 | /* Double-float varargs, 2 declared args */ | |
126 | ||
127 | double find_max_double(int num_vals, double first_val, ...) { | |
128 | double max_val = 0; | |
129 | double x; | |
130 | int i; | |
131 | va_list argp; | |
132 | va_start(argp, first_val); | |
133 | x = first_val; | |
134 | if (max_val < x) max_val = x; | |
d8f92b1f | 135 | printf("find_max(%d, %f", num_vals, first_val); |
c906108c SS |
136 | for (i = 1; i < num_vals; i++) { |
137 | x = va_arg(argp, double); | |
138 | if (max_val < x) max_val = x; | |
139 | printf(", %f", x); | |
140 | } | |
141 | printf(") returns %f\n", max_val); | |
142 | return max_val; | |
143 | } | |
144 | ||
6680506e YQ |
145 | |
146 | #ifdef TEST_COMPLEX | |
147 | float _Complex | |
148 | find_max_float_real (int num_vals, ...) | |
149 | { | |
150 | float _Complex max = 0.0F + 0.0iF; | |
151 | float _Complex x; | |
152 | va_list argp; | |
153 | int i; | |
154 | ||
155 | va_start(argp, num_vals); | |
156 | for (i = 0; i < num_vals; i++) | |
157 | { | |
158 | x = va_arg (argp, float _Complex); | |
159 | if (crealf (max) < crealf (x)) max = x; | |
160 | } | |
161 | ||
162 | return max; | |
163 | } | |
164 | ||
165 | double _Complex | |
166 | find_max_double_real (int num_vals, ...) | |
167 | { | |
168 | double _Complex max = 0.0 + 0.0i; | |
169 | double _Complex x; | |
170 | va_list argp; | |
171 | int i; | |
172 | ||
173 | va_start(argp, num_vals); | |
174 | for (i = 0; i < num_vals; i++) | |
175 | { | |
176 | x = va_arg (argp, double _Complex); | |
177 | if (creal (max) < creal (x)) max = x; | |
178 | } | |
179 | ||
180 | return max; | |
181 | } | |
182 | ||
183 | long double _Complex | |
184 | find_max_long_double_real (int num_vals, ...) | |
185 | { | |
186 | long double _Complex max = 0.0L + 0.0iL; | |
187 | long double _Complex x; | |
188 | va_list argp; | |
189 | int i; | |
190 | ||
191 | va_start(argp, num_vals); | |
192 | for (i = 0; i < num_vals; i++) | |
193 | { | |
194 | x = va_arg (argp, long double _Complex); | |
195 | if (creall (max) < creal (x)) max = x; | |
196 | } | |
197 | ||
198 | return max; | |
199 | } | |
200 | ||
201 | ||
202 | #endif /* TEST_COMPLEX */ |