Mathematica - How to compile BitShiftRight (or Left)?

I want to compile the Mathematica module because after speed.

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

My real function is longer, but has a very simple structure, uses lists and contains only the following function: Total, Table, BitAnd, If. All compilation and runtime options are useful (possibly) in my real function, and not on this one line.

I have installed

SetSystemOptions ["CompileOptions" → "CompileReportExternal" → True];

so that I can see what is happening and

CompilePrint [testC]

gives the following result:

  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 

As expected / feared from this thread, https://mathematica.stackexchange.com/a/1101/1403 BitShiftRight does not compile, and this call to MainEvaluate is a serious drag and drop of my function. I am very surprised that this is a very low level, the general function is not compiled, but BitAnd, BitNot, BitOr, BitXor! Does anyone know of a quick workaround? I can use the MathLink call for a C function, but my goal is to use this function in Manipulate [] and deploy it as a separate cdf file. And I understand that in this context I cannot use MathLink, right? By the way, is there any acceptable workaround there?

+6
source share
1 answer

If you divide by 32, the compiler will most likely rewrite this as a change. You should also try dividing by 32 in Mathematica directly, making sure your data remains packed (Developer`ToPackedArray []). The overhead of sending data back and forth will probably not be useful for calculations in C.

+3
source

Source: https://habr.com/ru/post/918711/


All Articles