| 44 |
|
static void |
| 45 |
|
new_kids(TNODE *pn) |
| 46 |
|
{ |
| 47 |
< |
pn->kid = (TNODE *)calloc(1<<ttrank, sizeof(TNODE)); |
| 47 |
> |
pn->kid = (TNODE *)calloc((size_t)1<<ttrank, sizeof(TNODE)); |
| 48 |
|
if (pn->kid == NULL) |
| 49 |
|
error(SYSTEM, "out of memory in new_kids"); |
| 50 |
|
} |
| 110 |
|
|
| 111 |
|
/* Set our trimming threshold */ |
| 112 |
|
static void |
| 113 |
< |
set_threshold() |
| 113 |
> |
set_threshold(void) |
| 114 |
|
{ |
| 115 |
|
int hsum = 0; |
| 116 |
|
int i; |
| 155 |
|
bkmin[j] = bmin[j] + (i>>(ttrank-1-j) & 1); |
| 156 |
|
val = (ttrank == 3) ? dval3(bkmin[0],bkmin[1],bkmin[2]) |
| 157 |
|
: dval4(bkmin[0],bkmin[1],bkmin[2],bkmin[3]); |
| 158 |
< |
printf(" %.4e", val); |
| 158 |
> |
printf((0.001<=val)&(val<10.) ? " %.7f" : " %.3e", val); |
| 159 |
|
} |
| 160 |
|
fputs(" }\n", stdout); |
| 161 |
|
return; |
| 201 |
|
|
| 202 |
|
if ((rowp == NULL) | (n <= 0)) |
| 203 |
|
return(0); |
| 204 |
< |
nread = fread(rowp, sizeof(float), n, stdin); |
| 204 |
> |
nread = getbinary(rowp, sizeof(float), n, stdin); |
| 205 |
|
if (nread != n) |
| 206 |
|
error(USER, "unexpected EOF on float input"); |
| 207 |
|
return(nread); |
| 227 |
|
if (rowbuf == NULL) |
| 228 |
|
error(SYSTEM, "out of memory in read_double"); |
| 229 |
|
} |
| 230 |
< |
nread = fread(rowbuf, sizeof(double), n, stdin); |
| 230 |
> |
nread = getbinary(rowbuf, sizeof(double), n, stdin); |
| 231 |
|
if (nread != n) |
| 232 |
|
error(USER, "unexpected EOF on double input"); |
| 233 |
|
for (i = 0; i < nread; i++) |
| 235 |
|
return(nread); |
| 236 |
|
} |
| 237 |
|
|
| 238 |
+ |
/* Truncate any negative values to zero */ |
| 239 |
+ |
static void |
| 240 |
+ |
noneg(float *varr, int n) |
| 241 |
+ |
{ |
| 242 |
+ |
int nnan = 0; |
| 243 |
+ |
|
| 244 |
+ |
while (n-- > 0) { |
| 245 |
+ |
#ifdef isnan |
| 246 |
+ |
if (isnan(*varr)) { |
| 247 |
+ |
*varr = 0; |
| 248 |
+ |
++nnan; |
| 249 |
+ |
} else |
| 250 |
+ |
#endif |
| 251 |
+ |
if (*varr < 0) *varr = 0; |
| 252 |
+ |
++varr; |
| 253 |
+ |
} |
| 254 |
+ |
if (nnan) |
| 255 |
+ |
fprintf(stderr, "Warning: BSDF data contains %d NaN values\n", |
| 256 |
+ |
nnan); |
| 257 |
+ |
} |
| 258 |
+ |
|
| 259 |
|
/* Load data array, filling zeroes for rank 3 demi-tensor */ |
| 260 |
|
static void |
| 261 |
< |
load_data() |
| 261 |
> |
load_data(void) |
| 262 |
|
{ |
| 263 |
|
int (*readf)(float *, int) = NULL; |
| 264 |
|
|
| 276 |
|
error(COMMAND, "unsupported input format"); |
| 277 |
|
break; |
| 278 |
|
} |
| 279 |
< |
datarr = (float *)calloc(1<<(log2g*ttrank), sizeof(float)); |
| 279 |
> |
datarr = (float *)calloc((size_t)1<<(log2g*ttrank), sizeof(float)); |
| 280 |
|
if (datarr == NULL) |
| 281 |
|
error(SYSTEM, "out of memory in load_data"); |
| 282 |
|
if (ttrank == 3) { |
| 310 |
|
} |
| 311 |
|
} else if (getc(stdin) != EOF) |
| 312 |
|
error(WARNING, "binary data past end of expected input"); |
| 313 |
+ |
|
| 314 |
+ |
noneg(datarr, 1<<(log2g*ttrank)); /* take precautions */ |
| 315 |
|
} |
| 316 |
|
|
| 317 |
|
/* Enforce reciprocity by averaging data values */ |
| 318 |
|
static void |
| 319 |
< |
do_reciprocity() |
| 319 |
> |
do_reciprocity(void) |
| 320 |
|
{ |
| 321 |
< |
float *v1p, *v2p; |
| 321 |
> |
const int siz = 1<<log2g; |
| 322 |
> |
float *v1p, *v2p; |
| 323 |
|
|
| 324 |
|
if (ttrank == 3) { |
| 325 |
|
int ix, ox, oy; |
| 326 |
< |
for (ix = 0; ix < 1<<(log2g-1); ix++) |
| 327 |
< |
for (ox = 0; ox < 1<<log2g; ox++) |
| 328 |
< |
for (oy = 0; oy < 1<<(log2g-1); oy++) { |
| 326 |
> |
for (ix = 0; ix < siz>>1; ix++) |
| 327 |
> |
for (ox = 0; ox < siz; ox++) |
| 328 |
> |
for (oy = 0; oy < siz>>1; oy++) { |
| 329 |
|
v1p = &dval3(ix,ox,oy); |
| 330 |
< |
v2p = &dval3(ix,ox,(1<<log2g)-1-oy); |
| 330 |
> |
v2p = &dval3(ix,ox,siz-1-oy); |
| 331 |
|
*v1p = *v2p = .5f*( *v1p + *v2p ); |
| 332 |
|
} |
| 333 |
|
} else /* ttrank == 4 */ { |
| 334 |
|
int ix, iy, ox, oy; |
| 335 |
< |
for (ix = 1; ix < 1<<log2g; ix++) |
| 336 |
< |
for (iy = 1; iy < 1<<log2g; iy++) |
| 335 |
> |
for (ix = 1; ix < siz; ix++) |
| 336 |
> |
for (iy = 1; iy < siz; iy++) |
| 337 |
|
for (ox = 0; ox < ix; ox++) |
| 338 |
|
for (oy = 0; oy < iy; oy++) { |
| 339 |
< |
v1p = &dval4(ix,iy,ox,oy); |
| 340 |
< |
v2p = &dval4(ox,oy,ix,iy); |
| 339 |
> |
v1p = &dval4(siz-1-ix,siz-1-iy,ox,oy); |
| 340 |
> |
v2p = &dval4(siz-1-ox,siz-1-oy,ix,iy); |
| 341 |
|
*v1p = *v2p = .5f*( *v1p + *v2p ); |
| 342 |
|
} |
| 343 |
|
} |
| 387 |
|
if (i < argc-1) |
| 388 |
|
goto userr; |
| 389 |
|
/* load input data */ |
| 390 |
< |
if (i == argc-1 && freopen(argv[i], "rb", stdin) == NULL) { |
| 390 |
> |
if (i == argc-1 && freopen(argv[i], "r", stdin) == NULL) { |
| 391 |
|
sprintf(errmsg, "cannot open input file '%s'", argv[i]); |
| 392 |
|
error(SYSTEM, errmsg); |
| 393 |
|
} |
| 394 |
|
if (infmt != 'a') |
| 395 |
|
SET_FILE_BINARY(stdin); |
| 396 |
+ |
#ifdef getc_unlocked /* avoid lock/unlock overhead */ |
| 397 |
+ |
flockfile(stdin); |
| 398 |
+ |
#endif |
| 399 |
|
load_data(); |
| 400 |
|
if (recipavg) |
| 401 |
|
do_reciprocity(); |