15083Sgblack@eecs.umich.edu// Copyright (c) 2007 The Hewlett-Packard Development Company
29582Snilay@cs.wisc.edu// Copyright (c) 2012-2013 Mark D. Hill and David A. Wood
311159Ssteve.reinhardt@amd.com// Copyright (c) 2015 Advanced Micro Devices, Inc.
411159Ssteve.reinhardt@amd.com//
55083Sgblack@eecs.umich.edu// All rights reserved.
65083Sgblack@eecs.umich.edu//
77087Snate@binkert.org// The license below extends only to copyright in the software and shall
87087Snate@binkert.org// not be construed as granting a license to any other intellectual
97087Snate@binkert.org// property including but not limited to intellectual property relating
107087Snate@binkert.org// to a hardware implementation of the functionality of the software
117087Snate@binkert.org// licensed hereunder.  You may use the software subject to the license
127087Snate@binkert.org// terms below provided that you ensure that this notice is replicated
137087Snate@binkert.org// unmodified and in its entirety in all distributions of the software,
147087Snate@binkert.org// modified or unmodified, in source code or in binary form.
155083Sgblack@eecs.umich.edu//
167087Snate@binkert.org// Redistribution and use in source and binary forms, with or without
177087Snate@binkert.org// modification, are permitted provided that the following conditions are
187087Snate@binkert.org// met: redistributions of source code must retain the above copyright
197087Snate@binkert.org// notice, this list of conditions and the following disclaimer;
207087Snate@binkert.org// redistributions in binary form must reproduce the above copyright
217087Snate@binkert.org// notice, this list of conditions and the following disclaimer in the
227087Snate@binkert.org// documentation and/or other materials provided with the distribution;
237087Snate@binkert.org// neither the name of the copyright holders nor the names of its
245083Sgblack@eecs.umich.edu// contributors may be used to endorse or promote products derived from
257087Snate@binkert.org// this software without specific prior written permission.
265083Sgblack@eecs.umich.edu//
275083Sgblack@eecs.umich.edu// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
285083Sgblack@eecs.umich.edu// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
295083Sgblack@eecs.umich.edu// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
305083Sgblack@eecs.umich.edu// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
315083Sgblack@eecs.umich.edu// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
325083Sgblack@eecs.umich.edu// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
335083Sgblack@eecs.umich.edu// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
345083Sgblack@eecs.umich.edu// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
355083Sgblack@eecs.umich.edu// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
365083Sgblack@eecs.umich.edu// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
375083Sgblack@eecs.umich.edu// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
385083Sgblack@eecs.umich.edu//
395083Sgblack@eecs.umich.edu// Authors: Gabe Black
409582Snilay@cs.wisc.edu//          Nilay Vaish
415083Sgblack@eecs.umich.edu
425083Sgblack@eecs.umich.edu//////////////////////////////////////////////////////////////////////////
435083Sgblack@eecs.umich.edu//
445083Sgblack@eecs.umich.edu// FpOp Microop templates
455083Sgblack@eecs.umich.edu//
465083Sgblack@eecs.umich.edu//////////////////////////////////////////////////////////////////////////
475083Sgblack@eecs.umich.edu
485083Sgblack@eecs.umich.edudef template MicroFpOpExecute {{
4912234Sgabeblack@google.com        Fault %(class_name)s::execute(ExecContext *xc,
505083Sgblack@eecs.umich.edu                Trace::InstRecord *traceData) const
515083Sgblack@eecs.umich.edu        {
525083Sgblack@eecs.umich.edu            Fault fault = NoFault;
535083Sgblack@eecs.umich.edu
545083Sgblack@eecs.umich.edu            DPRINTF(X86, "The data size is %d\n", dataSize);
555083Sgblack@eecs.umich.edu            %(op_decl)s;
565083Sgblack@eecs.umich.edu            %(op_rd)s;
575083Sgblack@eecs.umich.edu
585083Sgblack@eecs.umich.edu            if(%(cond_check)s)
595083Sgblack@eecs.umich.edu            {
605083Sgblack@eecs.umich.edu                %(code)s;
615083Sgblack@eecs.umich.edu                %(flag_code)s;
629765Sandreas@sandberg.pp.se                %(tag_code)s;
635083Sgblack@eecs.umich.edu                %(top_code)s;
645083Sgblack@eecs.umich.edu            }
655083Sgblack@eecs.umich.edu            else
665083Sgblack@eecs.umich.edu            {
675083Sgblack@eecs.umich.edu                %(else_code)s;
685083Sgblack@eecs.umich.edu            }
695083Sgblack@eecs.umich.edu
705083Sgblack@eecs.umich.edu            //Write the resulting state to the execution context
715083Sgblack@eecs.umich.edu            if(fault == NoFault)
725083Sgblack@eecs.umich.edu            {
735083Sgblack@eecs.umich.edu                %(op_wb)s;
745083Sgblack@eecs.umich.edu            }
755083Sgblack@eecs.umich.edu            return fault;
765083Sgblack@eecs.umich.edu        }
775083Sgblack@eecs.umich.edu}};
785083Sgblack@eecs.umich.edu
795083Sgblack@eecs.umich.edudef template MicroFpOpDeclare {{
805083Sgblack@eecs.umich.edu    class %(class_name)s : public %(base_class)s
815083Sgblack@eecs.umich.edu    {
825083Sgblack@eecs.umich.edu      public:
835083Sgblack@eecs.umich.edu        %(class_name)s(ExtMachInst _machInst,
847620Sgblack@eecs.umich.edu                const char * instMnem, uint64_t setFlags,
856345Sgblack@eecs.umich.edu                InstRegIndex _src1, InstRegIndex _src2, InstRegIndex _dest,
865083Sgblack@eecs.umich.edu                uint8_t _dataSize, int8_t _spm);
875083Sgblack@eecs.umich.edu
8812236Sgabeblack@google.com        Fault execute(ExecContext *, Trace::InstRecord *) const;
895083Sgblack@eecs.umich.edu    };
905083Sgblack@eecs.umich.edu}};
915083Sgblack@eecs.umich.edu
925083Sgblack@eecs.umich.edudef template MicroFpOpConstructor {{
9310184SCurtis.Dunham@arm.com    %(class_name)s::%(class_name)s(
947620Sgblack@eecs.umich.edu            ExtMachInst machInst, const char * instMnem, uint64_t setFlags,
956345Sgblack@eecs.umich.edu            InstRegIndex _src1, InstRegIndex _src2, InstRegIndex _dest,
965083Sgblack@eecs.umich.edu            uint8_t _dataSize, int8_t _spm) :
977620Sgblack@eecs.umich.edu        %(base_class)s(machInst, "%(mnemonic)s", instMnem, setFlags,
985083Sgblack@eecs.umich.edu                _src1, _src2, _dest, _dataSize, _spm,
995083Sgblack@eecs.umich.edu                %(op_class)s)
1005083Sgblack@eecs.umich.edu    {
1017626Sgblack@eecs.umich.edu        %(constructor)s;
1025083Sgblack@eecs.umich.edu    }
1035083Sgblack@eecs.umich.edu}};
1045083Sgblack@eecs.umich.edu
1055083Sgblack@eecs.umich.edulet {{
1065083Sgblack@eecs.umich.edu    # Make these empty strings so that concatenating onto
1075083Sgblack@eecs.umich.edu    # them will always work.
1085083Sgblack@eecs.umich.edu    header_output = ""
1095083Sgblack@eecs.umich.edu    decoder_output = ""
1105083Sgblack@eecs.umich.edu    exec_output = ""
1115083Sgblack@eecs.umich.edu
1125083Sgblack@eecs.umich.edu    class FpOpMeta(type):
1135083Sgblack@eecs.umich.edu        def buildCppClasses(self, name, Name, suffix, \
1149699Snilay@cs.wisc.edu                code, flag_code, cond_check, else_code, op_class):
1155083Sgblack@eecs.umich.edu
1165083Sgblack@eecs.umich.edu            # Globals to stick the output in
1175083Sgblack@eecs.umich.edu            global header_output
1185083Sgblack@eecs.umich.edu            global decoder_output
1195083Sgblack@eecs.umich.edu            global exec_output
1205083Sgblack@eecs.umich.edu
1215083Sgblack@eecs.umich.edu            # Stick all the code together so it can be searched at once
1225083Sgblack@eecs.umich.edu            allCode = "|".join((code, flag_code, cond_check, else_code))
1235083Sgblack@eecs.umich.edu
1245083Sgblack@eecs.umich.edu            # If there's something optional to do with flags, generate
1255083Sgblack@eecs.umich.edu            # a version without it and fix up this version to use it.
1265083Sgblack@eecs.umich.edu            if flag_code is not "" or cond_check is not "true":
1275083Sgblack@eecs.umich.edu                self.buildCppClasses(name, Name, suffix,
1289699Snilay@cs.wisc.edu                        code, "", "true", else_code, op_class)
1295083Sgblack@eecs.umich.edu                suffix = "Flags" + suffix
1305083Sgblack@eecs.umich.edu
1315083Sgblack@eecs.umich.edu            base = "X86ISA::FpOp"
1325083Sgblack@eecs.umich.edu
1335083Sgblack@eecs.umich.edu            # Get everything ready for the substitution
1349765Sandreas@sandberg.pp.se            iop_tag = InstObjParams(name, Name + suffix + "TopTag", base,
1359765Sandreas@sandberg.pp.se                    {"code" : code,
1369765Sandreas@sandberg.pp.se                     "flag_code" : flag_code,
1379765Sandreas@sandberg.pp.se                     "cond_check" : cond_check,
1389765Sandreas@sandberg.pp.se                     "else_code" : else_code,
1399765Sandreas@sandberg.pp.se                     "tag_code" : "FTW = genX87Tags(FTW, TOP, spm);",
1409765Sandreas@sandberg.pp.se                     "top_code" : "TOP = (TOP + spm + 8) % 8;",
1419765Sandreas@sandberg.pp.se                     "op_class" : op_class})
1425083Sgblack@eecs.umich.edu            iop_top = InstObjParams(name, Name + suffix + "Top", base,
1435083Sgblack@eecs.umich.edu                    {"code" : code,
1445083Sgblack@eecs.umich.edu                     "flag_code" : flag_code,
1455083Sgblack@eecs.umich.edu                     "cond_check" : cond_check,
1465083Sgblack@eecs.umich.edu                     "else_code" : else_code,
1479765Sandreas@sandberg.pp.se                     "tag_code" : ";",
1489699Snilay@cs.wisc.edu                     "top_code" : "TOP = (TOP + spm + 8) % 8;",
1499699Snilay@cs.wisc.edu                     "op_class" : op_class})
1505083Sgblack@eecs.umich.edu            iop = InstObjParams(name, Name + suffix, base,
1515083Sgblack@eecs.umich.edu                    {"code" : code,
1525083Sgblack@eecs.umich.edu                     "flag_code" : flag_code,
1535083Sgblack@eecs.umich.edu                     "cond_check" : cond_check,
1545083Sgblack@eecs.umich.edu                     "else_code" : else_code,
1559765Sandreas@sandberg.pp.se                     "tag_code" : ";",
1569699Snilay@cs.wisc.edu                     "top_code" : ";",
1579699Snilay@cs.wisc.edu                     "op_class" : op_class})
1585083Sgblack@eecs.umich.edu
1595083Sgblack@eecs.umich.edu            # Generate the actual code (finally!)
1609765Sandreas@sandberg.pp.se            header_output += MicroFpOpDeclare.subst(iop_tag)
1619765Sandreas@sandberg.pp.se            decoder_output += MicroFpOpConstructor.subst(iop_tag)
1629765Sandreas@sandberg.pp.se            exec_output += MicroFpOpExecute.subst(iop_tag)
1635083Sgblack@eecs.umich.edu            header_output += MicroFpOpDeclare.subst(iop_top)
1645083Sgblack@eecs.umich.edu            decoder_output += MicroFpOpConstructor.subst(iop_top)
1655083Sgblack@eecs.umich.edu            exec_output += MicroFpOpExecute.subst(iop_top)
1665083Sgblack@eecs.umich.edu            header_output += MicroFpOpDeclare.subst(iop)
1675083Sgblack@eecs.umich.edu            decoder_output += MicroFpOpConstructor.subst(iop)
1685083Sgblack@eecs.umich.edu            exec_output += MicroFpOpExecute.subst(iop)
1695083Sgblack@eecs.umich.edu
1705083Sgblack@eecs.umich.edu
1715083Sgblack@eecs.umich.edu        def __new__(mcls, Name, bases, dict):
1725083Sgblack@eecs.umich.edu            abstract = False
1735083Sgblack@eecs.umich.edu            name = Name.lower()
1745083Sgblack@eecs.umich.edu            if "abstract" in dict:
1755083Sgblack@eecs.umich.edu                abstract = dict['abstract']
1765083Sgblack@eecs.umich.edu                del dict['abstract']
1775083Sgblack@eecs.umich.edu
1785083Sgblack@eecs.umich.edu            cls = super(FpOpMeta, mcls).__new__(mcls, Name, bases, dict)
1795083Sgblack@eecs.umich.edu            if not abstract:
1805083Sgblack@eecs.umich.edu                cls.className = Name
1815083Sgblack@eecs.umich.edu                cls.mnemonic = name
1825083Sgblack@eecs.umich.edu                code = cls.code
1835083Sgblack@eecs.umich.edu                flag_code = cls.flag_code
1845083Sgblack@eecs.umich.edu                cond_check = cls.cond_check
1855083Sgblack@eecs.umich.edu                else_code = cls.else_code
1869699Snilay@cs.wisc.edu                op_class = cls.op_class
1875083Sgblack@eecs.umich.edu
1885083Sgblack@eecs.umich.edu                # Set up the C++ classes
1895083Sgblack@eecs.umich.edu                mcls.buildCppClasses(cls, name, Name, "",
1909699Snilay@cs.wisc.edu                        code, flag_code, cond_check, else_code, op_class)
1915083Sgblack@eecs.umich.edu
1925083Sgblack@eecs.umich.edu                # Hook into the microassembler dict
1935083Sgblack@eecs.umich.edu                global microopClasses
1945083Sgblack@eecs.umich.edu                microopClasses[name] = cls
1955083Sgblack@eecs.umich.edu
1965083Sgblack@eecs.umich.edu            return cls
1975083Sgblack@eecs.umich.edu
1989371Snilay@cs.wisc.edu    class FpUnaryOp(X86Microop):
1999371Snilay@cs.wisc.edu        __metaclass__ = FpOpMeta
2009371Snilay@cs.wisc.edu        # This class itself doesn't act as a microop
2019371Snilay@cs.wisc.edu        abstract = True
2025083Sgblack@eecs.umich.edu
2039371Snilay@cs.wisc.edu        # Default template parameter values
2049371Snilay@cs.wisc.edu        flag_code = ""
2059371Snilay@cs.wisc.edu        cond_check = "true"
2069371Snilay@cs.wisc.edu        else_code = ";"
2079699Snilay@cs.wisc.edu        op_class = "FloatAddOp"
2089371Snilay@cs.wisc.edu
2099371Snilay@cs.wisc.edu        def __init__(self, dest, src1, spm=0, \
2109765Sandreas@sandberg.pp.se                SetStatus=False, UpdateFTW=True, dataSize="env.dataSize"):
2119371Snilay@cs.wisc.edu            self.dest = dest
2129371Snilay@cs.wisc.edu            self.src1 = src1
2139371Snilay@cs.wisc.edu            self.src2 = "InstRegIndex(0)"
2149371Snilay@cs.wisc.edu            self.spm = spm
2159371Snilay@cs.wisc.edu            self.dataSize = dataSize
2169371Snilay@cs.wisc.edu            if SetStatus:
2179371Snilay@cs.wisc.edu                self.className += "Flags"
2189371Snilay@cs.wisc.edu            if spm:
2199371Snilay@cs.wisc.edu                self.className += "Top"
2209765Sandreas@sandberg.pp.se            if spm and UpdateFTW:
2219765Sandreas@sandberg.pp.se                self.className += "Tag"
2229371Snilay@cs.wisc.edu
2239371Snilay@cs.wisc.edu        def getAllocator(self, microFlags):
2249371Snilay@cs.wisc.edu            return '''new %(class_name)s(machInst, macrocodeBlock,
2259371Snilay@cs.wisc.edu                    %(flags)s, %(src1)s, %(src2)s, %(dest)s,
2269371Snilay@cs.wisc.edu                    %(dataSize)s, %(spm)d)''' % {
2279371Snilay@cs.wisc.edu                "class_name" : self.className,
2289371Snilay@cs.wisc.edu                "flags" : self.microFlagsText(microFlags),
2299371Snilay@cs.wisc.edu                "src1" : self.src1, "src2" : self.src2,
2309371Snilay@cs.wisc.edu                "dest" : self.dest,
2319371Snilay@cs.wisc.edu                "dataSize" : self.dataSize,
2329371Snilay@cs.wisc.edu                "spm" : self.spm}
2339371Snilay@cs.wisc.edu
2349371Snilay@cs.wisc.edu    class FpBinaryOp(X86Microop):
2355083Sgblack@eecs.umich.edu        __metaclass__ = FpOpMeta
2365083Sgblack@eecs.umich.edu        # This class itself doesn't act as a microop
2375083Sgblack@eecs.umich.edu        abstract = True
2385083Sgblack@eecs.umich.edu
2395083Sgblack@eecs.umich.edu        # Default template parameter values
2405083Sgblack@eecs.umich.edu        flag_code = ""
2415083Sgblack@eecs.umich.edu        cond_check = "true"
2425083Sgblack@eecs.umich.edu        else_code = ";"
2439699Snilay@cs.wisc.edu        op_class = "FloatAddOp"
2445083Sgblack@eecs.umich.edu
2455083Sgblack@eecs.umich.edu        def __init__(self, dest, src1, src2, spm=0, \
2469765Sandreas@sandberg.pp.se                SetStatus=False, UpdateFTW=True, dataSize="env.dataSize"):
2475083Sgblack@eecs.umich.edu            self.dest = dest
2485083Sgblack@eecs.umich.edu            self.src1 = src1
2495083Sgblack@eecs.umich.edu            self.src2 = src2
2505083Sgblack@eecs.umich.edu            self.spm = spm
2515083Sgblack@eecs.umich.edu            self.dataSize = dataSize
2525083Sgblack@eecs.umich.edu            if SetStatus:
2535083Sgblack@eecs.umich.edu                self.className += "Flags"
2545083Sgblack@eecs.umich.edu            if spm:
2555083Sgblack@eecs.umich.edu                self.className += "Top"
2569765Sandreas@sandberg.pp.se            if spm and UpdateFTW:
2579765Sandreas@sandberg.pp.se                self.className += "Tag"
2585083Sgblack@eecs.umich.edu
2597620Sgblack@eecs.umich.edu        def getAllocator(self, microFlags):
2607620Sgblack@eecs.umich.edu            return '''new %(class_name)s(machInst, macrocodeBlock,
2615083Sgblack@eecs.umich.edu                    %(flags)s, %(src1)s, %(src2)s, %(dest)s,
2625083Sgblack@eecs.umich.edu                    %(dataSize)s, %(spm)d)''' % {
2635083Sgblack@eecs.umich.edu                "class_name" : self.className,
2645083Sgblack@eecs.umich.edu                "flags" : self.microFlagsText(microFlags),
2655083Sgblack@eecs.umich.edu                "src1" : self.src1, "src2" : self.src2,
2665083Sgblack@eecs.umich.edu                "dest" : self.dest,
2675083Sgblack@eecs.umich.edu                "dataSize" : self.dataSize,
2685083Sgblack@eecs.umich.edu                "spm" : self.spm}
2695083Sgblack@eecs.umich.edu
2709371Snilay@cs.wisc.edu    class Movfp(FpUnaryOp):
2718588Sgblack@eecs.umich.edu        code = 'FpDestReg_uqw = FpSrcReg1_uqw;'
2728588Sgblack@eecs.umich.edu        else_code = 'FpDestReg_uqw = FpDestReg_uqw;'
2739211Snilay@cs.wisc.edu        cond_check = "checkCondition(ccFlagBits | cfofBits | dfBit | \
2749211Snilay@cs.wisc.edu                                     ecfBit | ezfBit, src2)"
27510313Snilay@cs.wisc.edu        op_class = 'IntAluOp'
2765083Sgblack@eecs.umich.edu
2779371Snilay@cs.wisc.edu    class Xorfp(FpBinaryOp):
2788588Sgblack@eecs.umich.edu        code = 'FpDestReg_uqw = FpSrcReg1_uqw ^ FpSrcReg2_uqw;'
2795083Sgblack@eecs.umich.edu
2809371Snilay@cs.wisc.edu    class Sqrtfp(FpBinaryOp):
2815083Sgblack@eecs.umich.edu        code = 'FpDestReg = sqrt(FpSrcReg2);'
2829699Snilay@cs.wisc.edu        op_class = 'FloatSqrtOp'
2835083Sgblack@eecs.umich.edu
2849371Snilay@cs.wisc.edu    class Cosfp(FpUnaryOp):
2859371Snilay@cs.wisc.edu        code = 'FpDestReg = cos(FpSrcReg1);'
2869699Snilay@cs.wisc.edu        op_class = 'FloatSqrtOp'
2879371Snilay@cs.wisc.edu
2889371Snilay@cs.wisc.edu    class Sinfp(FpUnaryOp):
2899371Snilay@cs.wisc.edu        code = 'FpDestReg = sin(FpSrcReg1);'
2909699Snilay@cs.wisc.edu        op_class = 'FloatSqrtOp'
2919371Snilay@cs.wisc.edu
2929582Snilay@cs.wisc.edu    class Tanfp(FpUnaryOp):
2939582Snilay@cs.wisc.edu        code = 'FpDestReg = tan(FpSrcReg1);'
2949699Snilay@cs.wisc.edu        op_class = 'FloatSqrtOp'
2959582Snilay@cs.wisc.edu
2969371Snilay@cs.wisc.edu
2975083Sgblack@eecs.umich.edu    # Conversion microops
2989371Snilay@cs.wisc.edu    class ConvOp(FpBinaryOp):
2995083Sgblack@eecs.umich.edu        abstract = True
3009699Snilay@cs.wisc.edu        op_class = 'FloatCvtOp'
3019894Sandreas@sandberg.pp.se        def __init__(self, dest, src1, **kwargs):
3026345Sgblack@eecs.umich.edu            super(ConvOp, self).__init__(dest, src1, \
3039894Sandreas@sandberg.pp.se                    "InstRegIndex(FLOATREG_MICROFP0)", \
3049894Sandreas@sandberg.pp.se                    **kwargs)
3055083Sgblack@eecs.umich.edu
3065083Sgblack@eecs.umich.edu    # These probably shouldn't look at the ExtMachInst directly to figure
3075083Sgblack@eecs.umich.edu    # out what size to use and should instead delegate that to the macroop's
3085083Sgblack@eecs.umich.edu    # constructor. That would be more efficient, and it would make the
3095083Sgblack@eecs.umich.edu    # microops a little more modular.
3105083Sgblack@eecs.umich.edu    class cvtf_i2d(ConvOp):
3115083Sgblack@eecs.umich.edu        code = '''
3125083Sgblack@eecs.umich.edu            X86IntReg intReg = SSrcReg1;
3135083Sgblack@eecs.umich.edu            if (REX_W)
3145083Sgblack@eecs.umich.edu                FpDestReg = intReg.SR;
3155083Sgblack@eecs.umich.edu            else
3165083Sgblack@eecs.umich.edu                FpDestReg = intReg.SE;
3175083Sgblack@eecs.umich.edu            '''
3185083Sgblack@eecs.umich.edu
3195083Sgblack@eecs.umich.edu    class cvtf_i2d_hi(ConvOp):
3205083Sgblack@eecs.umich.edu        code = 'FpDestReg = bits(SSrcReg1, 63, 32);'
3215083Sgblack@eecs.umich.edu
3225083Sgblack@eecs.umich.edu    class cvtf_d2i(ConvOp):
3235083Sgblack@eecs.umich.edu        code = '''
3245083Sgblack@eecs.umich.edu            int64_t intSrcReg1 = static_cast<int64_t>(FpSrcReg1);
3255083Sgblack@eecs.umich.edu            if (REX_W)
3265083Sgblack@eecs.umich.edu                SDestReg = intSrcReg1;
3275083Sgblack@eecs.umich.edu            else
3285083Sgblack@eecs.umich.edu                SDestReg = merge(SDestReg, intSrcReg1, 4);
3295083Sgblack@eecs.umich.edu            '''
3305083Sgblack@eecs.umich.edu
3319894Sandreas@sandberg.pp.se    # Convert two integers registers representing an 80-bit floating
3329894Sandreas@sandberg.pp.se    # point number to an x87 register.
3339894Sandreas@sandberg.pp.se    class cvtint_fp80(FpBinaryOp):
3349894Sandreas@sandberg.pp.se        code = '''
3359894Sandreas@sandberg.pp.se            uint8_t bits[10];
3369894Sandreas@sandberg.pp.se            *(uint64_t *)(bits + 0) = SSrcReg1;
3379894Sandreas@sandberg.pp.se            *(uint16_t *)(bits + 8) = (uint16_t)SSrcReg2;
3389894Sandreas@sandberg.pp.se            FpDestReg = loadFloat80(bits);
3399894Sandreas@sandberg.pp.se            '''
3409894Sandreas@sandberg.pp.se
3419894Sandreas@sandberg.pp.se    # Convert an x87 register (double) into extended precision and
3429894Sandreas@sandberg.pp.se    # extract the highest 64 bits.
3439894Sandreas@sandberg.pp.se    class cvtfp80h_int(ConvOp):
3449894Sandreas@sandberg.pp.se        code = '''
3459894Sandreas@sandberg.pp.se            char bits[10];
3469894Sandreas@sandberg.pp.se            storeFloat80(bits, FpSrcReg1);
3479894Sandreas@sandberg.pp.se            SDestReg = *(uint64_t *)(bits + 0);
3489894Sandreas@sandberg.pp.se            '''
3499894Sandreas@sandberg.pp.se
3509894Sandreas@sandberg.pp.se    # Convert an x87 register (double) into extended precision and
3519894Sandreas@sandberg.pp.se    # extract the lowest 16 bits.
3529894Sandreas@sandberg.pp.se    class cvtfp80l_int(ConvOp):
3539894Sandreas@sandberg.pp.se        code = '''
3549894Sandreas@sandberg.pp.se            char bits[10];
3559894Sandreas@sandberg.pp.se            storeFloat80(bits, FpSrcReg1);
3569894Sandreas@sandberg.pp.se            SDestReg = *(uint16_t *)(bits + 8);
3579894Sandreas@sandberg.pp.se            '''
3589894Sandreas@sandberg.pp.se
3595083Sgblack@eecs.umich.edu    # These need to consider size at some point. They'll always use doubles
3605083Sgblack@eecs.umich.edu    # for the moment.
3619371Snilay@cs.wisc.edu    class addfp(FpBinaryOp):
3625083Sgblack@eecs.umich.edu        code = 'FpDestReg = FpSrcReg1 + FpSrcReg2;'
3635083Sgblack@eecs.umich.edu
3649371Snilay@cs.wisc.edu    class mulfp(FpBinaryOp):
3655083Sgblack@eecs.umich.edu        code = 'FpDestReg = FpSrcReg1 * FpSrcReg2;'
3669699Snilay@cs.wisc.edu        op_class = 'FloatMultOp'
3675083Sgblack@eecs.umich.edu
3689371Snilay@cs.wisc.edu    class divfp(FpBinaryOp):
3695083Sgblack@eecs.umich.edu        code = 'FpDestReg = FpSrcReg1 / FpSrcReg2;'
3709699Snilay@cs.wisc.edu        op_class = 'FloatDivOp'
3715083Sgblack@eecs.umich.edu
3729371Snilay@cs.wisc.edu    class subfp(FpBinaryOp):
3735083Sgblack@eecs.umich.edu        code = 'FpDestReg = FpSrcReg1 - FpSrcReg2;'
3745083Sgblack@eecs.umich.edu
3759582Snilay@cs.wisc.edu    class Yl2xFp(FpBinaryOp):
3769582Snilay@cs.wisc.edu        code = '''
3779582Snilay@cs.wisc.edu            FpDestReg = FpSrcReg2 * (log(FpSrcReg1) / log(2));
3789582Snilay@cs.wisc.edu        '''
3799699Snilay@cs.wisc.edu        op_class = 'FloatSqrtOp'
3809582Snilay@cs.wisc.edu
3819582Snilay@cs.wisc.edu    class PremFp(FpBinaryOp):
3829582Snilay@cs.wisc.edu        code = '''
38313613Sgabeblack@google.com            RegVal new_fsw = FSW;
3849761Sandreas@sandberg.pp.se            int src1_exp;
3859761Sandreas@sandberg.pp.se            int src2_exp;
3869761Sandreas@sandberg.pp.se            std::frexp(FpSrcReg1, &src1_exp);
3879761Sandreas@sandberg.pp.se            std::frexp(FpSrcReg2, &src2_exp);
3889761Sandreas@sandberg.pp.se
38913441Sgabeblack@google.com            const int d = src2_exp - src1_exp;
3909761Sandreas@sandberg.pp.se            if (d < 64) {
39113441Sgabeblack@google.com                const int64_t q = std::trunc(FpSrcReg2 / FpSrcReg1);
3929761Sandreas@sandberg.pp.se                FpDestReg = FpSrcReg2 - FpSrcReg1 * q;
3939761Sandreas@sandberg.pp.se                new_fsw &= ~(CC0Bit | CC1Bit | CC2Bit | CC2Bit);
3949761Sandreas@sandberg.pp.se                new_fsw |= (q & 0x1) ? CC1Bit : 0;
3959761Sandreas@sandberg.pp.se                new_fsw |= (q & 0x2) ? CC3Bit : 0;
3969761Sandreas@sandberg.pp.se                new_fsw |= (q & 0x4) ? CC0Bit : 0;
3979761Sandreas@sandberg.pp.se            } else {
39813441Sgabeblack@google.com                const int n = 42;
39913441Sgabeblack@google.com                const int64_t qq = std::trunc(
40013441Sgabeblack@google.com                    FpSrcReg2 / std::ldexp(FpSrcReg1, d - n));
4019761Sandreas@sandberg.pp.se                FpDestReg = FpSrcReg2 - std::ldexp(FpSrcReg1 * qq, d - n);
4029761Sandreas@sandberg.pp.se                new_fsw |= CC2Bit;
4039761Sandreas@sandberg.pp.se            }
4049582Snilay@cs.wisc.edu        '''
4059699Snilay@cs.wisc.edu        op_class = 'FloatDivOp'
4069582Snilay@cs.wisc.edu
4079761Sandreas@sandberg.pp.se        flag_code = 'FSW = new_fsw;'
4089761Sandreas@sandberg.pp.se
4099371Snilay@cs.wisc.edu    class Compfp(FpBinaryOp):
4109765Sandreas@sandberg.pp.se        def __init__(self, src1, src2, spm=0, setStatus=False, updateFTW=True, \
4115083Sgblack@eecs.umich.edu                dataSize="env.dataSize"):
4126345Sgblack@eecs.umich.edu            super(Compfp, self).__init__("InstRegIndex(FLOATREG_MICROFP0)", \
4139765Sandreas@sandberg.pp.se                    src1, src2, spm, setStatus, updateFTW, dataSize)
4145083Sgblack@eecs.umich.edu        # This class sets the condition codes in rflags according to the
4155083Sgblack@eecs.umich.edu        # rules for comparing floating point.
4165083Sgblack@eecs.umich.edu        code = '''
4175083Sgblack@eecs.umich.edu            //               ZF PF CF
4185083Sgblack@eecs.umich.edu            // Unordered      1  1  1
4195083Sgblack@eecs.umich.edu            // Greater than   0  0  0
4205083Sgblack@eecs.umich.edu            // Less than      0  0  1
4215083Sgblack@eecs.umich.edu            // Equal          1  0  0
4225083Sgblack@eecs.umich.edu            //           OF = SF = AF = 0
4239010Snilay@cs.wisc.edu            ccFlagBits = ccFlagBits & ~(SFBit | AFBit | ZFBit | PFBit);
4249010Snilay@cs.wisc.edu            cfofBits = cfofBits & ~(OFBit | CFBit);
4259010Snilay@cs.wisc.edu
4269010Snilay@cs.wisc.edu            if (std::isnan(FpSrcReg1) || std::isnan(FpSrcReg2)) {
4279010Snilay@cs.wisc.edu                ccFlagBits = ccFlagBits | (ZFBit | PFBit);
4289010Snilay@cs.wisc.edu                cfofBits = cfofBits | CFBit;
4299010Snilay@cs.wisc.edu            }
4305083Sgblack@eecs.umich.edu            else if(FpSrcReg1 < FpSrcReg2)
4319010Snilay@cs.wisc.edu                cfofBits = cfofBits | CFBit;
4325083Sgblack@eecs.umich.edu            else if(FpSrcReg1 == FpSrcReg2)
4335083Sgblack@eecs.umich.edu                ccFlagBits = ccFlagBits | ZFBit;
4345083Sgblack@eecs.umich.edu        '''
4359699Snilay@cs.wisc.edu        op_class = 'FloatCmpOp'
4369470Snilay@cs.wisc.edu
4379470Snilay@cs.wisc.edu    class absfp(FpUnaryOp):
4389470Snilay@cs.wisc.edu        code = 'FpDestReg = fabs(FpSrcReg1);'
4399758Sandreas@sandberg.pp.se        flag_code = 'FSW = FSW & (~CC1Bit);'
4409470Snilay@cs.wisc.edu
4419470Snilay@cs.wisc.edu    class chsfp(FpUnaryOp):
4429470Snilay@cs.wisc.edu        code = 'FpDestReg = (-1) * (FpSrcReg1);'
4439758Sandreas@sandberg.pp.se        flag_code = 'FSW = FSW & (~CC1Bit);'
4449893Sandreas@sandberg.pp.se
4459893Sandreas@sandberg.pp.se    class Pop87(FpUnaryOp):
4469893Sandreas@sandberg.pp.se        def __init__(self, spm=1, UpdateFTW=True):
4479893Sandreas@sandberg.pp.se            super(Pop87, self).__init__(               \
4489893Sandreas@sandberg.pp.se                    "InstRegIndex(FLOATREG_MICROFP0)", \
4499893Sandreas@sandberg.pp.se                    "InstRegIndex(FLOATREG_MICROFP0)", \
4509893Sandreas@sandberg.pp.se                    spm=spm, SetStatus=False, UpdateFTW=UpdateFTW)
4519893Sandreas@sandberg.pp.se
4529893Sandreas@sandberg.pp.se        code = ''
45310313Snilay@cs.wisc.edu        op_class = 'IntAluOp'
4545083Sgblack@eecs.umich.edu}};
455