xref: /minix/lib/libc/gen/fixunstfdi_ieee754.c (revision f14fb602)
1*f14fb602SLionel Sambuc /*	$NetBSD: fixunstfdi_ieee754.c,v 1.1 2011/07/09 02:30:27 matt Exp $	*/
2*f14fb602SLionel Sambuc /*-
3*f14fb602SLionel Sambuc  * Copyright (c) 2011 The NetBSD Foundation, Inc.
4*f14fb602SLionel Sambuc  * All rights reserved.
5*f14fb602SLionel Sambuc  *
6*f14fb602SLionel Sambuc  * This code is derived from software contributed to The NetBSD Foundation
7*f14fb602SLionel Sambuc  * by Matt Thomas of 3am Software Foundry.
8*f14fb602SLionel Sambuc  *
9*f14fb602SLionel Sambuc  * Redistribution and use in source and binary forms, with or without
10*f14fb602SLionel Sambuc  * modification, are permitted provided that the following conditions
11*f14fb602SLionel Sambuc  * are met:
12*f14fb602SLionel Sambuc  * 1. Redistributions of source code must retain the above copyright
13*f14fb602SLionel Sambuc  *    notice, this list of conditions and the following disclaimer.
14*f14fb602SLionel Sambuc  * 2. Redistributions in binary form must reproduce the above copyright
15*f14fb602SLionel Sambuc  *    notice, this list of conditions and the following disclaimer in the
16*f14fb602SLionel Sambuc  *    documentation and/or other materials provided with the distribution.
17*f14fb602SLionel Sambuc  *
18*f14fb602SLionel Sambuc  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
19*f14fb602SLionel Sambuc  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20*f14fb602SLionel Sambuc  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21*f14fb602SLionel Sambuc  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
22*f14fb602SLionel Sambuc  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23*f14fb602SLionel Sambuc  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24*f14fb602SLionel Sambuc  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25*f14fb602SLionel Sambuc  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26*f14fb602SLionel Sambuc  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27*f14fb602SLionel Sambuc  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28*f14fb602SLionel Sambuc  * POSSIBILITY OF SUCH DAMAGE.
29*f14fb602SLionel Sambuc  */
30*f14fb602SLionel Sambuc 
31*f14fb602SLionel Sambuc #include <sys/cdefs.h>
32*f14fb602SLionel Sambuc 
33*f14fb602SLionel Sambuc #if defined(LIBC_SCCS) && !defined(lint)
34*f14fb602SLionel Sambuc __RCSID("$NetBSD: fixunstfdi_ieee754.c,v 1.1 2011/07/09 02:30:27 matt Exp $");
35*f14fb602SLionel Sambuc #endif /* LIBC_SCCS and not lint */
36*f14fb602SLionel Sambuc 
37*f14fb602SLionel Sambuc #include <stdbool.h>
38*f14fb602SLionel Sambuc #include <stdint.h>
39*f14fb602SLionel Sambuc #include <float.h>
40*f14fb602SLionel Sambuc #include <machine/ieee.h>
41*f14fb602SLionel Sambuc 
42*f14fb602SLionel Sambuc #if defined(__x86_64__) || defined(__i486__)
43*f14fb602SLionel Sambuc #define	FIXUNS	__fixunsxfdi
44*f14fb602SLionel Sambuc #else
45*f14fb602SLionel Sambuc #define	FIXUNS	__fixunstfdi
46*f14fb602SLionel Sambuc #endif
47*f14fb602SLionel Sambuc 
48*f14fb602SLionel Sambuc uint64_t __fixunsgen64(int, bool, size_t, size_t, const uint32_t *);
49*f14fb602SLionel Sambuc 
50*f14fb602SLionel Sambuc uint64_t FIXUNS(long double);
51*f14fb602SLionel Sambuc 
52*f14fb602SLionel Sambuc /*
53*f14fb602SLionel Sambuc  * Convert long double to uint64_t.  All operations are done module 2^64.
54*f14fb602SLionel Sambuc  */
55*f14fb602SLionel Sambuc uint64_t
FIXUNS(long double x)56*f14fb602SLionel Sambuc FIXUNS(long double x)
57*f14fb602SLionel Sambuc {
58*f14fb602SLionel Sambuc 	const union ieee_ext_u extu = { .extu_ld = x };
59*f14fb602SLionel Sambuc 	uint32_t frac[(EXT_FRACBITS + 31)/32 + 2];
60*f14fb602SLionel Sambuc 
61*f14fb602SLionel Sambuc 	frac[0] = 0;
62*f14fb602SLionel Sambuc 	frac[1] = 0;
63*f14fb602SLionel Sambuc 
64*f14fb602SLionel Sambuc 	EXT_TO_ARRAY32(extu, &frac[2]);
65*f14fb602SLionel Sambuc 
66*f14fb602SLionel Sambuc 	return __fixunsgen64(
67*f14fb602SLionel Sambuc 		extu.extu_ext.ext_exp - EXT_EXP_BIAS,
68*f14fb602SLionel Sambuc 		extu.extu_ext.ext_sign != 0,
69*f14fb602SLionel Sambuc 		LDBL_MANT_DIG,
70*f14fb602SLionel Sambuc 		EXT_FRACHBITS,
71*f14fb602SLionel Sambuc 		&frac[__arraycount(frac)-1]);
72*f14fb602SLionel Sambuc }
73