xref: /openbsd-src/lib/libm/src/s_fdim.c (revision 4a39ccd02c887d988c1a5398dd2142879056da5f)
1*4a39ccd0Sderaadt /*	$OpenBSD: s_fdim.c,v 1.5 2012/12/05 23:20:04 deraadt Exp $	*/
27b36286aSmartynas /*-
37b36286aSmartynas  * Copyright (c) 2004 David Schultz <das@FreeBSD.ORG>
47b36286aSmartynas  * All rights reserved.
57b36286aSmartynas  *
67b36286aSmartynas  * Redistribution and use in source and binary forms, with or without
77b36286aSmartynas  * modification, are permitted provided that the following conditions
87b36286aSmartynas  * are met:
97b36286aSmartynas  * 1. Redistributions of source code must retain the above copyright
107b36286aSmartynas  *    notice, this list of conditions and the following disclaimer.
117b36286aSmartynas  * 2. Redistributions in binary form must reproduce the above copyright
127b36286aSmartynas  *    notice, this list of conditions and the following disclaimer in the
137b36286aSmartynas  *    documentation and/or other materials provided with the distribution.
147b36286aSmartynas  *
157b36286aSmartynas  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
167b36286aSmartynas  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
177b36286aSmartynas  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
187b36286aSmartynas  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
197b36286aSmartynas  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
207b36286aSmartynas  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
217b36286aSmartynas  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
227b36286aSmartynas  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
237b36286aSmartynas  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
247b36286aSmartynas  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
257b36286aSmartynas  * SUCH DAMAGE.
267b36286aSmartynas  */
277b36286aSmartynas 
28390c8400Smartynas #include <float.h>
297b36286aSmartynas #include <math.h>
307b36286aSmartynas 
317b36286aSmartynas #define	DECL(type, fn)			\
327b36286aSmartynas type					\
337b36286aSmartynas fn(type x, type y)			\
347b36286aSmartynas {					\
357b36286aSmartynas 					\
367b36286aSmartynas 	if (isnan(x))			\
377b36286aSmartynas 		return (x);		\
387b36286aSmartynas 	if (isnan(y))			\
397b36286aSmartynas 		return (y);		\
407b36286aSmartynas 	return (x > y ? x - y : 0.0);	\
417b36286aSmartynas }
427b36286aSmartynas 
437b36286aSmartynas DECL(double, fdim)
447b36286aSmartynas DECL(float, fdimf)
458e30f2f1Skettenis DECL(long double, fdiml)
46