multiversion 0.1.1

Easy function multiversioning
Documentation

Multiversion

Build Status Build Status Rustc Version 1.31+ License Crates.io Rust Documentation

Function multiversioning macro/attribute for Rust.

Usage

Add the following to your dependencies in Cargo.toml:

[dependencies]
multiversion = "0.1"

Example

Automatic function multiversioning with the target_clones attribute, similar to GCC's target_clones attribute:

use multiversion::target_clones;

#[target_clones("[x86|x86_64]+avx", "x86+sse")]
fn square(x: &mut [f32]) {
    for v in x {
        *v *= *v;
    }
}

Manual function multiversioning with the multiversion! macro:

use multiversion::multiversion;

multiversion!{
    fn square(x: &mut [f32])
    "[x86|x86_64]+avx" => square_avx,
    "x86+sse" => square_sse,
    default => square_generic,
}

#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
#[target_feature(enable = "avx")]
unsafe fn square_avx(x: &mut [f32]) {
    for v in x {
        *v *= *v;
    }
}

#[cfg(target_arch = "x86")]
#[target_feature(enable = "sse")]
unsafe fn square_sse(x: &mut [f32]) {
    for v in x {
        *v *= *v;
    }
}

fn square_generic(x: &mut [f32]) {
    for v in x {
        *v *= *v;
    }
}

License

Multiversion is distributed under the terms of both the MIT license and the Apache License (Version 2.0).

See LICENSE-APACHE and LICENSE-MIT for details.