# HG changeset patch # User Paul Boddie # Date 1490400428 -3600 # Node ID fd57abd7f631a510dada8424f60044cb802a15ac # Parent c731ec23e6b10dc0948474eda9122e6ca4c37af0# Parent b9fb0046c252269be1ad95adf7a38ba06cb42193 Merged changes from the integer-fast-paths branch. diff -r c731ec23e6b1 -r fd57abd7f631 lib/native/__init__.py --- a/lib/native/__init__.py Fri Mar 24 22:46:46 2017 +0100 +++ b/lib/native/__init__.py Sat Mar 25 01:07:08 2017 +0100 @@ -24,8 +24,9 @@ from native.identity import is_, is_not from native.int import int_add, int_div, int_mod, int_mul, int_neg, int_pow, \ - int_sub, int_and, int_not, int_or, int_xor, int_lt, \ - int_gt, int_eq, int_ne, int_str + int_sub, int_and, int_not, int_or, int_xor, \ + int_eq, int_ge, int_gt, int_le, int_lt, int_ne, \ + int_str, is_int from native.introspection import object_getattr, isinstance, issubclass diff -r c731ec23e6b1 -r fd57abd7f631 lib/native/int.py --- a/lib/native/int.py Fri Mar 24 22:46:46 2017 +0100 +++ b/lib/native/int.py Sat Mar 25 01:07:08 2017 +0100 @@ -24,6 +24,8 @@ this program. If not, see . """ +def is_int(obj): pass + def int_add(self, other): pass def int_div(self, other): pass def int_mod(self, other): pass @@ -37,9 +39,11 @@ def int_or(self, other): pass def int_xor(self, other): pass -def int_lt(self, other): pass +def int_eq(self, other): pass +def int_ge(self, other): pass def int_gt(self, other): pass -def int_eq(self, other): pass +def int_le(self, other): pass +def int_lt(self, other): pass def int_ne(self, other): pass def int_str(self): pass diff -r c731ec23e6b1 -r fd57abd7f631 lib/operator/augmented.py --- a/lib/operator/augmented.py Fri Mar 24 22:46:46 2017 +0100 +++ b/lib/operator/augmented.py Sat Mar 25 01:07:08 2017 +0100 @@ -3,7 +3,7 @@ """ Operator support. -Copyright (C) 2010, 2013, 2015 Paul Boddie +Copyright (C) 2010, 2013, 2015, 2017 Paul Boddie This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software @@ -20,6 +20,9 @@ """ from operator.core import augassign +from native import int_add, int_div, int_mod, int_mul, int_pow, int_sub, \ + int_and, int_or, int_xor, \ + is_int # These functions defer method lookup by wrapping the attribute access in # lambda functions. Thus, the appropriate methods are defined locally, but no @@ -31,12 +34,18 @@ # Augmented assignment functions. def iadd(a, b): + if is_int(a) and is_int(b): + return int_add(a, b) return augassign(a, b, lambda a: a.__iadd__, lambda a: a.__add__, lambda b: b.__radd__) def iand_(a, b): + if is_int(a) and is_int(b): + return int_and(a, b) return augassign(a, b, lambda a: a.__iand__, lambda a: a.__and__, lambda b: b.__rand__) def idiv(a, b): + if is_int(a) and is_int(b): + return int_div(a, b) return augassign(a, b, lambda a: a.__idiv__, lambda a: a.__div__, lambda b: b.__rdiv__) def ifloordiv(a, b): @@ -46,24 +55,36 @@ return augassign(a, b, lambda a: a.__ilshift__, lambda a: a.__lshift__, lambda b: b.__rlshift__) def imod(a, b): + if is_int(a) and is_int(b): + return int_mod(a, b) return augassign(a, b, lambda a: a.__imod__, lambda a: a.__mod__, lambda b: b.__rmod__) def imul(a, b): + if is_int(a) and is_int(b): + return int_mul(a, b) return augassign(a, b, lambda a: a.__imul__, lambda a: a.__mul__, lambda b: b.__rmul__) def ior_(a, b): + if is_int(a) and is_int(b): + return int_or(a, b) return augassign(a, b, lambda a: a.__ior__, lambda a: a.__or__, lambda b: b.__ror__) def ipow(a, b): + if is_int(a) and is_int(b): + return int_pow(a, b) return augassign(a, b, lambda a: a.__ipow__, lambda a: a.__pow__, lambda b: b.__rpow__) def irshift(a, b): return augassign(a, b, lambda a: a.__irshift__, lambda a: a.__rshift__, lambda b: b.__rrshift__) def isub(a, b): + if is_int(a) and is_int(b): + return int_sub(a, b) return augassign(a, b, lambda a: a.__isub__, lambda a: a.__sub__, lambda b: b.__rsub__) def ixor(a, b): + if is_int(a) and is_int(b): + return int_xor(a, b) return augassign(a, b, lambda a: a.__ixor__, lambda a: a.__xor__, lambda b: b.__rxor__) # vim: tabstop=4 expandtab shiftwidth=4 diff -r c731ec23e6b1 -r fd57abd7f631 lib/operator/binary.py --- a/lib/operator/binary.py Fri Mar 24 22:46:46 2017 +0100 +++ b/lib/operator/binary.py Sat Mar 25 01:07:08 2017 +0100 @@ -3,7 +3,7 @@ """ Operator support. -Copyright (C) 2010, 2013, 2015, 2016 Paul Boddie +Copyright (C) 2010, 2013, 2015, 2016, 2017 Paul Boddie This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software @@ -20,6 +20,9 @@ """ from operator.core import binary_op, is_, is_not +from native import int_add, int_div, int_mod, int_mul, int_pow, int_sub, \ + int_and, int_not, int_or, int_xor, \ + is_int # These functions defer method lookup by wrapping the attribute access in # lambda functions. Thus, the appropriate methods are defined locally, but no @@ -28,15 +31,21 @@ # Binary operator functions. def add(a, b): + if is_int(a) and is_int(b): + return int_add(a, b) return binary_op(a, b, lambda a: a.__add__, lambda b: b.__radd__) def and_(a, b): + if is_int(a) and is_int(b): + return int_and(a, b) return binary_op(a, b, lambda a: a.__and__, lambda b: b.__rand__) def contains(a, b): return in_(b, a) def div(a, b): + if is_int(a) and is_int(b): + return int_div(a, b) return binary_op(a, b, lambda a: a.__div__, lambda b: b.__rdiv__) def floordiv(a, b): @@ -52,24 +61,36 @@ return binary_op(a, b, lambda a: a.__lshift__, lambda b: b.__rlshift__) def mod(a, b): + if is_int(a) and is_int(b): + return int_mod(a, b) return binary_op(a, b, lambda a: a.__mod__, lambda b: b.__rmod__) def mul(a, b): + if is_int(a) and is_int(b): + return int_mul(a, b) return binary_op(a, b, lambda a: a.__mul__, lambda b: b.__rmul__) def or_(a, b): + if is_int(a) and is_int(b): + return int_or(a, b) return binary_op(a, b, lambda a: a.__or__, lambda b: b.__ror__) def pow(a, b): + if is_int(a) and is_int(b): + return int_pow(a, b) return binary_op(a, b, lambda a: a.__pow__, lambda b: b.__rpow__) def rshift(a, b): return binary_op(a, b, lambda a: a.__rshift__, lambda b: b.__rrshift__) def sub(a, b): + if is_int(a) and is_int(b): + return int_sub(a, b) return binary_op(a, b, lambda a: a.__sub__, lambda b: b.__rsub__) def xor(a, b): + if is_int(a) and is_int(b): + return int_xor(a, b) return binary_op(a, b, lambda a: a.__xor__, lambda b: b.__rxor__) # vim: tabstop=4 expandtab shiftwidth=4 diff -r c731ec23e6b1 -r fd57abd7f631 lib/operator/comparison.py --- a/lib/operator/comparison.py Fri Mar 24 22:46:46 2017 +0100 +++ b/lib/operator/comparison.py Sat Mar 25 01:07:08 2017 +0100 @@ -3,7 +3,7 @@ """ Operator support. -Copyright (C) 2010, 2013, 2015, 2016 Paul Boddie +Copyright (C) 2010, 2013, 2015, 2016, 2017 Paul Boddie This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software @@ -20,6 +20,7 @@ """ from operator.core import binary_op +from native import int_eq, int_ge, int_gt, int_le, int_lt, int_ne, is_int # These functions defer method lookup by wrapping the attribute access in # lambda functions. Thus, the appropriate methods are defined locally, but no @@ -28,21 +29,33 @@ # Comparison functions. def eq(a, b): + if is_int(a) and is_int(b): + return int_eq(a, b) return binary_op(a, b, lambda a: a.__eq__, lambda b: b.__eq__, False) def ge(a, b): + if is_int(a) and is_int(b): + return int_ge(a, b) return binary_op(a, b, lambda a: a.__ge__, lambda b: b.__le__) def gt(a, b): + if is_int(a) and is_int(b): + return int_gt(a, b) return binary_op(a, b, lambda a: a.__gt__, lambda b: b.__lt__) def le(a, b): + if is_int(a) and is_int(b): + return int_le(a, b) return binary_op(a, b, lambda a: a.__le__, lambda b: b.__ge__) def lt(a, b): + if is_int(a) and is_int(b): + return int_lt(a, b) return binary_op(a, b, lambda a: a.__lt__, lambda b: b.__gt__) def ne(a, b): + if is_int(a) and is_int(b): + return int_ne(a, b) return binary_op(a, b, lambda a: a.__ne__, lambda b: b.__ne__, True) # vim: tabstop=4 expandtab shiftwidth=4 diff -r c731ec23e6b1 -r fd57abd7f631 lib/operator/unary.py --- a/lib/operator/unary.py Fri Mar 24 22:46:46 2017 +0100 +++ b/lib/operator/unary.py Sat Mar 25 01:07:08 2017 +0100 @@ -3,7 +3,7 @@ """ Operator support. -Copyright (C) 2010, 2013, 2015, 2016 Paul Boddie +Copyright (C) 2010, 2013, 2015, 2016, 2017 Paul Boddie This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software @@ -20,6 +20,7 @@ """ from operator.core import unary_op +from native.int import int_neg, int_not, is_int # These functions defer method lookup by wrapping the attribute access in # lambda functions. Thus, the appropriate methods are defined locally, but no @@ -31,12 +32,18 @@ return unary_op(a, lambda a: a.__invert__) def neg(a): + if is_int(a): + return int_neg(a) return unary_op(a, lambda a: a.__neg__) def not_(a): + if is_int(a): + return int_not(a) return not a def pos(a): + if is_int(a): + return a return unary_op(a, lambda a: a.__pos__) # vim: tabstop=4 expandtab shiftwidth=4 diff -r c731ec23e6b1 -r fd57abd7f631 templates/native/int.c --- a/templates/native/int.c Fri Mar 24 22:46:46 2017 +0100 +++ b/templates/native/int.c Sat Mar 25 01:07:08 2017 +0100 @@ -31,6 +31,11 @@ /* Integer operations. */ +__attr __fn_native_int_is_int(__attr __self, __attr obj) +{ + return __INTEGER(obj) ? __builtins___boolean_True : __builtins___boolean_False; +} + __attr __fn_native_int_int_add(__attr __self, __attr self, __attr other) { /* self and other interpreted as int */ @@ -187,6 +192,16 @@ return __new_int(i ^ j); } +__attr __fn_native_int_int_le(__attr __self, __attr self, __attr other) +{ + /* self and other interpreted as int */ + int i = __TOINT(self); + int j = __TOINT(other); + + /* Return a boolean result. */ + return i <= j ? __builtins___boolean_True : __builtins___boolean_False; +} + __attr __fn_native_int_int_lt(__attr __self, __attr self, __attr other) { /* self and other interpreted as int */ @@ -197,6 +212,16 @@ return i < j ? __builtins___boolean_True : __builtins___boolean_False; } +__attr __fn_native_int_int_ge(__attr __self, __attr self, __attr other) +{ + /* self and other interpreted as int */ + int i = __TOINT(self); + int j = __TOINT(other); + + /* Return a boolean result. */ + return i >= j ? __builtins___boolean_True : __builtins___boolean_False; +} + __attr __fn_native_int_int_gt(__attr __self, __attr self, __attr other) { /* self and other interpreted as int */ diff -r c731ec23e6b1 -r fd57abd7f631 templates/native/int.h --- a/templates/native/int.h Fri Mar 24 22:46:46 2017 +0100 +++ b/templates/native/int.h Sat Mar 25 01:07:08 2017 +0100 @@ -23,6 +23,7 @@ /* Integer operations. */ +__attr __fn_native_int_is_int(__attr __self, __attr obj); __attr __fn_native_int_int_add(__attr __self, __attr _data, __attr other); __attr __fn_native_int_int_sub(__attr __self, __attr _data, __attr other); __attr __fn_native_int_int_mul(__attr __self, __attr _data, __attr other); @@ -36,9 +37,11 @@ __attr __fn_native_int_int_or(__attr __self, __attr _data, __attr other); __attr __fn_native_int_int_xor(__attr __self, __attr _data, __attr other); -__attr __fn_native_int_int_lt(__attr __self, __attr _data, __attr other); +__attr __fn_native_int_int_eq(__attr __self, __attr _data, __attr other); +__attr __fn_native_int_int_ge(__attr __self, __attr _data, __attr other); __attr __fn_native_int_int_gt(__attr __self, __attr _data, __attr other); -__attr __fn_native_int_int_eq(__attr __self, __attr _data, __attr other); +__attr __fn_native_int_int_le(__attr __self, __attr _data, __attr other); +__attr __fn_native_int_int_lt(__attr __self, __attr _data, __attr other); __attr __fn_native_int_int_ne(__attr __self, __attr _data, __attr other); __attr __fn_native_int_int_str(__attr __self, __attr _data); diff -r c731ec23e6b1 -r fd57abd7f631 templates/native/system.c --- a/templates/native/system.c Fri Mar 24 22:46:46 2017 +0100 +++ b/templates/native/system.c Sat Mar 25 01:07:08 2017 +0100 @@ -29,7 +29,7 @@ __attr __fn_native_system_exit(__attr __self, __attr status) { - exit(__VALUE(status)); + exit(__TOINT(status)); return __builtins___none_None; }