forked from pycca/pycca
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cc_examples.py
92 lines (74 loc) · 2.21 KB
/
cc_examples.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# -*- coding: utf-8 -*-
import numpy as np
from pycca.cc import CCode, Function, Assign, Return
code = CCode([
Function('int', 'add_one', [('int', 'x')], [
Assign(x='x + 1'),
Return('x')
])
])
print code.dump_asm()
print "3 + 1 = %d" % code.add_one(3)
c = CCode([Function('double', 'fn', [], [Return(12.3)]) ])
print c.dump_asm()
print "12.3 = ", c.fn()
# Example: a little closer to C
#code = CCode([
#Function('int add_one(int x)', [
#Assign(x='x + 1'),
#Return('x')
#])
#])
# Example: GIL handling for long operations
#code = CCode([
#Function('int', 'add_one', [('int', 'x')], [
#Declare(['int', 'ts']),
#Assign(ts=Call(ctypes.pythonapi.PyThreadState_Get)),
#Assign(ts=ctypes.pythonapi.PyEval_SaveThread, 'ts'),
## GIL is released, now do work:
#Assign(x='x + 1'),
## Re-acquire GIL before returning:
#Call(ctypes.pythonapi.PyEval_RestoreThread, 'ts'),
#Return('x')
#])
#])
# Example: coding in pure C
#find_greater = compile("""
#int find_greater(int* array, int size, int threshold) {
#int i;
#for( i=0; i<size; i++) {
#if( array[i] > threshold )
#return i;
#}
#return -1;
#}
#""")
# Example: inserting objects into global namespace
#compile("""
#double exp_x_plus_one(double x) {
#return exp(x + 1);
#}
#""", globals={'exp': ctypes.cdll.LoadLibrary('m').exp})
#data = np.arange(10000)
#print "First > 5000:", find_greater(data, len(data), 5000)
# Examples: coding using with-blocks
# Note 1: No need for Assign in this example, which means we can
# use Python to parse expressions.
# Note 2: Might be confusing because variables outlive their function scope
# Note 3: This should be considered lower priority than parsing regular C.
#
#code = CCode()
#with code:
# exp = import_func(libm.exp)
# with Function('void', 'test_func', ('float', 'x')) as func:
# x = func.args[0]
# y = double(x+1)
# Return( exp(x * (y + 1))) )
#code = CCode()
#with code:
#with If(cond):
#statements
#with ElseIf(cond):
#statements
#with Else():
#statements