Skip to content

Commit

Permalink
Fix template bug in base::saturated_cast<>()
Browse files Browse the repository at this point in the history
Perform template check with UnderlyingType and clamp shift
value for SaturateFastAsmOp<>;

TBR=servolk@chromium.org

Change-Id: I8828b06a211515ee06d65c7dd1989b7cd84a841c
Reviewed-on: https://chromium-review.googlesource.com/572870
Commit-Queue: Justin Schuh <jschuh@chromium.org>
Reviewed-by: Justin Schuh <jschuh@chromium.org>
Cr-Commit-Position: refs/heads/master@{#486998}
  • Loading branch information
jschuh authored and Commit Bot committed Jul 15, 2017
1 parent 0bcc3cf commit c77d482
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 9 deletions.
6 changes: 2 additions & 4 deletions base/numerics/safe_conversions.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@

#include "base/numerics/safe_conversions_impl.h"

// TODO(jschuh): Investigate why these were failing to build for ios.
#if !defined(__APPLE__) && !defined(__native_client__) && \
(defined(__ARMEL__) || defined(__arch64__))
#if !defined(__native_client__) && (defined(__ARMEL__) || defined(__arch64__))
#include "base/numerics/safe_conversions_arm_impl.h"
#define BASE_HAS_OPTIMIZED_SAFE_CONVERSIONS (1)
#else
Expand Down Expand Up @@ -117,7 +115,7 @@ template <typename Dst,
constexpr Dst saturated_cast(Src value) {
using SrcType = typename UnderlyingType<Src>::type;
return !IsCompileTimeConstant(value) &&
SaturateFastAsmOp<Dst, Src>::is_supported &&
SaturateFastAsmOp<Dst, SrcType>::is_supported &&
std::is_same<SaturationHandler<Dst>,
SaturationDefaultLimits<Dst>>::value
? SaturateFastAsmOp<Dst, SrcType>::Do(value)
Expand Down
11 changes: 6 additions & 5 deletions base/numerics/safe_conversions_arm_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,15 @@ struct SaturateFastAsmOp {
if (std::is_signed<Dst>::value) {
asm("ssat %[dst], %[shift], %[src]"
: [dst] "=r"(result)
: [src] "r"(src), [shift] "n"(IntegerBitsPlusSign<Dst>::value));
: [src] "r"(src), [shift] "n"(IntegerBitsPlusSign<Dst>::value <= 32
? IntegerBitsPlusSign<Dst>::value
: 32));
} else {
asm("usat %[dst], %[shift], %[src]"
: [dst] "=r"(result)
:
[src] "r"(src), [shift] "n"(std::is_same<uint32_t, Dst>::value
? IntegerBitsPlusSign<Dst>::value - 1
: IntegerBitsPlusSign<Dst>::value));
: [src] "r"(src), [shift] "n"(IntegerBitsPlusSign<Dst>::value < 32
? IntegerBitsPlusSign<Dst>::value
: 31));
}
return static_cast<Dst>(result);
}
Expand Down

0 comments on commit c77d482

Please sign in to comment.