utility.cc revision 2980
12686Sksewell@umich.edu/*
22686Sksewell@umich.edu * Copyright (c) 2003-2006 The Regents of The University of Michigan
32686Sksewell@umich.edu * All rights reserved.
42686Sksewell@umich.edu *
52686Sksewell@umich.edu * Redistribution and use in source and binary forms, with or without
62686Sksewell@umich.edu * modification, are permitted provided that the following conditions are
72686Sksewell@umich.edu * met: redistributions of source code must retain the above copyright
82686Sksewell@umich.edu * notice, this list of conditions and the following disclaimer;
92686Sksewell@umich.edu * redistributions in binary form must reproduce the above copyright
102686Sksewell@umich.edu * notice, this list of conditions and the following disclaimer in the
112686Sksewell@umich.edu * documentation and/or other materials provided with the distribution;
122686Sksewell@umich.edu * neither the name of the copyright holders nor the names of its
132686Sksewell@umich.edu * contributors may be used to endorse or promote products derived from
142686Sksewell@umich.edu * this software without specific prior written permission.
152686Sksewell@umich.edu *
162686Sksewell@umich.edu * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
172686Sksewell@umich.edu * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
182686Sksewell@umich.edu * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
192686Sksewell@umich.edu * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
202686Sksewell@umich.edu * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
212686Sksewell@umich.edu * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
222686Sksewell@umich.edu * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
232686Sksewell@umich.edu * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
242686Sksewell@umich.edu * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
252686Sksewell@umich.edu * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
262686Sksewell@umich.edu * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
272706Sksewell@umich.edu *
282706Sksewell@umich.edu * Authors: Korey Sewell
292686Sksewell@umich.edu */
302686Sksewell@umich.edu
312980Sgblack@eecs.umich.edu#include "arch/mips/regfile.hh"
322686Sksewell@umich.edu#include "arch/mips/utility.hh"
332980Sgblack@eecs.umich.edu#include "base/misc.hh"
342686Sksewell@umich.edu#include "base/bitfield.hh"
352686Sksewell@umich.edu
362686Sksewell@umich.eduusing namespace MipsISA;
372686Sksewell@umich.edu
382686Sksewell@umich.eduuint64_t
392686Sksewell@umich.eduMipsISA::fpConvert(ConvertType cvt_type, double fp_val)
402686Sksewell@umich.edu{
412686Sksewell@umich.edu
422686Sksewell@umich.edu    switch (cvt_type)
432686Sksewell@umich.edu    {
442686Sksewell@umich.edu      case SINGLE_TO_DOUBLE:
452686Sksewell@umich.edu        {
462686Sksewell@umich.edu            double sdouble_val = fp_val;
472686Sksewell@umich.edu            void  *sdouble_ptr = &sdouble_val;
482686Sksewell@umich.edu            uint64_t sdp_bits  = *(uint64_t *) sdouble_ptr;
492686Sksewell@umich.edu            return sdp_bits;
502686Sksewell@umich.edu        }
512686Sksewell@umich.edu
522686Sksewell@umich.edu      case SINGLE_TO_WORD:
532686Sksewell@umich.edu        {
542686Sksewell@umich.edu            int32_t sword_val  = (int32_t) fp_val;
552686Sksewell@umich.edu            void  *sword_ptr   = &sword_val;
562686Sksewell@umich.edu            uint64_t sword_bits= *(uint32_t *) sword_ptr;
572686Sksewell@umich.edu            return sword_bits;
582686Sksewell@umich.edu        }
592686Sksewell@umich.edu
602686Sksewell@umich.edu      case WORD_TO_SINGLE:
612686Sksewell@umich.edu        {
622686Sksewell@umich.edu            float wfloat_val   = fp_val;
632686Sksewell@umich.edu            void  *wfloat_ptr  = &wfloat_val;
642686Sksewell@umich.edu            uint64_t wfloat_bits = *(uint32_t *) wfloat_ptr;
652686Sksewell@umich.edu            return wfloat_bits;
662686Sksewell@umich.edu        }
672686Sksewell@umich.edu
682686Sksewell@umich.edu      case WORD_TO_DOUBLE:
692686Sksewell@umich.edu        {
702686Sksewell@umich.edu            double wdouble_val = fp_val;
712686Sksewell@umich.edu            void  *wdouble_ptr = &wdouble_val;
722686Sksewell@umich.edu            uint64_t wdp_bits  = *(uint64_t *) wdouble_ptr;
732686Sksewell@umich.edu            return wdp_bits;
742686Sksewell@umich.edu        }
752686Sksewell@umich.edu
762686Sksewell@umich.edu      default:
772686Sksewell@umich.edu        panic("Invalid Floating Point Conversion Type (%d). See \"types.hh\" for List of Conversions\n",cvt_type);
782686Sksewell@umich.edu        return 0;
792686Sksewell@umich.edu    }
802686Sksewell@umich.edu}
812686Sksewell@umich.edu
822686Sksewell@umich.edudouble
832686Sksewell@umich.eduMipsISA::roundFP(double val, int digits)
842686Sksewell@umich.edu{
852686Sksewell@umich.edu    double digit_offset = pow(10.0,digits);
862686Sksewell@umich.edu    val = val * digit_offset;
872686Sksewell@umich.edu    val = val + 0.5;
882686Sksewell@umich.edu    val = floor(val);
892686Sksewell@umich.edu    val = val / digit_offset;
902686Sksewell@umich.edu    return val;
912686Sksewell@umich.edu}
922686Sksewell@umich.edu
932686Sksewell@umich.edudouble
942686Sksewell@umich.eduMipsISA::truncFP(double val)
952686Sksewell@umich.edu{
962686Sksewell@umich.edu    int trunc_val = (int) val;
972686Sksewell@umich.edu    return (double) trunc_val;
982686Sksewell@umich.edu}
992686Sksewell@umich.edu
1002686Sksewell@umich.edubool
1012686Sksewell@umich.eduMipsISA::getCondCode(uint32_t fcsr, int cc_idx)
1022686Sksewell@umich.edu{
1032686Sksewell@umich.edu    int shift = (cc_idx == 0) ? 23 : cc_idx + 24;
1042686Sksewell@umich.edu    bool cc_val = (fcsr >> shift) & 0x00000001;
1052686Sksewell@umich.edu    return cc_val;
1062686Sksewell@umich.edu}
1072686Sksewell@umich.edu
1082686Sksewell@umich.eduuint32_t
1092686Sksewell@umich.eduMipsISA::genCCVector(uint32_t fcsr, int cc_num, uint32_t cc_val)
1102686Sksewell@umich.edu{
1112686Sksewell@umich.edu    int cc_idx = (cc_num == 0) ? 23 : cc_num + 24;
1122686Sksewell@umich.edu
1132686Sksewell@umich.edu    fcsr = bits(fcsr, 31, cc_idx + 1) << cc_idx + 1 |
1142686Sksewell@umich.edu           cc_val << cc_idx |
1152686Sksewell@umich.edu           bits(fcsr, cc_idx - 1, 0);
1162686Sksewell@umich.edu
1172686Sksewell@umich.edu    return fcsr;
1182686Sksewell@umich.edu}
1192686Sksewell@umich.edu
1202686Sksewell@umich.eduuint32_t
1212686Sksewell@umich.eduMipsISA::genInvalidVector(uint32_t fcsr_bits)
1222686Sksewell@umich.edu{
1232686Sksewell@umich.edu    //Set FCSR invalid in "flag" field
1242686Sksewell@umich.edu    int invalid_offset = Invalid + Flag_Field;
1252686Sksewell@umich.edu    fcsr_bits = fcsr_bits | (1 << invalid_offset);
1262686Sksewell@umich.edu
1272686Sksewell@umich.edu    //Set FCSR invalid in "cause" flag
1282686Sksewell@umich.edu    int cause_offset = Invalid + Cause_Field;
1292686Sksewell@umich.edu    fcsr_bits = fcsr_bits | (1 << cause_offset);
1302686Sksewell@umich.edu
1312686Sksewell@umich.edu    return fcsr_bits;
1322686Sksewell@umich.edu}
1332686Sksewell@umich.edu
1342686Sksewell@umich.edubool
1352686Sksewell@umich.eduMipsISA::isNan(void *val_ptr, int size)
1362686Sksewell@umich.edu{
1372686Sksewell@umich.edu    switch (size)
1382686Sksewell@umich.edu    {
1392686Sksewell@umich.edu      case 32:
1402686Sksewell@umich.edu        {
1412686Sksewell@umich.edu            uint32_t val_bits = *(uint32_t *) val_ptr;
1422686Sksewell@umich.edu            return (bits(val_bits, 30, 23) == 0xFF);
1432686Sksewell@umich.edu        }
1442686Sksewell@umich.edu
1452686Sksewell@umich.edu      case 64:
1462686Sksewell@umich.edu        {
1472686Sksewell@umich.edu            uint64_t val_bits = *(uint64_t *) val_ptr;
1482686Sksewell@umich.edu            return (bits(val_bits, 62, 52) == 0x7FF);
1492686Sksewell@umich.edu        }
1502686Sksewell@umich.edu
1512686Sksewell@umich.edu      default:
1522686Sksewell@umich.edu        panic("Type unsupported. Size mismatch\n");
1532686Sksewell@umich.edu    }
1542686Sksewell@umich.edu}
1552686Sksewell@umich.edu
1562686Sksewell@umich.edu
1572686Sksewell@umich.edubool
1582686Sksewell@umich.eduMipsISA::isQnan(void *val_ptr, int size)
1592686Sksewell@umich.edu{
1602686Sksewell@umich.edu    switch (size)
1612686Sksewell@umich.edu    {
1622686Sksewell@umich.edu      case 32:
1632686Sksewell@umich.edu        {
1642686Sksewell@umich.edu            uint32_t val_bits = *(uint32_t *) val_ptr;
1652686Sksewell@umich.edu            return (bits(val_bits, 30, 22) == 0x1FE);
1662686Sksewell@umich.edu        }
1672686Sksewell@umich.edu
1682686Sksewell@umich.edu      case 64:
1692686Sksewell@umich.edu        {
1702686Sksewell@umich.edu            uint64_t val_bits = *(uint64_t *) val_ptr;
1712686Sksewell@umich.edu            return (bits(val_bits, 62, 51) == 0xFFE);
1722686Sksewell@umich.edu        }
1732686Sksewell@umich.edu
1742686Sksewell@umich.edu      default:
1752686Sksewell@umich.edu        panic("Type unsupported. Size mismatch\n");
1762686Sksewell@umich.edu    }
1772686Sksewell@umich.edu}
1782686Sksewell@umich.edu
1792686Sksewell@umich.edubool
1802686Sksewell@umich.eduMipsISA::isSnan(void *val_ptr, int size)
1812686Sksewell@umich.edu{
1822686Sksewell@umich.edu    switch (size)
1832686Sksewell@umich.edu    {
1842686Sksewell@umich.edu      case 32:
1852686Sksewell@umich.edu        {
1862686Sksewell@umich.edu            uint32_t val_bits = *(uint32_t *) val_ptr;
1872686Sksewell@umich.edu            return (bits(val_bits, 30, 22) == 0x1FF);
1882686Sksewell@umich.edu        }
1892686Sksewell@umich.edu
1902686Sksewell@umich.edu      case 64:
1912686Sksewell@umich.edu        {
1922686Sksewell@umich.edu            uint64_t val_bits = *(uint64_t *) val_ptr;
1932686Sksewell@umich.edu            return (bits(val_bits, 62, 51) == 0xFFF);
1942686Sksewell@umich.edu        }
1952686Sksewell@umich.edu
1962686Sksewell@umich.edu      default:
1972686Sksewell@umich.edu        panic("Type unsupported. Size mismatch\n");
1982686Sksewell@umich.edu    }
1992686Sksewell@umich.edu}
200