Skip to content

Commit cda65ec

Browse files
committed
Add functions to print floating-point values as strings of 1s and 0s
1 parent 00be740 commit cda65ec

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

include/utilities.hpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#ifndef UTILITIES_HPP
22
#define UTILITIES_HPP
3+
#include <bitset>
34
#include <cmath>
45
#include <iostream>
56
#include <iomanip>
@@ -39,6 +40,23 @@ void print_matrix(std::vector<T> A, const size_t m, const size_t n,
3940
}
4041
}
4142

43+
template <typename my_fp_type, typename my_int_type, size_t numTotalBits, size_t numFracBits>
44+
void printFloatingPointValueAsBinaryString(my_fp_type value) {
45+
46+
my_int_type intString = std::bit_cast<my_int_type>(value);
47+
48+
// Extract fields.
49+
bool sign = intString >> (numTotalBits - 1);
50+
my_int_type exponent = ((intString << 1) >> numFracBits);
51+
my_int_type fraction = (intString << (numTotalBits - numFracBits + 1)) >> (numTotalBits - numFracBits + 1);
52+
53+
// Print results
54+
std::cout << std::bitset<1>(sign) << " " << std::bitset<11>(exponent) << " " << std::bitset<52>(fraction) << std::endl;
55+
56+
// Full binary representation for reference
57+
// std::bitset<64>(intString)
58+
}
59+
4260
template <typename fp_t>
4361
std::vector<fp_t> reference_gemm (const std::vector<fp_t> &A,
4462
const std::vector<fp_t> &B,

0 commit comments

Comments
 (0)