Implement rounded_div

Round-to-nearest integral based division, optimized for unsigned integral.
Used in sceNpTrophyGetGameProgress.
Do not allow signed values for aligned_div(), align().
This commit is contained in:
Eladash 2019-12-16 21:56:14 +02:00 committed by kd-11
parent e30173a835
commit db4041e079
6 changed files with 31 additions and 16 deletions

View File

@ -440,10 +440,29 @@ union alignas(2) f16
CHECK_SIZE_ALIGN(f16, 2, 2); CHECK_SIZE_ALIGN(f16, 2, 2);
template <typename T, typename = std::enable_if_t<std::is_integral<T>::value>> template <typename T, typename = std::enable_if_t<std::is_integral<T>::value && std::is_unsigned<T>::value>>
constexpr T align(const T& value, ullong align) constexpr T align(T value, ullong align)
{ {
return static_cast<T>((value + (align - 1)) & ~(align - 1)); return static_cast<T>((value + (align - 1)) & (0 - align));
}
// General purpose aligned division, the result is rounded up not truncated
template <typename T, typename = std::enable_if_t<std::is_integral<T>::value && std::is_unsigned<T>::value>>
constexpr T aligned_div(T value, ullong align)
{
return static_cast<T>((value + align - 1) / align);
}
// General purpose aligned division, the result is rounded to nearest
template <typename T, typename = std::enable_if_t<std::is_integral<T>::value>>
constexpr T rounded_div(T value, std::conditional_t<std::is_signed<T>::value, llong, ullong> align)
{
if constexpr (std::is_unsigned<T>::value)
{
return static_cast<T>((value + (align / 2)) / align);
}
return static_cast<T>((value + (value < 0 ? 0 - align : align) / 2) / align);
} }
template <typename T, typename T2> template <typename T, typename T2>

View File

@ -769,7 +769,9 @@ error_code cellVdecGetPicItem(u32 handle, vm::pptr<CellVdecPicItem> picItem)
info->codecType = vdec->type; info->codecType = vdec->type;
info->startAddr = 0x00000123; // invalid value (no address for picture) info->startAddr = 0x00000123; // invalid value (no address for picture)
info->size = align(av_image_get_buffer_size(vdec->ctx->pix_fmt, vdec->ctx->width, vdec->ctx->height, 1), 128); const int buffer_size = av_image_get_buffer_size(vdec->ctx->pix_fmt, vdec->ctx->width, vdec->ctx->height, 1);
verify(HERE), (buffer_size >= 0);
info->size = align<u32>(buffer_size, 128);
info->auNum = 1; info->auNum = 1;
info->auPts[0].lower = static_cast<u32>(pts); info->auPts[0].lower = static_cast<u32>(pts);
info->auPts[0].upper = static_cast<u32>(pts >> 32); info->auPts[0].upper = static_cast<u32>(pts >> 32);

View File

@ -929,8 +929,9 @@ error_code sceNpTrophyGetGameProgress(u32 context, u32 handle, vm::ptr<s32> perc
const u32 trp_count = ctxt->tropusr->GetTrophiesCount(); const u32 trp_count = ctxt->tropusr->GetTrophiesCount();
verify(HERE), trp_count > 0 && trp_count <= 128; verify(HERE), trp_count > 0 && trp_count <= 128;
*percentage = static_cast<s32>(std::lround((unlocked * 100.) / trp_count)); // Round result to nearest
*percentage = rounded_div(unlocked * 100, trp_count);
return CELL_OK; return CELL_OK;
} }

View File

@ -364,8 +364,8 @@ namespace
} }
else else
{ {
current_subresource_layout.width_in_block = rsx::aligned_div(miplevel_width_in_texel, block_edge_in_texel); current_subresource_layout.width_in_block = aligned_div(miplevel_width_in_texel, block_edge_in_texel);
current_subresource_layout.height_in_block = rsx::aligned_div(miplevel_height_in_texel, block_edge_in_texel); current_subresource_layout.height_in_block = aligned_div(miplevel_height_in_texel, block_edge_in_texel);
} }
if (padded_row) if (padded_row)

View File

@ -779,7 +779,7 @@ namespace vk
set_parameters(cmd); set_parameters(cmd);
const u32 num_bytes_per_invocation = (sizeof(_BlockType) * optimal_group_size); const u32 num_bytes_per_invocation = (sizeof(_BlockType) * optimal_group_size);
const u32 linear_invocations = rsx::aligned_div(data_length, num_bytes_per_invocation); const u32 linear_invocations = aligned_div(data_length, num_bytes_per_invocation);
compute_task::run(cmd, linear_invocations); compute_task::run(cmd, linear_invocations);
} }
}; };

View File

@ -277,13 +277,6 @@ namespace rsx
return ((value + alignment - 1) / alignment) * alignment; return ((value + alignment - 1) / alignment) * alignment;
} }
// General purpose aligned division, the result is rounded up not truncated
template <typename T, typename U>
static inline T aligned_div(T value, U alignment)
{
return (value + alignment - 1) / alignment;
}
// Copy memory in inverse direction from source // Copy memory in inverse direction from source
// Used to scale negatively x axis while transfering image data // Used to scale negatively x axis while transfering image data
template <typename Ts = u8, typename Td = Ts> template <typename Ts = u8, typename Td = Ts>