c/flit.h
// Encodes v into buf and returns the serial octet size.
Encodes v into buf and returns the serial octet size.
Encodes v into buf and returns the serial octet size .
size_t flit64_enc(void* buf, uint64_t v) {
	if (v < 128) {
		*(uint8_t*)buf = (uint8_t)v << 1 | 1;
		return 1;
	}
	if (v >= (uint64_t)1 << 56) {
		uint8_t* p = (uint8_t*)buf;
		*p++ = 0;
		*(uint64_t*)p = v;
		return 9;
	}
	int lzc = __builtin_clzll(v);
	size_t e = ((63 - lzc) * 2454267027) >> 34;
	v <<= 1;
	v |= 1;
	v <<= e;
	*(uint64_t*)buf = v;
	return e + 1;
}
size_t flit64_enc ( void * buf , uint64_t v ) { if ( v < 128 ) { * ( uint8_t * ) buf = ( uint8_t ) v << 1 | 1 ; return 1 ; } if ( v >= ( uint64_t ) 1 << 56 ) { uint8_t * p = ( uint8_t * ) buf ; * p ++ = 0 ; * ( uint64_t * ) p = v ; return 9 ; } int lzc = __builtin_clzll ( v ) ; size_t e = ( ( 63 - lzc ) * 2454267027 ) >> 34 ; v <<= 1 ; v |= 1 ; v <<= e ; * ( uint64_t * ) buf = v ; return e + 1 ; }
Encodes v into buf and returns the serial octet size.
Encodes v into buf and returns the serial octet size .
// extra bytes = (bits - 1) / 7 = (63 - lzc) / 7