summaryrefslogtreecommitdiff
path: root/arch/alpha/isa/util.isa
blob: 9fbbf663691a4e6bac9cf972ee95c91ecc18402c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// -*- mode:c++ -*-

// Copyright (c) 2003-2005 The Regents of The University of Michigan
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met: redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer;
// redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution;
// neither the name of the copyright holders nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

output exec {{

    /// Return opa + opb, summing carry into third arg.
    inline uint64_t
    addc(uint64_t opa, uint64_t opb, int &carry)
    {
        uint64_t res = opa + opb;
        if (res < opa || res < opb)
            ++carry;
        return res;
    }

    /// Multiply two 64-bit values (opa * opb), returning the 128-bit
    /// product in res_hi and res_lo.
    inline void
    mul128(uint64_t opa, uint64_t opb, uint64_t &res_hi, uint64_t &res_lo)
    {
        // do a 64x64 --> 128 multiply using four 32x32 --> 64 multiplies
        uint64_t opa_hi = opa<63:32>;
        uint64_t opa_lo = opa<31:0>;
        uint64_t opb_hi = opb<63:32>;
        uint64_t opb_lo = opb<31:0>;

        res_lo = opa_lo * opb_lo;

        // The middle partial products logically belong in bit
        // positions 95 to 32.  Thus the lower 32 bits of each product
        // sum into the upper 32 bits of the low result, while the
        // upper 32 sum into the low 32 bits of the upper result.
        uint64_t partial1 = opa_hi * opb_lo;
        uint64_t partial2 = opa_lo * opb_hi;

        uint64_t partial1_lo = partial1<31:0> << 32;
        uint64_t partial1_hi = partial1<63:32>;
        uint64_t partial2_lo = partial2<31:0> << 32;
        uint64_t partial2_hi = partial2<63:32>;

        // Add partial1_lo and partial2_lo to res_lo, keeping track
        // of any carries out
        int carry_out = 0;
        res_lo = addc(partial1_lo, res_lo, carry_out);
        res_lo = addc(partial2_lo, res_lo, carry_out);

        // Now calculate the high 64 bits...
        res_hi = (opa_hi * opb_hi) + partial1_hi + partial2_hi + carry_out;
    }

    /// Map 8-bit S-floating exponent to 11-bit T-floating exponent.
    /// See Table 2-2 of Alpha AHB.
    inline int
    map_s(int old_exp)
    {
        int hibit = old_exp<7:>;
        int lobits = old_exp<6:0>;

        if (hibit == 1) {
            return (lobits == 0x7f) ? 0x7ff : (0x400 | lobits);
        }
        else {
            return (lobits == 0) ? 0 : (0x380 | lobits);
        }
    }

    /// Convert a 32-bit S-floating value to the equivalent 64-bit
    /// representation to be stored in an FP reg.
    inline uint64_t
    s_to_t(uint32_t s_val)
    {
        uint64_t tmp = s_val;
        return (tmp<31:> << 63 // sign bit
                | (uint64_t)map_s(tmp<30:23>) << 52 // exponent
                | tmp<22:0> << 29); // fraction
    }

    /// Convert a 64-bit T-floating value to the equivalent 32-bit
    /// S-floating representation to be stored in memory.
    inline int32_t
    t_to_s(uint64_t t_val)
    {
        return (t_val<63:62> << 30   // sign bit & hi exp bit
                | t_val<58:29>);     // rest of exp & fraction
    }
}};