fixunsdfdi_ieee754.c revision 1.1 1 1.1 matt /* $NetBSD: fixunsdfdi_ieee754.c,v 1.1 2013/08/24 00:51:48 matt Exp $ */
2 1.1 matt
3 1.1 matt /*-
4 1.1 matt * Copyright (c) 1992, 1993
5 1.1 matt * The Regents of the University of California. All rights reserved.
6 1.1 matt *
7 1.1 matt * This software was developed by the Computer Systems Engineering group
8 1.1 matt * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
9 1.1 matt * contributed to Berkeley.
10 1.1 matt *
11 1.1 matt * Redistribution and use in source and binary forms, with or without
12 1.1 matt * modification, are permitted provided that the following conditions
13 1.1 matt * are met:
14 1.1 matt * 1. Redistributions of source code must retain the above copyright
15 1.1 matt * notice, this list of conditions and the following disclaimer.
16 1.1 matt * 2. Redistributions in binary form must reproduce the above copyright
17 1.1 matt * notice, this list of conditions and the following disclaimer in the
18 1.1 matt * documentation and/or other materials provided with the distribution.
19 1.1 matt * 3. Neither the name of the University nor the names of its contributors
20 1.1 matt * may be used to endorse or promote products derived from this software
21 1.1 matt * without specific prior written permission.
22 1.1 matt *
23 1.1 matt * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 1.1 matt * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 1.1 matt * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 1.1 matt * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 1.1 matt * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 1.1 matt * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 1.1 matt * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 1.1 matt * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 1.1 matt * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 1.1 matt * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 1.1 matt * SUCH DAMAGE.
34 1.1 matt */
35 1.1 matt
36 1.1 matt #include <sys/cdefs.h>
37 1.1 matt #if defined(LIBC_SCCS) && !defined(lint)
38 1.1 matt __RCSID("$NetBSD: fixunsdfdi_ieee754.c,v 1.1 2013/08/24 00:51:48 matt Exp $");
39 1.1 matt #endif /* LIBC_SCCS and not lint */
40 1.1 matt
41 1.1 matt #if defined(SOFTFLOAT) || defined(__ARM_EABI__)
42 1.1 matt #include "softfloat/softfloat-for-gcc.h"
43 1.1 matt #endif
44 1.1 matt
45 1.1 matt #include "quad.h"
46 1.1 matt #include <limits.h>
47 1.1 matt #include <machine/ieee.h>
48 1.1 matt
49 1.1 matt /*
50 1.1 matt * Convert double to unsigned quad.
51 1.1 matt * Not sure what to do with negative numbers---for now, anything out
52 1.1 matt * of range becomes UQUAD_MAX.
53 1.1 matt */
54 1.1 matt u_quad_t
55 1.1 matt __fixunsdfdi(double x)
56 1.1 matt {
57 1.1 matt const union ieee_double_u ux = { .dblu_d = x };
58 1.1 matt signed int exp = ux.dblu_exp - DBL_EXP_BIAS;
59 1.1 matt u_quad_t r;
60 1.1 matt
61 1.1 matt if (ux.dblu_sign)
62 1.1 matt return UQUAD_MAX;
63 1.1 matt if (exp > 63)
64 1.1 matt return UQUAD_MAX;
65 1.1 matt if (exp < 0)
66 1.1 matt return 0;
67 1.1 matt
68 1.1 matt r = 1 << DBL_FRACHBITS; /* implicit bit */
69 1.1 matt r |= ux.dblu_frach;
70 1.1 matt exp -= DBL_FRACHBITS;
71 1.1 matt if (exp == 0)
72 1.1 matt return r;
73 1.1 matt if (exp < 0)
74 1.1 matt return r >> -exp;
75 1.1 matt
76 1.1 matt r <<= DBL_FRACLBITS;
77 1.1 matt r |= ux.dblu_fracl;
78 1.1 matt exp -= DBL_FRACLBITS;
79 1.1 matt if (exp == 0)
80 1.1 matt return r;
81 1.1 matt if (exp < 0)
82 1.1 matt return r >> -exp;
83 1.1 matt
84 1.1 matt return r << exp;
85 1.1 matt }
86