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
This commit is contained in:
atlv 2024-11-17 16:37:41 -05:00 committed by GitHub
parent 59863d3e8c
commit 1cb5604a17
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -955,12 +955,18 @@ impl ElementLayout {
/// Creates an [`ElementLayout`] for mesh data of the given class (vertex or /// Creates an [`ElementLayout`] for mesh data of the given class (vertex or
/// index) with the given byte size. /// index) with the given byte size.
fn new(class: ElementClass, size: u64) -> ElementLayout { fn new(class: ElementClass, size: u64) -> ElementLayout {
const {
assert!(4 == COPY_BUFFER_ALIGNMENT);
}
// this is equivalent to `4 / gcd(4,size)` but lets us not implement gcd.
// ping @atlv if above assert ever fails (likely never)
let elements_per_slot = [1, 4, 2, 4][size as usize & 3];
ElementLayout { ElementLayout {
class, class,
size, size,
// Make sure that slot boundaries begin and end on // Make sure that slot boundaries begin and end on
// `COPY_BUFFER_ALIGNMENT`-byte (4-byte) boundaries. // `COPY_BUFFER_ALIGNMENT`-byte (4-byte) boundaries.
elements_per_slot: (COPY_BUFFER_ALIGNMENT / gcd(size, COPY_BUFFER_ALIGNMENT)) as u32, elements_per_slot,
} }
} }
@ -1000,18 +1006,6 @@ impl GeneralSlab {
} }
} }
/// Returns the greatest common divisor of the two numbers.
///
/// <https://en.wikipedia.org/wiki/Euclidean_algorithm#Implementations>
fn gcd(mut a: u64, mut b: u64) -> u64 {
while b != 0 {
let t = b;
b = a % b;
a = t;
}
a
}
/// Returns a string describing the given buffer usages. /// Returns a string describing the given buffer usages.
fn buffer_usages_to_str(buffer_usages: BufferUsages) -> &'static str { fn buffer_usages_to_str(buffer_usages: BufferUsages) -> &'static str {
if buffer_usages.contains(BufferUsages::VERTEX) { if buffer_usages.contains(BufferUsages::VERTEX) {