fixdfdi_ieee754.c revision 1.1 1 1.1 matt /* $NetBSD: fixdfdi_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: fixdfdi_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 <stdbool.h>
48 1.1 matt #include <machine/ieee.h>
49 1.1 matt
50 1.1 matt /*
51 1.1 matt * Convert double to signed quad.
52 1.1 matt * Not sure what to do with negative numbers---for now, anything out
53 1.1 matt * of range becomes UQUAD_MAX.
54 1.1 matt */
55 1.1 matt quad_t
56 1.1 matt __fixdfdi(double x)
57 1.1 matt {
58 1.1 matt const union ieee_double_u ux = { .dblu_d = x };
59 1.1 matt signed int exp = ux.dblu_exp - DBL_EXP_BIAS;
60 1.1 matt const bool neg = ux.dblu_sign;
61 1.1 matt quad_t r;
62 1.1 matt
63 1.1 matt if (exp >= 62)
64 1.1 matt return neg ? QUAD_MIN : QUAD_MAX;
65 1.1 matt
66 1.1 matt r = 1 << DBL_FRACHBITS; /* implicit bit */
67 1.1 matt r |= ux.dblu_frach;
68 1.1 matt exp -= DBL_FRACHBITS;
69 1.1 matt if (exp < 0) {
70 1.1 matt r >>= -exp;
71 1.1 matt } else if (exp > 0) {
72 1.1 matt r <<= DBL_FRACLBITS;
73 1.1 matt r |= ux.dblu_fracl;
74 1.1 matt exp -= DBL_FRACLBITS;
75 1.1 matt if (exp < 0) {
76 1.1 matt r >>= -exp;
77 1.1 matt } else if (exp > 0) {
78 1.1 matt r <<= exp;
79 1.1 matt }
80 1.1 matt }
81 1.1 matt return neg ? -r : r;
82 1.1 matt }
83