util.isa revision 2632:1bb2f91485ea
1// -*- mode:c++ -*-
2
3// Copyright (c) 2003-2005 The Regents of The University of Michigan
4// All rights reserved.
5//
6// Redistribution and use in source and binary forms, with or without
7// modification, are permitted provided that the following conditions are
8// met: redistributions of source code must retain the above copyright
9// notice, this list of conditions and the following disclaimer;
10// redistributions in binary form must reproduce the above copyright
11// notice, this list of conditions and the following disclaimer in the
12// documentation and/or other materials provided with the distribution;
13// neither the name of the copyright holders nor the names of its
14// contributors may be used to endorse or promote products derived from
15// this software without specific prior written permission.
16//
17// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
29output exec {{
30
31    /// Return opa + opb, summing carry into third arg.
32    inline uint64_t
33    addc(uint64_t opa, uint64_t opb, int &carry)
34    {
35        uint64_t res = opa + opb;
36        if (res < opa || res < opb)
37            ++carry;
38        return res;
39    }
40
41    /// Multiply two 64-bit values (opa * opb), returning the 128-bit
42    /// product in res_hi and res_lo.
43    inline void
44    mul128(uint64_t opa, uint64_t opb, uint64_t &res_hi, uint64_t &res_lo)
45    {
46        // do a 64x64 --> 128 multiply using four 32x32 --> 64 multiplies
47        uint64_t opa_hi = opa<63:32>;
48        uint64_t opa_lo = opa<31:0>;
49        uint64_t opb_hi = opb<63:32>;
50        uint64_t opb_lo = opb<31:0>;
51
52        res_lo = opa_lo * opb_lo;
53
54        // The middle partial products logically belong in bit
55        // positions 95 to 32.  Thus the lower 32 bits of each product
56        // sum into the upper 32 bits of the low result, while the
57        // upper 32 sum into the low 32 bits of the upper result.
58        uint64_t partial1 = opa_hi * opb_lo;
59        uint64_t partial2 = opa_lo * opb_hi;
60
61        uint64_t partial1_lo = partial1<31:0> << 32;
62        uint64_t partial1_hi = partial1<63:32>;
63        uint64_t partial2_lo = partial2<31:0> << 32;
64        uint64_t partial2_hi = partial2<63:32>;
65
66        // Add partial1_lo and partial2_lo to res_lo, keeping track
67        // of any carries out
68        int carry_out = 0;
69        res_lo = addc(partial1_lo, res_lo, carry_out);
70        res_lo = addc(partial2_lo, res_lo, carry_out);
71
72        // Now calculate the high 64 bits...
73        res_hi = (opa_hi * opb_hi) + partial1_hi + partial2_hi + carry_out;
74    }
75
76    /// Map 8-bit S-floating exponent to 11-bit T-floating exponent.
77    /// See Table 2-2 of Alpha AHB.
78    inline int
79    map_s(int old_exp)
80    {
81        int hibit = old_exp<7:>;
82        int lobits = old_exp<6:0>;
83
84        if (hibit == 1) {
85            return (lobits == 0x7f) ? 0x7ff : (0x400 | lobits);
86        }
87        else {
88            return (lobits == 0) ? 0 : (0x380 | lobits);
89        }
90    }
91
92    /// Convert a 32-bit S-floating value to the equivalent 64-bit
93    /// representation to be stored in an FP reg.
94    inline uint64_t
95    s_to_t(uint32_t s_val)
96    {
97        uint64_t tmp = s_val;
98        return (tmp<31:> << 63 // sign bit
99                | (uint64_t)map_s(tmp<30:23>) << 52 // exponent
100                | tmp<22:0> << 29); // fraction
101    }
102
103    /// Convert a 64-bit T-floating value to the equivalent 32-bit
104    /// S-floating representation to be stored in memory.
105    inline int32_t
106    t_to_s(uint64_t t_val)
107    {
108        return (t_val<63:62> << 30   // sign bit & hi exp bit
109                | t_val<58:29>);     // rest of exp & fraction
110    }
111}};
112
113