1240dbabfSDavid Schultz /*-
2*4d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause
3d915a14eSPedro F. Giffuni *
4240dbabfSDavid Schultz * Copyright (c) 2004 David Schultz <das@FreeBSD.ORG>
5240dbabfSDavid Schultz * All rights reserved.
6240dbabfSDavid Schultz *
7240dbabfSDavid Schultz * Redistribution and use in source and binary forms, with or without
8240dbabfSDavid Schultz * modification, are permitted provided that the following conditions
9240dbabfSDavid Schultz * are met:
10240dbabfSDavid Schultz * 1. Redistributions of source code must retain the above copyright
11240dbabfSDavid Schultz * notice, this list of conditions and the following disclaimer.
12240dbabfSDavid Schultz * 2. Redistributions in binary form must reproduce the above copyright
13240dbabfSDavid Schultz * notice, this list of conditions and the following disclaimer in the
14240dbabfSDavid Schultz * documentation and/or other materials provided with the distribution.
15240dbabfSDavid Schultz *
16240dbabfSDavid Schultz * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17240dbabfSDavid Schultz * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18240dbabfSDavid Schultz * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19240dbabfSDavid Schultz * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20240dbabfSDavid Schultz * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21240dbabfSDavid Schultz * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22240dbabfSDavid Schultz * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23240dbabfSDavid Schultz * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24240dbabfSDavid Schultz * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25240dbabfSDavid Schultz * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26240dbabfSDavid Schultz * SUCH DAMAGE.
27240dbabfSDavid Schultz */
28240dbabfSDavid Schultz
29240dbabfSDavid Schultz #include <math.h>
30240dbabfSDavid Schultz
31240dbabfSDavid Schultz #include "fpmath.h"
32240dbabfSDavid Schultz
33240dbabfSDavid Schultz /*
34240dbabfSDavid Schultz * XXX These routines belong in libm, but they must remain in libc for
35240dbabfSDavid Schultz * binary compat until we can bump libm's major version number.
3634a49712SDimitry Andric *
3734a49712SDimitry Andric * Note this only applies to the dynamic versions of libm and libc, so
3834a49712SDimitry Andric * for the static and profiled versions we stub out the definitions.
3934a49712SDimitry Andric * Otherwise you cannot link statically to libm and libc at the same
4034a49712SDimitry Andric * time, when calling both functions.
41240dbabfSDavid Schultz */
42240dbabfSDavid Schultz
43294246bbSEd Maste #ifdef PIC
44f68ff1acSDimitry Andric /*
45f68ff1acSDimitry Andric * Because math.h defines __isnan and __isnanf as aliases for compatibility with
46f68ff1acSDimitry Andric * glibc and CUDA, we have to undefine them here to avoid redefinition errors.
47f68ff1acSDimitry Andric */
48f68ff1acSDimitry Andric #undef __isnan
49f68ff1acSDimitry Andric #undef __isnanf
50f68ff1acSDimitry Andric
51240dbabfSDavid Schultz __weak_reference(__isnan, isnan);
52240dbabfSDavid Schultz __weak_reference(__isnanf, isnanf);
53240dbabfSDavid Schultz
54240dbabfSDavid Schultz int
__isnan(double d)55240dbabfSDavid Schultz __isnan(double d)
56240dbabfSDavid Schultz {
57240dbabfSDavid Schultz union IEEEd2bits u;
58240dbabfSDavid Schultz
59240dbabfSDavid Schultz u.d = d;
60240dbabfSDavid Schultz return (u.bits.exp == 2047 && (u.bits.manl != 0 || u.bits.manh != 0));
61240dbabfSDavid Schultz }
62240dbabfSDavid Schultz
63240dbabfSDavid Schultz int
__isnanf(float f)64240dbabfSDavid Schultz __isnanf(float f)
65240dbabfSDavid Schultz {
66240dbabfSDavid Schultz union IEEEf2bits u;
67240dbabfSDavid Schultz
68240dbabfSDavid Schultz u.f = f;
69240dbabfSDavid Schultz return (u.bits.exp == 255 && u.bits.man != 0);
70240dbabfSDavid Schultz }
71294246bbSEd Maste #endif /* PIC */
72