xref: /minix/lib/libm/src/s_copysignl.c (revision 0a6a1f1d)
1*0a6a1f1dSLionel Sambuc /*	$NetBSD: s_copysignl.c,v 1.5 2015/05/14 19:26:12 joerg Exp $	*/
22fe8fb19SBen Gras 
32fe8fb19SBen Gras /*-
42fe8fb19SBen Gras  * Copyright (c) 2010 The NetBSD Foundation, Inc.
52fe8fb19SBen Gras  * All rights reserved.
62fe8fb19SBen Gras  *
72fe8fb19SBen Gras  * Redistribution and use in source and binary forms, with or without
82fe8fb19SBen Gras  * modification, are permitted provided that the following conditions
92fe8fb19SBen Gras  * are met:
102fe8fb19SBen Gras  * 1. Redistributions of source code must retain the above copyright
112fe8fb19SBen Gras  *    notice, this list of conditions and the following disclaimer.
122fe8fb19SBen Gras  * 2. Redistributions in binary form must reproduce the above copyright
132fe8fb19SBen Gras  *    notice, this list of conditions and the following disclaimer in the
142fe8fb19SBen Gras  *    documentation and/or other materials provided with the distribution.
152fe8fb19SBen Gras  *
162fe8fb19SBen Gras  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
172fe8fb19SBen Gras  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
182fe8fb19SBen Gras  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
192fe8fb19SBen Gras  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
202fe8fb19SBen Gras  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
212fe8fb19SBen Gras  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
222fe8fb19SBen Gras  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
232fe8fb19SBen Gras  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
242fe8fb19SBen Gras  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
252fe8fb19SBen Gras  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
262fe8fb19SBen Gras  * POSSIBILITY OF SUCH DAMAGE.
272fe8fb19SBen Gras  */
282fe8fb19SBen Gras #include <sys/cdefs.h>
29*0a6a1f1dSLionel Sambuc __RCSID("$NetBSD: s_copysignl.c,v 1.5 2015/05/14 19:26:12 joerg Exp $");
30*0a6a1f1dSLionel Sambuc #include "namespace.h"
312fe8fb19SBen Gras 
322fe8fb19SBen Gras #include <math.h>
332fe8fb19SBen Gras #include <machine/ieee.h>
342fe8fb19SBen Gras 
35*0a6a1f1dSLionel Sambuc #if defined(__HAVE_LONG_DOUBLE) || defined(__HAVE_IBM_LONGDOUBLE)
36*0a6a1f1dSLionel Sambuc 
37*0a6a1f1dSLionel Sambuc #ifdef __weak_alias
__weak_alias(copysignl,_copysignl)38*0a6a1f1dSLionel Sambuc __weak_alias(copysignl, _copysignl)
39*0a6a1f1dSLionel Sambuc #endif
40*0a6a1f1dSLionel Sambuc 
412fe8fb19SBen Gras /*
422fe8fb19SBen Gras  * copysignl(long double x, long double y)
432fe8fb19SBen Gras  * This function returns a value with the magnitude of x and the sign of y.
442fe8fb19SBen Gras  */
452fe8fb19SBen Gras #ifdef EXT_EXP_INFNAN
462fe8fb19SBen Gras long double
472fe8fb19SBen Gras copysignl(long double x, long double y)
482fe8fb19SBen Gras {
492fe8fb19SBen Gras 	union ieee_ext_u ux, uy;
502fe8fb19SBen Gras 
512fe8fb19SBen Gras 	ux.extu_ld = x;
522fe8fb19SBen Gras 	uy.extu_ld = y;
532fe8fb19SBen Gras 
542fe8fb19SBen Gras 	ux.extu_ext.ext_sign = uy.extu_ext.ext_sign;
552fe8fb19SBen Gras 
562fe8fb19SBen Gras 	return (ux.extu_ld);
572fe8fb19SBen Gras }
58*0a6a1f1dSLionel Sambuc #elif defined(__HAVE_IBM_LONGDOUBLE)
59*0a6a1f1dSLionel Sambuc long double
60*0a6a1f1dSLionel Sambuc copysignl(long double x, long double y)
61*0a6a1f1dSLionel Sambuc {
62*0a6a1f1dSLionel Sambuc 	union ldbl_u ux, uy;
63*0a6a1f1dSLionel Sambuc 
64*0a6a1f1dSLionel Sambuc 	ux.ldblu_ld = x;
65*0a6a1f1dSLionel Sambuc 	uy.ldblu_ld = y;
66*0a6a1f1dSLionel Sambuc 	ux.ldblu_d[0] = copysign(ux.ldblu_d[0], uy.ldblu_d[0]);
67*0a6a1f1dSLionel Sambuc 	ux.ldblu_d[1] = copysign(ux.ldblu_d[1], uy.ldblu_d[1]);
68*0a6a1f1dSLionel Sambuc 
69*0a6a1f1dSLionel Sambuc 	return ux.ldblu_ld;
70*0a6a1f1dSLionel Sambuc }
712fe8fb19SBen Gras #endif
72*0a6a1f1dSLionel Sambuc #endif /* __HAVE_LONG_DOUBLE || __HAVE_IBM_LONGDOUBLE */
73