debug messages

lean
Avril 4 years ago
parent 3dd8abc461
commit 6fe7baeff1
Signed by: flanchan
GPG Key ID: 284488987C31F630

@ -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 <utility>
template<typename... Args>
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>(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 */

@ -1,11 +1,14 @@
#include "impl.hpp"
#include <debug.h>
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();

@ -2,6 +2,8 @@
#include "impl.hpp"
#include <cmath>
#include <debug.h>
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<double, 2>& ar) : state(ar){}
inline constexpr frng(std::array<double, 2>&& 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<double, 2>& ar) : state(ar){P}
inline constexpr frng(std::array<double, 2>&& 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
}
};
}

@ -1,17 +1,19 @@
#pragma once
#include "impl.hpp"
#include <debug.h>
namespace rng
{
struct xoroshiro128plus : public RNG
{
using State = std::array<std::uint64_t, 2>;
inline constexpr xoroshiro128plus(std::uint64_t s0, std::uint64_t s1) : state({s0, s1}){}
inline constexpr xoroshiro128plus(std::array<std::uint64_t, 2>&& ar) : state(ar){}
inline constexpr xoroshiro128plus(const std::array<std::uint64_t, 2>& 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<std::uint64_t, 2>&& ar) : state(ar){P}
inline constexpr xoroshiro128plus(const std::array<std::uint64_t, 2>& 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;
};
}

@ -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

@ -0,0 +1,17 @@
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#include <debug.h>
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
}

@ -11,6 +11,8 @@
#include <rng.h>
#include <work.h>
#include <debug.h>
#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);

@ -11,6 +11,7 @@
#include <shuffle.hpp>
#include <work.h>
#include <debug.h>
template<typename T, typename Fn>
std::tuple<T, T> minmax_t(const span<T>& array, Fn keep)
@ -18,6 +19,7 @@ std::tuple<T, T> minmax_t(const span<T>& 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<array.size();i++)
{
if(!keep(array[i])) continue;
@ -51,26 +53,32 @@ namespace work
if constexpr(unshuffle)
{
auto [byte_l, byte_h] = minmax_t(map.as_span().reinterpret<std::int8_t>());
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>(), [](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<float>());
auto [long_l, long_h] = minmax_t(map.as_span().reinterpret<std::int64_t>());
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<std::int64_t>());
} else {
auto [long_l, long_h] = minmax_t(map.as_span().reinterpret<std::int64_t>());
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<std::int64_t>());
auto [float_l, float_h] = minmax_t(map.as_span().reinterpret<float>(), [](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<float>());
auto [byte_l, byte_h] = minmax_t(map.as_span().reinterpret<std::int8_t>());
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());
}

Loading…
Cancel
Save