Mathematica - 如何编译BitShiftRight(或左)?

Osc*_*r6E 6 wolfram-mathematica mathematical-optimization mathematica-8

我想编译一个Mathematica模块,因为我追求速度.

    testC = Compile[{{inputInt, _Integer}},
      Module[{outputInt, bitShift = 5},
      outputInt = BitShiftRight[inputInt, bitShift]
      ]
      , CompilationTarget -> "C", RuntimeOptions -> "Speed"
      , CompilationOptions -> {"ExpressionOptimization" -> True, 
       "InlineCompiledFunctions" -> True, 
       "InlineExternalDefinitions" -> True}
    ];
Run Code Online (Sandbox Code Playgroud)

我的实际功能更长,但结构非常简单,使用列表,只包含以下功能:Total,Table,BitAnd,If.所有编译和运行时选项在我的实际函数中都很有用(可能),而不是这一行提取.

我已经设定

SetSystemOptions ["CompileOptions" - >"CompileReportExternal" - > True];

确保我能看到发生了什么,并且

CompilePrint [TESTC]

给出以下结果

    1 argument
    3 Integer registers
Underflow checking off
Overflow checking off
Integer overflow checking off
RuntimeAttributes -> {}

    I0 = A1
    I1 = 5
    Result = I2

    1   I2 = MainEvaluate[ Hold[BitShiftRight][ I0, I1]]
    2   Return
Run Code Online (Sandbox Code Playgroud)

正如预期/担心从这个线程https://mathematica.stackexchange.com/a/1101/1403 BitShiftRight是不可编译的,这次调用MainEvaluate严重拖累我的功能.我非常惊讶,BitAnd,BitNot,BitOr,BitXor这种非常低级别的常用功能是不可编译的!有人知道(快速)解决方法吗?我可以使用MathLink调用C语言函数,但我的目标是在Manipulate []中使用此函数并将其部署为独立的cdf文件.我理解在这种情况下我不能使用MathLink,对吗?顺便问一下,那里有一些易处理的解决方法吗?

Jos*_*ell 3

如果除以 32,编译器可能会将其重写为移位。您还应该尝试直接在 Mathematica 中除以 32,确保您的数据是打包的(Developer`ToPackedArray[])。来回发送数据的开销可能不值得用 C 进行计算。