diff options
author | Erik Schnetter <schnetter@gmail.com> | 2013-09-01 23:45:23 -0400 |
---|---|---|
committer | Erik Schnetter <schnetter@gmail.com> | 2013-09-01 23:45:23 -0400 |
commit | ae370822f291af84098b7d56721fa25b38b70a8b (patch) | |
tree | 82af54c76c8eef886938cef9d5e84ee1831905f7 /test.cc | |
parent | 0d32864cbff42a693f9b5c415642746797f5e9ff (diff) | |
download | vecmathlib-ae370822f291af84098b7d56721fa25b38b70a8b.zip vecmathlib-ae370822f291af84098b7d56721fa25b38b70a8b.tar.gz |
Implement more integer functions: abs bitifthen clz isignbit max min popcount rotate
Rename integer signbit to isignbit to avoid name conflicts.
Diffstat (limited to 'test.cc')
-rw-r--r-- | test.cc | 64 |
1 files changed, 62 insertions, 2 deletions
@@ -2,6 +2,7 @@ #include "vecmathlib.h" +#include <algorithm> #include <cassert> #include <cmath> #include <cstdio> @@ -944,7 +945,7 @@ struct vecmathlib_test { template<typename T> static T local_sr(T x, T y) { return x>>y; } template<typename T> static T local_sl(T x, T y) { return x<<y; } - template<typename T> static bool local_signbit(T x) { return x<0; } + template<typename T> static bool local_isignbit(T x) { return x<0; } template<typename T> static bool local_eq(T x, T y) { return x==y; } template<typename T> static bool local_ne(T x, T y) { return x!=y; } template<typename T> static bool local_lt(T x, T y) { return x<y; } @@ -1027,7 +1028,7 @@ struct vecmathlib_test { check_int<IV,IV>(">>", local_sr, local_sr, x, y & IV(bits-1)); check_int<IV,IV>("<<", local_sl, local_sl, x, y & IV(bits-1)); - check_bool<IV>("signbit", local_signbit, vecmathlib::signbit, x); + check_bool<IV>("isignbit", local_isignbit, vecmathlib::isignbit, x); check_bool<IV,IV>("==", local_eq, local_veq, x, y); check_bool<IV,IV>("!=", local_ne, local_vne, x, y); check_bool<IV,IV>("<", local_lt, local_vlt, x, y); @@ -1075,6 +1076,64 @@ struct vecmathlib_test { check_real("flt_rounds", R(1.0) + 2*FP::epsilon(), rbase[0]); } + static int_t local_bitifthen(int_t x, int_t y, int_t z) + { + return (x & y) | (~x & z); + } + static int_t local_clz(int_t x) + { + int bits = CHAR_BIT * sizeof(x); + int res = 0; + for (; res<bits; ++res) { + if (x & (I(1) << (bits-res-1))) break; + } + return res; + } + static int_t local_max(int_t x, int_t y) + { + return std::max(x, y); + } + static int_t local_min(int_t x, int_t y) + { + return std::min(x, y); + } + static int_t local_popcount(int_t x) + { + int bits = CHAR_BIT * sizeof(x); + int res = 0; + for (int d=0; d<bits; ++d) { + if (x & (I(1) << d)) ++res; + } + return res; + } + static int_t local_rotate(int_t x, int_t n) + { + int_t mask = CHAR_BIT * sizeof(int_t) - 1; + int_t left = x << (n & mask); + int_t right = I(U(x) >> U(-n & mask)); + return left | right; + } + static void test_abs() + { + cout << " testing abs bitifthen clz isignbit max min popcount rotate...\n" << flush; + + for (int i=0; i<imax; ++i) { + const intvec_t x = random(I(-1000000), I(+1000000)); + const intvec_t y = random(I(-1000000), I(+1000000)); + const intvec_t z = random(I(-1000000), I(+1000000)); + + check_int<IV>("abs", std::abs, vecmathlib::abs, x); + check_int<IV,IV,IV>("bitifthen", + local_bitifthen, vecmathlib::bitifthen, x, y, z); + check_int<IV>("clz", local_clz, vecmathlib::clz, x); + check_int<IV,IV>("max", local_max, vecmathlib::max, x, y); + check_int<IV,IV>("min", local_min, vecmathlib::min, x, y); + check_int<IV>("popcount", local_popcount, vecmathlib::popcount, x); + check_int<IV,IV>("rotate", local_rotate, vecmathlib::rotate, x, y[0]); + check_int<IV,IV>("rotate", local_rotate, vecmathlib::rotate, x, y); + } + } + // Change signature: "int" -> "int_t" static real_t local_frexp0(real_t x) { @@ -1521,6 +1580,7 @@ struct vecmathlib_test { test_mem(); // Test "basic" functions first + test_abs(); test_fabs(); test_convert(); test_rcp(); |