Skip to content

Commit

Permalink
Change QPI::id back to m256i
Browse files Browse the repository at this point in the history
  • Loading branch information
philippwerner committed Apr 12, 2024
1 parent 7f6c223 commit a7a0f69
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 79 deletions.
39 changes: 39 additions & 0 deletions src/platform/m256.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@

#include <intrin.h>

// Used for all kinds of IDs, including in QPI and contracts.
// Existing interface and behavior should never be changed! (However, it may be extended.)
union m256i
{
// access for loops and compatability with __m256i
__int8 m256i_i8[32];
__int16 m256i_i16[16];
__int32 m256i_i32[8];
Expand All @@ -13,6 +16,42 @@ union m256i
unsigned __int32 m256i_u32[8];
unsigned __int64 m256i_u64[4];

// interface for QPI (no [] allowed)
struct
{
unsigned __int64 _0, _1, _2, _3;
} u64;
struct
{
__int64 _0, _1, _2, _3;
} i64;
struct
{
unsigned __int32 _0, _1, _2, _3, _4, _5, _6, _7;
} u32;
struct
{
__int32 _0, _1, _2, _3, _4, _5, _6, _7;
} i32;
struct
{
unsigned __int16 _0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15;
} u16;
struct
{
__int16 _0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15;
} i16;
struct
{
unsigned __int8 _0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15;
unsigned __int8 _16, _17, _18, _19, _20, _21, _22, _23, _24, _25, _26, _27, _28, _29, _30, _31;
} u8;
struct
{
__int8 _0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15;
__int8 _16, _17, _18, _19, _20, _21, _22, _23, _24, _25, _26, _27, _28, _29, _30, _31;
} i8;

m256i() = default;

m256i(unsigned long long ull0, unsigned long long ull1, unsigned long long ull2, unsigned long long ull3)
Expand Down
12 changes: 6 additions & 6 deletions src/smart_contracts/Qx.h
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ struct QX
PUBLIC(AssetAskOrders)

state._issuerAndAssetName = input.issuer;
state._issuerAndAssetName._0 = input.assetName;
state._issuerAndAssetName.u64._0 = input.assetName;

state._elementIndex = state._assetOrders.headIndex(state._issuerAndAssetName, 0);
state._elementIndex2 = 0;
Expand Down Expand Up @@ -287,7 +287,7 @@ struct QX
PUBLIC(AssetBidOrders)

state._issuerAndAssetName = input.issuer;
state._issuerAndAssetName._0 = input.assetName;
state._issuerAndAssetName.u64._0 = input.assetName;

state._elementIndex = state._assetOrders.headIndex(state._issuerAndAssetName);
state._elementIndex2 = 0;
Expand Down Expand Up @@ -496,7 +496,7 @@ struct QX
output.addedNumberOfShares = input.numberOfShares;

state._issuerAndAssetName = input.issuer;
state._issuerAndAssetName._0 = input.assetName;
state._issuerAndAssetName.u64._0 = input.assetName;

state._elementIndex = state._entityOrders.headIndex(invocator(), -input.price);
while (state._elementIndex != NULL_INDEX)
Expand Down Expand Up @@ -659,7 +659,7 @@ struct QX
output.addedNumberOfShares = input.numberOfShares;

state._issuerAndAssetName = input.issuer;
state._issuerAndAssetName._0 = input.assetName;
state._issuerAndAssetName.u64._0 = input.assetName;

state._elementIndex = state._entityOrders.tailIndex(invocator(), input.price);
while (state._elementIndex != NULL_INDEX)
Expand Down Expand Up @@ -823,7 +823,7 @@ struct QX
else
{
state._issuerAndAssetName = input.issuer;
state._issuerAndAssetName._0 = input.assetName;
state._issuerAndAssetName.u64._0 = input.assetName;

state._elementIndex = state._entityOrders.headIndex(invocator(), -input.price);
while (state._elementIndex != NULL_INDEX)
Expand Down Expand Up @@ -904,7 +904,7 @@ struct QX
else
{
state._issuerAndAssetName = input.issuer;
state._issuerAndAssetName._0 = input.assetName;
state._issuerAndAssetName.u64._0 = input.assetName;

state._elementIndex = state._entityOrders.tailIndex(invocator(), input.price);
while (state._elementIndex != NULL_INDEX)
Expand Down
82 changes: 12 additions & 70 deletions src/smart_contracts/qpi.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,59 +40,7 @@ namespace QPI
typedef signed long long sint64;
typedef unsigned long long uint64;

struct id
{
unsigned long long _0, _1, _2, _3;

id()
{
}

id(unsigned long long _0, unsigned long long _1, unsigned long long _2, unsigned long long _3)
{
this->_0 = _0;
this->_1 = _1;
this->_2 = _2;
this->_3 = _3;
}

id& operator = (__m256i anotherId)
{
*((__m256i*)&_0) = anotherId;

return *this;
}

id& operator = (id anotherId)
{
_0 = anotherId._0;
_1 = anotherId._1;
_2 = anotherId._2;
_3 = anotherId._3;

return *this;
}

bool operator == (id anotherId) const
{
return _0 == anotherId._0 && _1 == anotherId._1 && _2 == anotherId._2 && _3 == anotherId._3;
}

bool operator != (id anotherId) const
{
return _0 != anotherId._0 || _1 != anotherId._1 || _2 != anotherId._2 || _3 != anotherId._3;
}

bool operator < (id anotherId) const
{
return _3 < anotherId._3 || _2 < anotherId._2 || _1 < anotherId._1 || _0 < anotherId._0;
}

bool operator > (id anotherId) const
{
return _3 > anotherId._3 || _2 > anotherId._2 || _1 > anotherId._1 || _0 > anotherId._0;
}
};
typedef m256i id;

#define bit_2x bit_2
#define bit_4x bit_4
Expand Down Expand Up @@ -5103,7 +5051,7 @@ namespace QPI
// Return index of id pov in hash map _povs, or NULL_INDEX if not found
sint64 _povIndex(const id& pov) const
{
sint64 povIndex = pov._0 & (L - 1);
sint64 povIndex = pov.u64._0 & (L - 1);
for (sint64 counter = 0; counter < L; counter += 32)
{
uint64 flags = _getEncodedPovOccupationFlags(_povOccupationFlags, povIndex);
Expand Down Expand Up @@ -5619,7 +5567,7 @@ namespace QPI
if (_population < capacity() && _markRemovalCounter < capacity())
{
// search in pov hash map
sint64 povIndex = pov._0 & (L - 1);
sint64 povIndex = pov.u64._0 & (L - 1);
for (sint64 counter = 0; counter < L; counter += 32)
{
uint64 flags = _getEncodedPovOccupationFlags(_povOccupationFlags, povIndex);
Expand Down Expand Up @@ -5700,7 +5648,7 @@ namespace QPI
{
// find empty position in new pov hash map
const sint64 oldPovIndex = (oldPovIndexGroup << 5) + (oldPovIndexOffset >> 1);
sint64 newPovIndex = _povs[oldPovIndex].value._0 & (L - 1);
sint64 newPovIndex = _povs[oldPovIndex].value.u64._0 & (L - 1);
for (sint64 counter = 0; counter < L; counter += 32)
{
QPI::uint64 newFlags = _getEncodedPovOccupationFlags(_povOccupationFlagsBuffer, newPovIndex);
Expand Down Expand Up @@ -6047,7 +5995,7 @@ namespace QPI
id id,
::Entity& entity
) { // Returns "true" if the entity has been found, returns "false" otherwise
return ::__getEntity(m256i(id._0, id._1, id._2, id._3), entity);
return ::__getEntity(id, entity);
}

static uint8 hour(
Expand All @@ -6062,9 +6010,7 @@ namespace QPI

static id invocator(
) { // Returns the id of the user/contract who has triggered this contract; returns NULL_ID if there has been no user/contract
const m256i invocator = ::__invocator();

return id(invocator.m256i_u64[0], invocator.m256i_u64[1], invocator.m256i_u64[2], invocator.m256i_u64[3]);
return ::__invocator();
}

static sint64 issueAsset(
Expand All @@ -6074,7 +6020,7 @@ namespace QPI
sint64 numberOfShares,
uint64 unitOfMeasurement
) {
return ::__issueAsset(name, m256i(issuer._0, issuer._1, issuer._2, issuer._3), numberOfDecimalPlaces, numberOfShares, unitOfMeasurement);
return ::__issueAsset(name, issuer, numberOfDecimalPlaces, numberOfShares, unitOfMeasurement);
}

template <typename T>
Expand Down Expand Up @@ -6102,9 +6048,7 @@ namespace QPI
static id nextId(
id currentId
) {
const m256i nextId = ::__nextId(m256i(currentId._0, currentId._1, currentId._2, currentId._3));

return id(nextId.m256i_u64[0], nextId.m256i_u64[1], nextId.m256i_u64[2], nextId.m256i_u64[3]);
return ::__nextId(currentId);
}

static sint64 numberOfPossessedShares(
Expand All @@ -6115,14 +6059,12 @@ namespace QPI
uint16 ownershipManagingContractIndex,
uint16 possessionManagingContractIndex
) {
return ::__numberOfPossessedShares(assetName, m256i(issuer._0, issuer._1, issuer._2, issuer._3), m256i(owner._0, owner._1, owner._2, owner._3), m256i(possessor._0, possessor._1, possessor._2, possessor._3), ownershipManagingContractIndex, possessionManagingContractIndex);
return ::__numberOfPossessedShares(assetName, issuer, owner, possessor, ownershipManagingContractIndex, possessionManagingContractIndex);
}

static id originator(
) { // Returns the id of the user who has triggered the whole chain of invocations with their transaction; returns NULL_ID if there has been no user
const m256i originator = ::__originator();

return id(originator.m256i_u64[0], originator.m256i_u64[1], originator.m256i_u64[2], originator.m256i_u64[3]);
return ::__originator();
}

static uint8 second(
Expand All @@ -6139,7 +6081,7 @@ namespace QPI
id destination, // Destination to transfer to, use NULL_ID to destroy the transferred energy
sint64 amount // Energy amount to transfer, must be in [0..1'000'000'000'000'000] range
) { // Returns remaining energy amount; if the value is less than 0 then the attempt has failed, in this case the absolute value equals to the insufficient amount
return ::__transfer(m256i(destination._0, destination._1, destination._2, destination._3), amount);
return ::__transfer(destination, amount);
}

static sint64 transferShareOwnershipAndPossession(
Expand All @@ -6150,7 +6092,7 @@ namespace QPI
sint64 numberOfShares,
id newOwnerAndPossessor
) { // Returns remaining number of possessed shares satisfying all the conditions; if the value is less than 0 then the attempt has failed, in this case the absolute value equals to the insufficient number
return ::__transferShareOwnershipAndPossession(assetName, m256i(issuer._0, issuer._1, issuer._2, issuer._3), m256i(owner._0, owner._1, owner._2, owner._3), m256i(possessor._0, possessor._1, possessor._2, possessor._3), numberOfShares, m256i(newOwnerAndPossessor._0, newOwnerAndPossessor._1, newOwnerAndPossessor._2, newOwnerAndPossessor._3));
return ::__transferShareOwnershipAndPossession(assetName, issuer, owner, possessor, numberOfShares, newOwnerAndPossessor);
}

static uint8 year(
Expand Down
6 changes: 3 additions & 3 deletions test/qpi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ void checkPriorityQueue(const QPI::collection<T, capacity>& coll, const QPI::id&
{
if (print)
{
std::cout << "Priority queue ID(" << pov._0 << ", " << pov._1 << ", "
<< pov._2 << ", " << pov._3 << ")" << std::endl;
std::cout << "Priority queue ID(" << pov.u64._0 << ", " << pov.u64._1 << ", "
<< pov.u64._2 << ", " << pov.u64._3 << ")" << std::endl;
}
bool first = true;
QPI::sint64 elementIndex = coll.headIndex(pov);
Expand Down Expand Up @@ -63,7 +63,7 @@ void printPovElementCounts(const std::map<QPI::id, unsigned long long>& povEleme
{
QPI::id id = id_count_pair.first;
unsigned long long count = id_count_pair.second;
std::cout << "\t(" << id._0 << ", " << id._1 << ", " << id._2 << ", " << id._3 << "): " << count << std::endl;
std::cout << "\t(" << id.u64._0 << ", " << id.u64._1 << ", " << id.u64._2 << ", " << id.u64._3 << "): " << count << std::endl;
}
}

Expand Down

0 comments on commit a7a0f69

Please sign in to comment.