From 46a44543587894785baf2e4e06a922c895fd20bb Mon Sep 17 00:00:00 2001 From: Jeong YunWon Date: Fri, 5 Apr 2024 02:02:41 +0900 Subject: [PATCH] =?UTF-8?q?bigint/smallint=20=EB=B9=8C=EB=93=9C=20?= =?UTF-8?q?=ED=83=80=EA=B2=9F=20=EB=B6=84=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/ci.yaml | 5 +++-- Makefile | 27 +++++++++++++++----------- README.md | 3 ++- aheui/_compat.py | 40 ++++++++++++++++++++++++++++++++++++--- aheui/aheui.py | 6 ++---- aheui/int/bigint.py | 3 +++ aheui/int/smallint.py | 23 +++++++++++++++++++++- aheui/option.py | 4 ++-- aheui/warning.py | 2 +- 9 files changed, 88 insertions(+), 25 deletions(-) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 47c696d..a1ef5dc 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -60,8 +60,9 @@ jobs: run: | export RPYTHON="pypy $GITHUB_WORKSPACE/pypy/rpython/bin/rpython" cd $GITHUB_WORKSPACE - make + make -j 3 - name: Test with snippets run: | cd "$GITHUB_WORKSPACE/snippets" - AHEUI="$GITHUB_WORKSPACE/rpaheui-c" ./test.sh --disable logo integer + AHEUI="$GITHUB_WORKSPACE/rpaheui-c" ./test.sh --disable integer + AHEUI="$GITHUB_WORKSPACE/rpaheui-bigint-c" ./test.sh \ No newline at end of file diff --git a/Makefile b/Makefile index 5dbb95d..55ea7fb 100644 --- a/Makefile +++ b/Makefile @@ -3,30 +3,35 @@ RPYTHON?=../pypy/rpython/bin/rpython RPYTHONFLAGS?=--opt=jit --translation-jit_opencoder_model=big -all: aheui-c aheui-py +all: aheui-bigint-c aheui-c aheui-py version: echo "VERSION = '`git describe --tags`'" > aheui/version.py -aheui-py: +aheui-py: version cp rpaheui.py bin/aheui-py cp rpaheui.py bin/aheui -aheui-c: version +aheui-bigint-c: + RPAHEUI_BIGINT=1 $(RPYTHON) $(RPYTHONFLAGS) --output rpaheui-bigint-c rpaheui.py + +aheui-c: $(RPYTHON) $(RPYTHONFLAGS) rpaheui.py clean: - rm rpaheui-c + rm rpaheui-smallint rpaheui-bigint install: aheui-c - cp rpaheui-c /usr/local/bin/rpaheui + cp rpaheui-bigint /usr/local/bin/rpaheui-bigint + cp rpaheui-smallint /usr/local/bin/rpaheui ln -s /usr/local/bin/rpaheui /usr/local/bin/aheui -test: - if [ -e snippets ]; then cd snippets && git pull; else git clone https://github.com/aheui/snippets; fi - cd snippets && AHEUI="../rpaheui-c" bash test.sh +test-bigint: + cd snippets && AHEUI="../rpaheui-bigint-c" bash test.sh + +test-smallint: + cd snippets && AHEUI="../rpaheui-c" bash test.sh --disable integer -testpy: +test-py: pytest - if [ -e snippets ]; then cd snippets && git pull; else git clone https://github.com/aheui/snippets; fi - cd snippets && AHEUI=../rpaheui.py bash test.sh + cd snippets && AHEUI=../bin/aheui bash test.sh --disable logo diff --git a/README.md b/README.md index a6fd413..9af8b57 100644 --- a/README.md +++ b/README.md @@ -24,7 +24,8 @@ ``` git clone https://github.com/aheui/rpaheui make # RPYTHON 환경변수 설정 필요. rpython은 pypy 소스코드를 내려받으면 포함되어 있습니다. 버전은 github actions 설정을 참고해 주세요. -./aheui-c +./aheui-c <아희 코드 파일> +./aheui-bigint-c <큰 정수가 필요한 아희 코드 파일> ``` JIT로 속도 올리기 diff --git a/aheui/_compat.py b/aheui/_compat.py index c7a450e..43021e2 100644 --- a/aheui/_compat.py +++ b/aheui/_compat.py @@ -1,12 +1,34 @@ - # coding: utf-8 +from __future__ import absolute_import +import os + try: from rpython.rlib import jit from rpython.rlib.listsort import TimSort - TRACE_LIMIT = jit.PARAMETERS['trace_limit'] + + PYR = True + PY2 = False + PY3 = False + PY = -1 + + try: + USE_BIGINT = os.environ["RPAHEUI_BIGINT"] + except (KeyError, ValueError): + USE_BIGINT = '' + except ImportError: - """Python compatibility.""" + # Python compatibility + + import sys + + PY = sys.version_info.major + PYR = False + PY2 = PY == 2 + PY3 = PY == 3 + + USE_BIGINT = False + def omnipotent(*args, **kw): return args and args[0] @@ -61,3 +83,15 @@ def _unicode(i): def _unicode(i): return u'%d' % i _unicode(0) + + +try: + USE_BIGINT = os.environ["RPAHEUI_BIGINT"] +except (KeyError, ValueError): + USE_BIGINT = '' + + +if USE_BIGINT: + from aheui.int import bigint # Enable bigint in rpython build +else: + from aheui.int import smallint as bigint # noqa: F401 smallint or python support diff --git a/aheui/aheui.py b/aheui/aheui.py index 7970faf..eb20592 100644 --- a/aheui/aheui.py +++ b/aheui/aheui.py @@ -6,9 +6,8 @@ import os from aheui import const as c -from aheui._compat import jit, unichr, ord, _unicode +from aheui._compat import jit, unichr, ord, _unicode, bigint from aheui import compile -from aheui.int import smallint as bigint # import `bigint` to enable bigint from aheui.option import process_options from aheui.warning import WarningPool @@ -441,7 +440,6 @@ def mainloop(program, debug): return 0 - def open_w(filename): return os.open(filename, os.O_WRONLY | os.O_CREAT, 0o644) @@ -483,7 +481,7 @@ def entry_point(argv): cmd, source, contents, str_opt_level, target, aheuic_output, comment_aheuis, output, warning_limit, trace_limit = process_options(argv, os.environ) except SystemExit: return 1 - + warnings.limit = warning_limit if trace_limit >= 0: jit.set_param(driver, 'trace_limit', trace_limit) diff --git a/aheui/int/bigint.py b/aheui/int/bigint.py index 3e9f686..cfb8e66 100644 --- a/aheui/int/bigint.py +++ b/aheui/int/bigint.py @@ -3,6 +3,9 @@ from rpython.rlib.rbigint import rbigint +NAME = 'bigint' + + Int = rbigint diff --git a/aheui/int/smallint.py b/aheui/int/smallint.py index 6bc87fa..65ece6a 100644 --- a/aheui/int/smallint.py +++ b/aheui/int/smallint.py @@ -3,57 +3,78 @@ except ImportError: # python2 builtins = __builtins__ +from aheui._compat import jit, PY3 + + +NAME = 'smallint' + Int = int +@jit.elidable def fromstr(s): return int(s) +@jit.elidable def fromint(v): return v +@jit.elidable def fromlong(v): return v +@jit.elidable def toint(v): return v +@jit.elidable def tolonglong(v): return v +@jit.elidable def add(r1, r2): return r1 + r2 +@jit.elidable def sub(r1, r2): return r1 - r2 +@jit.elidable def mul(r1, r2): return r1 * r2 +@jit.elidable def div(r1, r2): return r1 // r2 +@jit.elidable def mod(r1, r2): return r1 % r2 +@jit.elidable def ge(r1, r2): return r1 >= r2 +@jit.elidable def str(r): - return builtins.str(r).encode('utf-8') + s = builtins.str(r) + if PY3: + s = s.encode('utf-8') + return s +@jit.elidable def eq_int(r, i): return r == i diff --git a/aheui/option.py b/aheui/option.py index 3618458..c3860ab 100644 --- a/aheui/option.py +++ b/aheui/option.py @@ -4,6 +4,7 @@ import os from aheui._argparse import ArgumentParser +from aheui._compat import bigint from aheui.version import VERSION from aheui import compile @@ -32,11 +33,10 @@ parser.add_argument('--no-c', '--no-c', narg='0', default='no', description='Do not generate `.aheuic` file automatically.', full_description='\tWhat is .aheuic? https://github.com/aheui/snippets/commit/cbb5a12e7cd2db771538ab28dfbc9ad1ada86f35\n') parser.add_argument('--warning-limit', '--warning-limit', default='', description='Set repetitive warning limit. '' fallbacks to environment variable `RPAHEUI_WARNING_LIMIT`. 0 means no warning. -1 means no limit. Default is 3.') parser.add_argument('--trace-limit', '--trace-limit', default='', description='Set JIT trace limit. '' fallbacks to environment variable `RPAHEUI_TRACE_LIMIT`.') -parser.add_argument('--version', '-v', narg='-1', default='no', description='Show program version', message=VERSION) +parser.add_argument('--version', '-v', narg='-1', default='no', description='Show program version', message=('%s %s' % (VERSION, bigint.NAME)).encode('utf-8')) parser.add_argument('--help', '-h', narg='-1', default='no', description='Show this help text') - def kwarg_or_environ(kwargs, environ, arg_key, env_key): if arg_key in kwargs and kwargs[arg_key] != '': return (1, kwargs[arg_key]) diff --git a/aheui/warning.py b/aheui/warning.py index 9354159..cfe044c 100644 --- a/aheui/warning.py +++ b/aheui/warning.py @@ -11,7 +11,7 @@ def __init__(self, name, message): def format(self, *args): return self.message % args - + WARNING_LIST = [ Warning(b'write-utf8-range', b'[Warning:UndefinedBehavior:write-utf8-range] value %x is out of unicode codepoint range.'),