From 63c370f9b8d7c58c52ecd85a68e3062b41522ff6 Mon Sep 17 00:00:00 2001 From: ov-4 Date: Mon, 5 Jan 2026 11:00:14 +0000 Subject: Data lab initialization --- datalab-handout/tests.c | 118 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 118 insertions(+) create mode 100644 datalab-handout/tests.c (limited to 'datalab-handout/tests.c') diff --git a/datalab-handout/tests.c b/datalab-handout/tests.c new file mode 100644 index 0000000..38eb9c6 --- /dev/null +++ b/datalab-handout/tests.c @@ -0,0 +1,118 @@ +/* Testing Code */ + +#include +#include + +/* Routines used by floation point test code */ + +/* Convert from bit level representation to floating point number */ +float u2f(unsigned u) { + union { + unsigned u; + float f; + } a; + a.u = u; + return a.f; +} + +/* Convert from floating point number to bit-level representation */ +unsigned f2u(float f) { + union { + unsigned u; + float f; + } a; + a.f = f; + return a.u; +} + +//1 +int test_bitXor(int x, int y) +{ + return x^y; +} +int test_tmin(void) { + return 0x80000000; +} +//2 +int test_isTmax(int x) { + return x == 0x7FFFFFFF; +} +int test_allOddBits(int x) { + int i; + for (i = 1; i < 32; i+=2) + if ((x & (1<>=1, cnt++) + ; + + return (int)(cnt + 1); +} +//float +unsigned test_floatScale2(unsigned uf) { + float f = u2f(uf); + float tf = 2*f; + if (isnan(f)) + return uf; + else + return f2u(tf); +} +int test_floatFloat2Int(unsigned uf) { + float f = u2f(uf); + int x = (int) f; + return x; +} +unsigned test_floatPower2(int x) { + float result = 1.0; + float p2 = 2.0; + int recip = (x < 0); + /* treat tmin specially */ + if ((unsigned)x == 0x80000000) { + return 0; + } + if (recip) { + x = -x; + p2 = 0.5; + } + while (x > 0) { + if (x & 0x1) + result = result * p2; + p2 = p2 * p2; + x >>= 1; + } + return f2u(result); +} -- cgit v1.3.1-4-g156e