Disable tests for strdup/strndup on __hpux__
[official-gcc.git] / gcc / ada / libgnat / a-ngcoty.adb
blob36b324b76b0e99c48053f1246e6f3231040e4d0e
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT RUN-TIME COMPONENTS --
4 -- --
5 -- A D A . N U M E R I C S . G E N E R I C _ C O M P L E X _ T Y P E S --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-2023, Free Software Foundation, Inc. --
10 -- --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. --
17 -- --
18 -- As a special exception under Section 7 of GPL version 3, you are granted --
19 -- additional permissions described in the GCC Runtime Library Exception, --
20 -- version 3.1, as published by the Free Software Foundation. --
21 -- --
22 -- You should have received a copy of the GNU General Public License and --
23 -- a copy of the GCC Runtime Library Exception along with this program; --
24 -- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see --
25 -- <http://www.gnu.org/licenses/>. --
26 -- --
27 -- GNAT was originally developed by the GNAT team at New York University. --
28 -- Extensive contributions were provided by Ada Core Technologies Inc. --
29 -- --
30 ------------------------------------------------------------------------------
32 with Ada.Numerics.Aux_Generic_Float;
34 package body Ada.Numerics.Generic_Complex_Types is
36 package Aux is new Ada.Numerics.Aux_Generic_Float (Real);
38 subtype R is Real'Base;
40 Two_Pi : constant R := R (2.0) * Pi;
41 Half_Pi : constant R := Pi / R (2.0);
43 ---------
44 -- "*" --
45 ---------
47 function "*" (Left, Right : Complex) return Complex is
49 Scale : constant R := R (R'Machine_Radix) ** ((R'Machine_Emax - 1) / 2);
50 -- In case of overflow, scale the operands by the largest power of the
51 -- radix (to avoid rounding error), so that the square of the scale does
52 -- not overflow itself.
54 X : R;
55 Y : R;
57 begin
58 X := Left.Re * Right.Re - Left.Im * Right.Im;
59 Y := Left.Re * Right.Im + Left.Im * Right.Re;
61 -- If either component overflows, try to scale (skip in fast math mode)
63 if not Standard'Fast_Math then
65 -- Note that the test below is written as a negation. This is to
66 -- account for the fact that X and Y may be NaNs, because both of
67 -- their operands could overflow. Given that all operations on NaNs
68 -- return false, the test can only be written thus.
70 if not (abs (X) <= R'Last) then
71 pragma Annotate
72 (CodePeer, Intentional,
73 "test always false", "test for infinity");
75 X := Scale**2 * ((Left.Re / Scale) * (Right.Re / Scale) -
76 (Left.Im / Scale) * (Right.Im / Scale));
77 end if;
79 if not (abs (Y) <= R'Last) then
80 pragma Annotate
81 (CodePeer, Intentional,
82 "test always false", "test for infinity");
84 Y := Scale**2 * ((Left.Re / Scale) * (Right.Im / Scale)
85 + (Left.Im / Scale) * (Right.Re / Scale));
86 end if;
87 end if;
89 return (X, Y);
90 end "*";
92 function "*" (Left, Right : Imaginary) return Real'Base is
93 begin
94 return -(R (Left) * R (Right));
95 end "*";
97 function "*" (Left : Complex; Right : Real'Base) return Complex is
98 begin
99 return Complex'(Left.Re * Right, Left.Im * Right);
100 end "*";
102 function "*" (Left : Real'Base; Right : Complex) return Complex is
103 begin
104 return (Left * Right.Re, Left * Right.Im);
105 end "*";
107 function "*" (Left : Complex; Right : Imaginary) return Complex is
108 begin
109 return Complex'(-(Left.Im * R (Right)), Left.Re * R (Right));
110 end "*";
112 function "*" (Left : Imaginary; Right : Complex) return Complex is
113 begin
114 return Complex'(-(R (Left) * Right.Im), R (Left) * Right.Re);
115 end "*";
117 function "*" (Left : Imaginary; Right : Real'Base) return Imaginary is
118 begin
119 return Left * Imaginary (Right);
120 end "*";
122 function "*" (Left : Real'Base; Right : Imaginary) return Imaginary is
123 begin
124 return Imaginary (Left * R (Right));
125 end "*";
127 ----------
128 -- "**" --
129 ----------
131 function "**" (Left : Complex; Right : Integer) return Complex is
132 Result : Complex := (1.0, 0.0);
133 Factor : Complex := Left;
134 Exp : Integer := Right;
136 begin
137 -- We use the standard logarithmic approach, Exp gets shifted right
138 -- testing successive low order bits and Factor is the value of the
139 -- base raised to the next power of 2. For positive exponents we
140 -- multiply the result by this factor, for negative exponents, we
141 -- divide by this factor.
143 if Exp >= 0 then
145 -- For a positive exponent, if we get a constraint error during
146 -- this loop, it is an overflow, and the constraint error will
147 -- simply be passed on to the caller.
149 while Exp /= 0 loop
150 if Exp rem 2 /= 0 then
151 Result := Result * Factor;
152 end if;
154 Factor := Factor * Factor;
155 Exp := Exp / 2;
156 end loop;
158 return Result;
160 else -- Exp < 0 then
162 -- For the negative exponent case, a constraint error during this
163 -- calculation happens if Factor gets too large, and the proper
164 -- response is to return 0.0, since what we essentially have is
165 -- 1.0 / infinity, and the closest model number will be zero.
167 begin
168 while Exp /= 0 loop
169 if Exp rem 2 /= 0 then
170 Result := Result * Factor;
171 end if;
173 Factor := Factor * Factor;
174 Exp := Exp / 2;
175 end loop;
177 return R'(1.0) / Result;
179 exception
180 when Constraint_Error =>
181 return (0.0, 0.0);
182 end;
183 end if;
184 end "**";
186 function "**" (Left : Imaginary; Right : Integer) return Complex is
187 M : constant R := R (Left) ** Right;
188 begin
189 case Right mod 4 is
190 when 0 => return (M, 0.0);
191 when 1 => return (0.0, M);
192 when 2 => return (-M, 0.0);
193 when 3 => return (0.0, -M);
194 when others => raise Program_Error;
195 end case;
196 end "**";
198 ---------
199 -- "+" --
200 ---------
202 function "+" (Right : Complex) return Complex is
203 begin
204 return Right;
205 end "+";
207 function "+" (Left, Right : Complex) return Complex is
208 begin
209 return Complex'(Left.Re + Right.Re, Left.Im + Right.Im);
210 end "+";
212 function "+" (Right : Imaginary) return Imaginary is
213 begin
214 return Right;
215 end "+";
217 function "+" (Left, Right : Imaginary) return Imaginary is
218 begin
219 return Imaginary (R (Left) + R (Right));
220 end "+";
222 function "+" (Left : Complex; Right : Real'Base) return Complex is
223 begin
224 return Complex'(Left.Re + Right, Left.Im);
225 end "+";
227 function "+" (Left : Real'Base; Right : Complex) return Complex is
228 begin
229 return Complex'(Left + Right.Re, Right.Im);
230 end "+";
232 function "+" (Left : Complex; Right : Imaginary) return Complex is
233 begin
234 return Complex'(Left.Re, Left.Im + R (Right));
235 end "+";
237 function "+" (Left : Imaginary; Right : Complex) return Complex is
238 begin
239 return Complex'(Right.Re, R (Left) + Right.Im);
240 end "+";
242 function "+" (Left : Imaginary; Right : Real'Base) return Complex is
243 begin
244 return Complex'(Right, R (Left));
245 end "+";
247 function "+" (Left : Real'Base; Right : Imaginary) return Complex is
248 begin
249 return Complex'(Left, R (Right));
250 end "+";
252 ---------
253 -- "-" --
254 ---------
256 function "-" (Right : Complex) return Complex is
257 begin
258 return (-Right.Re, -Right.Im);
259 end "-";
261 function "-" (Left, Right : Complex) return Complex is
262 begin
263 return (Left.Re - Right.Re, Left.Im - Right.Im);
264 end "-";
266 function "-" (Right : Imaginary) return Imaginary is
267 begin
268 return Imaginary (-R (Right));
269 end "-";
271 function "-" (Left, Right : Imaginary) return Imaginary is
272 begin
273 return Imaginary (R (Left) - R (Right));
274 end "-";
276 function "-" (Left : Complex; Right : Real'Base) return Complex is
277 begin
278 return Complex'(Left.Re - Right, Left.Im);
279 end "-";
281 function "-" (Left : Real'Base; Right : Complex) return Complex is
282 begin
283 return Complex'(Left - Right.Re, -Right.Im);
284 end "-";
286 function "-" (Left : Complex; Right : Imaginary) return Complex is
287 begin
288 return Complex'(Left.Re, Left.Im - R (Right));
289 end "-";
291 function "-" (Left : Imaginary; Right : Complex) return Complex is
292 begin
293 return Complex'(-Right.Re, R (Left) - Right.Im);
294 end "-";
296 function "-" (Left : Imaginary; Right : Real'Base) return Complex is
297 begin
298 return Complex'(-Right, R (Left));
299 end "-";
301 function "-" (Left : Real'Base; Right : Imaginary) return Complex is
302 begin
303 return Complex'(Left, -R (Right));
304 end "-";
306 ---------
307 -- "/" --
308 ---------
310 function "/" (Left, Right : Complex) return Complex is
311 a : constant R := Left.Re;
312 b : constant R := Left.Im;
313 c : constant R := Right.Re;
314 d : constant R := Right.Im;
316 begin
317 if c = 0.0 and then d = 0.0 then
318 raise Constraint_Error;
319 else
320 return Complex'(Re => ((a * c) + (b * d)) / (c ** 2 + d ** 2),
321 Im => ((b * c) - (a * d)) / (c ** 2 + d ** 2));
322 end if;
323 end "/";
325 function "/" (Left, Right : Imaginary) return Real'Base is
326 begin
327 return R (Left) / R (Right);
328 end "/";
330 function "/" (Left : Complex; Right : Real'Base) return Complex is
331 begin
332 return Complex'(Left.Re / Right, Left.Im / Right);
333 end "/";
335 function "/" (Left : Real'Base; Right : Complex) return Complex is
336 a : constant R := Left;
337 c : constant R := Right.Re;
338 d : constant R := Right.Im;
339 begin
340 return Complex'(Re => (a * c) / (c ** 2 + d ** 2),
341 Im => -((a * d) / (c ** 2 + d ** 2)));
342 end "/";
344 function "/" (Left : Complex; Right : Imaginary) return Complex is
345 a : constant R := Left.Re;
346 b : constant R := Left.Im;
347 d : constant R := R (Right);
349 begin
350 return (b / d, -(a / d));
351 end "/";
353 function "/" (Left : Imaginary; Right : Complex) return Complex is
354 b : constant R := R (Left);
355 c : constant R := Right.Re;
356 d : constant R := Right.Im;
358 begin
359 return (Re => b * d / (c ** 2 + d ** 2),
360 Im => b * c / (c ** 2 + d ** 2));
361 end "/";
363 function "/" (Left : Imaginary; Right : Real'Base) return Imaginary is
364 begin
365 return Imaginary (R (Left) / Right);
366 end "/";
368 function "/" (Left : Real'Base; Right : Imaginary) return Imaginary is
369 begin
370 return Imaginary (-(Left / R (Right)));
371 end "/";
373 ---------
374 -- "<" --
375 ---------
377 function "<" (Left, Right : Imaginary) return Boolean is
378 begin
379 return R (Left) < R (Right);
380 end "<";
382 ----------
383 -- "<=" --
384 ----------
386 function "<=" (Left, Right : Imaginary) return Boolean is
387 begin
388 return R (Left) <= R (Right);
389 end "<=";
391 ---------
392 -- ">" --
393 ---------
395 function ">" (Left, Right : Imaginary) return Boolean is
396 begin
397 return R (Left) > R (Right);
398 end ">";
400 ----------
401 -- ">=" --
402 ----------
404 function ">=" (Left, Right : Imaginary) return Boolean is
405 begin
406 return R (Left) >= R (Right);
407 end ">=";
409 -----------
410 -- "abs" --
411 -----------
413 function "abs" (Right : Imaginary) return Real'Base is
414 begin
415 return abs R (Right);
416 end "abs";
418 --------------
419 -- Argument --
420 --------------
422 function Argument (X : Complex) return Real'Base is
423 a : constant R := X.Re;
424 b : constant R := X.Im;
425 arg : R;
427 begin
428 if b = 0.0 then
430 if a >= 0.0 then
431 return 0.0;
432 else
433 return R'Copy_Sign (Pi, b);
434 end if;
436 elsif a = 0.0 then
438 if b >= 0.0 then
439 return Half_Pi;
440 else
441 return -Half_Pi;
442 end if;
444 else
445 arg := Aux.Atan (abs (b / a));
447 if a > 0.0 then
448 if b > 0.0 then
449 return arg;
450 else -- b < 0.0
451 return -arg;
452 end if;
454 else -- a < 0.0
455 if b >= 0.0 then
456 return Pi - arg;
457 else -- b < 0.0
458 return -(Pi - arg);
459 end if;
460 end if;
461 end if;
463 exception
464 when Constraint_Error =>
465 if b > 0.0 then
466 return Half_Pi;
467 else
468 return -Half_Pi;
469 end if;
470 end Argument;
472 function Argument (X : Complex; Cycle : Real'Base) return Real'Base is
473 begin
474 if Cycle > 0.0 then
475 return Argument (X) * Cycle / Two_Pi;
476 else
477 raise Argument_Error;
478 end if;
479 end Argument;
481 ----------------------------
482 -- Compose_From_Cartesian --
483 ----------------------------
485 function Compose_From_Cartesian (Re, Im : Real'Base) return Complex is
486 begin
487 return (Re, Im);
488 end Compose_From_Cartesian;
490 function Compose_From_Cartesian (Re : Real'Base) return Complex is
491 begin
492 return (Re, 0.0);
493 end Compose_From_Cartesian;
495 function Compose_From_Cartesian (Im : Imaginary) return Complex is
496 begin
497 return (0.0, R (Im));
498 end Compose_From_Cartesian;
500 ------------------------
501 -- Compose_From_Polar --
502 ------------------------
504 function Compose_From_Polar (
505 Modulus, Argument : Real'Base)
506 return Complex
508 begin
509 if Modulus = 0.0 then
510 return (0.0, 0.0);
511 else
512 return (Modulus * Aux.Cos (Argument),
513 Modulus * Aux.Sin (Argument));
514 end if;
515 end Compose_From_Polar;
517 function Compose_From_Polar (
518 Modulus, Argument, Cycle : Real'Base)
519 return Complex
521 Arg : Real'Base;
523 begin
524 if Modulus = 0.0 then
525 return (0.0, 0.0);
527 elsif Cycle > 0.0 then
528 if Argument = 0.0 then
529 return (Modulus, 0.0);
531 elsif Argument = Cycle / 4.0 then
532 return (0.0, Modulus);
534 elsif Argument = Cycle / 2.0 then
535 return (-Modulus, 0.0);
537 elsif Argument = 3.0 * Cycle / R (4.0) then
538 return (0.0, -Modulus);
539 else
540 Arg := Two_Pi * Argument / Cycle;
541 return (Modulus * Aux.Cos (Arg),
542 Modulus * Aux.Sin (Arg));
543 end if;
544 else
545 raise Argument_Error;
546 end if;
547 end Compose_From_Polar;
549 ---------------
550 -- Conjugate --
551 ---------------
553 function Conjugate (X : Complex) return Complex is
554 begin
555 return Complex'(X.Re, -X.Im);
556 end Conjugate;
558 --------
559 -- Im --
560 --------
562 function Im (X : Complex) return Real'Base is
563 begin
564 return X.Im;
565 end Im;
567 function Im (X : Imaginary) return Real'Base is
568 begin
569 return R (X);
570 end Im;
572 -------------
573 -- Modulus --
574 -------------
576 function Modulus (X : Complex) return Real'Base is
577 Re2, Im2 : R;
579 begin
581 begin
582 Re2 := X.Re ** 2;
584 -- To compute (a**2 + b**2) ** (0.5) when a**2 may be out of bounds,
585 -- compute a * (1 + (b/a) **2) ** (0.5). On a machine where the
586 -- squaring does not raise constraint_error but generates infinity,
587 -- we can use an explicit comparison to determine whether to use
588 -- the scaling expression.
590 -- The scaling expression is computed in double format throughout
591 -- in order to prevent inaccuracies on machines where not all
592 -- immediate expressions are rounded, such as PowerPC.
594 -- ??? same weird test, why not Re2 > R'Last ???
595 if not (Re2 <= R'Last) then
596 raise Constraint_Error;
597 end if;
599 exception
600 when Constraint_Error =>
601 pragma Assert (X.Re /= 0.0);
602 return R (abs (X.Re))
603 * Aux.Sqrt (1.0 + (R (X.Im) / R (X.Re)) ** 2);
604 end;
606 begin
607 Im2 := X.Im ** 2;
609 -- ??? same weird test
610 if not (Im2 <= R'Last) then
611 raise Constraint_Error;
612 end if;
614 exception
615 when Constraint_Error =>
616 pragma Assert (X.Im /= 0.0);
617 return R (abs (X.Im))
618 * Aux.Sqrt (1.0 + (R (X.Re) / R (X.Im)) ** 2);
619 end;
621 -- Now deal with cases of underflow. If only one of the squares
622 -- underflows, return the modulus of the other component. If both
623 -- squares underflow, use scaling as above.
625 if Re2 = 0.0 then
627 if X.Re = 0.0 then
628 return abs (X.Im);
630 elsif Im2 = 0.0 then
632 if X.Im = 0.0 then
633 return abs (X.Re);
635 else
636 if abs (X.Re) > abs (X.Im) then
637 return R (abs (X.Re))
638 * Aux.Sqrt (1.0 + (R (X.Im) / R (X.Re)) ** 2);
639 else
640 return R (abs (X.Im))
641 * Aux.Sqrt (1.0 + (R (X.Re) / R (X.Im)) ** 2);
642 end if;
643 end if;
645 else
646 return abs (X.Im);
647 end if;
649 elsif Im2 = 0.0 then
650 return abs (X.Re);
652 -- In all other cases, the naive computation will do
654 else
655 return Aux.Sqrt (Re2 + Im2);
656 end if;
657 end Modulus;
659 --------
660 -- Re --
661 --------
663 function Re (X : Complex) return Real'Base is
664 begin
665 return X.Re;
666 end Re;
668 ------------
669 -- Set_Im --
670 ------------
672 procedure Set_Im (X : in out Complex; Im : Real'Base) is
673 begin
674 X.Im := Im;
675 end Set_Im;
677 procedure Set_Im (X : out Imaginary; Im : Real'Base) is
678 begin
679 X := Imaginary (Im);
680 end Set_Im;
682 ------------
683 -- Set_Re --
684 ------------
686 procedure Set_Re (X : in out Complex; Re : Real'Base) is
687 begin
688 X.Re := Re;
689 end Set_Re;
691 end Ada.Numerics.Generic_Complex_Types;