Skip to content

Commit 5003c58

Browse files
authored
Merge pull request #11 from Sacusa/patch-1
Fix floating point conversions and comparisons; fix immediate value parsing
2 parents 61d490a + 239e5ae commit 5003c58

File tree

2 files changed

+254
-129
lines changed

2 files changed

+254
-129
lines changed

src/hwacc/LLVMRead/src/basic_block.cc

Lines changed: 72 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1762,57 +1762,83 @@ BasicBlock::addNode(std::shared_ptr<InstructionBase> Node) {
17621762

17631763
std::string
17641764
BasicBlock::convertImmediate(std::string dataType, std::string immediateValue) {
1765-
int arr1 = 0;
1766-
int arr2 = 0;
1767-
int integer = 0;
1768-
double doub;
1769-
float flt;
1770-
std::string temp;
1771-
char *array = &immediateValue[0];
1772-
char *end;
1773-
if (_debug) DPRINTF(LLVMParse, "Type: %s, Value: %s\n",dataType, immediateValue);
1774-
if(dataType.compare("double") == 0) {
1775-
if(immediateValue[1] == 'x') {
1776-
doub = strtol(array, &end, 16);
1777-
uint64_t convert = (uint64_t) doub;
1778-
doub = *((double*)&convert);
1779-
temp = std::to_string(doub);
1780-
} else temp = sciToDecimal(immediateValue);
1781-
} else if(dataType.compare("float") == 0) {
1782-
if(immediateValue[1] == 'x') {
1783-
flt = strtol(array, &end, 16);
1784-
uint64_t convert = (uint64_t) flt;
1785-
doub = *((float*)&convert);
1786-
temp = std::to_string(flt);
1787-
} else temp = sciToDecimal(immediateValue);
1788-
} else { // Integer Value
1789-
if(immediateValue[1] == 'x') {
1790-
integer = strtol(array, &end, 0);
1791-
temp = std::to_string(integer);
1792-
} else temp = sciToDecimal(immediateValue);
1793-
}
1794-
if (_debug) DPRINTF(LLVMParse, "Value: %s, %d, %d, %d\n", temp, doub, arr1, arr2);
1795-
return temp;
1765+
int arr1 = 0;
1766+
int arr2 = 0;
1767+
int integer = 0;
1768+
double doub;
1769+
float flt;
1770+
std::string temp;
1771+
char *array = &immediateValue[0];
1772+
char *end;
1773+
if (_debug) DPRINTF(LLVMParse, "Type: %s, Value: %s\n",dataType, immediateValue);
1774+
if (dataType.compare("double") == 0) {
1775+
if (immediateValue[1] == 'x') {
1776+
uint64_t doub_hex = strtoll(array, &end, 16);
1777+
memcpy(&doub, &doub_hex, 8);
1778+
temp = std::to_string(doub);
1779+
} else temp = sciToDecimal(immediateValue);
1780+
} else if (dataType.compare("float") == 0) {
1781+
if (immediateValue[1] == 'x') {
1782+
// LLVM stores immediate arguments as double-precision values,
1783+
// even for single-precision arithmetic.
1784+
uint64_t doub_hex = strtoll(array, &end, 16);
1785+
memcpy(&doub, &doub_hex, 8);
1786+
flt = (float) doub;
1787+
temp = std::to_string(flt);
1788+
} else temp = sciToDecimal(immediateValue);
1789+
} else { // Integer Value
1790+
if (immediateValue[1] == 'x') {
1791+
integer = strtol(array, &end, 0);
1792+
temp = std::to_string(integer);
1793+
} else temp = sciToDecimal(immediateValue);
1794+
}
1795+
if (_debug) DPRINTF(LLVMParse, "Value: %s, %d, %d, %d\n", temp, doub, arr1, arr2);
1796+
return temp;
17961797
}
17971798

17981799
std::string
17991800
BasicBlock::sciToDecimal(std::string immediateValue) {
1800-
int decimalLocation = 0;
1801-
int magnitudeLoc = 0;
1802-
int magnitude = 0;
1801+
int decimalLocation = 0;
1802+
int magnitudeLoc = 0;
1803+
int sign;
1804+
int magnitude = 0;
18031805

1804-
for(int i = 0; i < immediateValue.length()-1; i++) {
1805-
if(immediateValue[i] == '.') decimalLocation = i;
1806-
if(immediateValue[i] == 'e') magnitudeLoc = i;
1807-
}
1808-
magnitude = atol(immediateValue.substr(magnitudeLoc+2).c_str());
1809-
for(int i = decimalLocation; i < decimalLocation+magnitude; i++) {
1810-
immediateValue[i] = immediateValue[i+1];
1811-
}
1812-
immediateValue[decimalLocation+magnitude] = '.';
1813-
immediateValue = immediateValue.substr(0,magnitudeLoc);
1806+
for (int i = 0; i < immediateValue.length()-1; i++) {
1807+
if (immediateValue[i] == '.') decimalLocation = i;
1808+
if (immediateValue[i] == 'e') {
1809+
magnitudeLoc = i;
1810+
if (immediateValue[i+1] == '-') sign = 1;
1811+
}
1812+
}
1813+
1814+
magnitude = atol(immediateValue.substr(magnitudeLoc+2).c_str());
18141815

1815-
return immediateValue;
1816+
if (sign == 1) {
1817+
if (magnitude >= decimalLocation) {
1818+
immediateValue = immediateValue.substr(0, decimalLocation) +
1819+
immediateValue.substr(decimalLocation+1,
1820+
magnitudeLoc-decimalLocation-1);
1821+
1822+
immediateValue = "0." +
1823+
std::string(magnitude-decimalLocation, '0') + immediateValue;
1824+
} else {
1825+
for (int i = decimalLocation; i > (decimalLocation-magnitude);
1826+
i--) {
1827+
char temp = immediateValue[i];
1828+
immediateValue[i] = immediateValue[i-1];
1829+
immediateValue[i-1] = temp;
1830+
}
1831+
immediateValue = immediateValue.substr(0,magnitudeLoc);
1832+
}
1833+
} else {
1834+
for (int i = decimalLocation; i < decimalLocation+magnitude; i++) {
1835+
immediateValue[i] = immediateValue[i+1];
1836+
}
1837+
immediateValue[decimalLocation+magnitude] = '.';
1838+
immediateValue = immediateValue.substr(0,magnitudeLoc);
1839+
}
1840+
1841+
return immediateValue;
18161842
}
18171843

18181844
void
@@ -2011,4 +2037,4 @@ BasicBlock::setDebug(bool dbg) {
20112037
for (auto node : _Nodes) {
20122038
node->_debug = dbg;
20132039
}
2014-
}
2040+
}

0 commit comments

Comments
 (0)