Skip to content

Stage 0 sync #50

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Mar 14, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 12 additions & 13 deletions source/abstract/Archive.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ ArchiveHandle::ArchiveHandle(void *archiveStart) : m_startAddress(archiveStart)
RawArchive *rawArchive = reinterpret_cast<RawArchive *>(archiveStart);
assert(rawArchive->isValidSignature());

m_nodesAddress = static_cast<u8 *>(archiveStart) + parse<u32>(rawArchive->m_nodesOffset);
m_filesAddress = static_cast<u8 *>(archiveStart) + parse<u32>(rawArchive->m_filesOffset);
m_nodesAddress = static_cast<u8 *>(archiveStart) + parse<u32>(rawArchive->nodesOffset);
m_filesAddress = static_cast<u8 *>(archiveStart) + parse<u32>(rawArchive->filesOffset);

// "The right bound of the root node is the number of nodes"
m_count = parse<u32>(node(0)->m_directory.m_next);
m_count = parse<u32>(node(0)->m_directory.next);
// Strings exist directly after the last node
m_strings = reinterpret_cast<const char *>(reinterpret_cast<Node *>(m_nodesAddress) + m_count);
m_currentNode = 0;
Expand All @@ -38,11 +38,11 @@ s32 ArchiveHandle::convertPathToEntryId(const char *path) const {
if (path[0] == '.') {
if (path[1] == '.') {
if (path[2] == '/') {
entryId = node(entryId)->m_directory.m_parent;
entryId = node(entryId)->m_directory.parent;
path += 3;
continue;
} else if (path[2] == '\0') {
return node(entryId)->m_directory.m_parent;
return node(entryId)->m_directory.parent;
} else {
// Malformed "..*" case
return -1;
Expand All @@ -64,7 +64,7 @@ s32 ArchiveHandle::convertPathToEntryId(const char *path) const {

bool found = false;
const u32 anchor = entryId++;
while (entryId < parse<u32>(node(anchor)->m_directory.m_next)) {
while (entryId < parse<u32>(node(anchor)->m_directory.next)) {
if (!node(anchor)->isDirectory() && endOfPath) {
entryId++;
continue;
Expand Down Expand Up @@ -107,13 +107,13 @@ bool ArchiveHandle::open(s32 entryId, FileInfo &info) const {
return false;
}

info.m_startOffset = parse<u32>(node_->m_file.m_startAddress);
info.m_length = parse<u32>(node_->m_file.m_length);
info.startOffset = parse<u32>(node_->file.startAddress);
info.length = parse<u32>(node_->file.length);
return true;
}

void *ArchiveHandle::getFileAddress(const FileInfo &info) const {
return static_cast<u8 *>(m_startAddress) + info.m_startOffset;
return static_cast<u8 *>(m_startAddress) + info.startOffset;
}

ArchiveHandle::Node *ArchiveHandle::node(s32 entryId) const {
Expand All @@ -126,16 +126,15 @@ void *ArchiveHandle::startAddress() const {
}

bool ArchiveHandle::RawArchive::isValidSignature() const {
auto signature = parse<u32>(m_signature);
return signature == U8_SIGNATURE;
return parse<u32>(signature) == U8_SIGNATURE;
}

bool ArchiveHandle::Node::isDirectory() const {
return !!(m_str[0]);
return !!(str[0]);
}

u32 ArchiveHandle::Node::stringOffset() const {
return parse<u32>(m_val) & 0xFFFFFF;
return parse<u32>(val) & 0xFFFFFF;
}

} // namespace Abstract
34 changes: 17 additions & 17 deletions source/abstract/Archive.hh
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,21 @@ namespace Abstract {
struct RawArchive {
bool isValidSignature() const;

u32 m_signature;
u32 m_nodesOffset;
u32 m_nodesSize;
u32 m_filesOffset;
u32 signature;
u32 nodesOffset;
u32 nodesSize;
u32 filesOffset;
};

class ArchiveHandle {
public:
struct RawArchive {
bool isValidSignature() const;

u32 m_signature;
u32 m_nodesOffset;
u32 m_nodesSize;
u32 m_filesOffset;
u32 signature;
u32 nodesOffset;
u32 nodesSize;
u32 filesOffset;
};

// TODO: union
Expand All @@ -34,24 +34,24 @@ public:
u32 stringOffset() const;

union {
u32 m_val;
u8 m_str[4];
u32 val;
u8 str[4];
};
union {
struct {
u32 m_parent;
u32 m_next;
u32 parent;
u32 next;
} m_directory;
struct {
u32 m_startAddress;
u32 m_length;
} m_file;
u32 startAddress;
u32 length;
} file;
};
};

struct FileInfo {
u32 m_startOffset;
u32 m_length;
u32 startOffset;
u32 length;
};

ArchiveHandle(void *archiveStart);
Expand Down
16 changes: 16 additions & 0 deletions source/egg/geom/Sphere.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include "Sphere.hh"

namespace EGG {

Sphere3f::Sphere3f(const EGG::Vector3f &v, f32 r) : pos(v), radius(r) {}

bool Sphere3f::isInsideOtherSphere(const Sphere3f &rhs) const {
f32 radiusDiff = rhs.radius - radius;
if (radiusDiff < 0.0f) {
return false;
}

return rhs.pos.ps_sqDistance(this->pos) < radiusDiff * radiusDiff;
}

} // namespace EGG
16 changes: 16 additions & 0 deletions source/egg/geom/Sphere.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#pragma once

#include "egg/math/Vector.hh"

namespace EGG {

struct Sphere3f {
Sphere3f(const Vector3f &v, f32 r);

bool isInsideOtherSphere(const Sphere3f &rhs) const;

Vector3f pos;
f32 radius;
};

} // namespace EGG
36 changes: 18 additions & 18 deletions source/egg/math/BoundBox.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,21 @@ BoundBox2f::BoundBox2f() {
BoundBox2f::~BoundBox2f() = default;

void BoundBox2f::resetBound() {
mMin.set(std::numeric_limits<f32>::max());
mMax.set(-std::numeric_limits<f32>::max());
min.set(std::numeric_limits<f32>::max());
max.set(-std::numeric_limits<f32>::max());
}

void BoundBox2f::setDirect(const Vector2f &min, const Vector2f &max) {
mMax = max;
mMin = min;
void BoundBox2f::setDirect(const Vector2f &vMin, const Vector2f &vMax) {
max = vMax;
min = vMin;
}

void BoundBox2f::setMin(const Vector2f &min) {
mMin = min;
void BoundBox2f::setMin(const Vector2f &v) {
min = v;
}

void BoundBox2f::setMax(const Vector2f &max) {
mMax = max;
void BoundBox2f::setMax(const Vector2f &v) {
max = v;
}

BoundBox3f::BoundBox3f() {
Expand All @@ -35,21 +35,21 @@ BoundBox3f::BoundBox3f() {
BoundBox3f::~BoundBox3f() = default;

void BoundBox3f::resetBound() {
mMin.set(std::numeric_limits<f32>::max());
mMax.set(-std::numeric_limits<f32>::max());
min.set(std::numeric_limits<f32>::max());
max.set(-std::numeric_limits<f32>::max());
}

void BoundBox3f::setDirect(const Vector3f &min, const Vector3f &max) {
mMax = max;
mMin = min;
void BoundBox3f::setDirect(const Vector3f &vMin, const Vector3f &vMax) {
max = vMax;
min = vMin;
}

void BoundBox3f::setMin(const Vector3f &min) {
mMin = min;
void BoundBox3f::setMin(const Vector3f &v) {
min = v;
}

void BoundBox3f::setMax(const Vector3f &max) {
mMax = max;
void BoundBox3f::setMax(const Vector3f &v) {
max = v;
}

} // namespace EGG
20 changes: 10 additions & 10 deletions source/egg/math/BoundBox.hh
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,25 @@ struct BoundBox2f {
~BoundBox2f();

void resetBound();
void setDirect(const Vector2f &max, const Vector2f &min);
void setMin(const Vector2f &min);
void setMax(const Vector2f &max);
void setDirect(const Vector2f &vMax, const Vector2f &vMin);
void setMin(const Vector2f &v);
void setMax(const Vector2f &v);

Vector2f mMin;
Vector2f mMax;
Vector2f min;
Vector2f max;
};

struct BoundBox3f {
BoundBox3f();
~BoundBox3f();

void resetBound();
void setDirect(const Vector3f &max, const Vector3f &min);
void setMin(const Vector3f &min);
void setMax(const Vector3f &max);
void setDirect(const Vector3f &vMin, const Vector3f &vMax);
void setMin(const Vector3f &V);
void setMax(const Vector3f &v);

Vector3f mMin;
Vector3f mMax;
Vector3f min;
Vector3f max;
};

} // namespace EGG
23 changes: 16 additions & 7 deletions source/egg/math/Math.cc
Original file line number Diff line number Diff line change
Expand Up @@ -270,17 +270,11 @@ f32 sqrt(f32 x) {

// CREDIT: Hanachan
f32 frsqrt(f32 x) {
auto extract = [](f64 val) -> f64 {
u64 bits = std::bit_cast<u64>(val);
bits = (bits & 0xfffffffff8000000ULL) + (bits & 0x8000000);
return std::bit_cast<f64>(bits);
};

// frsqrte instruction
f64 est = frsqrte(x);

// Newton-Raphson refinement
f32 tmp0 = static_cast<f32>(est * extract(est));
f32 tmp0 = static_cast<f32>(est * force25Bit(est));
f32 tmp1 = static_cast<f32>(est * 0.5f);
f32 tmp2 = static_cast<f32>(3.0f - static_cast<f64>(tmp0) * static_cast<f64>(x));
return tmp1 * tmp2;
Expand Down Expand Up @@ -328,4 +322,19 @@ f32 acos(f32 x) {
return std::acos(x);
}

f32 abs(f32 x) {
return std::abs(x);
}

f32 fma(f32 x, f32 y, f32 z) {
return static_cast<f32>(
static_cast<f64>(x) * force25Bit(static_cast<f64>(y)) + static_cast<f64>(z));
}

f64 force25Bit(f64 x) {
u64 bits = std::bit_cast<u64>(x);
bits = (bits & 0xfffffffff8000000ULL) + (bits & 0x8000000);
return std::bit_cast<f64>(bits);
}

} // namespace EGG::Mathf
13 changes: 10 additions & 3 deletions source/egg/math/Math.hh
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#define F_PI 3.1415927f
#define DEG2RAD (F_PI / 180.0f)
#define RAD2DEG (180.0f / F_PI)
#define DEG2FIDX (256.0f / 360.0f)
#define RAD2FIDX (128.0f / F_PI)

Expand All @@ -18,15 +19,21 @@ f32 sin(f32 x);
f32 cos(f32 x);
f32 acos(f32 x);

f32 abs(f32 x);

f32 fma(f32 x, f32 y, f32 z);

f64 force25Bit(f64 x);

// sin/cos struct
struct SinCosEntry {
f32 sinVal, cosVal, sinDt, cosDt;
};

// frsqrte matching
struct BaseAndDec {
int m_base;
int m_dec;
int base;
int dec;
};

union c64 {
Expand Down Expand Up @@ -124,7 +131,7 @@ static inline f64 frsqrte(const f64 val) {
const int i = static_cast<int>(mantissa >> 37);
const int index = i / 2048 + (odd_exponent ? 16 : 0);
const auto &entry = frsqrte_expected[index];
input.u |= static_cast<uint64_t>(entry.m_base - entry.m_dec * (i % 2048)) << 26;
input.u |= static_cast<uint64_t>(entry.base - entry.dec * (i % 2048)) << 26;

return input.f;
}
Expand Down
Loading