Skip to content

[libc] fix -Wshorten-64-to-32 #74392

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from

Conversation

nickdesaulniers
Copy link
Member

@nickdesaulniers nickdesaulniers commented Dec 5, 2023

32b arm failed when enabling -Werror:

libc/src/string/memory_utils/utils.h:50:36: error: implicit conversion loses
integer precision: 'unsigned long long' to 'size_t' (aka 'unsigned int')
[-Werror,-Wshorten-64-to-32]
  return value == 0 ? value : 1ULL << log2s(value);
  ~~~~~~                      ~~~~~^~~~~~~~~~~~~~~
libc/src/string/memory_utils/utils.h:56:50: error: implicit conversion loses
integer precision: 'unsigned long long' to 'size_t' (aka 'unsigned int')
[-Werror,-Wshorten-64-to-32]
  return is_power2_or_zero(value) ? value : 1ULL << (log2s(value) + 1);
  ~~~~~~                                    ~~~~~^~~~~~~~~~~~~~~~~~~~~

arm-linux-gnueabi is ILP32. size_t is an 'unsigned int' and 'long' is the same
bit width as int.

Use an unsigned long literal rather than an unsigned long long literal to avoid
the implicit promotion to unsigned long long which would then be truncated to
unsigned int, as hinted at by the warning.

32b arm failed when enabling -Werror:
libc/src/string/memory_utils/utils.h:50:36: error: implicit conversion loses
integer precision: 'unsigned long long' to 'size_t' (aka 'unsigned int')
[-Werror,-Wshorten-64-to-32]
  return value == 0 ? value : 1ULL << log2s(value);
  ~~~~~~                      ~~~~~^~~~~~~~~~~~~~~
libc/src/string/memory_utils/utils.h:56:50: error: implicit conversion loses
integer precision: 'unsigned long long' to 'size_t' (aka 'unsigned int')
[-Werror,-Wshorten-64-to-32]
  return is_power2_or_zero(value) ? value : 1ULL << (log2s(value) + 1);
  ~~~~~~                                    ~~~~~^~~~~~~~~~~~~~~~~~~~~

arm-linux-gnueabi is ILP32. size_t is an 'unsigned int' and 'long' is the same
bit width as `int`.

Use an unsigned long literal rather than an unsigned long long literal to avoid
the implicit promotion to unsigned long long which would then be truncated to
unsigned int, as hinted at by the warning.

Link: https://lab.llvm.org/buildbot/#/builders/229/builds/20596
@llvmbot
Copy link
Member

llvmbot commented Dec 5, 2023

@llvm/pr-subscribers-libc

Author: Nick Desaulniers (nickdesaulniers)

Changes

32b arm failed when enabling -Werror:
libc/src/string/memory_utils/utils.h:50:36: error: implicit conversion loses
integer precision: 'unsigned long long' to 'size_t' (aka 'unsigned int')
[-Werror,-Wshorten-64-to-32]
return value == 0 ? value : 1ULL << log2s(value);

libc/src/string/memory_utils/utils.h:56:50: error: implicit conversion loses
integer precision: 'unsigned long long' to 'size_t' (aka 'unsigned int')
[-Werror,-Wshorten-64-to-32]
return is_power2_or_zero(value) ? value : 1ULL &lt;&lt; (log2s(value) + 1);
~~~~~~                                    ~~~~~^~~~~~~~~~~~~~~~~~~~~

arm-linux-gnueabi is ILP32. size_t is an 'unsigned int' and 'long' is the same
bit width as `int`.

Use an unsigned long literal rather than an unsigned long long literal to avoid
the implicit promotion to unsigned long long which would then be truncated to
unsigned int, as hinted at by the warning.


---
Full diff: https://github.com/llvm/llvm-project/pull/74392.diff


1 Files Affected:

- (modified) libc/src/string/memory_utils/utils.h (+2-2) 


``````````diff
diff --git a/libc/src/string/memory_utils/utils.h b/libc/src/string/memory_utils/utils.h
index 9c293185a2e9f..5e176f1100922 100644
--- a/libc/src/string/memory_utils/utils.h
+++ b/libc/src/string/memory_utils/utils.h
@@ -47,13 +47,13 @@ LIBC_INLINE constexpr size_t log2s(size_t value) {
// Returns the first power of two preceding value or value if it is already a
// power of two (or 0 when value is 0).
LIBC_INLINE constexpr size_t le_power2(size_t value) {
-  return value == 0 ? value : 1ULL << log2s(value);
+  return value == 0 ? value : 1UL << log2s(value);
}

// Returns the first power of two following value or value if it is already a
// power of two (or 0 when value is 0).
LIBC_INLINE constexpr size_t ge_power2(size_t value) {
-  return is_power2_or_zero(value) ? value : 1ULL << (log2s(value) + 1);
+  return is_power2_or_zero(value) ? value : 1UL << (log2s(value) + 1);
}

// Returns the number of bytes to substract from ptr to get to the previous

``````````

</details>

@gchatelet
Copy link
Contributor

This is a duplicate of #74260 and fixed by dead code removal in #73939 (relanded as #74446).

@gchatelet gchatelet closed this Dec 5, 2023
@nickdesaulniers nickdesaulniers deleted the le_pow2 branch December 5, 2023 16:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants