Skip to content

Commit

Permalink
refactor: change all float endianness to decimal endianness
Browse files Browse the repository at this point in the history
  • Loading branch information
mookums committed Nov 24, 2023
1 parent 4a70b3a commit 9ae2b85
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 23 deletions.
12 changes: 6 additions & 6 deletions src/database-options.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
#include "decimal.hpp"

LOST_CLI_OPTION("min-mag" , decimal , minMag , 100 , STR_TO_DECIMAL(optarg) , kNoDefaultArgument)
LOST_CLI_OPTION("max-stars" , int , maxStars , 10000 , atoi(optarg) , kNoDefaultArgument)
LOST_CLI_OPTION("max-stars" , int , maxStars , 10000 , atoi(optarg) , kNoDefaultArgument)
LOST_CLI_OPTION("min-separation" , decimal , minSeparation , 0.08 , STR_TO_DECIMAL(optarg) , kNoDefaultArgument)
LOST_CLI_OPTION("kvector" , bool , kvector , false , atobool(optarg), true)
LOST_CLI_OPTION("kvector" , bool , kvector , false , atobool(optarg), true)
LOST_CLI_OPTION("kvector-min-distance" , decimal , kvectorMinDistance , 0.5 , STR_TO_DECIMAL(optarg) , kNoDefaultArgument)
LOST_CLI_OPTION("kvector-max-distance" , decimal , kvectorMaxDistance , 15 , STR_TO_DECIMAL(optarg) , kNoDefaultArgument)
LOST_CLI_OPTION("kvector-distance-bins" , long , kvectorNumDistanceBins, 10000 , atol(optarg) , kNoDefaultArgument)
LOST_CLI_OPTION("swap-integer-endianness", bool , swapIntegerEndianness , false , atobool(optarg), true)
LOST_CLI_OPTION("swap-float-endianness" , bool , swapFloatEndianness , false , atobool(optarg), true)
LOST_CLI_OPTION("output" , std::string, outputPath , "-" , optarg , kNoDefaultArgument)
LOST_CLI_OPTION("kvector-distance-bins" , long , kvectorNumDistanceBins , 10000 , atol(optarg) , kNoDefaultArgument)
LOST_CLI_OPTION("swap-integer-endianness", bool , swapIntegerEndianness , false , atobool(optarg), true)
LOST_CLI_OPTION("swap-decimal-endianness", bool , swapDecimalEndianness , false , atobool(optarg), true)
LOST_CLI_OPTION("output" , std::string, outputPath , "-" , optarg , kNoDefaultArgument)
2 changes: 1 addition & 1 deletion src/io.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ typedef StarIdAlgorithm *(*StarIdAlgorithmFactory)();
typedef AttitudeEstimationAlgorithm *(*AttitudeEstimationAlgorithmFactory)();

SerializeContext serFromDbValues(const DatabaseOptions &values) {
return SerializeContext(values.swapIntegerEndianness, values.swapFloatEndianness);
return SerializeContext(values.swapIntegerEndianness, values.swapDecimalEndianness);
}

MultiDatabaseDescriptor GenerateDatabases(const Catalog &catalog, const DatabaseOptions &values) {
Expand Down
27 changes: 11 additions & 16 deletions src/serialize-helpers.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* Helpers to serialize and deserialize arbitrary data types to disk
*
* The serialization and deserialization helpers here assume that (a) integers and floating point
* The serialization and deserialization helpers here assume that (a) integers and decimal point
* numbers are stored in the same format on the source and target systems except for endianness, and
* (b) the target system requires no greater than n-byte alignment for n-byte values (eg, an int32_t
* only needs 4-byte alignment, not 8-byte), and (c) the database itself will be aligned at a
Expand All @@ -23,16 +23,18 @@
#include <vector>
#include <algorithm>

#include "decimal.hpp"

namespace lost {

class SerializeContext {
public:
SerializeContext(bool swapIntegerEndianness, bool swapFloatEndianness)
: swapIntegerEndianness(swapIntegerEndianness), swapFloatEndianness(swapFloatEndianness) { }
SerializeContext(bool swapIntegerEndianness, bool swapDecimalEndianness)
: swapIntegerEndianness(swapIntegerEndianness), swapDecimalEndianness(swapDecimalEndianness) { }
SerializeContext() : SerializeContext(false, false) { }

bool swapIntegerEndianness;
bool swapFloatEndianness;
bool swapDecimalEndianness;
std::vector<unsigned char> buffer;
};

Expand Down Expand Up @@ -66,8 +68,8 @@ void SwapEndianness(unsigned char *buffer) {
}

/// Swap the endianness of a value if necessary. Uses
/// LOST_DATABASE_{SOURCE,TARGET}_INTEGER_ENDIANNESS to determine to switch all values but float and
/// double, which use LOST_DATABASE_{SOURCE,TARGET}_FLOAT_ENDIANNESS.
/// LOST_DATABASE_{SOURCE,TARGET}_INTEGER_ENDIANNESS to determine to switch all values but decimal
/// which uses LOST_DATABASE_{SOURCE,TARGET}_DECIMAL_ENDIANNESS.
template <typename T>
void SwapEndiannessIfNecessary(unsigned char *buffer, SerializeContext *ser) {
if (ser->swapIntegerEndianness) {
Expand All @@ -78,16 +80,9 @@ void SwapEndiannessIfNecessary(unsigned char *buffer, SerializeContext *ser) {
// template specializations

template <>
inline void SwapEndiannessIfNecessary<float>(unsigned char *buffer, SerializeContext *ser) {
if (ser->swapFloatEndianness) {
SwapEndianness<float>(buffer);
}
}

template <>
inline void SwapEndiannessIfNecessary<double>(unsigned char *buffer, SerializeContext *ser) {
if (ser->swapFloatEndianness) {
SwapEndianness<double>(buffer);
inline void SwapEndiannessIfNecessary<decimal>(unsigned char *buffer, SerializeContext *ser) {
if (ser->swapDecimalEndianness) {
SwapEndianness<decimal>(buffer);
}
}

Expand Down

0 comments on commit 9ae2b85

Please sign in to comment.