Skip to content

Commit 71181a5

Browse files
committed
box: Use isinf() and signbit() when isinff() is not available
Instead of using bytewise comparison, use isinf() and hope it works correctly with single precision floats on the target platform; additionally, since the negative return value for negative infinities is a GNU extension, use signbit() to check if the number is a positive or negative infinity.
1 parent e5f8e9c commit 71181a5

File tree

1 file changed

+32
-24
lines changed

1 file changed

+32
-24
lines changed

src/graphene-box.c

Lines changed: 32 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -436,18 +436,22 @@ graphene_box_is_empty (const graphene_box_t *box)
436436
return (isinff (vmin[0]) == 1 && isinff (vmin[1]) == 1 && isinff (vmin[2]) == 1) &&
437437
(isinff (vmax[0]) == -1 && isinff (vmax[1]) == -1 && isinff (vmax[2]) == -1);
438438
#else
439-
graphene_simd4f_t neg_inf = graphene_simd4f_init (-INFINITY, -INFINITY, -INFINITY, 0.f);
440-
graphene_simd4f_t pos_inf = graphene_simd4f_init (INFINITY, INFINITY, INFINITY, 0.f);
441-
442-
/* This is only every going to be valid for boxes that we have
443-
* initialized ourselves, because we use the same values; the
444-
* bitwise comparison will not hold for infinities generated by
445-
* other operations
446-
*/
447-
int min_cmp = memcmp (&box->min.value, &pos_inf, sizeof (graphene_simd4f_t));
448-
int max_cmp = memcmp (&box->max.value, &neg_inf, sizeof (graphene_simd4f_t));
449-
450-
return min_cmp == 0 && max_cmp == 0;
439+
float vmin[3], vmax[3];
440+
441+
graphene_simd4f_dup_3f (box->min.value, vmin);
442+
graphene_simd4f_dup_3f (box->max.value, vmax);
443+
444+
bool vmin_pos_inf =
445+
(isinf (vmin[0]) && signbit (vmin[0]) == 0) &&
446+
(isinf (vmin[1]) && signbit (vmin[1]) == 0) &&
447+
(isinf (vmin[2]) && signbit (vmin[2]) == 0);
448+
449+
bool vmax_neg_inf =
450+
(isinf (vmax[0]) && signbit (vmax[0]) < 0) &&
451+
(isinf (vmax[1]) && signbit (vmax[1]) < 0) &&
452+
(isinf (vmax[2]) && signbit (vmax[2]) < 0);
453+
454+
return vmin_pos_inf && vmax_neg_inf;
451455
#endif
452456
}
453457

@@ -463,18 +467,22 @@ graphene_box_is_infinity (const graphene_box_t *box)
463467
return (isinff (vmin[0]) == -1 && isinff (vmin[1]) == -1 && isinff (vmin[2]) == -1) &&
464468
(isinff (vmax[0]) == 1 && isinff (vmax[1]) == 1 && isinff (vmax[2]) == 1);
465469
#else
466-
graphene_simd4f_t neg_inf = graphene_simd4f_init (-INFINITY, -INFINITY, -INFINITY, 0.f);
467-
graphene_simd4f_t pos_inf = graphene_simd4f_init (INFINITY, INFINITY, INFINITY, 0.f);
468-
469-
/* This is only every going to be valid for boxes that we have
470-
* initialized ourselves, because we use the same values; the
471-
* bitwise comparison will not hold for infinities generated by
472-
* other operations
473-
*/
474-
int min_cmp = memcmp (&box->min.value, &neg_inf, sizeof (graphene_simd4f_t));
475-
int max_cmp = memcmp (&box->max.value, &pos_inf, sizeof (graphene_simd4f_t));
476-
477-
return min_cmp == 0 && max_cmp == 0;
470+
float vmin[3], vmax[3];
471+
472+
graphene_simd4f_dup_3f (box->min.value, vmin);
473+
graphene_simd4f_dup_3f (box->max.value, vmax);
474+
475+
bool vmin_neg_inf =
476+
(isinf (vmin[0]) && signbit (vmin[0]) < 0) &&
477+
(isinf (vmin[1]) && signbit (vmin[1]) < 0) &&
478+
(isinf (vmin[2]) && signbit (vmin[2]) < 0);
479+
480+
bool vmax_pos_inf =
481+
(isinf (vmax[0]) && signbit (vmax[0]) == 0) &&
482+
(isinf (vmax[1]) && signbit (vmax[1]) == 0) &&
483+
(isinf (vmax[2]) && signbit (vmax[2]) == 0);
484+
485+
return vmin_neg_inf && vmax_pos_inf;
478486
#endif
479487
}
480488

0 commit comments

Comments
 (0)