Files

242 lines
6.9 KiB
C

/* M6 float/double gate driver.
*
* Calls the compiled float/double functions in m6-float.ll through the M6
* flib ABI and writes the expected GSP result words to the marker array
* defined by the entry assembly. Compiling this driver also exercises the
* compiler's caller side (hidden float/double return slot, stack argument
* pushes, LSW-first doubles) and the callee side of its own C functions
* (float_add, double_add, float_ret, double_ret).
*
* Values are written as raw GSP words, not converted: a float union member
* and an array of two 32-bit words for a GSP double (u[0] = LSW, u[1] = MSW).
*
* Marker word n lives at float_markers[n], i.e. 0x20 bits above the previous
* one; scripts/m6/float-check.lua reads them by bit address from the linker
* map.
*/
typedef unsigned int u32;
typedef union {
float f;
u32 u;
} fu;
typedef union {
double d;
u32 u[2];
} du;
extern volatile u32 float_markers[];
/* Callees defined in m6-float.ll. */
extern float fadd(float, float);
extern float fsub(float, float);
extern float fmul(float, float);
extern float fdiv(float, float);
extern float fneg(float);
extern double dadd(double, double);
extern double dsub(double, double);
extern double dmul(double, double);
extern double ddiv(double, double);
extern double dneg(double);
extern int fcmpeq(float, float);
extern int fcmplt(float, float);
extern int fcmple(float, float);
extern int fcmpgt(float, float);
extern int fcmpge(float, float);
extern int fcmpne(float, float);
extern int dcmplt(double, double);
extern int dcmpeq(double, double);
extern int ftoi(float);
extern int ftou(float);
extern int dtoi(double);
extern int dtou(double);
extern float itof(int);
extern float utof(u32);
extern double itod(int);
extern double utod(u32);
extern double ftod(float);
extern float dtof(double);
/* Control flow on floating-point comparisons. These are the shapes that were
* missing: `if (a < b)` on float/double and `?:` on a double. */
static int fb_lt(float a, float b)
{
if (a < b)
return 11;
return 22;
}
static int db_lt(double a, double b)
{
if (a < b)
return 33;
return 44;
}
/* A while loop whose condition is a float compare and whose body is float
* arithmetic: a(float) = 1.5, b(float) = 4.0 iterates 1.5, 2.5, 3.5 and stops
* at 4.5, so it counts 3. */
static int fcount_lt(float a, float b)
{
int n = 0;
while (a < b) {
a = a + 1.0f;
n++;
}
return n;
}
/* An f64 select with an integer condition. */
static double dsel_i(int c, double x, double y)
{
return c ? x : y;
}
/* An f64 select with a floating-point condition. */
static double dsel_f(double a, double b, double x, double y)
{
return (a < b) ? x : y;
}
/* GSP has no signed zero: -0.0 must lower to the same all-zero words as +0.0,
* exactly as GSPA prints `.float -0.0`. These markers pin that. */
static float fnegzero(void)
{
return -0.0f;
}
static float fposzero(void)
{
return 0.0f;
}
static double dnegzero(void)
{
return -0.0;
}
static void mark_f(u32 n, float v)
{
fu x;
x.f = v;
float_markers[n] = x.u;
}
static void mark_d(u32 n, double v)
{
du x;
x.d = v;
float_markers[n] = x.u[0]; /* LSW: the lower address in GSP order */
float_markers[n + 1] = x.u[1]; /* MSW */
}
/* C-side callees: the compiler must emit the flib calls for these bodies. */
float float_add(float a, float b)
{
return a + b;
}
double double_add(double a, double b)
{
return a + b;
}
float float_ret(void)
{
return 1.5f;
}
double double_ret(void)
{
return 2.5;
}
void float_run(void)
{
/* Single-precision arithmetic, including the reversed subtraction order
* and a negative result. */
mark_f(0, fadd(1.5f, 2.5f)); /* 4.0 */
mark_f(1, fsub(2.5f, 1.5f)); /* 1.0 */
mark_f(2, fsub(1.5f, 2.5f)); /* -1.0 */
mark_f(3, fmul(4.0f, 2.0f)); /* 8.0 */
mark_f(4, fdiv(4.0f, 2.0f)); /* 2.0 */
mark_f(5, fneg(1.5f)); /* -1.5 */
mark_f(6, float_add(1.5f, 2.5f)); /* C body */
mark_f(7, float_ret()); /* C no-argument hidden return */
/* Double-precision arithmetic, LSW then MSW. */
mark_d(8, dadd(1.5, 2.5)); /* 4.0 */
mark_d(10, dsub(2.5, 1.5)); /* 1.0 */
mark_d(12, dsub(1.5, 2.5)); /* -1.0 */
mark_d(14, dmul(4.0, 2.0)); /* 8.0 */
mark_d(16, ddiv(4.0, 2.0)); /* 2.0 */
mark_d(18, dneg(1.5)); /* -1.5 */
mark_d(20, double_add(1.5, 2.5)); /* C body */
mark_d(22, double_ret()); /* C no-argument hidden return */
/* Float compares: true and false cases. */
float_markers[24] = (u32)fcmpeq(1.5f, 1.5f);
float_markers[25] = (u32)fcmpeq(1.5f, 2.5f);
float_markers[26] = (u32)fcmplt(1.5f, 2.5f);
float_markers[27] = (u32)fcmplt(2.5f, 1.5f);
float_markers[28] = (u32)fcmple(2.5f, 2.5f);
float_markers[29] = (u32)fcmpgt(2.5f, 1.5f);
float_markers[30] = (u32)fcmpge(1.5f, 2.5f);
float_markers[31] = (u32)fcmpne(1.5f, 2.5f);
float_markers[32] = (u32)fcmpne(1.5f, 1.5f);
/* Double compares. */
float_markers[33] = (u32)dcmplt(1.5, 2.5);
float_markers[34] = (u32)dcmplt(2.5, 1.5);
float_markers[35] = (u32)dcmpeq(2.5, 2.5);
/* float/double -> integer: truncation toward zero. */
float_markers[36] = (u32)ftoi(2.5f); /* 2 */
float_markers[37] = (u32)ftoi(-1.5f); /* -1 */
float_markers[38] = (u32)ftou(3.9f); /* 3 */
float_markers[39] = (u32)dtoi(2.5); /* 2 */
float_markers[40] = (u32)dtou(3.9); /* 3 */
/* integer -> float/double. */
mark_f(41, itof(7)); /* 7.0 */
mark_f(42, utof(7)); /* 7.0 */
mark_f(43, itof(-3)); /* -3.0 */
mark_d(44, itod(7)); /* 7.0 */
mark_d(46, utod(7)); /* 7.0 */
mark_d(48, ftod(1.5f)); /* 1.5 */
mark_f(50, dtof(2.5)); /* 2.5 */
/* Constants materialised from the GSP pool, including a negative one. */
mark_f(51, 7.0f);
mark_f(52, -3.0f);
mark_d(53, 7.0);
mark_d(55, -3.0);
float_markers[57] = (u32)fcmpge(2.5f, 2.5f); /* 1 */
float_markers[58] = (u32)fcmpgt(1.5f, 1.5f); /* 0 */
float_markers[59] = (u32)dtoi(-1.5); /* -1 */
float_markers[60] = (u32)fcmple(2.5f, 1.5f); /* 0 */
float_markers[61] = (u32)dcmpeq(1.5, 2.5); /* 0 */
/* Control flow on float/double comparisons: the true and false path of a
* float and a double branch, and a float-compared while loop. */
float_markers[62] = (u32)fb_lt(1.5f, 2.5f); /* true: 11 */
float_markers[63] = (u32)fb_lt(2.5f, 1.5f); /* false: 22 */
float_markers[64] = (u32)db_lt(1.5, 2.5); /* true: 33 */
float_markers[65] = (u32)db_lt(2.5, 1.5); /* false: 44 */
float_markers[66] = (u32)fcount_lt(1.5f, 4.0f); /* 3 iterations */
/* ?: on a double, with an integer and a floating-point condition. */
mark_d(67, dsel_i(1, 1.5, 2.5)); /* 1.5 */
mark_d(69, dsel_i(0, 1.5, 2.5)); /* 2.5 */
mark_d(71, dsel_f(1.5, 2.5, 4.0, 8.0)); /* 4.0 */
mark_d(73, dsel_f(2.5, 1.5, 4.0, 8.0)); /* 8.0 */
/* Signed zero: both spellings lower to all-zero GSP words. */
mark_f(75, fnegzero()); /* 0 */
mark_f(76, fposzero()); /* 0 */
mark_d(77, dnegzero()); /* 0 / 0 */
}