首页 文章

从Rust FFI检索浮点指针

提问于
浏览
1

Ubuntu 13.04,Rust 0.6 . 我正在尝试使用Rust FFI来操作openblas . 以下代码无法按预期方式工作 .

use core::io::println;
use core::libc::{c_int, c_float};
use core::vec::raw::to_ptr;

extern mod openblas {
    /*  Single precision dot product.
        sdot takes 5 args: number of elements,
                           pointer to a,
                           storage spacing for elements of a,
                           pointer to b,
                           storage spacing for elements of b
        returns a^T . b
    */
    fn cblas_sdot(N: c_int, x: *c_float, incx: c_int,
                  y: *c_float, incy: c_int) -> *c_float;
}

pub fn sdot(a: &[c_float], b: &[c_float]) -> *c_float {
    unsafe {
        openblas::cblas_sdot(3 as c_int, to_ptr(a), 1 as c_int,
                             to_ptr(b), 1 as c_int)
    }
}

fn main() {
    let a = [1. as c_float, 2. as c_float, 4. as c_float];
    let b = [1. as c_float, 2. as c_float, 5. as c_float];
    let x = sdot(a, b);
    println(fmt!("%?", x));

}

编辑:我希望它打印25,作为借来的指针 . 我得到了140125321300160,这可能是浮动的地址 .

1 回答

  • 4

    cblas_sdot 函数不应该返回指针 .

    来自BLAS Reference

    cblas_sdot计算两个向量的点积(单精度) . float cblas_sdot(const int N,const float * X,const int incX,const float * Y,const int incY);

相关问题