1570af302Sopenharmony_ci// commit: f657fe4b9f734d7fdea515af8dffbf7c28ce4fbc 2013-09-05
2570af302Sopenharmony_ci// classify invalid x86 ld80 representations (this is ub, we follow the fpu)
3570af302Sopenharmony_ci// test printf("%La") as well
4570af302Sopenharmony_ci#include <math.h>
5570af302Sopenharmony_ci#include <float.h>
6570af302Sopenharmony_ci#include <stdint.h>
7570af302Sopenharmony_ci#include <stdio.h>
8570af302Sopenharmony_ci#include <string.h>
9570af302Sopenharmony_ci#include "test.h"
10570af302Sopenharmony_ci
11570af302Sopenharmony_ci#if LDBL_MANT_DIG==64
12570af302Sopenharmony_cistatic char *strclass(int c)
13570af302Sopenharmony_ci{
14570af302Sopenharmony_ci#define C(n) case n: return #n;
15570af302Sopenharmony_ci	switch (c) {
16570af302Sopenharmony_ci	C(FP_NAN)
17570af302Sopenharmony_ci	C(FP_INFINITE)
18570af302Sopenharmony_ci	C(FP_ZERO)
19570af302Sopenharmony_ci	C(FP_SUBNORMAL)
20570af302Sopenharmony_ci	C(FP_NORMAL)
21570af302Sopenharmony_ci	}
22570af302Sopenharmony_ci	return "invalid";
23570af302Sopenharmony_ci}
24570af302Sopenharmony_ci
25570af302Sopenharmony_ci#define T(f, desc, c, cwant, s, swant) do{ \
26570af302Sopenharmony_ci	c = fpclassify(f); \
27570af302Sopenharmony_ci	if (c != cwant) \
28570af302Sopenharmony_ci		t_error("fpclassify(%s) failed: got %s want %s\n", desc, strclass(c), #cwant); \
29570af302Sopenharmony_ci	memset(s, 0, sizeof(s)); \
30570af302Sopenharmony_ci	if (snprintf(s, sizeof(s), "%La", f) >= sizeof(s)) \
31570af302Sopenharmony_ci		t_error("snprintf(\"%%La\", %s) failed with invalid return value\n", desc); \
32570af302Sopenharmony_ci	if (strcmp(s,swant) != 0) \
33570af302Sopenharmony_ci		t_error("snprintf(\"%%La\", %s) failed: got \"%.*s\" want %s\n", desc, sizeof(s), s, #swant); \
34570af302Sopenharmony_ci}while(0)
35570af302Sopenharmony_ci
36570af302Sopenharmony_ciint main(void)
37570af302Sopenharmony_ci{
38570af302Sopenharmony_ci	union {
39570af302Sopenharmony_ci		long double f;
40570af302Sopenharmony_ci		struct {
41570af302Sopenharmony_ci			uint64_t m;
42570af302Sopenharmony_ci			uint16_t se;
43570af302Sopenharmony_ci		} i;
44570af302Sopenharmony_ci	} u;
45570af302Sopenharmony_ci	int c;
46570af302Sopenharmony_ci	int r;
47570af302Sopenharmony_ci	char s[32];
48570af302Sopenharmony_ci
49570af302Sopenharmony_ci	u.f = 0;
50570af302Sopenharmony_ci	u.i.m = (uint64_t)1<<63;
51570af302Sopenharmony_ci	T(u.f, "zero with msb set", c, FP_NORMAL, s, "0x1p-16382");
52570af302Sopenharmony_ci	u.i.m++;
53570af302Sopenharmony_ci	T(u.f, "subnormal with msb set", c, FP_NORMAL, s, "0x1.0000000000000002p-16382");
54570af302Sopenharmony_ci	u.f=1;
55570af302Sopenharmony_ci	u.i.m=0;
56570af302Sopenharmony_ci	T(u.f, "normal with msb unset", c, FP_NAN, s, "nan");
57570af302Sopenharmony_ci	u.f=INFINITY;
58570af302Sopenharmony_ci	u.i.m=0;
59570af302Sopenharmony_ci	T(u.f, "infinity with msb unset", c, FP_NAN, s, "nan");
60570af302Sopenharmony_ci	u.f=NAN;
61570af302Sopenharmony_ci	u.i.m&=(uint64_t)-1/2;
62570af302Sopenharmony_ci	T(u.f, "nan with msb unset", c, FP_NAN, s, "nan");
63570af302Sopenharmony_ci	return t_status;
64570af302Sopenharmony_ci}
65570af302Sopenharmony_ci#else
66570af302Sopenharmony_ciint main(void)
67570af302Sopenharmony_ci{
68570af302Sopenharmony_ci	return 0;
69570af302Sopenharmony_ci}
70570af302Sopenharmony_ci#endif
71