<commit_before>#include <stdio.h>
#include <assert.h>
#include <math.h>

#define	EMIT_ASN_DEBUG	1
#include <REAL.h>

static char reconstructed[2][512];
static int reconstr_lens[2];

static int
callback(const void *buffer, size_t size, void *app_key) {
	char *buf = reconstructed[app_key ? 1 : 0];
	int *len = &reconstr_lens[app_key ? 1 : 0];

	if(*len + size >= sizeof(reconstructed[0]))
		return -1;

	memcpy(buf + *len, buffer, size);
	*len += size;

	return 0;
}

static void
check_str_repr(double d, const char *sample, const char *canonical_sample) {
	ssize_t s1, s2;

	reconstr_lens[1] = reconstr_lens[0] = 0;

	s1 = REAL__dump(d, 0, callback, 0);
	assert(s1 < sizeof(reconstructed[0]));
	assert(s1 == reconstr_lens[0]);
	reconstructed[0][s1] = '\0';

	s2 = REAL__dump(d, 1, callback, (void *)1);
	assert(s2 < sizeof(reconstructed[1]));
	assert(s2 == reconstr_lens[1]);
	reconstructed[1][s2] = '\0';

	if(sample) {
		printf("Checking %f->[%s] against [%s]%s\n",
			d, reconstructed[0], sample,
			canonical_sample ? " (canonical follows...)" : ""
		);
		assert(!strcmp(reconstructed[0], sample));
	}
	if(canonical_sample) {
		printf("Checking %f->[%s] against [%s] (canonical)\n",
			d, reconstructed[1], canonical_sample);
		assert(!strcmp(reconstructed[1], canonical_sample));
	}
}

static void
check_impl(REAL_t *rn, double orig_dbl, const char *sample, const char *canonical_sample, int line) {
	double val;
	uint8_t *p, *end;
	int ret;

	printf("Line %d: double value %.12f [", line, orig_dbl);
	for(p = (uint8_t *)&orig_dbl, end = p + sizeof(double); p < end ; p++)
		printf("%02x", *p);
	printf("] (ilogb %d)\n", ilogb(orig_dbl));

	val = frexp(orig_dbl, &ret);
	printf("frexp(%f, %d): [", val, ret);
	for(p = (uint8_t *)&val, end = p + sizeof(double); p < end ; p++)<commit_msg>slightly prettier code<commit_after>#include <stdio.h>
#include <assert.h>
#include <math.h>

#define	EMIT_ASN_DEBUG	1
#include <REAL.h>

static char reconstructed[2][512];
static int reconstr_lens[2];

static int
callback(const void *buffer, size_t size, void *app_key) {
	char *buf = reconstructed[app_key ? 1 : 0];
	int *len = &reconstr_lens[app_key ? 1 : 0];

	if(*len + size >= sizeof(reconstructed[0]))
		return -1;

	memcpy(buf + *len, buffer, size);
	*len += size;

	return 0;
}

static char *
d2s(double d, int canonical, const char *str) {
	ssize_t s;

	reconstr_lens[canonical] = 0;
	s = REAL__dump(d, canonical, callback, (void *)(ptrdiff_t)canonical);
	assert(s < sizeof(reconstructed[canonical]));
	assert(s == reconstr_lens[canonical]);
	reconstructed[canonical][s] = '\0';	// ASCIIZ
	return reconstructed[canonical];
}

/*
 * Verify that a string representation of a given floating point value
 * is as given in the (sample) and (canonical_sample) arguments.
 */
static void
check_str_repr(double d, const char *sample, const char *canonical_sample) {
	char *s0, *s1;

	s0 = d2s(d, 0, sample);
	s1 = d2s(d, 1, canonical_sample);

	if(sample) {
		printf("Checking %f->[%s] against [%s]%s\n",
			d, s0, sample,
			canonical_sample ? " (canonical follows...)" : ""
		);
		assert(!strcmp(s0, sample));
	}
	if(canonical_sample) {
		printf("Checking %f->[%s] against [%s] (canonical)\n",
			d, s1, canonical_sample);
		assert(!strcmp(s1, canonical_sample));
	}
}

static void
check_impl(REAL_t *rn, double orig_dbl, const char *sample, const char *canonical_sample, int line) {
	double val;
	uint8_t *p, *end;
	int ret;

	printf("Line %d: double value %.12f [", line, orig_dbl);
	for(p = (uint8_t *)&orig_dbl, end = p + sizeof(double); p < end ; p++)
		printf("%02x", *p);
	printf("] (ilogb %d)\n", ilogb(orig_dbl));

	val = frexp(orig_dbl, &ret);
	printf("frexp(%f, %d): [", val, ret);
	for(p = (uint8_t *)&val, end = p + sizeof(double); p < end ; p++)
{'fraction_non_alphanumeric': 0.18135818908122503, 'fraction_numerical': 0.02237017310252996, 'mean_word_length': 4.841368584758943, 'pattern_counts': {'":': 0, '<': 18, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4796123', 'n_tokens_mistral': 1591, 'n_tokens_neox': 1480, 'n_words': 373}