Files
spark/src/lib/md/PriceSize.h
T

172 lines
4.7 KiB
C++

#pragma once
#include "common.h"
#include "lib/os/os.h"
#include "lib/utils/warn_ctrl.h"
#include "lib/utils/bts_array_size.h"
#include "lib/utils/bts_assert.h"
#include "lib/utils/bts_no_copy.h"
#include <atomic>
#include <limits>
#include <cmath>
namespace bts {
namespace md {
typedef double Price;
typedef uint16_t Size;
#define LOAD(x) x.load(std::memory_order_acquire)
#define STORE(x,value) x.store(value, std::memory_order_release)
static const double powers_10[] = {
1E-20, 1E-19, 1E-18, 1E-17, 1E-16, 1E-15, 1E-14, 1E-13, 1E-12, 1E-11, 1E-10, 1E-9, 1E-8, 1E-7, 1E-6, 1E-5, 1E-4, 1E-3, 1E-2, 1E-1,
1E0,
1E1, 1E2, 1E3, 1E4, 1E5, 1E6, 1E7, 1E8, 1E9, 1E10, 1E11, 1E12, 1E13, 1E14, 1E15, 1E16, 1E17, 1E18, 1E19, 1E20
};
static const int max_abs_power_10 = BTS_ARRAYSIZE(powers_10) / 2;
inline double pow10(int power) {
const int offset = power + max_abs_power_10;
const int powers_10_sz = BTS_ARRAYSIZE(powers_10);
bts_assert_d(offset >= 0 && offset < powers_10_sz);
if (offset >= 0 && offset < powers_10_sz) {
return powers_10[offset];
}
return std::numeric_limits<double>().quiet_NaN();
}
int to_exponent(double value);
template <class Mantissa, class Exponent>
inline bool compact_mantissa(Mantissa& mantissa, Exponent& exponent) {
bool result = false;
// If the mantissa is larger than 39 bits will allow attempt to shrink it
while (mantissa > 0x8000000000 || mantissa < -0x8000000000) {
mantissa /= 10;
exponent += 1;
result = true;
}
return result;
}
class PriceSize {
union {
uint64_t _val64;
// ISO C++ prohibits anonymous structs
GCC_DIAG_OFF("-Wpedantic")
struct {
#ifdef _WIN32
// VC++ only packs bit fields when the declared type is the same size
int64_t _mantissa : 40;
int64_t _exponent : 8;
uint64_t _size : 16;
#else
int64_t _mantissa : 40;
int8_t _exponent;
uint16_t _size;
#endif
};
GCC_DIAG_RESTORE()
};
friend class SingleWriterPriceSizeStore;
PriceSize(uint64_t val) : _val64(val) { }
public:
// fast path
PriceSize(int64_t mantissa, int8_t exponent, Size size) :
_mantissa(mantissa),
_exponent(exponent),
_size(size)
{
// Attempt to guard against too large mantissa
if (compact_mantissa(mantissa, exponent)) {
_mantissa = mantissa;
_exponent = exponent;
}
}
PriceSize() : _val64(PriceSize::NO_MARKET._val64) { }
// quick copy
PriceSize(const PriceSize& other) : _val64(other._val64) { }
PriceSize& operator=(const PriceSize& other) {
_val64 = other._val64;
return *this;
}
bool operator==(const PriceSize& other) const {
return _val64 == other._val64;
}
bool operator!=(const PriceSize& other) const {
return _val64 != other._val64;
}
BTS_EXPORT static PriceSize NO_MARKET;
template <uint8_t PRECISION>
static PriceSize from_double(double price, Size size) {
if (size == 0 || os::is_nan(price)) return NO_MARKET;
double temp;
if (fabs(modf(price, &temp)) != 0.0) {
const double rounding = 0.5 * pow10(-PRECISION);
price += (price > 0) ? rounding : -rounding;
price *= pow10(PRECISION);
return PriceSize((int64_t)price, -PRECISION, size);
}
return PriceSize((int64_t)price, 0, size);
}
static PriceSize from_double(double price, int precision, Size size) {
if (size == 0 || os::is_nan(price)) return NO_MARKET;
double temp;
if (fabs(modf(price, &temp)) != 0.0) {
const double rounding = 0.5 * pow10(-precision);
price += (price > 0) ? rounding : -rounding;
price *= pow10(precision);
return PriceSize((int64_t)price, -precision, size);
}
return PriceSize((int64_t)price, 0, size);
}
//
// Accessors
int64_t mantissa() const {
return _mantissa;
}
int8_t exponent() const {
return _exponent;
}
// This call is relatively slow so don't call it often
double price() const {
if (_exponent == -128) return NO_PRICE;
return mantissa() * pow10(exponent());
}
Size size() const {
return _size;
}
};
static_assert(sizeof(PriceSize) == sizeof(uint64_t), "PriceSize must be 64 bits for atomic assignment");
class SingleWriterPriceSizeStore {
std::atomic<uint64_t> _store;
BTS_NO_COPY(SingleWriterPriceSizeStore);
public:
SingleWriterPriceSizeStore() : _store() {
store(PriceSize::NO_MARKET);
}
bool store(PriceSize ps) {
uint64_t cur = LOAD(_store);
if (cur != ps._val64) {
STORE(_store, ps._val64);
return true;
}
return false;
}
PriceSize load() const {
uint64_t val = LOAD(_store);
return PriceSize(val);
}
};
}}//namespace