Skip to content
Snippets Groups Projects
Verified Commit ced7b74b authored by mirabilos's avatar mirabilos Committed by mirabilos
Browse files

6′19″ runtime, not the best but not too shabby on X61@1.8GHz

parent d468700b
No related branches found
No related tags found
No related merge requests found
../../i2c/ilist
\ No newline at end of file
/* ./ilist 'unsigned int' */
#include <err.h>
#include <limits.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "inputs.h"
#define NELEM(a) (sizeof(a) / sizeof((a)[0]))
#define N NELEM(I)
int
main(void)
{
size_t n;
unsigned long long res;
res = 0;
for (n = 0; n < N; ++n) {
register unsigned long long x, y, z;
#define mix() do { \
x ^= y; \
} while (/* CONSTCOND */ 0)
#define prune() do { \
x &= 16777215; \
} while (/* CONSTCOND */ 0)
x = I[n];
for (z = 0; z < 2000U; ++z) {
y = x * 64U;
mix();
prune();
y = x / 32U;
mix();
prune();
y = x * 2048U;
mix();
prune();
}
res += x;
printf("%u: %u\n", I[n], (unsigned)x);
}
printf("%llu\n", res);
return (0);
}
/* echo 123 >c2 */
/* printf '%u\n' 1 2 3 2024 >c3 */
/* ./ilist 'unsigned int' */
#define _GNU_SOURCE
#include <err.h>
#include <limits.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "inputs.h"
#define NELEM(a) (sizeof(a) / sizeof((a)[0]))
#define N NELEM(I)
#ifdef EX2
#define R 10U
#else
#define R 2000U
#endif
static unsigned char p[N][R] = {0};
static signed char pd[N][R] = {0};
int
main(void)
{
size_t n;
unsigned long bestres = 0;
signed char seq[4];
signed char best[4] /* GCC… */ = { -66, -66, -66, -66 };
for (n = 0; n < N; ++n) {
register unsigned long long x, y, z;
unsigned char o;
#define mix() do { \
x ^= y; \
} while (/* CONSTCOND */ 0)
#define prune() do { \
x &= 16777215; \
} while (/* CONSTCOND */ 0)
x = I[n];
#ifdef EX2
printf("%8u: %u\n", (unsigned)x, (unsigned)(x % 10));
#endif
for (z = 0; z < R; ++z) {
o = x % 10;
y = x * 64U;
mix();
prune();
y = x / 32U;
mix();
prune();
y = x * 2048U;
mix();
prune();
p[n][z] = x % 10U;
pd[n][z] = (int)p[n][z] - (int)o;
#ifdef EX2
printf("%8u: %u (%d)\n", (unsigned)x,
(unsigned)p[n][z], (int)pd[n][z]);
#endif
}
}
#ifdef EX2
printf("D: p\tpd\tfor #0\n");
for (n = 0; n < R; ++n)
printf(" %u\t%d\n", (unsigned)p[0][n], (int)pd[0][n]);
#endif
printf("trying…\n");
for (seq[0] = -9; seq[0] <= 9; ++seq[0])
for (seq[1] = -9; seq[1] <= 9; ++seq[1])
for (seq[2] = -9; seq[2] <= 9; ++seq[2])
for (seq[3] = -9; seq[3] <= 9; ++seq[3]) {
unsigned long res = 0;
for (n = 0; n < N; ++n) {
const signed char *fp;
fp = memmem(pd[n] + 1U,
R - 1U,
seq, 4U);
if (!fp)
continue;
#ifdef EX2
printf("\t%zu got %d,%d,%d,%d at %zu: %u\n",
n, seq[0], seq[1], seq[2], seq[3],
(size_t)(fp - pd[n]),
p[n][(size_t)(fp - pd[n]) + 3U]);
#endif
res += p[n][(size_t)(fp - pd[n]) + 3U];
}
if (res > bestres) {
printf("new best %d,%d,%d,%d ⇒ %lu\n",
seq[0], seq[1], seq[2], seq[3], res);
best[0] = seq[0];
best[1] = seq[1];
best[2] = seq[2];
best[3] = seq[3];
bestres = res;
}
}
printf("done: %d,%d,%d,%d ⇒ %lu\n",
best[0], best[1], best[2], best[3], bestres);
return (0);
}
#!/bin/mksh
if [[ -z $1 ]]; then
print -r -- "E: syntax: ${0##*/} <data type> (e.g. 'unsigned long long')"
exit 1
fi
set -o noglob
dtype=$1
set -e
doone() {
print -r -- 'static const' $dtype 'I[] = {'
sed -e $'s/^/\t/' -e 's/$/,/' <"$1"
print -r -- '};'
}
exec >inputs.h
print '#ifndef PROD'
print
if [[ -s c2 ]]; then
print '#if defined(EX2)'
doone c2
for t in 3 4 5 6 7 8 9; do
if [[ -s c$t ]]; then
print "#elif defined(EX$t)"
doone c$t
fi
done
print '#else'
doone c
print '#endif'
else
doone c
fi
print
print '#else'
print
doone i
print
print '#endif'
exec >/dev/null
exit 0
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment