[flang-commits] [flang] 3aee64a - [flang] Fix build problems on osx

peter klausler via flang-commits flang-commits at lists.llvm.org
Thu Jun 18 11:16:02 PDT 2020


Author: peter klausler
Date: 2020-06-18T11:14:25-07:00
New Revision: 3aee64a9e038814a6d4c64d37d35e5ccf27e4934

URL: https://github.com/llvm/llvm-project/commit/3aee64a9e038814a6d4c64d37d35e5ccf27e4934
DIFF: https://github.com/llvm/llvm-project/commit/3aee64a9e038814a6d4c64d37d35e5ccf27e4934.diff

LOG: [flang] Fix build problems on osx

Summary:
Fix build problems encountered on osx in two files.
The one in character.cpp fixes a legitimate bug that
elicited a valid warning.

Reviewers: tskeith, PeteSteinfeld, sscalpone, jdoerfert, DavidTruby

Reviewed By: tskeith, PeteSteinfeld

Subscribers: llvm-commits, flang-commits

Tags: #flang, #llvm

Differential Revision: https://reviews.llvm.org/D82107

Added: 
    

Modified: 
    flang/include/flang/Common/bit-population-count.h
    flang/runtime/character.cpp

Removed: 
    


################################################################################
diff  --git a/flang/include/flang/Common/bit-population-count.h b/flang/include/flang/Common/bit-population-count.h
index 83d767ce0eee..e1aeb78f9a37 100644
--- a/flang/include/flang/Common/bit-population-count.h
+++ b/flang/include/flang/Common/bit-population-count.h
@@ -14,11 +14,14 @@
 // in its argument.  POPPAR is a parity function that returns true
 // when POPCNT is odd.
 
-#include <cinttypes>
+#include <climits>
+#include <type_traits>
 
 namespace Fortran::common {
 
-inline constexpr int BitPopulationCount(std::uint64_t x) {
+template <typename INT,
+    std::enable_if_t<(sizeof(INT) > 4 && sizeof(INT) <= 8), int> = 0>
+inline constexpr int BitPopulationCount(INT x) {
   // In each of the 32 2-bit fields, count the bits that were present.
   // This leaves a value [0..2] in each of these 2-bit fields.
   x = (x & 0x5555555555555555) + ((x >> 1) & 0x5555555555555555);
@@ -34,7 +37,9 @@ inline constexpr int BitPopulationCount(std::uint64_t x) {
   return (x & 0x7f) + (x >> 32);
 }
 
-inline constexpr int BitPopulationCount(std::uint32_t x) {
+template <typename INT,
+    std::enable_if_t<(sizeof(INT) > 2 && sizeof(INT) <= 4), int> = 0>
+inline constexpr int BitPopulationCount(INT x) {
   // In each of the 16 2-bit fields, count the bits that were present.
   // This leaves a value [0..2] in each of these 2-bit fields.
   x = (x & 0x55555555) + ((x >> 1) & 0x55555555);
@@ -48,7 +53,8 @@ inline constexpr int BitPopulationCount(std::uint32_t x) {
   return (x & 0x3f) + (x >> 16);
 }
 
-inline constexpr int BitPopulationCount(std::uint16_t x) {
+template <typename INT, std::enable_if_t<sizeof(INT) == 2, int> = 0>
+inline constexpr int BitPopulationCount(INT x) {
   // In each of the 8 2-bit fields, count the bits that were present.
   // This leaves a value [0..2] in each of these 2-bit fields.
   x = (x & 0x5555) + ((x >> 1) & 0x5555);
@@ -60,7 +66,8 @@ inline constexpr int BitPopulationCount(std::uint16_t x) {
   return (x & 0x1f) + (x >> 8);
 }
 
-inline constexpr int BitPopulationCount(std::uint8_t x) {
+template <typename INT, std::enable_if_t<sizeof(INT) == 1, int> = 0>
+inline constexpr int BitPopulationCount(INT x) {
   // In each of the 4 2-bit fields, count the bits that were present.
   // This leaves a value [0..2] in each of these 2-bit fields.
   x = (x & 0x55) + ((x >> 1) & 0x55);
@@ -70,17 +77,19 @@ inline constexpr int BitPopulationCount(std::uint8_t x) {
   return (x & 0xf) + (x >> 4);
 }
 
-template <typename UINT> inline constexpr bool Parity(UINT x) {
+template <typename INT> inline constexpr bool Parity(INT x) {
   return BitPopulationCount(x) & 1;
 }
 
 // "Parity is for farmers." -- Seymour R. Cray
 
-template <typename UINT> inline constexpr int TrailingZeroBitCount(UINT x) {
+template <typename INT> inline constexpr int TrailingZeroBitCount(INT x) {
   if ((x & 1) != 0) {
     return 0; // fast path for odd values
+  } else if (x == 0) {
+    return CHAR_BIT * sizeof x;
   } else {
-    return BitPopulationCount(static_cast<UINT>(x ^ (x - 1))) - !!x;
+    return BitPopulationCount(static_cast<INT>(x ^ (x - 1))) - 1;
   }
 }
 } // namespace Fortran::common

diff  --git a/flang/runtime/character.cpp b/flang/runtime/character.cpp
index 23d381676cdb..c241afd33fb7 100644
--- a/flang/runtime/character.cpp
+++ b/flang/runtime/character.cpp
@@ -688,7 +688,7 @@ void RTNAME(Trim)(Descriptor &result, const Descriptor &string,
   }
   result.Establish(string.type(), resultBytes, nullptr, 0);
   RUNTIME_CHECK(terminator, result.Allocate(nullptr, nullptr) == CFI_SUCCESS);
-  std::memcmp(result.OffsetElement(), string.OffsetElement(), resultBytes);
+  std::memcpy(result.OffsetElement(), string.OffsetElement(), resultBytes);
 }
 
 void RTNAME(CharacterMax)(Descriptor &accumulator, const Descriptor &x,


        


More information about the flang-commits mailing list