Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 2 additions & 0 deletions cmake/sac-core-ext.txt
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ structures/ScalarArith.xsac Core
structures/String.sac Core
structures/StringArray.sac Ext
structures/Structures.sac Core
structures/Vector3d.xsac Ext
structures/Vector3f.xsac Ext

# System
system/Clock.sac Core
Expand Down
171 changes: 171 additions & 0 deletions include/Vector3.mac
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
#pragma safe

module VECTOR3;

export all;

struct VECTOR3 {
REAL VX;
REAL VY;
REAL VZ;
};

inline REAL
l2norm(struct VECTOR3 v)
{
x = _mul_SxS_(v.VX, v.VX);
y = _mul_SxS_(v.VY, v.VY);
z = _mul_SxS_(v.VZ, v.VZ);
return Math::sqrt(_add_SxS_(_add_SxS_(x, y), z));
}

/******************************************************************************
*
* Binary operations
*
******************************************************************************/

#define VEC3D_BIN_AxA(name, op) \
inline struct VECTOR3[d>0:shp] \
name(struct VECTOR3[d>0:shp] a, struct VECTOR3[d>0:shp] b) \
{ \
return { iv -> VECTOR3 { op(_sel_VxA_(iv, a).VX, _sel_VxA_(iv, b).VX), \
op(_sel_VxA_(iv, a).VY, _sel_VxA_(iv, b).VY), \
op(_sel_VxA_(iv, a).VZ, _sel_VxA_(iv, b).VZ) } \
| iv < shp }; \
}

#define VEC3D_BIN_AxS(name, op) \
inline struct VECTOR3[d>0:shp] \
name(struct VECTOR3[d>0:shp] a, struct VECTOR3 b) \
{ \
return { iv -> VECTOR3 { op(_sel_VxA_(iv, a).VX, b.VX), \
op(_sel_VxA_(iv, a).VY, b.VY), \
op(_sel_VxA_(iv, a).VZ, b.VZ) } \
| iv < shp }; \
}

#define VEC3D_BIN_AxX(name, op) \
inline struct VECTOR3[d>0:shp] name(struct VECTOR3[d>0:shp] a, REAL b) \
{ \
return { iv -> VECTOR3 { op(_sel_VxA_(iv, a).VX, b), \
op(_sel_VxA_(iv, a).VY, b), \
op(_sel_VxA_(iv, a).VZ, b) } \
| iv < shp }; \
}

#define VEC3D_BIN_SxS(name, op) \
inline struct VECTOR3 name(struct VECTOR3 a, struct VECTOR3 b) \
{ \
return VECTOR3 { op(a.VX, b.VX), op(a.VY, b.VY), op(a.VZ, b.VZ) }; \
}

#define VEC3D_BIN_SxX(name, op) \
inline struct VECTOR3 name(struct VECTOR3 a, REAL b) \
{ \
return VECTOR3 { op(a.VX, b), op(a.VY, b), op(a.VZ, b) }; \
}

#define VEC3D_BIN(name, op) \
VEC3D_BIN_AxA(name, op) \
VEC3D_BIN_AxS(name, op) \
VEC3D_BIN_AxX(name, op) \
VEC3D_BIN_SxS(name, op) \
VEC3D_BIN_SxX(name, op)

VEC3D_BIN(+, _add_SxS_)
VEC3D_BIN(-, _sub_SxS_)
VEC3D_BIN(*, _mul_SxS_)
VEC3D_BIN(/, _div_SxS_)

/******************************************************************************
*
* Unary operations
*
******************************************************************************/

#define VEC3D_UNARY_A(name, op) \
inline struct VECTOR3[d>0:shp] name(struct VECTOR3[d>0:shp] a) \
{ \
return { iv -> VECTOR3 { op(_sel_VxA_(iv, a).VX), \
op(_sel_VxA_(iv, a).VY), \
op(_sel_VxA_(iv, a).VZ) } \
| iv < shp }; \
}

#define VEC3D_UNARY_S(name, op) \
inline struct VECTOR3 name(struct VECTOR3 a) \
{ \
return VECTOR3{ op(a.VX), op(a.VY), op(a.VZ) }; \
}

#define VEC3D_UNARY(name, op) \
VEC3D_UNARY_A(name, op) \
VEC3D_UNARY_S(name, op)

VEC3D_UNARY(-, _neg_S_)

/******************************************************************************
*
* Equality operations
*
******************************************************************************/

#define VEC3D_EQ_AxA(name, op, and_or) \
inline bool[d>0:shp] name(struct VECTOR3[d>0:shp] a, struct VECTOR3[d>0:shp] b)\
{ \
return with { \
(_mul_SxV_(0, shp) <= iv < shp) : \
and_or(op(_sel_VxA_(iv, a).VX, _sel_VxA_(iv, b).VX), \
and_or(op(_sel_VxA_(iv, a).VY, _sel_VxA_(iv, b).VY), \
op(_sel_VxA_(iv, a).VZ, _sel_VxA_(iv, b).VZ))); \
}: genarray(shp, true); \
}

#define VEC3D_EQ_AxS(name, op, and_or) \
inline bool[d>0:shp] name(struct VECTOR3[d>0:shp] a, struct VECTOR3 b) \
{ \
return with { \
(_mul_SxV_(0, shp) <= iv < shp) : \
and_or(op(_sel_VxA_(iv, a).VX, b.VX), \
and_or(op(_sel_VxA_(iv, a).VY, b.VY), \
op(_sel_VxA_(iv, a).VZ, b.VZ))); \
}: genarray(shp, true); \
}

#define VEC3D_EQ_AxX(name, op, and_or) \
inline bool[d>0:shp] name(struct VECTOR3[d>0:shp] a, REAL b) \
{ \
return with { \
(_mul_SxV_(0, shp) <= iv < shp) : \
and_or(op(_sel_VxA_(iv, a).VX, b), \
and_or(op(_sel_VxA_(iv, a).VY, b), \
op(_sel_VxA_(iv, a).VZ, b))); \
}: genarray(shp, true); \
}

#define VEC3D_EQ_SxS(name, op, and_or) \
inline bool name(struct VECTOR3 a, struct VECTOR3 b) \
{ \
return and_or(op(a.VX, b.VX), \
and_or(op(a.VY, b.VY), \
op(a.VZ, b.VZ))); \
}

#define VEC3D_EQ_SxX(name, op, and_or) \
inline bool name(struct VECTOR3 a, REAL b) \
{ \
return and_or(op(a.VX, b), \
and_or(op(a.VY, b), \
op(a.VZ, b))); \
}

#define VEC3D_EQ(name, op, and_or) \
VEC3D_EQ_AxA(name, op, and_or) \
VEC3D_EQ_AxS(name, op, and_or) \
VEC3D_EQ_AxX(name, op, and_or) \
VEC3D_EQ_SxS(name, op, and_or) \
VEC3D_EQ_SxX(name, op, and_or)

VEC3D_EQ(==, _eq_SxS_, _and_SxS_)
VEC3D_EQ(!=, _neq_SxS_, _or_SxS_)
5 changes: 5 additions & 0 deletions src/structures/Structures.sac
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ import Color8: all;
import Complex: all;
import List: all;
import Quaternion: all;
/* Importing both currently causes a name clash, because the same C function
* name is being generated for both the double and float variants.
* Until this has been fixed, we must exclude them from this module. */
//import Vector3d: all;
//import Vector3f: all;
#endif

export all;
7 changes: 7 additions & 0 deletions src/structures/Vector3d.xsac
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#define REAL double
#define VECTOR3 Vector3d
#define VX xd
#define VY yd
#define VZ zd

#include "Vector3.mac"
7 changes: 7 additions & 0 deletions src/structures/Vector3f.xsac
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#define REAL float
#define VECTOR3 Vector3f
#define VX xf
#define VY yf
#define VZ zf

#include "Vector3.mac"
Loading