Skip to content

Instantly share code, notes, and snippets.

@therontarigo
Created March 10, 2025 20:09
Show Gist options
  • Select an option

  • Save therontarigo/7c7f642d7baa4c053285f6eaf4be2f1f to your computer and use it in GitHub Desktop.

Select an option

Save therontarigo/7c7f642d7baa4c053285f6eaf4be2f1f to your computer and use it in GitHub Desktop.
FreeBSD D47638 float conversion rounding

https://reviews.freebsd.org/D47638

test.c

#include <stdio.h>
#include <string.h>
#define _AMD64_INCLUDE_PARAM_H_
#include "src/sys/sys/param.h"
#define __assert_unreachable() __unreachable()
#include "src/sys/sys/soundcard.h"
#include "src/sys/dev/sound/pcm/sound.h"
#include "src/sys/dev/sound/pcm/pcm.h"

int
main(int argc, char **argv)
{
	for (int i = -100; i < 100; i++) {
		float x = (float)((double)i * (1.41421 / (1l << 34)));
		intpcm_t v = pcm_sample_read(&x, AFMT_FLOAT);
		double y = (double)v / PCM_S32_MAX;
		printf("%16.9e %10d %16.9e %16.9e\n", x, v, y, y - x);
	}
	return 0;
}

plot.py

import numpy as np
import matplotlib.pyplot as plt
[x,v,y,err] = np.loadtxt("res.txt").T
plt.plot(x,err)
plt.savefig("error.png")
plt.show()

Error with INTPCM_T(fv * (float)PCM_S32_MAX); error-cast

Error with INTPCM_T(roundf(fv * (float)PCM_S32_MAX)); error-roundf

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment