]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/opencsd/decoder/source/trc_printable_elem.cpp
Re-add opencsd as a vendor import from the dist directory
[FreeBSD/FreeBSD.git] / contrib / opencsd / decoder / source / trc_printable_elem.cpp
1 /*
2  * \file       trc_printable_elem.cpp
3  * \brief      OpenCSD : 
4  * 
5  * \copyright  Copyright (c) 2015, ARM Limited. All Rights Reserved.
6  */
7
8 /* 
9  * Redistribution and use in source and binary forms, with or without modification, 
10  * are permitted provided that the following conditions are met:
11  * 
12  * 1. Redistributions of source code must retain the above copyright notice, 
13  * this list of conditions and the following disclaimer.
14  * 
15  * 2. Redistributions in binary form must reproduce the above copyright notice, 
16  * this list of conditions and the following disclaimer in the documentation 
17  * and/or other materials provided with the distribution. 
18  * 
19  * 3. Neither the name of the copyright holder nor the names of its contributors 
20  * may be used to endorse or promote products derived from this software without 
21  * specific prior written permission. 
22  * 
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 'AS IS' AND 
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
25  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
26  * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
27  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 
28  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 
29  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 
30  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
31  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 
32  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
33  */ 
34
35 #include "common/trc_printable_elem.h"
36 #include <cassert>
37 #include <cstring>
38 #if defined(_MSC_VER) && (_MSC_VER < 1900)
39  /** VS2010 does not support inttypes - remove when VS2010 support is dropped */
40 #define __PRI64_PREFIX "ll"
41 #define PRIX64 __PRI64_PREFIX "X"
42 #define PRIu64 __PRI64_PREFIX "u"
43 #define PRIu32 "u"
44 #else
45 #include <cinttypes>
46 #endif
47
48 void trcPrintableElem::getValStr(std::string &valStr, const int valTotalBitSize, const int valValidBits, const uint64_t value, const bool asHex /* = true*/, const int updateBits /* = 0*/)
49 {
50     static char szStrBuffer[128];
51     static char szFormatBuffer[32];
52
53     assert((valTotalBitSize >= 4) && (valTotalBitSize <= 64));
54
55     uint64_t LimitMask = ~0ULL;
56     LimitMask >>= 64-valTotalBitSize;
57     valStr = "0x";
58
59     if(asHex)
60     {
61         int numHexChars = valTotalBitSize / 4;
62         numHexChars += ((valTotalBitSize % 4) > 0) ? 1 : 0;
63
64         int validChars = valValidBits / 4;
65         if((valValidBits % 4) > 0) validChars++; 
66                 if (validChars < numHexChars)
67                 {
68                         int QM = numHexChars - validChars;
69                         while (QM)
70                         {
71                                 QM--;
72                                 valStr += "?";
73                         }
74                 }
75         if(valValidBits > 32)
76         {
77             sprintf(szFormatBuffer,"%%0%dllX",validChars);  // create the format
78             sprintf(szStrBuffer,szFormatBuffer,value); // fill the buffer
79         }
80         else
81         {
82             sprintf(szFormatBuffer,"%%0%dlX",validChars);  // create the format
83             sprintf(szStrBuffer,szFormatBuffer,(uint32_t)value); // fill the buffer
84         }
85         valStr+=szStrBuffer;
86         if(valValidBits < valTotalBitSize)
87         {
88             sprintf(szStrBuffer," (%d:0)", valValidBits-1);
89             valStr+=szStrBuffer;
90         }
91         
92         if(updateBits)
93         {
94             uint64_t updateMask = ~0ULL;
95             updateMask >>= 64-updateBits;
96             sprintf(szStrBuffer," ~[0x%" PRIX64 "]",value & updateMask);
97             valStr+=szStrBuffer;
98         }
99     }
100     else
101     {
102         valStr = "";
103         if(valValidBits < valTotalBitSize)
104             valStr += "??";
105         if(valValidBits > 32)
106         {
107             sprintf(szStrBuffer,"%" PRIu64 ,value);
108         }
109         else
110         {
111             sprintf(szStrBuffer,"%" PRIu32 ,(uint32_t)value);
112         }
113         valStr +=  szStrBuffer;
114         if(valValidBits < valTotalBitSize)
115         {
116             sprintf(szStrBuffer," (%d:0)", valValidBits-1);
117             valStr+=szStrBuffer;
118         }
119     }
120 }
121
122
123 /* End of File trc_printable_elem.cpp */