@@ -105,7 +105,7 @@ inline xpti::uid128_t make_uid128(uint64_t FileID, uint64_t FuncID, int Line,
105105 int Col) {
106106 xpti::uid128_t UID;
107107 UID.p1 = (FileID << 32 ) | FuncID;
108- UID.p2 = ((uint64_t )Col << 32 ) | Line;
108+ UID.p2 = ((uint64_t )Col << 32 ) | ( uint64_t ) Line;
109109 UID.instance = 0 ;
110110 return UID;
111111}
@@ -147,7 +147,15 @@ struct hash_t {
147147 // /
148148 // / @param value A 64-bit integer for which the bit count is to be calculated.
149149 // / @return The number of bits required to represent the input value.
150- int bit_count (uint64_t value) { return (int )log2 (value) + 1 ; }
150+ unsigned bit_count (uint64_t value) {
151+ // FIXME: using std::log2 is imprecise. There is no guarantee that the
152+ // implementation has actual integer overload for log2 and it is allowed to
153+ // do a static_cast<double>(value) to compute the result. Not every integer
154+ // is represetntable as floating-point, meaning that byte representation of
155+ // value as double may not be the same as for integer, resulting in
156+ // different result.
157+ return static_cast <unsigned >(std::log2 (value)) + 1 ;
158+ }
151159
152160 // / @brief Compacts the file ID, function ID, line number, and column number
153161 // / into a single 64-bit hash value.
@@ -164,7 +172,8 @@ struct hash_t {
164172 // / @param col An integer that represents the column number.
165173 // / @return A 64-bit hash value that represents the compacted file ID,
166174 // / function ID, line number, and column number.
167- uint64_t compact (uint64_t file_id, uint64_t func_id, int line, int col) {
175+ uint64_t compact (uint64_t file_id, uint64_t func_id, uint64_t line,
176+ uint64_t col) {
168177 uint64_t funcB, lineB, colB;
169178 // Figure out the bit counts necessary to represent the input values
170179 funcB = bit_count (func_id);
@@ -177,9 +186,9 @@ struct hash_t {
177186 hash <<= funcB;
178187 hash = hash | func_id;
179188 hash <<= lineB;
180- hash = hash | ( uint64_t ) line;
189+ hash = hash | line;
181190 hash <<= colB;
182- hash = hash | ( uint64_t ) col;
191+ hash = hash | col;
183192#ifdef DEBUG
184193 uint64_t fileB = bit_count (file_id);
185194 std::cout << " Total bits: " << (fileB + funcB + lineB + colB) << " \n " ;
@@ -203,7 +212,7 @@ struct hash_t {
203212 // / @param line An integer that represents the line number.
204213 // / @return A 64-bit hash value that represents the compacted file ID,
205214 // / function ID, and line number.
206- uint64_t compact_short (uint64_t file_id, uint64_t func_id, int line) {
215+ uint64_t compact_short (uint64_t file_id, uint64_t func_id, uint64_t line) {
207216 uint64_t funcB, lineB;
208217 funcB = bit_count (func_id);
209218 lineB = bit_count (line);
@@ -215,7 +224,7 @@ struct hash_t {
215224 hash <<= funcB;
216225 hash = hash | func_id;
217226 hash <<= lineB;
218- hash = hash | ( uint64_t ) line;
227+ hash = hash | line;
219228#ifdef DEBUG
220229 uint64_t fileB = bit_count (file_id);
221230 std::cout << " Total bits: " << (fileB + funcB + lineB) << " \n " ;
@@ -312,7 +321,8 @@ struct uid_t {
312321} // namespace xpti
313322
314323namespace xpti {
315- constexpr int invalid_id = -1 ;
324+ template <typename T = uint32_t >
325+ constexpr T invalid_id = std::numeric_limits<T>::max();
316326constexpr uint64_t invalid_uid = 0 ;
317327constexpr uint8_t default_vendor = 0 ;
318328
@@ -428,11 +438,11 @@ struct payload_t {
428438 // / Absolute path of the source file; may have to to be unicode string
429439 const char *source_file = nullptr ;
430440 // / Line number information to correlate the trace point
431- uint32_t line_no = invalid_id;
441+ uint32_t line_no = invalid_id<> ;
432442 // / For a complex statement, column number may be needed to resolve the
433443 // / trace point; currently none of the compiler builtins return a valid
434444 // / column no
435- uint32_t column_no = invalid_id;
445+ uint32_t column_no = invalid_id<> ;
436446 // / Kernel/lambda/function address
437447 const void *code_ptr_va = nullptr ;
438448 // / Internal bookkeeping slot - do not change.
@@ -451,10 +461,10 @@ struct payload_t {
451461 // indicates a partial but valid payload.
452462 payload_t (const void *codeptr) {
453463 code_ptr_va = codeptr;
454- name = nullptr ; // /< Invalid name string pointer
455- source_file = nullptr ; // /< Invalid source file string pointer
456- line_no = invalid_id; // /< Invalid line number
457- column_no = invalid_id; // /< Invalid column number
464+ name = nullptr ; // /< Invalid name string pointer
465+ source_file = nullptr ; // /< Invalid source file string pointer
466+ line_no = invalid_id<> ; // /< Invalid line number
467+ column_no = invalid_id<> ; // /< Invalid column number
458468 if (codeptr) {
459469 flags = (uint64_t )payload_flag_t ::CodePointerAvailable;
460470 }
@@ -517,8 +527,8 @@ struct payload_t {
517527 // / Capture the rest of the parameters
518528 name = kname;
519529 source_file = sf;
520- line_no = line;
521- column_no = col;
530+ line_no = static_cast < uint32_t >( line) ;
531+ column_no = static_cast < uint32_t >( col) ;
522532 if (kname && kname[0 ] != ' \0 ' ) {
523533 flags = (uint64_t )payload_flag_t ::NameAvailable;
524534 }
0 commit comments