Merge pull request #15395 from ZehMatt/refactor/common-numerics

Remove numerics from common.h and use it as needed
This commit is contained in:
ζeh Matt 2021-09-11 14:27:07 -07:00 committed by GitHub
commit 65a484105e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
25 changed files with 75 additions and 55 deletions

View File

@ -9,6 +9,7 @@
#include "TrackPlaceAction.h"
#include "../core/Numerics.hpp"
#include "../management/Finance.h"
#include "../ride/RideData.h"
#include "../ride/Track.h"
@ -453,7 +454,7 @@ GameActions::Result::Ptr TrackPlaceAction::Execute() const
// Remove walls in the directions this track intersects
uint8_t intersectingDirections = wallEdges[blockIndex];
intersectingDirections ^= 0x0F;
intersectingDirections = rol4(intersectingDirections, _origin.direction);
intersectingDirections = Numerics::rol4(intersectingDirections, _origin.direction);
for (int32_t i = 0; i < NumOrthogonalDirections; i++)
{
if (intersectingDirections & (1 << i))

View File

@ -19,15 +19,12 @@
#endif
#include "Diagnostic.h"
#include "core/Numerics.hpp"
#include <cassert>
#include <cstddef>
#include <cstdint>
#include <stdexcept>
using namespace Numerics;
using utf8 = char;
using utf8string = utf8*;
using const_utf8string = const utf8*;
@ -41,15 +38,6 @@ using const_utf8string = const utf8*;
using codepoint_t = uint32_t;
using colour_t = uint8_t;
const constexpr auto rol8 = rol<uint8_t>;
const constexpr auto ror8 = ror<uint8_t>;
const constexpr auto rol16 = rol<uint16_t>;
const constexpr auto ror16 = ror<uint16_t>;
const constexpr auto rol32 = rol<uint32_t>;
const constexpr auto ror32 = ror<uint32_t>;
const constexpr auto rol64 = rol<uint64_t>;
const constexpr auto ror64 = ror<uint64_t>;
namespace
{
[[maybe_unused]] constexpr bool is_power_of_2(int v)

View File

@ -16,6 +16,7 @@
#include "FileScanner.h"
#include "FileStream.h"
#include "JobPool.h"
#include "Numerics.hpp"
#include "Path.hpp"
#include <chrono>
@ -156,7 +157,7 @@ private:
stats.TotalFileSize += fileInfo->Size;
stats.FileDateModifiedChecksum ^= static_cast<uint32_t>(fileInfo->LastModified >> 32)
^ static_cast<uint32_t>(fileInfo->LastModified & 0xFFFFFFFF);
stats.FileDateModifiedChecksum = ror32(stats.FileDateModifiedChecksum, 5);
stats.FileDateModifiedChecksum = Numerics::ror32(stats.FileDateModifiedChecksum, 5);
stats.PathChecksum += GetPathChecksum(path);
files.push_back(std::move(path));

View File

@ -25,6 +25,7 @@
#include "FileScanner.h"
#include "Memory.hpp"
#include "Numerics.hpp"
#include "Path.hpp"
#include "String.hpp"
@ -363,7 +364,7 @@ void Path::QueryDirectory(QueryDirectoryResult* result, const std::string& patte
result->TotalFileSize += fileInfo->Size;
result->FileDateModifiedChecksum ^= static_cast<uint32_t>(fileInfo->LastModified >> 32)
^ static_cast<uint32_t>(fileInfo->LastModified & 0xFFFFFFFF);
result->FileDateModifiedChecksum = ror32(result->FileDateModifiedChecksum, 5);
result->FileDateModifiedChecksum = Numerics::ror32(result->FileDateModifiedChecksum, 5);
result->PathChecksum += GetPathChecksum(path);
}
}

View File

@ -68,4 +68,13 @@ namespace Numerics
return (x >> shift | x << (4 - shift)) & 0x0F;
}
const constexpr auto rol8 = rol<uint8_t>;
const constexpr auto ror8 = ror<uint8_t>;
const constexpr auto rol16 = rol<uint16_t>;
const constexpr auto ror16 = ror<uint16_t>;
const constexpr auto rol32 = rol<uint32_t>;
const constexpr auto ror32 = ror<uint32_t>;
const constexpr auto rol64 = rol<uint64_t>;
const constexpr auto ror64 = ror<uint64_t>;
} // namespace Numerics

View File

@ -19,6 +19,7 @@
# include "../OpenRCT2.h"
# include "../config/Config.h"
# include "../core/Numerics.hpp"
# include "../core/String.hpp"
# include "../localisation/Localisation.h"
# include "../localisation/LocalisationService.h"
@ -186,7 +187,7 @@ static uint32_t ttf_surface_cache_hash(TTF_Font* font, std::string_view text)
uint32_t hash = static_cast<uint32_t>(((reinterpret_cast<uintptr_t>(font) * 23) ^ 0xAAAAAAAA) & 0xFFFFFFFF);
for (auto c : text)
{
hash = ror32(hash, 3) ^ (c * 13);
hash = Numerics::ror32(hash, 3) ^ (c * 13);
}
return hash;
}

View File

@ -13,6 +13,7 @@
#include "../Game.h"
#include "../Intro.h"
#include "../config/Config.h"
#include "../core/Numerics.hpp"
#include "../interface/Screenshot.h"
#include "../interface/Viewport.h"
#include "../interface/Window.h"
@ -583,7 +584,7 @@ void X8DrawingContext::FillRect(uint32_t colour, int32_t left, int32_t top, int3
for (int32_t i = 0; i < height; i++)
{
uint8_t* nextdst = dst + dpi->width + dpi->pitch;
uint32_t p = ror32(crossPattern, 1);
uint32_t p = Numerics::ror32(crossPattern, 1);
p = (p & 0xFFFF0000) | width;
// Fill every other pixel with the colour

View File

@ -21,6 +21,7 @@
#include "../core/IStream.hpp"
#include "../core/Memory.hpp"
#include "../core/MemoryStream.h"
#include "../core/Numerics.hpp"
#include "../core/Path.hpp"
#include "../core/String.hpp"
#include "../localisation/Localisation.h"
@ -757,11 +758,11 @@ int32_t object_calculate_checksum(const rct_object_entry* entry, const void* dat
uint32_t checksum = 0xF369A75B;
checksum ^= entryBytePtr[0];
checksum = rol32(checksum, 11);
checksum = Numerics::rol32(checksum, 11);
for (int32_t i = 4; i < 12; i++)
{
checksum ^= entryBytePtr[i];
checksum = rol32(checksum, 11);
checksum = Numerics::rol32(checksum, 11);
}
const uint8_t* dataBytes = reinterpret_cast<const uint8_t*>(data);
@ -772,12 +773,12 @@ int32_t object_calculate_checksum(const rct_object_entry* entry, const void* dat
{
checksum ^= dataBytes[j];
}
checksum = rol32(checksum, 11);
checksum = Numerics::rol32(checksum, 11);
}
for (size_t i = dataLength32; i < dataLength; i++)
{
checksum ^= dataBytes[i];
checksum = rol32(checksum, 11);
checksum = Numerics::rol32(checksum, 11);
}
return static_cast<int32_t>(checksum);

View File

@ -9,6 +9,7 @@
#include "../../Game.h"
#include "../../config/Config.h"
#include "../../core/Numerics.hpp"
#include "../../interface/Viewport.h"
#include "../../localisation/Localisation.h"
#include "../../object/LargeSceneryObject.h"
@ -278,7 +279,7 @@ void PaintLargeScenery(paint_session* session, uint8_t direction, uint16_t heigh
if (edi & 0xF00)
{
edi &= 0xF000;
edi = rol16(edi, direction);
edi = Numerics::rol16(edi, direction);
esi = (edi & 0xF) | (edi >> 12);
}
const CoordsXYZ bbOffset = { s98E3C4[esi].offset, height };

View File

@ -9,6 +9,7 @@
#include "../../Game.h"
#include "../../config/Config.h"
#include "../../core/Numerics.hpp"
#include "../../drawing/LightFX.h"
#include "../../interface/Viewport.h"
#include "../../localisation/Localisation.h"
@ -174,7 +175,7 @@ static void path_bit_bins_paint(
// Edges have been rotated around the rotation to check addition status
// this will also need to be rotated.
binIsFull = !(pathElement.GetAdditionStatus() & ror8(0x3, (2 * session->CurrentRotation)));
binIsFull = !(pathElement.GetAdditionStatus() & Numerics::ror8(0x3, (2 * session->CurrentRotation)));
if (binIsFull)
imageId += 8;
}
@ -195,7 +196,7 @@ static void path_bit_bins_paint(
// Edges have been rotated around the rotation to check addition status
// this will also need to be rotated.
binIsFull = !(pathElement.GetAdditionStatus() & ror8(0xC, (2 * session->CurrentRotation)));
binIsFull = !(pathElement.GetAdditionStatus() & Numerics::ror8(0xC, (2 * session->CurrentRotation)));
if (binIsFull)
imageId += 8;
}
@ -217,7 +218,7 @@ static void path_bit_bins_paint(
// Edges have been rotated around the rotation to check addition status
// this will also need to be rotated.
binIsFull = !(pathElement.GetAdditionStatus() & ror8(0x30, (2 * session->CurrentRotation)));
binIsFull = !(pathElement.GetAdditionStatus() & Numerics::ror8(0x30, (2 * session->CurrentRotation)));
if (binIsFull)
imageId += 8;
}
@ -239,7 +240,7 @@ static void path_bit_bins_paint(
// Edges have been rotated around the rotation to check addition status
// this will also need to be rotated.
binIsFull = !(pathElement.GetAdditionStatus() & ror8(0xC0, (2 * session->CurrentRotation)));
binIsFull = !(pathElement.GetAdditionStatus() & Numerics::ror8(0xC0, (2 * session->CurrentRotation)));
if (binIsFull)
imageId += 8;
}

View File

@ -14,6 +14,7 @@
#include "../../OpenRCT2.h"
#include "../../config/Config.h"
#include "../../core/Guard.hpp"
#include "../../core/Numerics.hpp"
#include "../../drawing/Drawing.h"
#include "../../interface/Colour.h"
#include "../../interface/Viewport.h"
@ -950,7 +951,7 @@ static std::pair<int32_t, int32_t> surface_get_height_above_water(
}
else
{
localSurfaceShape = ror4(surfaceShape ^ TILE_ELEMENT_SURFACE_RAISED_CORNERS_MASK, 2);
localSurfaceShape = Numerics::ror4(surfaceShape ^ TILE_ELEMENT_SURFACE_RAISED_CORNERS_MASK, 2);
}
}
}
@ -1340,7 +1341,7 @@ void PaintSurface(paint_session* session, uint8_t direction, uint16_t height, co
// Owned land boundary fences
session->InteractionType = ViewportInteractionItem::ParkEntrance;
uint8_t rotatedFences = rol4(tileElement.GetParkFences(), rotation);
uint8_t rotatedFences = Numerics::rol4(tileElement.GetParkFences(), rotation);
for (const auto& fenceData : _tileSurfaceBoundaries)
{

View File

@ -12,6 +12,7 @@
#include "../../Game.h"
#include "../../Input.h"
#include "../../config/Config.h"
#include "../../core/Numerics.hpp"
#include "../../drawing/Drawing.h"
#include "../../interface/Viewport.h"
#include "../../localisation/Localisation.h"
@ -427,7 +428,7 @@ void paint_util_set_segment_support_height(paint_session* session, int32_t segme
uint16_t paint_util_rotate_segments(uint16_t segments, uint8_t rotation)
{
uint8_t temp = segments & 0xFF;
temp = rol8(temp, rotation * 2);
temp = Numerics::rol8(temp, rotation * 2);
return (segments & 0xFF00) | temp;
}

View File

@ -13,6 +13,7 @@
#include "../audio/audio.h"
#include "../config/Config.h"
#include "../core/Guard.hpp"
#include "../core/Numerics.hpp"
#include "../interface/Window_internal.h"
#include "../localisation/Localisation.h"
#include "../management/Finance.h"
@ -6001,7 +6002,7 @@ bool Guest::UpdateWalkingFindBin()
uint8_t bin_quantities = pathElement->GetAdditionStatus();
// Rotate the bin to the correct edge. Makes it easier for next calc.
bin_quantities = ror8(ror8(bin_quantities, chosen_edge), chosen_edge);
bin_quantities = Numerics::ror8(Numerics::ror8(bin_quantities, chosen_edge), chosen_edge);
for (uint8_t free_edge = 4; free_edge != 0; free_edge--)
{
@ -6012,7 +6013,7 @@ bool Guest::UpdateWalkingFindBin()
break;
}
chosen_edge = (chosen_edge + 1) & 0x3;
bin_quantities = ror8(bin_quantities, 2);
bin_quantities = Numerics::ror8(bin_quantities, 2);
if ((free_edge - 1) == 0)
return 0;
}

View File

@ -10,6 +10,7 @@
#include "SawyerChunkReader.h"
#include "../core/IStream.hpp"
#include "../core/Numerics.hpp"
// malloc is very slow for large allocations in MSVC debug builds as it allocates
// memory on a special debug heap and then initialises all the memory to 0xCC.
@ -295,7 +296,7 @@ size_t SawyerChunkReader::DecodeChunkRotate(void* dst, size_t dstCapacity, const
uint8_t code = 1;
for (size_t i = 0; i < srcLength; i++)
{
dst8[i] = ror8(src8[i], code);
dst8[i] = Numerics::ror8(src8[i], code);
code = (code + 2) % 8;
}
return srcLength;

View File

@ -10,6 +10,7 @@
#include "SawyerChunkWriter.h"
#include "../core/IStream.hpp"
#include "../core/Numerics.hpp"
#include "../util/SawyerCoding.h"
// Maximum buffer size to store compressed data, maximum of 16 MiB
@ -99,7 +100,7 @@ void SawyerChunkWriter::WriteChunkTrack(const void* src, size_t length)
{
uint8_t newByte = ((checksum & 0xFF) + data[i]) & 0xFF;
checksum = (checksum & 0xFFFFFF00) + newByte;
checksum = rol32(checksum, 3);
checksum = Numerics::rol32(checksum, 3);
}
checksum -= 0x1D4C1;

View File

@ -10,6 +10,7 @@
#include "SawyerEncoding.h"
#include "../core/IStream.hpp"
#include "../core/Numerics.hpp"
#include "RCT12.h"
#include <algorithm>
@ -80,7 +81,7 @@ namespace SawyerEncoding
{
uint8_t newByte = ((checksum & 0xFF) + *data) & 0xFF;
checksum = (checksum & 0xFFFFFF00) + newByte;
checksum = rol32(checksum, 3);
checksum = Numerics::rol32(checksum, 3);
}
uint32_t fileChecksum = stream->ReadValue<uint32_t>();

View File

@ -18,6 +18,7 @@
#include "../config/Config.h"
#include "../core/FileStream.h"
#include "../core/IStream.hpp"
#include "../core/Numerics.hpp"
#include "../core/String.hpp"
#include "../interface/Viewport.h"
#include "../interface/Window.h"
@ -64,6 +65,8 @@
#include <iterator>
#include <optional>
#define ENCRYPT_MONEY(money) (static_cast<money32>(Numerics::ror32((money), 13) ^ 0xF4EC9621))
S6Exporter::S6Exporter()
{
RemoveTracklessRides = false;
@ -597,11 +600,11 @@ uint32_t S6Exporter::GetLoanHash(money32 initialCash, money32 bankLoan, uint32_t
{
int32_t value = 0x70093A;
value -= initialCash;
value = ror32(value, 5);
value = Numerics::ror32(value, 5);
value -= bankLoan;
value = ror32(value, 7);
value = Numerics::ror32(value, 7);
value += maxBankLoan;
value = ror32(value, 3);
value = Numerics::ror32(value, 3);
return value;
}

View File

@ -18,6 +18,7 @@
#include "../core/Console.hpp"
#include "../core/FileStream.h"
#include "../core/IStream.hpp"
#include "../core/Numerics.hpp"
#include "../core/Path.hpp"
#include "../core/Random.hpp"
#include "../core/String.hpp"
@ -70,6 +71,8 @@
#include <algorithm>
#define DECRYPT_MONEY(money) (static_cast<money32>(Numerics::rol32((money) ^ 0xF4EC9621, 13)))
/**
* Class to import RollerCoaster Tycoon 2 scenarios (*.SC6) and saved games (*.SV6).
*/

View File

@ -9,6 +9,7 @@
#include "../common.h"
#include "../core/File.h"
#include "../core/Numerics.hpp"
#include "../core/Path.hpp"
#include "RCT2.h"
@ -53,8 +54,8 @@ static std::vector<uint8_t> CreateMask(const EncryptionKey& key)
{
uint32_t s0 = seed0;
uint32_t s1 = seed1 ^ 0xF7654321;
seed0 = rol32(s1, 25) + s0;
seed1 = rol32(s0, 29);
seed0 = Numerics::rol32(s1, 25) + s0;
seed1 = Numerics::rol32(s0, 29);
result[i + 0] = (s0 >> 3) & 0xFF;
result[i + 1] = (s0 >> 11) & 0xFF;
result[i + 2] = (s0 >> 19) & 0xFF;

View File

@ -24,6 +24,7 @@
#include "../config/Config.h"
#include "../core/FixedVector.h"
#include "../core/Guard.hpp"
#include "../core/Numerics.hpp"
#include "../interface/Window.h"
#include "../localisation/Date.h"
#include "../localisation/Localisation.h"
@ -2234,7 +2235,7 @@ static void ride_shop_connected(Ride* ride)
const auto& ted = GetTrackElementDescriptor(track_type);
uint8_t entrance_directions = ted.SequenceProperties[0] & 0xF;
uint8_t tile_direction = trackElement->GetDirection();
entrance_directions = rol4(entrance_directions, tile_direction);
entrance_directions = Numerics::rol4(entrance_directions, tile_direction);
// Now each bit in entrance_directions stands for an entrance direction to check
if (entrance_directions == 0)
@ -3043,7 +3044,7 @@ static Vehicle* vehicle_create_car(
vehicle->vehicle_type = vehicleEntryIndex;
vehicle->SubType = carIndex == 0 ? Vehicle::Type::Head : Vehicle::Type::Tail;
vehicle->var_44 = ror32(vehicleEntry->spacing, 10) & 0xFFFF;
vehicle->var_44 = Numerics::ror32(vehicleEntry->spacing, 10) & 0xFFFF;
auto edx = vehicleEntry->spacing >> 1;
*remainingDistance -= edx;
vehicle->remaining_distance = *remainingDistance;
@ -3666,9 +3667,9 @@ static bool ride_create_cable_lift(ride_id_t rideIndex, bool isApplying)
uint32_t ebx = 0;
for (int32_t i = 0; i < 5; i++)
{
uint32_t edx = ror32(0x15478, 10);
uint32_t edx = Numerics::ror32(0x15478, 10);
uint16_t var_44 = edx & 0xFFFF;
edx = rol32(edx, 10) >> 1;
edx = Numerics::rol32(edx, 10) >> 1;
ebx -= edx;
int32_t remaining_distance = ebx;
ebx -= edx;

View File

@ -33,6 +33,7 @@
#include "../audio/audio.h"
#include "../core/DataSerialiser.h"
#include "../core/File.h"
#include "../core/Numerics.hpp"
#include "../core/String.hpp"
#include "../drawing/X8DrawingEngine.h"
#include "../localisation/Localisation.h"
@ -1420,7 +1421,7 @@ static std::optional<money32> track_design_place_maze(TrackDesign* td6, const Co
}
break;
default:
maze_entry = rol16(maze_element.maze_entry, rotation * 4);
maze_entry = Numerics::rol16(maze_element.maze_entry, rotation * 4);
if (_trackDesignPlaceOperation == PTD_OPERATION_PLACE_TRACK_PREVIEW)
{

View File

@ -7,6 +7,7 @@
* OpenRCT2 is licensed under the GNU General Public License version 3.
*****************************************************************************/
#include "../../core/Numerics.hpp"
#include "../../interface/Viewport.h"
#include "../../paint/Paint.h"
#include "../../paint/Supports.h"
@ -49,7 +50,7 @@ static void maze_paint_setup(
const TrackElement& trackElement)
{
uint16_t maze_entry = trackElement.GetMazeEntry();
maze_entry = rol16(maze_entry, direction * 4);
maze_entry = Numerics::rol16(maze_entry, direction * 4);
uint32_t rotation = session->CurrentRotation;
// draw ground

View File

@ -19,6 +19,7 @@
#include "../core/FileIndex.hpp"
#include "../core/FileStream.h"
#include "../core/MemoryStream.h"
#include "../core/Numerics.hpp"
#include "../core/Path.hpp"
#include "../core/String.hpp"
#include "../localisation/Language.h"
@ -526,7 +527,7 @@ private:
// Rotate each byte of mp.dat left by 4 bits to convert
for (size_t i = 0; i < mpdat.size(); i++)
{
mpdat[i] = rol8(mpdat[i], 4);
mpdat[i] = Numerics::rol8(mpdat[i], 4);
}
File::WriteAllBytes(dstPath, mpdat.data(), mpdat.size());

View File

@ -9,6 +9,7 @@
#include "SawyerCoding.h"
#include "../core/Numerics.hpp"
#include "../platform/platform.h"
#include "../scenario/Scenario.h"
#include "Util.h"
@ -113,10 +114,10 @@ size_t sawyercoding_decode_sc4(const uint8_t* src, uint8_t* dst, size_t length,
for (size_t i = 0x60018; i <= std::min(decodedLength - 1, static_cast<size_t>(0x1F8350)); i += 4)
{
dst[i + 1] = ror8(dst[i + 1], 3);
dst[i + 1] = Numerics::ror8(dst[i + 1], 3);
uint32_t* code = reinterpret_cast<uint32_t*>(&dst[i]);
*code = rol32(*code, 9);
*code = Numerics::rol32(*code, 9);
}
return decodedLength;
@ -148,7 +149,7 @@ size_t sawyercoding_encode_td6(const uint8_t* src, uint8_t* dst, size_t length)
{
uint8_t new_byte = ((checksum & 0xFF) + dst[i]) & 0xFF;
checksum = (checksum & 0xFFFFFF00) + new_byte;
checksum = rol32(checksum, 3);
checksum = Numerics::rol32(checksum, 3);
}
checksum -= 0x1D4C1;
@ -167,7 +168,7 @@ int32_t sawyercoding_validate_track_checksum(const uint8_t* src, size_t length)
{
uint8_t new_byte = ((checksum & 0xFF) + src[i]) & 0xFF;
checksum = (checksum & 0xFFFFFF00) + new_byte;
checksum = rol32(checksum, 3);
checksum = Numerics::rol32(checksum, 3);
}
if (checksum - 0x1D4C1 == file_checksum)
@ -383,7 +384,7 @@ static void encode_chunk_rotate(uint8_t* buffer, size_t length)
uint8_t code = 1;
for (i = 0; i < length; i++)
{
buffer[i] = rol8(buffer[i], code);
buffer[i] = Numerics::rol8(buffer[i], code);
code = (code + 2) % 8;
}
}
@ -401,7 +402,7 @@ int32_t sawyercoding_detect_file_type(const uint8_t* src, size_t length)
for (i = 0; i < length - 4; i++)
{
actualChecksum = (actualChecksum & 0xFFFFFF00) | (((actualChecksum & 0xFF) + static_cast<uint8_t>(src[i])) & 0xFF);
actualChecksum = rol32(actualChecksum, 3);
actualChecksum = Numerics::rol32(actualChecksum, 3);
}
return sawyercoding_detect_rct1_version(checksum - actualChecksum);

View File

@ -13,9 +13,6 @@
#include "../ride/Ride.h"
#include "Map.h"
#define DECRYPT_MONEY(money) (static_cast<money32>(rol32((money) ^ 0xF4EC9621, 13)))
#define ENCRYPT_MONEY(money) (static_cast<money32>(ror32((money), 13) ^ 0xF4EC9621))
#define MAX_ENTRANCE_FEE MONEY(200, 00)
constexpr const uint8_t ParkRatingHistoryUndefined = std::numeric_limits<uint8_t>::max();