Skip to content

Commit 1cb5604

Browse files
authored
remove gcd impl from bevy_render (#16419)
# Objective - bevy_render (poorly) implements gcd (which should be in bevy_math but theres not enough justification to have it there either anyways cus its just one usage) ## Solution - hardcoded LUT replacement for the one usage ## Testing - verified the alternative implementation of 4/gcd(4,x) agreed with original for 0..200
1 parent 59863d3 commit 1cb5604

File tree

1 file changed

+7
-13
lines changed

1 file changed

+7
-13
lines changed

crates/bevy_render/src/mesh/allocator.rs

+7-13
Original file line numberDiff line numberDiff line change
@@ -955,12 +955,18 @@ impl ElementLayout {
955955
/// Creates an [`ElementLayout`] for mesh data of the given class (vertex or
956956
/// index) with the given byte size.
957957
fn new(class: ElementClass, size: u64) -> ElementLayout {
958+
const {
959+
assert!(4 == COPY_BUFFER_ALIGNMENT);
960+
}
961+
// this is equivalent to `4 / gcd(4,size)` but lets us not implement gcd.
962+
// ping @atlv if above assert ever fails (likely never)
963+
let elements_per_slot = [1, 4, 2, 4][size as usize & 3];
958964
ElementLayout {
959965
class,
960966
size,
961967
// Make sure that slot boundaries begin and end on
962968
// `COPY_BUFFER_ALIGNMENT`-byte (4-byte) boundaries.
963-
elements_per_slot: (COPY_BUFFER_ALIGNMENT / gcd(size, COPY_BUFFER_ALIGNMENT)) as u32,
969+
elements_per_slot,
964970
}
965971
}
966972

@@ -1000,18 +1006,6 @@ impl GeneralSlab {
10001006
}
10011007
}
10021008

1003-
/// Returns the greatest common divisor of the two numbers.
1004-
///
1005-
/// <https://en.wikipedia.org/wiki/Euclidean_algorithm#Implementations>
1006-
fn gcd(mut a: u64, mut b: u64) -> u64 {
1007-
while b != 0 {
1008-
let t = b;
1009-
b = a % b;
1010-
a = t;
1011-
}
1012-
a
1013-
}
1014-
10151009
/// Returns a string describing the given buffer usages.
10161010
fn buffer_usages_to_str(buffer_usages: BufferUsages) -> &'static str {
10171011
if buffer_usages.contains(BufferUsages::VERTEX) {

0 commit comments

Comments
 (0)