shithub: femtolisp

ref: e68062e70197ebe7aded32ef48bf2aefac2c26b6
dir: /llt.c/

View raw version
#include "llt.h"
#include "random.h"
#include "ieee754.h"

double D_PNAN, D_NNAN, D_PINF, D_NINF;
float F_PNAN, F_NNAN, F_PINF, F_NINF;

void
llt_init(void)
{
	D_PNAN = D_NNAN = strtod("+NaN", nil);
	D_PINF = D_NINF = strtod("+Inf", nil);

	union ieee754_double *d;
	d = (union ieee754_double *)&D_NNAN;
	d->ieee.negative = 1;
	d = (union ieee754_double *)&D_NINF;
	d->ieee.negative = 1;

#ifdef __plan9__
	*(uint32_t*)&F_PNAN = 0x7fc00000;
	*(uint32_t*)&F_NNAN = 0xffc00000;
	*(uint32_t*)&F_PINF = 0x7f800000;
	*(uint32_t*)&F_NINF = 0xff800000;
#else
	union ieee754_float *f;
	F_PNAN = F_NNAN = strtof("+NaN", nil);
	F_PINF = F_NINF = strtof("+Inf", nil);
	f = (union ieee754_float *)&F_NNAN;
	f->ieee.negative = 1;
	f = (union ieee754_float *)&F_NINF;
	f->ieee.negative = 1;
#endif

	randomize();
	ios_init_stdstreams();
}

char *
uint2str(char *dest, size_t len, uint64_t num, uint32_t base)
{
	int i = len-1;
	uint64_t b = (uint64_t)base;
	char ch;
	dest[i--] = '\0';
	while(i >= 0){
		ch = (char)(num % b);
		if(ch < 10)
			ch += '0';
		else
			ch = ch-10+'a';
		dest[i--] = ch;
		num /= b;
		if(num == 0)
			break;
	}
	return &dest[i+1];
}

int
isdigit_base(char c, int base)
{
	if(base < 11)
		return c >= '0' && c < '0'+base;
	return (c >= '0' && c <= '9') || (c >= 'a' && c < 'a'+base-10) || (c >= 'A' && c < 'A'+base-10);
}