14e30c129SJesús Blázquez /*
24e30c129SJesús Blázquez * SPDX-License-Identifier: BSD-2-Clause
34e30c129SJesús Blázquez *
44e30c129SJesús Blázquez * Copyright (c) 2004 David Schultz <das@FreeBSD.ORG>
54e30c129SJesús Blázquez * All rights reserved.
64e30c129SJesús Blázquez *
74e30c129SJesús Blázquez * Redistribution and use in source and binary forms, with or without
84e30c129SJesús Blázquez * modification, are permitted provided that the following conditions
94e30c129SJesús Blázquez * are met:
104e30c129SJesús Blázquez * 1. Redistributions of source code must retain the above copyright
114e30c129SJesús Blázquez * notice, this list of conditions and the following disclaimer.
124e30c129SJesús Blázquez * 2. Redistributions in binary form must reproduce the above copyright
134e30c129SJesús Blázquez * notice, this list of conditions and the following disclaimer in the
144e30c129SJesús Blázquez * documentation and/or other materials provided with the distribution.
154e30c129SJesús Blázquez *
164e30c129SJesús Blázquez * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
174e30c129SJesús Blázquez * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
184e30c129SJesús Blázquez * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
194e30c129SJesús Blázquez * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
204e30c129SJesús Blázquez * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
214e30c129SJesús Blázquez * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
224e30c129SJesús Blázquez * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
234e30c129SJesús Blázquez * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
244e30c129SJesús Blázquez * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
254e30c129SJesús Blázquez * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
264e30c129SJesús Blázquez * SUCH DAMAGE.
274e30c129SJesús Blázquez */
284e30c129SJesús Blázquez
294e30c129SJesús Blázquez #include <math.h>
304e30c129SJesús Blázquez
314e30c129SJesús Blázquez #include "fpmath.h"
324e30c129SJesús Blázquez
334e30c129SJesús Blázquez #ifdef USE_BUILTIN_FMINIMUM_MAGF
344e30c129SJesús Blázquez float
fminimum_magf(float x,float y)354e30c129SJesús Blázquez fminimum_magf(float x, float y)
364e30c129SJesús Blázquez {
374e30c129SJesús Blázquez return (__builtin_fminimum_magf(x, y));
384e30c129SJesús Blázquez }
394e30c129SJesús Blázquez #else
404e30c129SJesús Blázquez float
fminimum_magf(float x,float y)414e30c129SJesús Blázquez fminimum_magf(float x, float y)
424e30c129SJesús Blázquez {
434e30c129SJesús Blázquez union IEEEf2bits u[2];
444e30c129SJesús Blázquez
454e30c129SJesús Blázquez u[0].f = x;
464e30c129SJesús Blázquez u[1].f = y;
474e30c129SJesús Blázquez
484e30c129SJesús Blázquez /* Handle NaN according to ISO/IEC 60559. NaN argument -> NaN return */
49*f6e1f27eSJesús Blázquez if (isnan(x) || isnan(y))
504e30c129SJesús Blázquez return (NAN);
514e30c129SJesús Blázquez
524e30c129SJesús Blázquez float ax = fabsf(x);
534e30c129SJesús Blázquez float ay = fabsf(y);
544e30c129SJesús Blázquez
554e30c129SJesús Blázquez if (ay < ax)
564e30c129SJesús Blázquez return (y);
574e30c129SJesús Blázquez if (ax < ay)
584e30c129SJesús Blázquez return (x);
594e30c129SJesús Blázquez
604e30c129SJesús Blázquez /* If magnitudes are equal, we break the tie with the sign */
614e30c129SJesús Blázquez if (u[0].bits.sign != u[1].bits.sign)
624e30c129SJesús Blázquez return (u[u[1].bits.sign].f);
634e30c129SJesús Blázquez
644e30c129SJesús Blázquez return (x);
654e30c129SJesús Blázquez }
664e30c129SJesús Blázquez #endif
674e30c129SJesús Blázquez
684e30c129SJesús Blázquez
69