From 6fe7baeff10ecc02f264b0923cbf661aa8fb2a31 Mon Sep 17 00:00:00 2001 From: Avril Date: Fri, 27 Nov 2020 14:47:29 +0000 Subject: [PATCH] debug messages --- lean/include/debug.h | 43 +++++++++++++++++++++++++++ lean/include/rng/drng.hpp | 7 +++-- lean/include/rng/frng.hpp | 14 +++++---- lean/include/rng/xoroshiro128plus.hpp | 13 ++++---- lean/include/shuffle3.h | 3 +- lean/src/debug.c | 17 +++++++++++ lean/src/main.c | 6 ++++ lean/src/work.cpp | 8 +++++ 8 files changed, 98 insertions(+), 13 deletions(-) create mode 100644 lean/include/debug.h create mode 100644 lean/src/debug.c diff --git a/lean/include/debug.h b/lean/include/debug.h new file mode 100644 index 0000000..334e6d1 --- /dev/null +++ b/lean/include/debug.h @@ -0,0 +1,43 @@ +#ifndef _DEBUG_H +#define _DEBUG_H + +#ifdef __cplusplus +extern "C" { +#endif + +struct debuginfo { + const char* file; + const char* function; + int line; +}; + +void _do_dprintf(struct debuginfo di, const char* fmt, ...); + +#ifdef __cplusplus +extern "C++" { +#include + template + inline void _real_dprintf(const char* file, const char* function, int line, const char* fmt, Args&&... args) + { +#ifdef DEBUG + debuginfo i = { file, function, line }; + _do_dprintf(i, fmt, std::forward(args)...); +#endif + } +#define D_dprintf(fmt, ...) _real_dprintf(__FILE__, __func__, __LINE__, fmt __VA_OPT__(,) __VA_ARGS__) +} +#else + +#ifdef DEBUG +#define D_dprintf(fmt, ...) _do_dprintf( (struct debuginfo){.file = __FILE__, .function = __func__, .line = __LINE__}, fmt __VA_OPT__(,) __VA_ARGS__) +#else +static inline void _do__nothing(const char* fmt, ...) {} +#define D_dprintf(fmt, ...) _do__nothing(fmt __VA_OPT__(,) __VA_ARGS__) //(fmt __VA_OPT__(,) __VA_ARGS__, (void)0) +#endif +#endif + +#ifdef __cplusplus +} +#endif + +#endif /* _DEBUG_H */ diff --git a/lean/include/rng/drng.hpp b/lean/include/rng/drng.hpp index 56cf6a0..217fdee 100644 --- a/lean/include/rng/drng.hpp +++ b/lean/include/rng/drng.hpp @@ -1,11 +1,14 @@ #include "impl.hpp" - +#include namespace rng { struct drng : public RNG { - inline drng(std::uint32_t seed) : state(seed){sample();} + inline drng(std::uint32_t seed) : state(seed){ + D_dprintf("drng: seeded with %u", seed); + //dprintf(" dummy run sample: %f", sample()); + } inline drng() : drng(1){} static drng from_time(); diff --git a/lean/include/rng/frng.hpp b/lean/include/rng/frng.hpp index c98cc86..4b9c3f0 100644 --- a/lean/include/rng/frng.hpp +++ b/lean/include/rng/frng.hpp @@ -2,6 +2,8 @@ #include "impl.hpp" #include +#include + namespace rng { struct frng : public RNG @@ -27,11 +29,12 @@ namespace rng return fract(sin(dot(state, vec2)) * 43758.5453); } - inline constexpr frng(double s1, double s2) : state({s1, s2}){} - inline constexpr frng(const std::array& ar) : state(ar){} - inline constexpr frng(std::array&& ar) : state(ar){} - inline constexpr frng(const double (&ar)[2]) : state({ar[0], ar[1]}) {} - +#define P D_dprintf("frng: seeded with (%f, %f)", state[0], state[1]); + inline constexpr frng(double s1, double s2) : state({s1, s2}){P} + inline constexpr frng(const std::array& ar) : state(ar){P} + inline constexpr frng(std::array&& ar) : state(ar){P} + inline constexpr frng(const double (&ar)[2]) : state({ar[0], ar[1]}) {P} +#undef P inline constexpr double next_double() override { return sample(); } inline constexpr float next_float() override { return (float)sample(); } protected: @@ -60,3 +63,4 @@ namespace rng } }; } + diff --git a/lean/include/rng/xoroshiro128plus.hpp b/lean/include/rng/xoroshiro128plus.hpp index d5553d7..d0dddc1 100644 --- a/lean/include/rng/xoroshiro128plus.hpp +++ b/lean/include/rng/xoroshiro128plus.hpp @@ -1,17 +1,19 @@ #pragma once #include "impl.hpp" +#include namespace rng { struct xoroshiro128plus : public RNG { using State = std::array; - inline constexpr xoroshiro128plus(std::uint64_t s0, std::uint64_t s1) : state({s0, s1}){} - inline constexpr xoroshiro128plus(std::array&& ar) : state(ar){} - inline constexpr xoroshiro128plus(const std::array& ar) : state(ar){} - inline constexpr xoroshiro128plus(const std::uint64_t (&ar)[2]) : state({ar[0], ar[1]}){} - +#define P D_dprintf("xorng: seeded with (%lu, %lu)", state[0], state[1]); + inline constexpr xoroshiro128plus(std::uint64_t s0, std::uint64_t s1) : state({s0, s1}){P} + inline constexpr xoroshiro128plus(std::array&& ar) : state(ar){P} + inline constexpr xoroshiro128plus(const std::array& ar) : state(ar){P} + inline constexpr xoroshiro128plus(const std::uint64_t (&ar)[2]) : state({ar[0], ar[1]}){P} +#undef P std::uint64_t next_ulong(); using RNG::next_long; std::int64_t next_long() override; @@ -24,3 +26,4 @@ namespace rng State state; }; } + diff --git a/lean/include/shuffle3.h b/lean/include/shuffle3.h index 1ac7dff..d70d74f 100644 --- a/lean/include/shuffle3.h +++ b/lean/include/shuffle3.h @@ -14,12 +14,13 @@ extern "C" { #define _FORCE_INLINE __attribute__((gnu_inline)) extern inline #endif + /* #ifdef DEBUG #define dprintf(fmt, ...) printf("[dbg @" __FILE__ "->%s:%d] " fmt "\n", __func__, __LINE__ __VA_OPT__(,) __VA_ARGS__) #else #define dprintf(fmt, ...) #endif - +*/ extern const char* _prog_name; #ifdef __cplusplus diff --git a/lean/src/debug.c b/lean/src/debug.c new file mode 100644 index 0000000..0755ed2 --- /dev/null +++ b/lean/src/debug.c @@ -0,0 +1,17 @@ +#include +#include +#include + +#include + +void _do_dprintf(struct debuginfo info, const char* fmt, ...) +{ +#ifdef DEBUG + va_list li; + va_start(li, fmt); + fprintf(stderr, "[dbg @%s->%s:%d]: ", info.file, info.function,info.line); + vfprintf(stderr, fmt, li); + fprintf(stderr, "\n"); + va_end(li); +#endif +} diff --git a/lean/src/main.c b/lean/src/main.c index f10df9c..7e5e736 100644 --- a/lean/src/main.c +++ b/lean/src/main.c @@ -11,6 +11,8 @@ #include #include +#include + #define noreturn __attribute__((noreturn)) void _Static_assert(sizeof(float)==sizeof(uint32_t), "float is not 32 bits"); @@ -41,6 +43,7 @@ int main(int argc, char** argv) if( !argv[1] || *(argv[1]) != '-') help_then_exit(); + D_dprintf("Parsing `%c'", argv[1][1]); switch(argv[1][1]) { case 's': @@ -50,6 +53,7 @@ int main(int argc, char** argv) fprintf(stderr, "Error: -s expected file argument.\n"); return 1; } + D_dprintf("parsed.op = %d", OP_SHUFFLE_IP); break; case 'u': parsed.op = OP_UNSHUFFLE_IP; @@ -58,6 +62,7 @@ int main(int argc, char** argv) fprintf(stderr, "Error: -u expected file argument.\n"); return 1; } + D_dprintf("parsed.op = %d", OP_UNSHUFFLE_IP); break; case 'h': usage(); @@ -65,6 +70,7 @@ int main(int argc, char** argv) default: fprintf(stderr, "Error: unknown argument `%s'\n\n", argv[1]); help_then_exit(); + panic("Unreachable"); } return do_work(parsed); diff --git a/lean/src/work.cpp b/lean/src/work.cpp index bf8c8a8..20c9ed4 100644 --- a/lean/src/work.cpp +++ b/lean/src/work.cpp @@ -11,6 +11,7 @@ #include #include +#include template std::tuple minmax_t(const span& array, Fn keep) @@ -18,6 +19,7 @@ std::tuple minmax_t(const span& array, Fn keep) T highest; T lowest; bool init=false; + D_dprintf("minmax_t: %p (%lu)", array.as_ptr(), array.size()); for(std::size_t i=0;i()); + D_dprintf("MMX res (s8): %d -- %d", byte_l, byte_h); rng::drng drng((std::int32_t) ((0xfffa << 16) | (byte_l<<7) | byte_h )); rng::unshuffle(drng, map.as_span()); auto [float_l, float_h] = minmax_t(map.as_span().reinterpret(), [](float f) -> bool { return !( (f!=f) || f < -FLT_MAX || f > FLT_MAX); }); + D_dprintf("MMX res (f32): %f -- %f", float_l, float_h); rng::frng frng(float_l, float_h); rng::unshuffle(frng, map.as_span().reinterpret()); auto [long_l, long_h] = minmax_t(map.as_span().reinterpret()); + D_dprintf("MMX res (u64): %ld -- %ld", long_l, long_h); rng::xoroshiro128plus xorng(*(const std::uint64_t*)&long_l, *(const std::uint64_t*)&long_h); rng::unshuffle(xorng, map.as_span().reinterpret()); } else { auto [long_l, long_h] = minmax_t(map.as_span().reinterpret()); + D_dprintf("MMX res (u64): %ld -- %ld", long_l, long_h); rng::xoroshiro128plus xorng(*(const std::uint64_t*)&long_l, *(const std::uint64_t*)&long_h); rng::shuffle(xorng, map.as_span().reinterpret()); auto [float_l, float_h] = minmax_t(map.as_span().reinterpret(), [](float f) -> bool { return !( (f!=f) || f < -FLT_MAX || f > FLT_MAX); }); + D_dprintf("MMX res (f32): %f -- %f", float_l, float_h); rng::frng frng(float_l, float_h); rng::shuffle(frng, map.as_span().reinterpret()); auto [byte_l, byte_h] = minmax_t(map.as_span().reinterpret()); + D_dprintf("MMX res (s8): %d -- %d", byte_l, byte_h); rng::drng drng((std::int32_t) ((0xfffa << 16) | (byte_l<<7) | byte_h )); rng::shuffle(drng, map.as_span()); }