[llvm] [libc] [libc][math] Implement nan(f|l) functions (PR #76690)

via llvm-commits llvm-commits at lists.llvm.org
Thu Jan 4 09:28:39 PST 2024


================
@@ -1055,6 +1056,29 @@ hexadecimal_string_to_float(const char *__restrict src,
   return output;
 }
 
+LIBC_INLINE uint64_t
+nan_mantissa_from_ncharseq(const cpp::string_view ncharseq) {
+  uint64_t nan_mantissa = 0;
+
+  if (ncharseq.data() != nullptr && isdigit(*ncharseq.data())) {
+    // This is to prevent errors when StorageType is larger than 64
+    // bits, since strtointeger only supports up to 64 bits. This is
+    // actually more than is required by the specification, which says
+    // for the input type "NAN(n-char-sequence)" that "the meaning of
+    // the n-char sequence is implementation-defined."
+    auto strtoint_result = strtointeger<uint64_t>(ncharseq.data(), 0);
+
+    // We intentionally ignore the error returned in strtoint_result
+    // because the specification doesn't talk about exceptions while
+    // handling n-char-sequence.
+    nan_mantissa = strtoint_result.value;
----------------
lntue wrote:

it is already initialized to 0 above, so maybe it is better to just do
```
if (!strtoint_result.has_error())
  nan_mantissa = strtoint_result.result;
```

https://github.com/llvm/llvm-project/pull/76690


More information about the llvm-commits mailing list