// Copyright 2015, VIXL authors // All rights reserved. // // Redistribution and use in source and binary forms, with or without // modification, are permitted provided that the following conditions are met: // // * Redistributions of source code must retain the above copyright notice, // this list of conditions and the following disclaimer. // * Redistributions in binary form must reproduce the above copyright notice, // this list of conditions and the following disclaimer in the documentation // and/or other materials provided with the distribution. // * Neither the name of ARM Limited nor the names of its contributors may be // used to endorse or promote products derived from this software without // specific prior written permission. // // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS CONTRIBUTORS "AS IS" AND // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// Wrapper class for passing FP16 values through the assembler. // This is purely to aid with type checking/casting. class Float16 { public: explicit Float16(double dvalue);
Float16() : rawbits_(0x0) {} friend uint16_t Float16ToRawbits(Float16 value); friend Float16 RawbitsToFloat16(uint16_t bits);
protected:
uint16_t rawbits_;
};
// Floating point representation.
uint16_t Float16ToRawbits(Float16 value);
// Internal simulation class used solely by the simulator to // provide an abstraction layer for any half-precision arithmetic. class SimFloat16 : public Float16 { public: // TODO: We should investigate making this constructor explicit. // This is currently difficult to do due to a number of templated // functions in the simulator which rely on returning double values.
SimFloat16(double dvalue) : Float16(dvalue) {} // NOLINT(runtime/explicit)
SimFloat16(Float16 f) { // NOLINT(runtime/explicit)
this->rawbits_ = Float16ToRawbits(f);
}
SimFloat16() : Float16() {}
SimFloat16 operator-() const;
SimFloat16 operator+(SimFloat16 rhs) const;
SimFloat16 operator-(SimFloat16 rhs) const;
SimFloat16 operator*(SimFloat16 rhs) const;
SimFloat16 operator/(SimFloat16 rhs) const; booloperator<(SimFloat16 rhs) const; booloperator>(SimFloat16 rhs) const; booloperator==(SimFloat16 rhs) const; booloperator!=(SimFloat16 rhs) const; // This is necessary for conversions peformed in (macro asm) Fmov. booloperator==(double rhs) const; operatordouble() const;
};
} // namespace internal
// An fpclassify() function for 16-bit half-precision floats. int Float16Classify(Float16 value);
VIXL_DEPRECATED("Float16Classify", inlineint float16classify(uint16_t value)) { return Float16Classify(RawbitsToFloat16(value));
}
// Convert the NaN in 'num' to a quiet NaN. inlinedouble ToQuietNaN(double num) { const uint64_t kFP64QuietNaNMask = UINT64_C(0x0008000000000000);
VIXL_ASSERT(IsNaN(num)); return RawbitsToDouble(DoubleToRawbits(num) | kFP64QuietNaNMask);
}
template <typename T>
T ReverseBits(T value) {
VIXL_ASSERT((sizeof(value) == 1) || (sizeof(value) == 2) ||
(sizeof(value) == 4) || (sizeof(value) == 8));
T result = 0; for (unsigned i = 0; i < (sizeof(value) * 8); i++) {
result = (result << 1) | (value & 1);
value >>= 1;
} return result;
}
template <typename T> inline T SignExtend(T val, int bitSize) {
VIXL_ASSERT(bitSize > 0);
T mask = (T(2) << (bitSize - 1)) - T(1);
val &= mask;
T sign_bits = -((val >> (bitSize - 1)) << bitSize);
val |= sign_bits; return val;
}
template <typename T>
T ReverseBytes(T value, int block_bytes_log2) {
VIXL_ASSERT((sizeof(value) == 4) || (sizeof(value) == 8));
VIXL_ASSERT((1U << block_bytes_log2) <= sizeof(value)); // Split the 64-bit value into an 8-bit array, where b[0] is the least // significant byte, and b[7] is the most significant.
uint8_t bytes[8];
uint64_t mask = UINT64_C(0xff00000000000000); for (int i = 7; i >= 0; i--) {
bytes[i] = (static_cast<uint64_t>(value) & mask) >> (i * 8);
mask >>= 8;
}
// Permutation tables for REV instructions. // permute_table[0] is used by REV16_x, REV16_w // permute_table[1] is used by REV32_x, REV_w // permute_table[2] is used by REV_x
VIXL_ASSERT((0 < block_bytes_log2) && (block_bytes_log2 < 4)); staticconst uint8_t permute_table[3][8] = {{6, 7, 4, 5, 2, 3, 0, 1},
{4, 5, 6, 7, 0, 1, 2, 3},
{0, 1, 2, 3, 4, 5, 6, 7}};
uint64_t temp = 0; for (int i = 0; i < 8; i++) {
temp <<= 8;
temp |= bytes[permute_table[block_bytes_log2 - 1][i]];
}
// Pointer alignment // TODO: rename/refactor to make it specific to instructions. template <unsigned ALIGN, typename T> inlinebool IsAligned(T pointer) {
VIXL_ASSERT(sizeof(pointer) == sizeof(intptr_t)); // NOLINT(runtime/sizeof) // Use C-style casts to get static_cast behaviour for integral types (T), and // reinterpret_cast behaviour for other types. return IsAligned((intptr_t)(pointer), ALIGN);
}
// Increment a pointer until it has the specified alignment. The alignment must // be a power of two. template <class T>
T AlignUp(T pointer, typenameUnsigned<sizeof(T) * kBitsPerByte>::type alignment) {
VIXL_ASSERT(IsPowerOf2(alignment)); // Use C-style casts to get static_cast behaviour for integral types (T), and // reinterpret_cast behaviour for other types.
size_t mask = alignment - 1;
T result = (T)((pointer_raw + mask) & ~mask);
VIXL_ASSERT(result >= pointer);
return result;
}
// Decrement a pointer until it has the specified alignment. The alignment must // be a power of two. template <class T>
T AlignDown(T pointer, typenameUnsigned<sizeof(T) * kBitsPerByte>::type alignment) {
VIXL_ASSERT(IsPowerOf2(alignment)); // Use C-style casts to get static_cast behaviour for integral types (T), and // reinterpret_cast behaviour for other types.
// bits[29..25] are all set or all cleared.
uint32_t b_pattern = (bits >> 16) & 0x3e00; if (b_pattern != 0 && b_pattern != 0x3e00) { returnfalse;
} // bit[30] and bit[29] are opposite. if (((bits ^ (bits << 1)) & 0x40000000) == 0) { returnfalse;
} returntrue;
} staticbool IsImmFP64(double imm) { // Valid values will have the form: // aBbb.bbbb.bbcd.efgh.0000.0000.0000.0000 // 0000.0000.0000.0000.0000.0000.0000.0000
uint64_t bits = DoubleToRawbits(imm); // bits[47..0] are cleared. if ((bits & 0x0000ffffffffffff) != 0) { returnfalse;
} // bits[61..54] are all set or all cleared.
uint32_t b_pattern = (bits >> 48) & 0x3fc0; if ((b_pattern != 0) && (b_pattern != 0x3fc0)) { returnfalse;
} // bit[62] and bit[61] are opposite. if (((bits ^ (bits << 1)) & (UINT64_C(1) << 62)) == 0) { returnfalse;
} returntrue;
}
};
class BitField { // ForEachBitHelper is a functor that will call // bool ForEachBitHelper::execute(ElementType id) const // and expects a boolean in return whether to continue (if true) // or stop (if false) // check_set will check if the bits are on (true) or off(false) template <typename ForEachBitHelper, bool check_set> bool ForEachBit(const ForEachBitHelper& helper) { for (int i = 0; static_cast<size_t>(i) < bitfield_.size(); i++) { if (bitfield_[i] == check_set) if (!helper.execute(i)) returnfalse;
} returntrue;
}
// For each bit not set in the bitfield call the execute functor // execute. // ForEachBitSetHelper::execute returns true if the iteration through // the bits can continue, otherwise it will stop. // struct ForEachBitSetHelper { // bool execute(int /*id*/) { return false; } // }; template <typename ForEachBitNotSetHelper> bool ForEachBitNotSet(const ForEachBitNotSetHelper& helper) { return ForEachBit<ForEachBitNotSetHelper, false>(helper);
}
// For each bit set in the bitfield call the execute functor // execute. template <typename ForEachBitSetHelper> bool ForEachBitSet(const ForEachBitSetHelper& helper) { return ForEachBit<ForEachBitSetHelper, true>(helper);
}
private:
std::vector<bool> bitfield_;
};
namespace internal {
typedef int64_t Int64; class Uint64; class Uint128;
enum FPRounding { // The first four values are encodable directly by FPCR<RMode>.
FPTieEven = 0x0,
FPPositiveInfinity = 0x1,
FPNegativeInfinity = 0x2,
FPZero = 0x3,
// The final rounding modes are only available when explicitly specified by // the instruction (such as with fcvta). It cannot be set in FPCR.
FPTieAway,
FPRoundOdd
};
// Assemble the specified IEEE-754 components into the target type and apply // appropriate rounding. // sign: 0 = positive, 1 = negative // exponent: Unbiased IEEE-754 exponent. // mantissa: The mantissa of the input. The top bit (which is not encoded for // normal IEEE-754 values) must not be omitted. This bit has the // value 'pow(2, exponent)'. // // The input value is assumed to be a normalized value. That is, the input may // not be infinity or NaN. If the source value is subnormal, it must be // normalized before calling this function such that the highest set bit in the // mantissa has the value 'pow(2, exponent)'. // // Callers should use FPRoundToFloat or FPRoundToDouble directly, rather than // calling a templated FPRound. template <class T, int ebits, int mbits>
T FPRound(int64_t sign,
int64_t exponent,
uint64_t mantissa,
FPRounding round_mode) {
VIXL_ASSERT((sign == 0) || (sign == 1));
// Only FPTieEven and FPRoundOdd rounding modes are implemented.
VIXL_ASSERT((round_mode == FPTieEven) || (round_mode == FPRoundOdd));
// Rounding can promote subnormals to normals, and normals to infinities. For // example, a double with exponent 127 (FLT_MAX_EXP) would appear to be // encodable as a float, but rounding based on the low-order mantissa bits // could make it overflow. With ties-to-even rounding, this value would become // an infinity.
// ---- Rounding Method ---- // // The exponent is irrelevant in the rounding operation, so we treat the // lowest-order bit that will fit into the result ('onebit') as having // the value '1'. Similarly, the highest-order bit that won't fit into // the result ('halfbit') has the value '0.5'. The 'point' sits between // 'onebit' and 'halfbit': // // These bits fit into the result. // |---------------------| // mantissa = 0bxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx // || // / | // / halfbit // onebit // // For subnormal outputs, the range of representable bits is smaller and // the position of onebit and halfbit depends on the exponent of the // input, but the method is otherwise similar. // // onebit(frac) // | // | halfbit(frac) halfbit(adjusted) // | / / // | | | // 0b00.0 (exact) -> 0b00.0 (exact) -> 0b00 // 0b00.0... -> 0b00.0... -> 0b00 // 0b00.1 (exact) -> 0b00.0111..111 -> 0b00 // 0b00.1... -> 0b00.1... -> 0b01 // 0b01.0 (exact) -> 0b01.0 (exact) -> 0b01 // 0b01.0... -> 0b01.0... -> 0b01 // 0b01.1 (exact) -> 0b01.1 (exact) -> 0b10 // 0b01.1... -> 0b01.1... -> 0b10 // 0b10.0 (exact) -> 0b10.0 (exact) -> 0b10 // 0b10.0... -> 0b10.0... -> 0b10 // 0b10.1 (exact) -> 0b10.0111..111 -> 0b10 // 0b10.1... -> 0b10.1... -> 0b11 // 0b11.0 (exact) -> 0b11.0 (exact) -> 0b11 // ... / | / | // / | / | // / | // adjusted = frac - (halfbit(mantissa) & ~onebit(frac)); / | // // mantissa = (mantissa >> shift) + halfbit(adjusted);
// Bail out early for zero inputs. if (mantissa == 0) { returnstatic_cast<T>(sign << sign_offset);
}
// If all bits in the exponent are set, the value is infinite or NaN. // This is true for all binary IEEE-754 formats. staticconstint infinite_exponent = (1 << ebits) - 1; staticconstint max_normal_exponent = infinite_exponent - 1;
// Apply the exponent bias to encode it for the result. Doing this early makes // it easy to detect values that will be infinite or subnormal.
exponent += max_normal_exponent >> 1;
if (exponent > max_normal_exponent) { // Overflow: the input is too large for the result type to represent. if (round_mode == FPTieEven) { // FPTieEven rounding mode handles overflows using infinities.
exponent = infinite_exponent;
mantissa = 0;
} else {
VIXL_ASSERT(round_mode == FPRoundOdd); // FPRoundOdd rounding mode handles overflows using the largest magnitude // normal number.
exponent = max_normal_exponent;
mantissa = (UINT64_C(1) << exponent_offset) - 1;
} returnstatic_cast<T>((sign << sign_offset) |
(exponent << exponent_offset) |
(mantissa << mantissa_offset));
}
// Calculate the shift required to move the top mantissa bit to the proper // place in the destination type. constint highest_significant_bit = 63 - CountLeadingZeros(mantissa); int shift = highest_significant_bit - mbits;
if (exponent <= 0) { // The output will be subnormal (before rounding). // For subnormal outputs, the shift must be adjusted by the exponent. The +1 // is necessary because the exponent of a subnormal value (encoded as 0) is // the same as the exponent of the smallest normal value (encoded as 1).
shift += -exponent + 1;
// Handle inputs that would produce a zero output. // // Shifts higher than highest_significant_bit+1 will always produce a zero // result. A shift of exactly highest_significant_bit+1 might produce a // non-zero result after rounding. if (shift > (highest_significant_bit + 1)) { if (round_mode == FPTieEven) { // The result will always be +/-0.0. returnstatic_cast<T>(sign << sign_offset);
} else {
VIXL_ASSERT(round_mode == FPRoundOdd);
VIXL_ASSERT(mantissa != 0); // For FPRoundOdd, if the mantissa is too small to represent and // non-zero return the next "odd" value. returnstatic_cast<T>((sign << sign_offset) | 1);
}
}
// Properly encode the exponent for a subnormal output.
exponent = 0;
} else { // Clear the topmost mantissa bit, since this is not encoded in IEEE-754 // normal values.
mantissa &= ~(UINT64_C(1) << highest_significant_bit);
}
// The casts below are only well-defined for unsigned integers.
VIXL_STATIC_ASSERT(std::numeric_limits<T>::is_integer);
VIXL_STATIC_ASSERT(!std::numeric_limits<T>::is_signed);
if (shift > 0) { if (round_mode == FPTieEven) { // We have to shift the mantissa to the right. Some precision is lost, so // we need to apply rounding.
uint64_t onebit_mantissa = (mantissa >> (shift)) & 1;
uint64_t halfbit_mantissa = (mantissa >> (shift - 1)) & 1;
uint64_t adjustment = (halfbit_mantissa & ~onebit_mantissa);
uint64_t adjusted = mantissa - adjustment;
T halfbit_adjusted = (adjusted >> (shift - 1)) & 1;
T result = static_cast<T>((sign << sign_offset) | (exponent << exponent_offset) |
((mantissa >> shift) << mantissa_offset));
// A very large mantissa can overflow during rounding. If this happens, // the exponent should be incremented and the mantissa set to 1.0 // (encoded as 0). Applying halfbit_adjusted after assembling the float // has the nice side-effect that this case is handled for free. // // This also handles cases where a very large finite value overflows to // infinity, or where a very large subnormal value overflows to become // normal. return result + halfbit_adjusted;
} else {
VIXL_ASSERT(round_mode == FPRoundOdd); // If any bits at position halfbit or below are set, onebit (ie. the // bottom bit of the resulting mantissa) must be set.
uint64_t fractional_bits = mantissa & ((UINT64_C(1) << shift) - 1); if (fractional_bits != 0) {
mantissa |= UINT64_C(1) << shift;
}
returnstatic_cast<T>((sign << sign_offset) |
(exponent << exponent_offset) |
((mantissa >> shift) << mantissa_offset));
}
} else { // We have to shift the mantissa to the left (or not at all). The input // mantissa is exactly representable in the output mantissa, so apply no // rounding correction. returnstatic_cast<T>((sign << sign_offset) |
(exponent << exponent_offset) |
((mantissa << -shift) << mantissa_offset));
}
}
// See FPRound for a description of this function. inlinedouble FPRoundToDouble(int64_t sign,
int64_t exponent,
uint64_t mantissa,
FPRounding round_mode) {
uint64_t bits =
FPRound<uint64_t, kDoubleExponentBits, kDoubleMantissaBits>(sign,
exponent,
mantissa,
round_mode); return RawbitsToDouble(bits);
}
// See FPRound for a description of this function. inline Float16 FPRoundToFloat16(int64_t sign,
int64_t exponent,
uint64_t mantissa,
FPRounding round_mode) { return RawbitsToFloat16(
FPRound<uint16_t,
kFloat16ExponentBits,
kFloat16MantissaBits>(sign, exponent, mantissa, round_mode));
}
// See FPRound for a description of this function. staticinlinefloat FPRoundToFloat(int64_t sign,
int64_t exponent,
uint64_t mantissa,
FPRounding round_mode) {
uint32_t bits =
FPRound<uint32_t, kFloatExponentBits, kFloatMantissaBits>(sign,
exponent,
mantissa,
round_mode); return RawbitsToFloat(bits);
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.