Ptex

PtexHalfTableGen.cpp
Go to the documentation of this file.
1#include "PtexHalf.h"
2#include <stdio.h>
3
5 static bool PtexHalfInit(uint32_t* h2fTable, uint16_t* f2hTable)
6{
7 union { int i; float f; } u;
8
9 for (int h = 0; h < 65536; h++) {
10 int s = (h & 0x8000)<<16;
11 int m = h & 0x03ff;
12 int e = h&0x7c00;
13
14 if (unsigned(e-1) < ((31<<10)-1)) {
15 // normal case
16 u.i = s|(((e+0x1c000)|m)<<13);
17 }
18 else if (e == 0) {
19 // denormalized
20 if (!(h&0x8000)) u.f = float(5.9604644775390625e-08*m);
21 else u.f = float(-5.9604644775390625e-08*m);
22 }
23 else {
24 // inf/nan, preserve low bits of m for nan code
25 u.i = s|0x7f800000|(m<<13);
26 }
27 h2fTable[h] = u.i;
28 }
29
30 for (int i = 0; i < 512; i++) {
31 int f = i << 23;
32 int e = (f & 0x7f800000) - 0x38000000;
33 // normalized iff (0 < e < 31)
34 if (unsigned(e-1) < ((31<<23)-1)) {
35 int s = ((f>>16) & 0x8000);
36 int m = f & 0x7fe000;
37 // add bit 12 to round
38 f2hTable[i] = (uint16_t)((s|((e|m)>>13))+((f>>12)&1));
39 }
40 }
41
42 return 1;
43}
44
45
46 int main()
47{
48 FILE* fp = fopen("PtexHalfTables.h", "w");
49 if (!fp) {
50 perror("Can't write PtexHalfTable.h");
51 return 1;
52 }
53 uint32_t h2fTable[65536];
54 uint16_t f2hTable[512];
55 PtexHalfInit(h2fTable, f2hTable);
56 fprintf(fp, "PTEXAPI uint32_t PtexHalf::h2fTable[65536] = {");
57 for (int i = 0; i < 65536; i++) {
58 if (i % 8 == 0) fprintf(fp, "\n");
59 fprintf(fp, " 0x%08x", h2fTable[i]);
60 if (i != 65535) fprintf(fp, ",");
61 }
62 fprintf(fp, "\n};\n");
63 fprintf(fp, "PTEXAPI uint16_t PtexHalf::f2hTable[512] = {");
64 for (int i = 0; i < 512; i++) {
65 if (i % 8 == 0) fprintf(fp, "\n");
66 fprintf(fp, " 0x%04x", f2hTable[i]);
67 if (i != 511) fprintf(fp, ",");
68 }
69 fprintf(fp, "\n};\n");
70 fclose(fp);
71 return 0;
72}
PtexHalfInit
static bool PtexHalfInit(uint32_t *h2fTable, uint16_t *f2hTable)
Table initializations.
Definition PtexHalfTableGen.cpp:5
main
int main()
Definition PtexHalfTableGen.cpp:46
PtexHalf.h
Half-precision floating-point type.