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//
29// Authors: Steve Reinhardt
30
31////////////////////////////////////////////////////////////////////
32//
33// Utility functions for execute methods
34//
35
36output exec {{
37
38    /// Return opa + opb, summing carry into third arg.
39    inline uint64_t
40    addc(uint64_t opa, uint64_t opb, int &carry)
41    {
42        uint64_t res = opa + opb;
43        if (res < opa || res < opb)
44            ++carry;
45        return res;
46    }
47
48    /// Multiply two 64-bit values (opa * opb), returning the 128-bit
49    /// product in res_hi and res_lo.
50    inline void
51    mul128(uint64_t opa, uint64_t opb, uint64_t &res_hi, uint64_t &res_lo)
52    {
53        // do a 64x64 --> 128 multiply using four 32x32 --> 64 multiplies
54        uint64_t opa_hi = opa<63:32>;
55        uint64_t opa_lo = opa<31:0>;
56        uint64_t opb_hi = opb<63:32>;
57        uint64_t opb_lo = opb<31:0>;
58
59        res_lo = opa_lo * opb_lo;
60
61        // The middle partial products logically belong in bit
62        // positions 95 to 32.  Thus the lower 32 bits of each product
63        // sum into the upper 32 bits of the low result, while the
64        // upper 32 sum into the low 32 bits of the upper result.
65        uint64_t partial1 = opa_hi * opb_lo;
66        uint64_t partial2 = opa_lo * opb_hi;
67
68        uint64_t partial1_lo = partial1<31:0> << 32;
69        uint64_t partial1_hi = partial1<63:32>;
70        uint64_t partial2_lo = partial2<31:0> << 32;
71        uint64_t partial2_hi = partial2<63:32>;
72
73        // Add partial1_lo and partial2_lo to res_lo, keeping track
74        // of any carries out
75        int carry_out = 0;
76        res_lo = addc(partial1_lo, res_lo, carry_out);
77        res_lo = addc(partial2_lo, res_lo, carry_out);
78
79        // Now calculate the high 64 bits...
80        res_hi = (opa_hi * opb_hi) + partial1_hi + partial2_hi + carry_out;
81    }
82
83    /// Map 8-bit S-floating exponent to 11-bit T-floating exponent.
84    /// See Table 2-2 of Alpha AHB.
85    inline int
86    map_s(int old_exp)
87    {
88        int hibit = old_exp<7:>;
89        int lobits = old_exp<6:0>;
90
91        if (hibit == 1) {
92            return (lobits == 0x7f) ? 0x7ff : (0x400 | lobits);
93        }
94        else {
95            return (lobits == 0) ? 0 : (0x380 | lobits);
96        }
97    }
98
99    /// Convert a 32-bit S-floating value to the equivalent 64-bit
100    /// representation to be stored in an FP reg.
101    inline uint64_t
102    s_to_t(uint32_t s_val)
103    {
104        uint64_t tmp = s_val;
105        return (tmp<31:> << 63 // sign bit
106                | (uint64_t)map_s(tmp<30:23>) << 52 // exponent
107                | tmp<22:0> << 29); // fraction
108    }
109
110    /// Convert a 64-bit T-floating value to the equivalent 32-bit
111    /// S-floating representation to be stored in memory.
112    inline int32_t
113    t_to_s(uint64_t t_val)
114    {
115        return (t_val<63:62> << 30   // sign bit & hi exp bit
116                | t_val<58:29>);     // rest of exp & fraction
117    }
118}};
119
120