Skip to content

Commit

Permalink
Extend vector constants to four components
Browse files Browse the repository at this point in the history
  • Loading branch information
Petri Häkkinen committed Nov 13, 2023
1 parent 5ea64be commit 996db08
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 18 deletions.
8 changes: 4 additions & 4 deletions Compiler/include/Luau/BytecodeBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class BytecodeBuilder
int32_t addConstantNil();
int32_t addConstantBoolean(bool value);
int32_t addConstantNumber(double value);
int32_t addConstantVector(float x, float y, float z);
int32_t addConstantVector(float x, float y, float z, float w);
int32_t addConstantString(StringRef value);
int32_t addImport(uint32_t iid);
int32_t addConstantTable(const TableShape& shape);
Expand Down Expand Up @@ -159,7 +159,7 @@ class BytecodeBuilder
{
bool valueBoolean;
double valueNumber;
float valueVector[3];
float valueVector[4];
unsigned int valueString; // index into string table
uint32_t valueImport; // 10-10-10-2 encoded import id
uint32_t valueTable; // index into tableShapes[]
Expand All @@ -171,9 +171,9 @@ class BytecodeBuilder
{
Constant::Type type;
// Note: this stores value* from Constant; when type is Type_Number, this stores the same bits as double does but in uint64_t.
// for Type_Vector, x and y are stored in value and z is stored in extra.
// For Type_Vector, x and y are stored in 'value' and z and w are stored in 'extra'.
uint64_t value;
uint32_t extra = 0;
uint64_t extra = 0;

bool operator==(const ConstantKey& key) const
{
Expand Down
12 changes: 9 additions & 3 deletions Compiler/src/BuiltinFolding.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,13 @@ static Constant cnum(double v)
return res;
}

static Constant cvector(double x, double y, double z)
static Constant cvector(double x, double y, double z, double w)
{
Constant res = {Constant::Type_Vector};
res.valueVector[0] = (float)x;
res.valueVector[1] = (float)y;
res.valueVector[2] = (float)z;
res.valueVector[3] = (float)w;
return res;
}

Expand Down Expand Up @@ -472,11 +473,16 @@ Constant foldBuiltin(int bfid, const Constant* args, size_t count)
break;

case LBF_VECTOR:
if (FFlag::LuauVectorLiterals && count == 3 &&
if (FFlag::LuauVectorLiterals && count >= 3 &&
args[0].type == Constant::Type_Number &&
args[1].type == Constant::Type_Number &&
args[2].type == Constant::Type_Number)
return cvector(args[0].valueNumber, args[1].valueNumber, args[2].valueNumber);
{
if (count == 3)
return cvector(args[0].valueNumber, args[1].valueNumber, args[2].valueNumber, 0.0);
else if (count == 4 && args[3].type == Constant::Type_Number)
return cvector(args[0].valueNumber, args[1].valueNumber, args[2].valueNumber, args[3].valueNumber);
}
break;
}

Expand Down
20 changes: 14 additions & 6 deletions Compiler/src/BytecodeBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -154,17 +154,18 @@ size_t BytecodeBuilder::ConstantKeyHash::operator()(const ConstantKey& key) cons
{
if (key.type == Constant::Type_Vector)
{
uint32_t i[3];
static_assert(sizeof(key.value) + sizeof(key.extra) == sizeof(i), "Expecting vector to have three 32-bit components");
uint32_t i[4];
static_assert(sizeof(key.value) + sizeof(key.extra) == sizeof(i), "Expecting vector to have four 32-bit components");
memcpy(i, &key.value, sizeof(i));

// scramble bits to make sure that integer coordinates have entropy in lower bits
i[0] ^= i[0] >> 17;
i[1] ^= i[1] >> 17;
i[2] ^= i[2] >> 17;
i[3] ^= i[3] >> 17;

// Optimized Spatial Hashing for Collision Detection of Deformable Objects
uint32_t h = (i[0] * 73856093) ^ (i[1] * 19349663) ^ (i[2] * 83492791);
uint32_t h = (i[0] * 73856093) ^ (i[1] * 19349663) ^ (i[2] * 83492791) ^ (i[3] * 39916801);

return size_t(h);
}
Expand Down Expand Up @@ -355,18 +356,20 @@ int32_t BytecodeBuilder::addConstantNumber(double value)
return addConstant(k, c);
}

int32_t BytecodeBuilder::addConstantVector(float x, float y, float z)
int32_t BytecodeBuilder::addConstantVector(float x, float y, float z, float w)
{
Constant c = {Constant::Type_Vector};
c.valueVector[0] = x;
c.valueVector[1] = y;
c.valueVector[2] = z;
c.valueVector[3] = w;

ConstantKey k = {Constant::Type_Vector};
static_assert(sizeof(k.value) == sizeof(x) + sizeof(y) && sizeof(k.extra) == sizeof(z), "Expecting vector to have three 32-bit components");
static_assert(sizeof(k.value) == sizeof(x) + sizeof(y) && sizeof(k.extra) == sizeof(z) + sizeof(w), "Expecting vector to have four 32-bit components");
memcpy(&k.value, &x, sizeof(x));
memcpy((char*)&k.value + sizeof(x), &y, sizeof(y));
memcpy(&k.extra, &z, sizeof(z));
memcpy((char*)&k.extra + sizeof(z), &w, sizeof(w));

return addConstant(k, c);
}
Expand Down Expand Up @@ -693,6 +696,7 @@ void BytecodeBuilder::writeFunction(std::string& ss, uint32_t id, uint8_t flags)
writeFloat(ss, c.valueVector[0]);
writeFloat(ss, c.valueVector[1]);
writeFloat(ss, c.valueVector[2]);
writeFloat(ss, c.valueVector[3]);
break;

case Constant::Type_String:
Expand Down Expand Up @@ -1684,7 +1688,11 @@ void BytecodeBuilder::dumpConstant(std::string& result, int k) const
formatAppend(result, "%.17g", data.valueNumber);
break;
case Constant::Type_Vector:
formatAppend(result, "%.9g, %.9g, %.9g", data.valueVector[0], data.valueVector[1], data.valueVector[2]);
// 3-vectors is the most common configuration, so truncate to three components if possible
if (data.valueVector[3] == 0.0)
formatAppend(result, "%.9g, %.9g, %.9g", data.valueVector[0], data.valueVector[1], data.valueVector[2]);
else
formatAppend(result, "%.9g, %.9g, %.9g, %.9g", data.valueVector[0], data.valueVector[1], data.valueVector[2], data.valueVector[3]);
break;
case Constant::Type_String:
{
Expand Down
4 changes: 2 additions & 2 deletions Compiler/src/Compiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1238,7 +1238,7 @@ struct Compiler
break;

case Constant::Type_Vector:
cid = bytecode.addConstantVector(c->valueVector[0], c->valueVector[1], c->valueVector[2]);
cid = bytecode.addConstantVector(c->valueVector[0], c->valueVector[1], c->valueVector[2], c->valueVector[3]);
break;

case Constant::Type_String:
Expand Down Expand Up @@ -2069,7 +2069,7 @@ struct Compiler

case Constant::Type_Vector:
{
int32_t cid = bytecode.addConstantVector(cv->valueVector[0], cv->valueVector[1], cv->valueVector[2]);
int32_t cid = bytecode.addConstantVector(cv->valueVector[0], cv->valueVector[1], cv->valueVector[2], cv->valueVector[3]);
emitLoadK(target, cid);
}
break;
Expand Down
3 changes: 2 additions & 1 deletion Compiler/src/ConstantFolding.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ static bool constantsEqual(const Constant& la, const Constant& ra)
return ra.type == Constant::Type_Vector &&
la.valueVector[0] == ra.valueVector[0] &&
la.valueVector[1] == ra.valueVector[1] &&
la.valueVector[2] == ra.valueVector[2];
la.valueVector[2] == ra.valueVector[2] &&
la.valueVector[3] == ra.valueVector[3];

case Constant::Type_String:
return ra.type == Constant::Type_String && la.stringLength == ra.stringLength && memcmp(la.valueString, ra.valueString, la.stringLength) == 0;
Expand Down
2 changes: 1 addition & 1 deletion Compiler/src/ConstantFolding.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ struct Constant
{
bool valueBoolean;
double valueNumber;
float valueVector[3];
float valueVector[4];
const char* valueString = nullptr; // length stored in stringLength
};

Expand Down
3 changes: 2 additions & 1 deletion VM/src/lvmload.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,8 @@ int luau_load(lua_State* L, const char* chunkname, const char* data, size_t size
float x = read<float>(data, size, offset);
float y = read<float>(data, size, offset);
float z = read<float>(data, size, offset);
setvvalue(&p->k[j], x, y, z, 0.0f);
float w = read<float>(data, size, offset);
setvvalue(&p->k[j], x, y, z, w);
break;
}

Expand Down
7 changes: 7 additions & 0 deletions tests/Compiler.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4494,6 +4494,13 @@ GETIMPORT R0 1 [print]
LOADK R1 K2 [1, 2, 3]
CALL R0 1 0
RETURN R0 0
)");

CHECK_EQ("\n" + compileFunction("print(Vector3.new(1, 2, 3, 4))", 0, 2, /*enableVectors*/ true), R"(
GETIMPORT R0 1 [print]
LOADK R1 K2 [1, 2, 3, 4]
CALL R0 1 0
RETURN R0 0
)");
}

Expand Down

0 comments on commit 996db08

Please sign in to comment.