OSDN Git Service

2014-02-28 Joey Ye <joey.ye@arm.com>
[pf3gnuchains/gcc-fork.git] / libgcc / config / arm / fp16.c
1 /* Half-float conversion routines.
2
3    Copyright (C) 2008, 2009 Free Software Foundation, Inc.
4    Contributed by CodeSourcery.
5
6    This file is free software; you can redistribute it and/or modify it
7    under the terms of the GNU General Public License as published by the
8    Free Software Foundation; either version 3, or (at your option) any
9    later version.
10
11    This file is distributed in the hope that it will be useful, but
12    WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14    General Public License for more details.
15
16    Under Section 7 of GPL version 3, you are granted additional
17    permissions described in the GCC Runtime Library Exception, version
18    3.1, as published by the Free Software Foundation.
19
20    You should have received a copy of the GNU General Public License and
21    a copy of the GCC Runtime Library Exception along with this program;
22    see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
23    <http://www.gnu.org/licenses/>.  */
24
25 static inline unsigned short
26 __gnu_f2h_internal(unsigned int a, int ieee)
27 {
28   unsigned short sign = (a >> 16) & 0x8000;
29   int aexp = (a >> 23) & 0xff;
30   unsigned int mantissa = a & 0x007fffff;
31   unsigned int mask;
32   unsigned int increment;
33
34   if (aexp == 0xff)
35     {
36       if (!ieee)
37         return sign;
38       return sign | 0x7e00 | (mantissa >> 13);
39     }
40   
41   if (aexp == 0 && mantissa == 0)
42     return sign;
43
44   aexp -= 127;
45
46   /* Decimal point between bits 22 and 23.  */
47   mantissa |= 0x00800000;
48   if (aexp < -14)
49     {
50       mask = 0x007fffff;
51       if (aexp < -25)
52         aexp = -26;
53       else if (aexp != -25)
54         mask >>= 24 + aexp;
55     }
56   else
57     mask = 0x00001fff;
58
59   /* Round.  */
60   if (mantissa & mask)
61     {
62       increment = (mask + 1) >> 1;
63       if ((mantissa & mask) == increment)
64         increment = mantissa & (increment << 1);
65       mantissa += increment;
66       if (mantissa >= 0x01000000)
67         {
68           mantissa >>= 1;
69           aexp++;
70         }
71     }
72
73   if (ieee)
74     {
75       if (aexp > 15)
76         return sign | 0x7c00;
77     }
78   else
79     {
80       if (aexp > 16)
81         return sign | 0x7fff;
82     }
83
84   if (aexp < -24)
85     return sign;
86
87   if (aexp < -14)
88     {
89       mantissa >>= -14 - aexp;
90       aexp = -14;
91     }
92
93   /* We leave the leading 1 in the mantissa, and subtract one
94      from the exponent bias to compensate.  */
95   return sign | (((aexp + 14) << 10) + (mantissa >> 13));
96 }
97
98 unsigned int
99 __gnu_h2f_internal(unsigned short a, int ieee)
100 {
101   unsigned int sign = (unsigned int)(a & 0x8000) << 16;
102   int aexp = (a >> 10) & 0x1f;
103   unsigned int mantissa = a & 0x3ff;
104
105   if (aexp == 0x1f && ieee)
106     return sign | 0x7f800000 | (mantissa << 13);
107
108   if (aexp == 0)
109     {
110       int shift;
111
112       if (mantissa == 0)
113         return sign;
114
115       shift = __builtin_clz(mantissa) - 21;
116       mantissa <<= shift;
117       aexp = -shift;
118     }
119
120   return sign | (((aexp + 0x70) << 23) + (mantissa << 13));
121 }
122
123 unsigned short
124 __gnu_f2h_ieee(unsigned int a)
125 {
126   return __gnu_f2h_internal(a, 1);
127 }
128
129 unsigned int
130 __gnu_h2f_ieee(unsigned short a)
131 {
132   return __gnu_h2f_internal(a, 1);
133 }
134
135 unsigned short
136 __gnu_f2h_alternative(unsigned int x)
137 {
138   return __gnu_f2h_internal(x, 0);
139 }
140
141 unsigned int
142 __gnu_h2f_alternative(unsigned short a)
143 {
144   return __gnu_h2f_internal(a, 0);
145 }