sc_unsigned_subref.inc revision 12854
14661Sksewell@umich.edu/*****************************************************************************
25268Sksewell@umich.edu
35268Sksewell@umich.edu  Licensed to Accellera Systems Initiative Inc. (Accellera) under one or
44661Sksewell@umich.edu  more contributor license agreements.  See the NOTICE file distributed
55268Sksewell@umich.edu  with this work for additional information regarding copyright ownership.
65268Sksewell@umich.edu  Accellera licenses this file to you under the Apache License, Version 2.0
75268Sksewell@umich.edu  (the "License"); you may not use this file except in compliance with the
85268Sksewell@umich.edu  License.  You may obtain a copy of the License at
95268Sksewell@umich.edu
105268Sksewell@umich.edu    http://www.apache.org/licenses/LICENSE-2.0
115268Sksewell@umich.edu
125268Sksewell@umich.edu  Unless required by applicable law or agreed to in writing, software
135268Sksewell@umich.edu  distributed under the License is distributed on an "AS IS" BASIS,
145268Sksewell@umich.edu  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
154661Sksewell@umich.edu  implied.  See the License for the specific language governing
165268Sksewell@umich.edu  permissions and limitations under the License.
175268Sksewell@umich.edu
185268Sksewell@umich.edu *****************************************************************************/
195268Sksewell@umich.edu
205268Sksewell@umich.edu/*****************************************************************************
215268Sksewell@umich.edu
225268Sksewell@umich.edu  sc_unsigned_subref.h -- Proxy class that is declared in sc_unsigned.h.
235268Sksewell@umich.edu
245268Sksewell@umich.edu  Original Author: Ali Dasdan, Synopsys, Inc.
255268Sksewell@umich.edu
265268Sksewell@umich.edu *****************************************************************************/
275222Sksewell@umich.edu
285254Sksewell@umich.edu/*****************************************************************************
294661Sksewell@umich.edu
304661Sksewell@umich.edu  MODIFICATION LOG - modifiers, enter your name, affiliation, date and
314661Sksewell@umich.edu  changes you are making here.
324661Sksewell@umich.edu
334661Sksewell@umich.edu      Name, Affiliation, Date:
344661Sksewell@umich.edu  Description of Modification:
354661Sksewell@umich.edu
364661Sksewell@umich.edu *****************************************************************************/
374661Sksewell@umich.edu
384661Sksewell@umich.edu
394661Sksewell@umich.edu// ----------------------------------------------------------------------------
404661Sksewell@umich.edu//  CLASS : sc_unsigned_subref_r
416329Sgblack@eecs.umich.edu//
424661Sksewell@umich.edu//  Proxy class for sc_unsigned part selection (r-value only).
436376Sgblack@eecs.umich.edu// ----------------------------------------------------------------------------
446329Sgblack@eecs.umich.edu
454661Sksewell@umich.edu// concatenation support
464661Sksewell@umich.edu
474661Sksewell@umich.eduuint64
484661Sksewell@umich.edusc_unsigned_subref_r::concat_get_uint64() const // #### Speed up!
494661Sksewell@umich.edu{
504661Sksewell@umich.edu    sc_unsigned a(m_obj_p, m_left, m_right);
514661Sksewell@umich.edu    return a.to_uint64();
524661Sksewell@umich.edu}
534661Sksewell@umich.edu
544661Sksewell@umich.edu
554661Sksewell@umich.edubool
564661Sksewell@umich.edusc_unsigned_subref_r::concat_get_ctrl(sc_digit *dst_p, int low_i) const
574661Sksewell@umich.edu// #### Speed up!
586376Sgblack@eecs.umich.edu{
596376Sgblack@eecs.umich.edu    sc_unsigned a(m_obj_p, m_left, m_right);
604661Sksewell@umich.edu    return a.concat_get_ctrl(dst_p, low_i);
614661Sksewell@umich.edu}
624661Sksewell@umich.edu
634661Sksewell@umich.edubool
644661Sksewell@umich.edusc_unsigned_subref_r::concat_get_data(sc_digit *dst_p, int low_i) const
654661Sksewell@umich.edu// #### Speed up!
666376Sgblack@eecs.umich.edu{
676376Sgblack@eecs.umich.edu    sc_unsigned a(m_obj_p, m_left, m_right);
684661Sksewell@umich.edu    return a.concat_get_data(dst_p, low_i);
694661Sksewell@umich.edu}
704661Sksewell@umich.edu
714661Sksewell@umich.edu// implicit conversion to sc_unsigned
724661Sksewell@umich.edusc_unsigned_subref_r::operator sc_unsigned () const
734661Sksewell@umich.edu{
744661Sksewell@umich.edu    return sc_unsigned(m_obj_p, m_left, m_right);
754661Sksewell@umich.edu}
764661Sksewell@umich.edu
774661Sksewell@umich.edu
786378Sgblack@eecs.umich.edu// explicit conversions
796378Sgblack@eecs.umich.eduint
804661Sksewell@umich.edusc_unsigned_subref_r::to_int() const
814661Sksewell@umich.edu{
826378Sgblack@eecs.umich.edu    sc_unsigned a(m_obj_p, m_left, m_right);
836378Sgblack@eecs.umich.edu    return a.to_int();
846378Sgblack@eecs.umich.edu}
854661Sksewell@umich.edu
864661Sksewell@umich.eduunsigned int
874661Sksewell@umich.edusc_unsigned_subref_r::to_uint() const
884661Sksewell@umich.edu{
894661Sksewell@umich.edu    sc_unsigned a(m_obj_p, m_left, m_right);
904661Sksewell@umich.edu    return a.to_uint();
914661Sksewell@umich.edu}
924661Sksewell@umich.edu
934661Sksewell@umich.edulong
944661Sksewell@umich.edusc_unsigned_subref_r::to_long() const
954661Sksewell@umich.edu{
964661Sksewell@umich.edu    sc_unsigned a(m_obj_p, m_left, m_right);
974661Sksewell@umich.edu    return a.to_long();
984661Sksewell@umich.edu}
994661Sksewell@umich.edu
1004661Sksewell@umich.eduunsigned long
1014661Sksewell@umich.edusc_unsigned_subref_r::to_ulong() const
1026378Sgblack@eecs.umich.edu{
1036378Sgblack@eecs.umich.edu    sc_unsigned a(m_obj_p, m_left, m_right);
1046378Sgblack@eecs.umich.edu    return a.to_ulong();
1054661Sksewell@umich.edu}
1064661Sksewell@umich.edu
1074661Sksewell@umich.eduint64
1084661Sksewell@umich.edusc_unsigned_subref_r::to_int64() const
1094661Sksewell@umich.edu{
1104661Sksewell@umich.edu    sc_unsigned a(m_obj_p, m_left, m_right);
1114661Sksewell@umich.edu    return a.to_int64();
1126376Sgblack@eecs.umich.edu}
1136376Sgblack@eecs.umich.edu
1144661Sksewell@umich.eduuint64
1154661Sksewell@umich.edusc_unsigned_subref_r::to_uint64() const
1166221Snate@binkert.org{
1176376Sgblack@eecs.umich.edu    sc_unsigned a(m_obj_p, m_left, m_right);
1186376Sgblack@eecs.umich.edu    return a.to_uint64();
1196376Sgblack@eecs.umich.edu}
1204661Sksewell@umich.edu
1216376Sgblack@eecs.umich.edudouble
1224661Sksewell@umich.edusc_unsigned_subref_r::to_double() const
1236376Sgblack@eecs.umich.edu{
1246376Sgblack@eecs.umich.edu    sc_unsigned a(m_obj_p, m_left, m_right);
1254661Sksewell@umich.edu    return a.to_double();
1266376Sgblack@eecs.umich.edu}
1276376Sgblack@eecs.umich.edu
1284661Sksewell@umich.edu
1296376Sgblack@eecs.umich.edu// explicit conversion to character string
1306376Sgblack@eecs.umich.educonst std::string
1314661Sksewell@umich.edusc_unsigned_subref_r::to_string(sc_numrep numrep) const
1326376Sgblack@eecs.umich.edu{
1334661Sksewell@umich.edu    sc_unsigned a(length());
1344661Sksewell@umich.edu    a = *this;
1356376Sgblack@eecs.umich.edu    return a.to_string(numrep);
1366376Sgblack@eecs.umich.edu}
1374661Sksewell@umich.edu
1384661Sksewell@umich.educonst std::string
1396376Sgblack@eecs.umich.edusc_unsigned_subref_r::to_string(sc_numrep numrep, bool w_prefix) const
1404661Sksewell@umich.edu{
1416376Sgblack@eecs.umich.edu    sc_unsigned a(length());
1424661Sksewell@umich.edu    a = *this;
1436376Sgblack@eecs.umich.edu    return a.to_string(numrep, w_prefix);
1444661Sksewell@umich.edu}
1456376Sgblack@eecs.umich.edu
1464661Sksewell@umich.edu
1476376Sgblack@eecs.umich.edu// ----------------------------------------------------------------------------
1484661Sksewell@umich.edu//  CLASS : sc_unsigned_subref
1496376Sgblack@eecs.umich.edu//
1504661Sksewell@umich.edu//  Proxy class for sc_unsigned part selection (r-value and l-value).
1514661Sksewell@umich.edu// ----------------------------------------------------------------------------
1526376Sgblack@eecs.umich.edu
1536376Sgblack@eecs.umich.edu// assignment operators
1544661Sksewell@umich.edu
1554661Sksewell@umich.educonst sc_unsigned_subref &
1564661Sksewell@umich.edusc_unsigned_subref::operator = (const sc_unsigned_subref_r &a)
1574661Sksewell@umich.edu{
1584661Sksewell@umich.edu    return operator = ((sc_unsigned)(a));
1595991Ssteve.reinhardt@amd.com}
1604661Sksewell@umich.edu
1614661Sksewell@umich.educonst sc_unsigned_subref &
1624661Sksewell@umich.edusc_unsigned_subref::operator = (const sc_unsigned_subref &a)
1634661Sksewell@umich.edu{
1646376Sgblack@eecs.umich.edu    if (this == &a) {
1656376Sgblack@eecs.umich.edu        return *this;
1666376Sgblack@eecs.umich.edu    }
1674661Sksewell@umich.edu    return operator = ((sc_unsigned)(a));
1684661Sksewell@umich.edu}
1694661Sksewell@umich.edu
1704661Sksewell@umich.educonst sc_unsigned_subref &
1714661Sksewell@umich.edusc_unsigned_subref::operator = (const sc_unsigned &v)
1724661Sksewell@umich.edu{
1734661Sksewell@umich.edu    int i;
1744661Sksewell@umich.edu    int l = sc_min(m_left, v.nbits - 1 + m_right);
1754661Sksewell@umich.edu
1764661Sksewell@umich.edu    for (i = m_right; i <= l; ++i)
1776376Sgblack@eecs.umich.edu        m_obj_p->set(i, v.test(i - m_right));
1786376Sgblack@eecs.umich.edu    for (; i <= m_left; i++)
1794661Sksewell@umich.edu        m_obj_p->set(i, v.test(l));
1804661Sksewell@umich.edu    return *this;
1814661Sksewell@umich.edu}
1824661Sksewell@umich.edu
1836376Sgblack@eecs.umich.educonst sc_unsigned_subref &
1844661Sksewell@umich.edusc_unsigned_subref::operator = (const sc_signed_subref_r &v)
1856221Snate@binkert.org{
1866376Sgblack@eecs.umich.edu    return operator = ((sc_unsigned)(v));
1876376Sgblack@eecs.umich.edu}
1886376Sgblack@eecs.umich.edu
1896376Sgblack@eecs.umich.educonst sc_unsigned_subref &
1906376Sgblack@eecs.umich.edusc_unsigned_subref::operator = (const sc_signed &v)
1916376Sgblack@eecs.umich.edu{
1924661Sksewell@umich.edu    int i;
1936376Sgblack@eecs.umich.edu    int l = sc_min(m_left, v.nbits - 1 + m_right);
1946376Sgblack@eecs.umich.edu
1956376Sgblack@eecs.umich.edu    for (i = m_right; i <= l; ++i)
1966376Sgblack@eecs.umich.edu        m_obj_p->set(i, v.test(i - m_right));
1976376Sgblack@eecs.umich.edu    for (; i <= m_left; i++)
1984661Sksewell@umich.edu        m_obj_p->set(i, 0);
1994661Sksewell@umich.edu    return *this;
2004661Sksewell@umich.edu}
2014661Sksewell@umich.edu
2024661Sksewell@umich.educonst sc_unsigned_subref &
2036376Sgblack@eecs.umich.edusc_unsigned_subref::operator = (unsigned long v)
2046376Sgblack@eecs.umich.edu{
2056376Sgblack@eecs.umich.edu    for (int i = m_right; i <= m_left; ++i) {
2066376Sgblack@eecs.umich.edu        m_obj_p->set(i, static_cast<bool>(v & 1));
2076376Sgblack@eecs.umich.edu        v >>= 1;
2084661Sksewell@umich.edu    }
2094661Sksewell@umich.edu    return *this;
2105561Snate@binkert.org}
2116376Sgblack@eecs.umich.edu
2126376Sgblack@eecs.umich.educonst sc_unsigned_subref &
2136376Sgblack@eecs.umich.edusc_unsigned_subref::operator = (long v)
2144661Sksewell@umich.edu{
2154661Sksewell@umich.edu    unsigned long v2 = (unsigned long)v;
2164661Sksewell@umich.edu    for (int i = m_right; i <= m_left; ++i) {
2174661Sksewell@umich.edu        m_obj_p->set(i, static_cast<bool>(v2 & 1));
2186376Sgblack@eecs.umich.edu        v2 >>= 1;
2196376Sgblack@eecs.umich.edu    }
2204661Sksewell@umich.edu    return *this;
2216376Sgblack@eecs.umich.edu}
2226376Sgblack@eecs.umich.edu
2234661Sksewell@umich.educonst sc_unsigned_subref &
2244661Sksewell@umich.edusc_unsigned_subref::operator = (uint64 v)
2254661Sksewell@umich.edu{
2264661Sksewell@umich.edu    for (int i = m_right; i <= m_left; ++i) {
2274661Sksewell@umich.edu        m_obj_p->set(i, static_cast<bool>(v & 1));
2284661Sksewell@umich.edu        v >>= 1;
2294661Sksewell@umich.edu    }
2304661Sksewell@umich.edu    return *this;
2314661Sksewell@umich.edu}
2324661Sksewell@umich.edu
2334661Sksewell@umich.educonst sc_unsigned_subref &
2344661Sksewell@umich.edusc_unsigned_subref::operator = (int64 v)
2354661Sksewell@umich.edu{
2364661Sksewell@umich.edu    uint64 v2 = (uint64)v;
2374661Sksewell@umich.edu    for (int i = m_right; i <= m_left; ++i) {
2384661Sksewell@umich.edu        m_obj_p->set(i, static_cast<bool>(v2 & 1));
2394661Sksewell@umich.edu        v2 >>= 1;
2406376Sgblack@eecs.umich.edu    }
2416376Sgblack@eecs.umich.edu    return *this;
2424661Sksewell@umich.edu}
2436376Sgblack@eecs.umich.edu
2446376Sgblack@eecs.umich.educonst sc_unsigned_subref &
2456376Sgblack@eecs.umich.edusc_unsigned_subref::operator = (double v)
2464661Sksewell@umich.edu{
2474661Sksewell@umich.edu    is_bad_double(v);
2484661Sksewell@umich.edu
2494661Sksewell@umich.edu    int nb = m_left - m_right + 1;
2504661Sksewell@umich.edu    int nd = DIV_CEIL(nb);
2514661Sksewell@umich.edu
2524661Sksewell@umich.edu#ifdef SC_MAX_NBITS
2534661Sksewell@umich.edu    sc_digit d[MAX_NDIGITS];
2544661Sksewell@umich.edu#else
2554661Sksewell@umich.edu    sc_digit *d = new sc_digit[nd];
2564661Sksewell@umich.edu#endif
2574661Sksewell@umich.edu
2586376Sgblack@eecs.umich.edu    if (v < 0)
2596376Sgblack@eecs.umich.edu        v = -v;
2604661Sksewell@umich.edu
2616376Sgblack@eecs.umich.edu    int i = 0;
2626376Sgblack@eecs.umich.edu    while (std::floor(v) && (i < nd)) {
2636376Sgblack@eecs.umich.edu        d[i++] = (sc_digit) std::floor(remainder(v, DIGIT_RADIX));
2644661Sksewell@umich.edu        v /= DIGIT_RADIX;
2656376Sgblack@eecs.umich.edu    }
2664661Sksewell@umich.edu    vec_zero(i, nd, d);
2674661Sksewell@umich.edu
2684661Sksewell@umich.edu    sc_digit val = 1; // Bit value.
2694661Sksewell@umich.edu    int j = 0; // Current digit in d.
2704661Sksewell@umich.edu
2714661Sksewell@umich.edu    i = 0; // Current bit in d.
272    while (i < nb) {
273        m_obj_p->set(i + m_right, (bool)(d[j] & val));
274        ++i;
275        if (i % BITS_PER_DIGIT == 0) {
276            val = 1;
277            ++j;
278        } else {
279            val <<= 1;
280        }
281    }
282#ifndef SC_MAX_NBITS
283    delete [] d;
284#endif
285    return *this;
286}
287
288const sc_unsigned_subref &
289sc_unsigned_subref::operator = (const sc_int_base &a)
290{
291    return operator = ((int64)a);
292}
293
294const sc_unsigned_subref &
295sc_unsigned_subref::operator = (const sc_uint_base &a)
296{
297    return operator = ((uint64)a);
298}
299
300// concatenation methods
301void
302sc_unsigned_subref::concat_set(int64 src, int low_i)
303{
304    int i;
305    int l;
306    bool sign = src < 0;
307
308    if (low_i < 64) {
309        src = src >> low_i;
310        l = sc_min(m_left, (63 - low_i) + m_right);
311        for (i = m_right; i <= l; ++i) {
312            m_obj_p->set(i, src & 1);
313            src = src >> 1;
314        }
315        for (; i <= m_left; i++)
316            m_obj_p->set(sign);
317    } else {
318        for (i = m_right; i <= m_left; ++i)
319            m_obj_p->set(sign);
320    }
321}
322
323void
324sc_unsigned_subref::concat_set(const sc_signed &src, int low_i)
325{
326    int i;
327    int l;
328    int src_i;
329    bool sign = src.test(src.nbits - 1);
330    l = src.nbits - (low_i + 1);
331    if (l >= 0) {
332        src_i = low_i;
333        l = sc_min(m_left, l + m_right);
334        for (i = m_right; i <= l; ++i, src_i++) {
335            m_obj_p->set(i, src.test(src_i));
336        }
337        for (; i <= m_left; i++)
338            m_obj_p->set(i, sign);
339    } else {
340        for (i = m_right; i <= m_left; ++i)
341            m_obj_p->set(i, sign);
342    }
343}
344
345void
346sc_unsigned_subref::concat_set(const sc_unsigned &src, int low_i)
347{
348    int i;
349    int l;
350    int src_i;
351    l = src.nbits - (low_i + 2);
352    if (l >= 0) {
353        src_i = low_i;
354        l = sc_min(m_left, l + m_right);
355        for (i = m_right; i <= l; ++i, src_i++) {
356            m_obj_p->set(i, src.test(src_i));
357        }
358        for (; i <= m_left; i++)
359            m_obj_p->set(i, false);
360    } else {
361        for (i = m_right; i <= m_left; ++i)
362            m_obj_p->set(i, false);
363    }
364}
365
366void
367sc_unsigned_subref::concat_set(uint64 src, int low_i)
368{
369    int  i;
370    int  l;
371
372    if (low_i < 64) {
373        src = src >> low_i;
374        l = sc_min(m_left, (63 - low_i) + m_right);
375        for (i = m_right; i <= l; ++i) {
376            m_obj_p->set(i, src & 1);
377            src = src >> 1;
378        }
379        for (; i <= m_left; i++)
380            m_obj_p->set(false);
381    } else {
382        for (i = m_right; i <= m_left; ++i)
383            m_obj_p->set(false);
384    }
385}
386
387// other methods
388void
389sc_unsigned_subref::scan(::std::istream &is)
390{
391    std::string s;
392    is >> s;
393    *this = s.c_str();
394}
395