Skip to content

Commit c08bda9

Browse files
author
hartsantler
committed
working prototype of SIMD float32x4 using dart backend.
1 parent 95fae01 commit c08bda9

4 files changed

Lines changed: 50 additions & 1 deletion

File tree

pythonjs/python_to_pythonjs.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,8 @@ def visit_Import(self, node):
343343
pass ## pythonjs/fakelibs/sys.py
344344
elif alias.name == 'subprocess':
345345
pass ## pythonjs/fakelibs/subprocess.py
346+
elif alias.name == 'numpy':
347+
pass
346348

347349
elif alias.name == 'json' or alias.name == 'os':
348350
pass ## part of builtins.py
@@ -1911,7 +1913,15 @@ def visit_Call(self, node):
19111913
return '__js_typed_array(%s)' %','.join(args)
19121914

19131915
#########################################
1914-
if isinstance(node.func, ast.Attribute) and isinstance(node.func.value, Name) and node.func.value.id == 'pythonjs' and node.func.attr == 'configure':
1916+
if isinstance(node.func, ast.Attribute) and isinstance(node.func.value, Name) and node.func.value.id == 'numpy' and node.func.attr == 'array':
1917+
args = [self.visit(arg) for arg in node.args]
1918+
if node.keywords:
1919+
kwargs = [ '%s=%s' %(x.arg, self.visit(x.value)) for x in node.keywords]
1920+
return 'numpy.array(%s, %s)' %( ','.join(args), ','.join(kwargs) )
1921+
else:
1922+
return 'numpy.array(%s)' %','.join(args)
1923+
1924+
elif isinstance(node.func, ast.Attribute) and isinstance(node.func.value, Name) and node.func.value.id == 'pythonjs' and node.func.attr == 'configure':
19151925
for kw in node.keywords:
19161926
if kw.arg == 'javascript':
19171927
if kw.value.id == 'True':

pythonjs/pythonjs.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,9 @@ def visit_Call(self, node):
297297
else:
298298
raise SyntaxError( args )
299299

300+
elif name == 'numpy.array':
301+
return self._visit_call_helper_numpy_array(node)
302+
300303
elif name == 'JSObject':
301304
return self._visit_call_helper_JSObject( node )
302305

@@ -345,6 +348,8 @@ def inline_helper_remap_names(self, remap):
345348
def inline_helper_return_id(self, return_id):
346349
return "var __returns__%s = null;"%return_id
347350

351+
def _visit_call_helper_numpy_array(self, node):
352+
return self.visit(node.args[0]) ## TODO typed arrays
348353

349354
def _visit_call_helper_list(self, node):
350355
name = self.visit(node.func)

pythonjs/pythonjs_to_dart.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -485,6 +485,19 @@ def _visit_call_helper_list(self, node):
485485
return '%s(%s)' % (name, args)
486486

487487

488+
def _visit_call_helper_numpy_array(self, node):
489+
simd = {
490+
'float32': 'Float32x4'
491+
}
492+
args = ','.join( [self.visit(a) for a in node.args[0].elts] )
493+
if node.keywords:
494+
for key in node.keywords:
495+
if key.arg == 'dtype':
496+
if isinstance(key.value, ast.Attribute) and key.value.attr in simd:
497+
return 'new %s(%s)' %(simd[key.value.attr] ,args)
498+
return '[%s]' %args ## TODO
499+
500+
488501
def _visit_call_helper_instanceof(self, node):
489502
args = map(self.visit, node.args)
490503
if len(args) == 2:

regtests/typed/float32x4.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
"""simd float32x4"""
2+
import numpy
3+
4+
def main():
5+
float32x4 a = numpy.array( [1.1, 1.2, 1.3, 0.4], dtype=numpy.float32 )
6+
float32x4 b = numpy.array( [1.9, 1.8, 1.7, 0.6], dtype=numpy.float32 )
7+
8+
c = a + b
9+
print(c)
10+
11+
if PYTHON == 'PYTHONJS':
12+
TestError( c.x==3.0 )
13+
TestError( c.y==3.0 )
14+
TestError( c.z==3.0 )
15+
TestError( c.w==1.0 )
16+
17+
else:
18+
TestError( c[0]==3.0 )
19+
TestError( c[1]==3.0 )
20+
TestError( c[2]==3.0 )
21+
TestError( c[3]==1.0 )

0 commit comments

Comments
 (0)