Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

lndpj/bround

Open more actions menu

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

19 Commits
19 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

bround

bround, broundf, broundh, ubround, ubroundf, ubroundh - mantissa overflow round to nearest un/signed byte for C23 and above

About

static inline uint8_t ubroundf(float x)
{
        x += (float)((1u   << (FLT_MANT_DIG - 1)) + (1u  << (FLT_MANT_DIG - 2)));
        return *(uint8_t*)&x;
}
static inline uint8_t ubroundh(_Float16 x)
{
        x += (_Float16)((1u   << (HALF_MANT_DIG - 1)) + (1u  << (HALF_MANT_DIG - 2)));
        return *(uint8_t*)&x;
}

Example

VGA6 to RGB8 using ubroundf compared to SHR6

#include <stdlib.h>
#include <stdio.h>
#include "bround.h"

static inline uint8_t vga2rgb(uint8_t v6) { return ubroundf(v6*255.0f/63.0f); }
static inline uint8_t shr6lvl(uint8_t v6) { return (v6 << 2) | (v6 >> 4);     }

int main(int argc, char** argv)
{
	printf("vga: man/shr\n");
	for(uint8_t i = 0; i < 64; i++)
		printf("%03hhu: %03hhu/%03hhu\n", i, vga2rgb(i), shr6lvl(i));
	exit(EXIT_SUCCESS);
}

About

bround, broundf, ubround, ubroundf - mantissa overflow round to nearest byte

Topics

Resources

Stars

Watchers

Forks

Releases

Packages

Contributors

Languages

Morty Proxy This is a proxified and sanitized view of the page, visit original site.