1136387Sstefanf/*-
2136387Sstefanf * Copyright (c) 2004 Stefan Farfeleder
3136387Sstefanf * All rights reserved.
4136387Sstefanf *
5136387Sstefanf * Redistribution and use in source and binary forms, with or without
6136387Sstefanf * modification, are permitted provided that the following conditions
7136387Sstefanf * are met:
8136387Sstefanf * 1. Redistributions of source code must retain the above copyright
9136387Sstefanf *    notice, this list of conditions and the following disclaimer.
10136387Sstefanf * 2. Redistributions in binary form must reproduce the above copyright
11136387Sstefanf *    notice, this list of conditions and the following disclaimer in the
12136387Sstefanf *    documentation and/or other materials provided with the distribution.
13136387Sstefanf *
14136387Sstefanf * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15136387Sstefanf * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16136387Sstefanf * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17136387Sstefanf * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18136387Sstefanf * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19136387Sstefanf * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20136387Sstefanf * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21136387Sstefanf * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22136387Sstefanf * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23136387Sstefanf * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24136387Sstefanf * SUCH DAMAGE.
25136387Sstefanf *
26136387Sstefanf * $FreeBSD$
27136387Sstefanf */
28136387Sstefanf
29136387Sstefanf#include <assert.h>
30136387Sstefanf#include <float.h>
31136387Sstefanf#include <limits.h>
32136387Sstefanf#include <math.h>
33136387Sstefanf#include <stdio.h>
34136387Sstefanf#include <stdlib.h>
35136387Sstefanf
36136387Sstefanfint
37136387Sstefanfmain(void)
38136387Sstefanf{
39136387Sstefanf	char buf[128], *end;
40136387Sstefanf	double d;
41136387Sstefanf	float f;
42136387Sstefanf	long double ld;
43136387Sstefanf	int e, i;
44136387Sstefanf
45137587Snik	printf("1..3\n");
46136387Sstefanf	assert(ilogb(0) == FP_ILOGB0);
47136387Sstefanf	assert(ilogb(NAN) == FP_ILOGBNAN);
48136387Sstefanf	assert(ilogb(INFINITY) == INT_MAX);
49136387Sstefanf	for (e = DBL_MIN_EXP - DBL_MANT_DIG; e < DBL_MAX_EXP; e++) {
50136387Sstefanf		snprintf(buf, sizeof(buf), "0x1.p%d", e);
51136387Sstefanf		d = strtod(buf, &end);
52136387Sstefanf		assert(*end == '\0');
53136387Sstefanf		i = ilogb(d);
54136387Sstefanf		assert(i == e);
55136387Sstefanf	}
56137587Snik	printf("ok 1 - ilogb\n");
57136387Sstefanf
58136387Sstefanf	assert(ilogbf(0) == FP_ILOGB0);
59136387Sstefanf	assert(ilogbf(NAN) == FP_ILOGBNAN);
60136387Sstefanf	assert(ilogbf(INFINITY) == INT_MAX);
61136387Sstefanf	for (e = FLT_MIN_EXP - FLT_MANT_DIG; e < FLT_MAX_EXP; e++) {
62136387Sstefanf		snprintf(buf, sizeof(buf), "0x1.p%d", e);
63136387Sstefanf		f = strtof(buf, &end);
64136387Sstefanf		assert(*end == '\0');
65136387Sstefanf		i = ilogbf(f);
66136387Sstefanf		assert(i == e);
67136387Sstefanf	}
68137587Snik	printf("ok 2 - ilogbf\n");
69136387Sstefanf
70136387Sstefanf	assert(ilogbl(0) == FP_ILOGB0);
71136387Sstefanf	assert(ilogbl(NAN) == FP_ILOGBNAN);
72136387Sstefanf	assert(ilogbl(INFINITY) == INT_MAX);
73136387Sstefanf	for (e = LDBL_MIN_EXP - LDBL_MANT_DIG; e < LDBL_MAX_EXP; e++) {
74136387Sstefanf		snprintf(buf, sizeof(buf), "0x1.p%d", e);
75136387Sstefanf		ld = strtold(buf, &end);
76136387Sstefanf		assert(*end == '\0');
77136387Sstefanf		i = ilogbl(ld);
78136387Sstefanf		assert(i == e);
79136387Sstefanf	}
80137587Snik	printf("ok 3 - ilogbl\n");
81136387Sstefanf
82136387Sstefanf	return (0);
83136387Sstefanf}
84