Skip to content

Instantly share code, notes, and snippets.

@basic-calculus
Created January 11, 2023 18:52
Show Gist options
  • Select an option

  • Save basic-calculus/4851b6b94d68d3e42dc153141a12a45a to your computer and use it in GitHub Desktop.

Select an option

Save basic-calculus/4851b6b94d68d3e42dc153141a12a45a to your computer and use it in GitHub Desktop.
Portable code for https://outerproduct.net/trivial/2023-01-11_nan.html GCC compiles this right
#include <math.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
/* quiet NANs aren't always defined???
confused about this bit
*/
#define F32 NAN
struct f32;
static struct f32 f32_unbox(uint64_t x);
static uint64_t f32_box(struct f32 x);
static struct f32 f32_add(struct f32 x, struct f32 y);
struct f32 {
float tag;
float value;
};
int main(int argc, char **argv)
{
float tag = F32;
float value = 8;
printf("%f %f\n", tag, value);
return 0;
}
uint64_t add_raw(uint64_t x, uint64_t y)
{
return f32_box(f32_add(f32_unbox(x), f32_unbox(y)));
}
static uint64_t f32_box(struct f32 x)
{
uint64_t y = {0};
memcpy(&y, &x, sizeof y);
return y;
}
static struct f32 f32_unbox(uint64_t x)
{
struct f32 y = {0};
memcpy(&y, &x, sizeof y);
return y;
}
static struct f32 f32_add(struct f32 x, struct f32 y)
{
return (struct f32) {
.value = x.value + y.value,
.tag = x.tag + y.tag
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment