Improve name: "count-one-bits" is better than "popcount".
[gnulib.git] / tests / test-ldexpl.c
1 /* Test of multiplying a 'long double' by a power of 2.
2    Copyright (C) 2007 Free Software Foundation, Inc.
3
4    This program is free software; you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation; either version 2, or (at your option)
7    any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program; if not, write to the Free Software Foundation,
16    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
17
18 /* Written by Bruno Haible <bruno@clisp.org>, 2007.  */
19
20 #include <config.h>
21
22 #include <math.h>
23
24 #include <float.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27
28 #include "fpucw.h"
29 #include "isnanl-nolibm.h"
30
31 #define ASSERT(expr) \
32   do                                                                         \
33     {                                                                        \
34       if (!(expr))                                                           \
35         {                                                                    \
36           fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
37           abort ();                                                          \
38         }                                                                    \
39     }                                                                        \
40   while (0)
41
42 int
43 main ()
44 {
45   int i;
46   long double x;
47   long double y;
48   DECL_LONG_DOUBLE_ROUNDING
49
50   BEGIN_LONG_DOUBLE_ROUNDING ();
51
52   { /* NaN.  */
53     x = 0.0L / 0.0L;
54     y = ldexpl (x, 0); ASSERT (isnanl (y));
55     y = ldexpl (x, 5); ASSERT (isnanl (y));
56     y = ldexpl (x, -5); ASSERT (isnanl (y));
57   }
58
59   { /* Positive infinity.  */
60     x = 1.0L / 0.0L;
61     y = ldexpl (x, 0); ASSERT (y == x);
62     y = ldexpl (x, 5); ASSERT (y == x);
63     y = ldexpl (x, -5); ASSERT (y == x);
64   }
65
66   { /* Negative infinity.  */
67     x = -1.0L / 0.0L;
68     y = ldexpl (x, 0); ASSERT (y == x);
69     y = ldexpl (x, 5); ASSERT (y == x);
70     y = ldexpl (x, -5); ASSERT (y == x);
71   }
72
73   { /* Positive zero.  */
74     x = 0.0L;
75     y = ldexpl (x, 0); ASSERT (y == x); ASSERT (!signbit (x));
76     y = ldexpl (x, 5); ASSERT (y == x); ASSERT (!signbit (x));
77     y = ldexpl (x, -5); ASSERT (y == x); ASSERT (!signbit (x));
78   }
79
80   { /* Negative zero.  */
81     x = -0.0L;
82     y = ldexpl (x, 0); ASSERT (y == x); ASSERT (signbit (x));
83     y = ldexpl (x, 5); ASSERT (y == x); ASSERT (signbit (x));
84     y = ldexpl (x, -5); ASSERT (y == x); ASSERT (signbit (x));
85   }
86
87   { /* Positive finite number.  */
88     x = 1.73205L;
89     y = ldexpl (x, 0); ASSERT (y == x);
90     y = ldexpl (x, 5); ASSERT (y == x * 32.0L);
91     y = ldexpl (x, -5); ASSERT (y == x * 0.03125L);
92   }
93
94   { /* Negative finite number.  */
95     x = -20.085536923187667742L;
96     y = ldexpl (x, 0); ASSERT (y == x);
97     y = ldexpl (x, 5); ASSERT (y == x * 32.0L);
98     y = ldexpl (x, -5); ASSERT (y == x * 0.03125L);
99   }
100
101   for (i = 1, x = 1.73205L; i <= LDBL_MAX_EXP; i++, x *= 2.0L)
102     {
103       y = ldexpl (x, 0); ASSERT (y == x);
104       y = ldexpl (x, 5); ASSERT (y == x * 32.0L);
105       y = ldexpl (x, -5); ASSERT (y == x * 0.03125L);
106     }
107   for (i = 1, x = 1.73205L; i >= LDBL_MIN_EXP; i--, x *= 0.5L)
108     {
109       y = ldexpl (x, 0); ASSERT (y == x);
110       y = ldexpl (x, 5); ASSERT (y == x * 32.0L);
111       if (i - 5 >= LDBL_MIN_EXP)
112         {
113           y = ldexpl (x, -5); ASSERT (y == x * 0.03125L);
114         }
115     }
116   for (; i >= LDBL_MIN_EXP - 100 && x > 0.0L; i--, x *= 0.5L)
117     {
118       y = ldexpl (x, 0); ASSERT (y == x);
119       y = ldexpl (x, 5); ASSERT (y == x * 32.0L);
120     }
121
122   return 0;
123 }