Skip to content

Instantly share code, notes, and snippets.

@liuliu
Last active August 29, 2015 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save liuliu/8878583 to your computer and use it in GitHub Desktop.
Save liuliu/8878583 to your computer and use it in GitHub Desktop.
generate half precision table
#include <stdio.h>
#include <stdint.h>
uint32_t basetable[512];
uint32_t shifttable[512];
void generatetables(){
unsigned int i;
int e;
for(i=0; i<256; ++i){
e=i-127;
if(e<-24){ // Very small numbers map to zero
basetable[i|0x000]=0x0000;
basetable[i|0x100]=0x8000;
shifttable[i|0x000]=24;
shifttable[i|0x100]=24;
}
else if(e<-14){ // Small numbers map to denorms
basetable[i|0x000]=(0x0400>>(-e-14));
basetable[i|0x100]=(0x0400>>(-e-14)) | 0x8000;
shifttable[i|0x000]=-e-1;
shifttable[i|0x100]=-e-1;
}
else if(e<=15){ // Normal numbers just lose precision
basetable[i|0x000]=((e+15)<<10);
basetable[i|0x100]=((e+15)<<10) | 0x8000;
shifttable[i|0x000]=13;
shifttable[i|0x100]=13;
}
else if(e<128){ // Large numbers map to Infinity
basetable[i|0x000]=0x7C00;
basetable[i|0x100]=0xFC00;
shifttable[i|0x000]=24;
shifttable[i|0x100]=24;
}
else{ // Infinity and NaN's stay Infinity and NaN's
basetable[i|0x000]=0x7C00;
basetable[i|0x100]=0xFC00;
shifttable[i|0x000]=13;
shifttable[i|0x100]=13;
}
}
}
unsigned int convertmantissa(unsigned int i){
unsigned int m=i<<13;
unsigned int e=0;
// Zero exponent
while(!(m&0x00800000)){
// While not normalized
e-=0x00800000;
// Decrement exponent (1<<23)
m<<=1;
// Shift mantissa
}
m&=~0x00800000;
// Clear leading 1 bit
e+=
0x38800000;
// Adjust bias ((127-14)<<23)
return m | e;
// Return combined number
}
int main(void)
{
uint32_t mantissatable[2048];
mantissatable[0] = 0;
int i, j;
for (i = 1; i < 1024; i++)
mantissatable[i] = convertmantissa(i);
for (i = 1024; i < 2048; i++)
mantissatable[i] = 0x38000000 + ((i-1024)<<13);
/*
for (i = 0; i < 256; i++)
{
for (j = 0; j < 7; j++)
printf("0x%x, ", mantissatable[i * 8 + j]);
printf("0x%x,\n", mantissatable[i * 8 + 7]);
}
*/
generatetables();
/*
for (i = 0; i < 32; i++)
{
for (j = 0; j < 15; j++)
printf("0x%x, ", basetable[i * 16 + j]);
printf("0x%x,\n", basetable[i * 16 + 15]);
}
*/
uint32_t exponenttable[64];
exponenttable[0] = 0;
exponenttable[32]= 0x80000000;
for (i = 1; i < 31; i++)
exponenttable[i] = i<<23;
for (i = 33; i < 63; i++)
exponenttable[i] = 0x80000000 + ((i-32)<<23);
exponenttable[31]= 0x47800000;
exponenttable[63]= 0xC7800000;
for (i = 0; i < 8; i++)
{
for (j = 0; j < 7; j++)
printf("0x%x, ", exponenttable[i * 8 + j]);
printf("0x%x,\n", exponenttable[i * 8 + 7]);
}
uint32_t offsettable[64];
for (i = 0; i < 64; i++)
offsettable[i] = 1024;
offsettable[0] = 0;
offsettable[32]= 0;
/*
for (i = 0; i < 8; i++)
{
for (j = 0; j < 7; j++)
printf("0x%x, ", offsettable[i * 8 + j]);
printf("0x%x,\n", offsettable[i * 8 + 7]);
}
*/
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment