项目作者: psinghal20

项目描述 :
Auto-Vec is a proc macro to vectorize your functions. Let your Scalars Vectorize!
高级语言: Rust
项目地址: git://github.com/psinghal20/Auto-Vec.git
创建时间: 2020-05-11T13:08:35Z
项目社区:https://github.com/psinghal20/Auto-Vec

开源协议:MIT License

下载


Auto-Vec

Auto-vec is simple proc macro to vectorize your scalar functions.

Example

  1. #[auto_vec]
  2. pub fn foo(arg1: i64, arg2: i32) -> i64 {
  3. return (arg1 + arg2 as i64);
  4. }

Allowing you to do something like this:

  1. fn main() {
  2. let results = foo_vec(vec![1,2,3,4], vec![1,2,3,4]);
  3. // returned vector is of form [foo(arg1[0], arg2[0]), ...]
  4. assert_eq!(results, vec![2, 4, 6, 8]);
  5. }

It keeps the original function and allows computation of the specified function over a vector of inputs. The generated function inherits the visibility and generics from the original function definition. The generated function takes the vectors as mutable so as to take ownership of the inner elements.

The vectorized function expects to get input vectors of the equal length, and panics if that is not the case.

  1. // Panics due to unequal length of input vectors
  2. foo_vec(vec![1,2,3], vec![1,2]);

Auto vec also requires the function to have one or more input types(other than self) and a return type, and fails to compile otherwise.

  1. #[auto_vec]
  2. pub fn bar() -> String; // Compile time error
  3. #[auto_vec]
  4. pub fn ha(a:i64); // Compile time error

It allows vectorization of methods too, accepting vectors for every argument except the first self argument.

  1. struct T;
  2. impl T {
  3. #[auto_vec]
  4. fn foo(&self, arg1: usize, arg2: usize) -> usize {
  5. return arg1 + arg2;
  6. }
  7. }
  8. fn main() {
  9. let a = T{};
  10. assert_eq!(a.foo_vec(vec![1, 2, 3], vec![1, 2, 3]), vec![2, 4, 6])
  11. }

Warning: Methods accepting self receviers with a specific type, such as self: Box<Self> are not parsed correctly currently and lead to wrong code generation. Auto Vec currently works only for untyped self receviers.

License

Auto-Vec is under the MIT license. See the LICENSE file for details.