Skip to content

Commit

Permalink
Format code
Browse files Browse the repository at this point in the history
  • Loading branch information
Triton Library committed Sep 6, 2024
1 parent f675ce2 commit ae652c8
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 36 deletions.
2 changes: 1 addition & 1 deletion .build_number
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1596
1597
2 changes: 1 addition & 1 deletion src/libtriton/arch/arm/aarch64/aarch64Cpu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -543,8 +543,8 @@ namespace triton {
imm.setShiftValue(op->shift.value);

inst.operands.push_back(triton::arch::OperandWrapper(imm));
}
break;
}

default:
/* NOTE: CIMM, and missing one are not supported yet. */
Expand Down
26 changes: 12 additions & 14 deletions src/libtriton/arch/immediate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,35 +30,33 @@ namespace triton {
this->setValue(value, size);
}


Immediate::Immediate(double value, triton::uint32 size /* bytes */, triton::arch::endianness_e platform_endianness) {
triton::uint64 imm_value;

auto need_swap = sys_endianness != platform_endianness;

if (size == sizeof(double)) {
static_assert(sizeof(double) == sizeof(triton::uint64),
"Unexpected double type size");
if (size == sizeof(double)) {
static_assert(sizeof(double) == sizeof(triton::uint64), "Unexpected double type size");
std::memcpy(&imm_value, &value, sizeof(double));
if (need_swap) {
imm_value = utils::byteswap(imm_value);
}
}

else if (size == sizeof(float)) { // single-precision
float fvalue = static_cast<float>(value);
triton::uint32 repr;
static_assert(sizeof(float) == sizeof(uint32_t),
"Unexpected float type size");
static_assert(sizeof(float) == sizeof(uint32_t), "Unexpected float type size");
std::memcpy(&repr, &fvalue, sizeof(float));

imm_value = need_swap ? static_cast<triton::uint64>(utils::byteswap(repr))
: static_cast<triton::uint64>(repr);
} else if (size == 2) { // half-precision
imm_value = need_swap ? static_cast<triton::uint64>(utils::byteswap(repr)) : static_cast<triton::uint64>(repr);
}

else if (size == 2) { // half-precision
float fvalue = static_cast<float>(value);
triton::uint16 repr = sf::f32_to_f16(fvalue);
imm_value = need_swap ? static_cast<triton::uint64>(utils::byteswap(repr))
: static_cast<triton::uint64>(repr);

triton::uint16 repr = triton::sf::f32_to_f16(fvalue);
imm_value = need_swap ? static_cast<triton::uint64>(utils::byteswap(repr)) : static_cast<triton::uint64>(repr);
}

else {
throw triton::exceptions::Immediate("Immediate::Immediate(double): Invalid encoding size.");
}
Expand Down
5 changes: 1 addition & 4 deletions src/libtriton/includes/triton/coreUtils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,7 @@ namespace triton {
template <> TRITON_EXPORT triton::uint512 cast(const triton::uint80& value);

template <typename T>
std::enable_if_t<
std::is_unsigned_v<T>,
T>
byteswap(T value) {
std::enable_if_t<std::is_unsigned_v<T>, T> byteswap(T value) {
std::array<std::byte, sizeof(value)> repr;
std::memcpy(&repr, &value, sizeof(value));
std::reverse(repr.begin(), repr.end());
Expand Down
13 changes: 7 additions & 6 deletions src/libtriton/includes/triton/softfloat.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,13 @@ namespace triton {
* \addtogroup softfloat
* @{
*/

//! Cast 32-bit floating point value to 16-bit according to IEEE-754
auto f32_to_f16(float value) -> uint16_t;

}
//! Cast 32-bit floating point value to 16-bit according to IEEE-754
auto f32_to_f16(float value) -> uint16_t;

}
/*! @} End of softfloat namespace */
};
/*! @} End of triton namespace */
};

#endif /* TRITON_SOFTFLOAT_HPP */
#endif /* TRITON_SOFTFLOAT_HPP */
27 changes: 17 additions & 10 deletions src/libtriton/utils/softfloat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,31 +12,38 @@
namespace triton {
namespace sf {

auto f32_to_f16(float value) -> uint16_t {
uint16_t f32_to_f16(float value) {
uint32_t f;
static_assert(sizeof(float) == sizeof(uint32_t),
"Unexpected float type size");

static_assert(sizeof(float) == sizeof(uint32_t), "Unexpected float type size");
std::memcpy(&f, &value, sizeof(uint32_t));
uint16_t sign = (f >> 16) & 0x8000;
int16_t exponent = ((f >> 23) & 0xff) - 127 + 15;
uint16_t mantissa = (f >> 13) & 0x3ff;

uint16_t sign = ((f >> 16) & 0x8000);
int16_t exponent = ((f >> 23) & 0xff) - 127 + 15;
uint16_t mantissa = ((f >> 13) & 0x3ff);

if (exponent <= 0) {
if (exponent < -10) {
return sign;
}
mantissa = (mantissa | 0x400) >> (1 - exponent);
return sign | mantissa;
} else if (exponent == 0xff - (127 - 15)) {
}

else if (exponent == 0xff - (127 - 15)) {
if (mantissa) {
return sign | 0x7fff;
} else {
return sign | 0x7c00;
}
} else if (exponent > 30) {
}

else if (exponent > 30) {
return sign | 0x7c00;
}

return sign | (exponent << 10) | mantissa;
}

} /* sf namespace */
} /* triton namespace */
}; /* sf namespace */
}; /* triton namespace */

0 comments on commit ae652c8

Please sign in to comment.