Author: Amaury Forgeot d'Arc <[email protected]>
Branch: decimal-libmpdec
Changeset: r71552:3b950bcc282d
Date: 2014-05-12 23:13 +0200
http://bitbucket.org/pypy/pypy/changeset/3b950bcc282d/
Log: Lot of new Context operations
diff --git a/pypy/module/_decimal/interp_context.py
b/pypy/module/_decimal/interp_context.py
--- a/pypy/module/_decimal/interp_context.py
+++ b/pypy/module/_decimal/interp_context.py
@@ -214,6 +214,42 @@
self.capitals, rffi.cast(lltype.Signed, self.ctx.c_clamp),
flags, traps))
+ # Unary arithmetic functions
+ def unary_method(self, space, mpd_func, w_x):
+ from pypy.module._decimal import interp_decimal
+ w_a = interp_decimal.convert_op_raise(space, self, w_x)
+ w_result = interp_decimal.W_Decimal.allocate(space)
+ with self.catch_status(space) as (ctx, status_ptr):
+ mpd_func(w_result.mpd, w_a.mpd, ctx, status_ptr)
+ return w_result
+
+ def abs_w(self, space, w_x):
+ return self.unary_method(space, rmpdec.mpd_qabs, w_x)
+ def exp_w(self, space, w_x):
+ return self.unary_method(space, rmpdec.mpd_qexp, w_x)
+ def ln_w(self, space, w_x):
+ return self.unary_method(space, rmpdec.mpd_qln, w_x)
+ def log10_w(self, space, w_x):
+ return self.unary_method(space, rmpdec.mpd_qlog10, w_x)
+ def minus_w(self, space, w_x):
+ return self.unary_method(space, rmpdec.mpd_qminus, w_x)
+ def next_minus_w(self, space, w_x):
+ return self.unary_method(space, rmpdec.mpd_qnext_minus, w_x)
+ def next_plus_w(self, space, w_x):
+ return self.unary_method(space, rmpdec.mpd_qnext_plus, w_x)
+ def normalize_w(self, space, w_x):
+ return self.unary_method(space, rmpdec.mpd_qreduce, w_x)
+ def plus_w(self, space, w_x):
+ return self.unary_method(space, rmpdec.mpd_qplus, w_x)
+ def to_integral_w(self, space, w_x):
+ return self.unary_method(space, rmpdec.mpd_qround_to_int, w_x)
+ def to_integral_exact_w(self, space, w_x):
+ return self.unary_method(space, rmpdec.mpd_qround_to_intx, w_x)
+ def to_integral_value_w(self, space, w_x):
+ return self.unary_method(space, rmpdec.mpd_qround_to_int, w_x)
+ def sqrt_w(self, space, w_x):
+ return self.unary_method(space, rmpdec.mpd_qsqrt, w_x)
+
# Binary arithmetic functions
def binary_method(self, space, mpd_func, w_x, w_y):
from pypy.module._decimal import interp_decimal
@@ -231,7 +267,60 @@
return self.binary_method(space, rmpdec.mpd_qmul, w_x, w_y)
def divide_w(self, space, w_x, w_y):
return self.binary_method(space, rmpdec.mpd_qdiv, w_x, w_y)
+ def compare_w(self, space, w_x, w_y):
+ return self.binary_method(space, rmpdec.mpd_qcompare, w_x, w_y)
+ def compare_signal_w(self, space, w_x, w_y):
+ return self.binary_method(space, rmpdec.mpd_qcompare_signal, w_x, w_y)
+ def divide_int_w(self, space, w_x, w_y):
+ return self.binary_method(space, rmpdec.mpd_qdivint, w_x, w_y)
+ def divmod_w(self, space, w_x, w_y):
+ from pypy.module._decimal import interp_decimal
+ return interp_decimal.W_Decimal.divmod_impl(space, self, w_x, w_y)
+ def max_w(self, space, w_x, w_y):
+ return self.binary_method(space, rmpdec.mpd_qmax, w_x, w_y)
+ def max_mag_w(self, space, w_x, w_y):
+ return self.binary_method(space, rmpdec.mpd_qmax_mag, w_x, w_y)
+ def min_w(self, space, w_x, w_y):
+ return self.binary_method(space, rmpdec.mpd_qmin, w_x, w_y)
+ def min_mag_w(self, space, w_x, w_y):
+ return self.binary_method(space, rmpdec.mpd_qmin_mag, w_x, w_y)
+ def next_toward_w(self, space, w_x, w_y):
+ return self.binary_method(space, rmpdec.mpd_qnext_toward, w_x, w_y)
+ def quantize_w(self, space, w_x, w_y):
+ return self.binary_method(space, rmpdec.mpd_qquantize, w_x, w_y)
+ def remainder_w(self, space, w_x, w_y):
+ return self.binary_method(space, rmpdec.mpd_qrem, w_x, w_y)
+ def remainder_near_w(self, space, w_x, w_y):
+ return self.binary_method(space, rmpdec.mpd_qrem_near, w_x, w_y)
+ # Ternary operations
+ def power_w(self, space, w_a, w_b, w_modulo=None):
+ from pypy.module._decimal import interp_decimal
+ w_a, w_b = interp_decimal.convert_binop_raise(space, self, w_a, w_b)
+ if not space.is_none(w_modulo):
+ w_modulo = interp_decimal.convert_op_raise(space, self, w_modulo)
+ else:
+ w_modulo = None
+ w_result = interp_decimal.W_Decimal.allocate(space)
+ with self.catch_status(space) as (ctx, status_ptr):
+ if w_modulo:
+ rmpdec.mpd_qpowmod(w_result.mpd, w_a.mpd, w_b.mpd,
w_modulo.mpd,
+ ctx, status_ptr)
+ else:
+ rmpdec.mpd_qpow(w_result.mpd, w_a.mpd, w_b.mpd,
+ ctx, status_ptr)
+ return w_result
+
+ def fma_w(self, space, w_v, w_w, w_x):
+ from pypy.module._decimal import interp_decimal
+ w_a = interp_decimal.convert_op_raise(space, self, w_v)
+ w_b = interp_decimal.convert_op_raise(space, self, w_w)
+ w_c = interp_decimal.convert_op_raise(space, self, w_x)
+ w_result = interp_decimal.W_Decimal.allocate(space)
+ with self.catch_status(space) as (ctx, status_ptr):
+ rmpdec.mpd_qfma(w_result.mpd, w_a.mpd, w_b.mpd, w_c.mpd,
+ ctx, status_ptr)
+ return w_result
def descr_new_context(space, w_subtype, __args__):
w_result = space.allocate_instance(W_Context, w_subtype)
@@ -257,11 +346,40 @@
clear_flags=interp2app(W_Context.clear_flags_w),
clear_traps=interp2app(W_Context.clear_traps_w),
create_decimal=interp2app(W_Context.create_decimal_w),
- # Operations
+ # Unary Operations
+ abs=interp2app(W_Context.abs_w),
+ exp=interp2app(W_Context.exp_w),
+ ln=interp2app(W_Context.ln_w),
+ log10=interp2app(W_Context.log10_w),
+ minus=interp2app(W_Context.minus_w),
+ next_minus=interp2app(W_Context.next_minus_w),
+ next_plus=interp2app(W_Context.next_plus_w),
+ normalize=interp2app(W_Context.normalize_w),
+ plus=interp2app(W_Context.plus_w),
+ to_integral=interp2app(W_Context.to_integral_w),
+ to_integral_exact=interp2app(W_Context.to_integral_exact_w),
+ to_integral_value=interp2app(W_Context.to_integral_value_w),
+ sqrt=interp2app(W_Context.sqrt_w),
+ # Binary Operations
add=interp2app(W_Context.add_w),
subtract=interp2app(W_Context.subtract_w),
multiply=interp2app(W_Context.multiply_w),
divide=interp2app(W_Context.divide_w),
+ compare=interp2app(W_Context.compare_w),
+ compare_signal=interp2app(W_Context.compare_signal_w),
+ divide_int=interp2app(W_Context.divide_int_w),
+ divmod=interp2app(W_Context.divmod_w),
+ max=interp2app(W_Context.max_w),
+ max_mag=interp2app(W_Context.max_mag_w),
+ min=interp2app(W_Context.min_w),
+ min_mag=interp2app(W_Context.min_mag_w),
+ next_toward=interp2app(W_Context.next_toward_w),
+ quantize=interp2app(W_Context.quantize_w),
+ remainder=interp2app(W_Context.remainder_w),
+ remainder_near=interp2app(W_Context.remainder_near_w),
+ # Ternary operations
+ power=interp2app(W_Context.power_w),
+ fma=interp2app(W_Context.fma_w),
)
diff --git a/pypy/module/_decimal/interp_decimal.py
b/pypy/module/_decimal/interp_decimal.py
--- a/pypy/module/_decimal/interp_decimal.py
+++ b/pypy/module/_decimal/interp_decimal.py
@@ -238,9 +238,7 @@
return binary_number_method(space, rmpdec.mpd_qrem, w_other, self)
@staticmethod
- def divmod_impl(space, w_x, w_y):
- context = interp_context.getcontext(space)
-
+ def divmod_impl(space, context, w_x, w_y):
w_err, w_a, w_b = convert_binop(space, context, w_x, w_y)
if w_err:
return w_err
@@ -252,9 +250,11 @@
return space.newtuple([w_q, w_r])
def descr_divmod(self, space, w_other):
- return W_Decimal.divmod_impl(space, self, w_other)
+ context = interp_context.getcontext(space)
+ return W_Decimal.divmod_impl(space, context, self, w_other)
def descr_rdivmod(self, space, w_other):
- return W_Decimal.divmod_impl(space, w_other, self)
+ context = interp_context.getcontext(space)
+ return W_Decimal.divmod_impl(space, context, w_other, self)
@staticmethod
def pow_impl(space, w_base, w_exp, w_mod):
diff --git a/rpython/rlib/rmpdec.py b/rpython/rlib/rmpdec.py
--- a/rpython/rlib/rmpdec.py
+++ b/rpython/rlib/rmpdec.py
@@ -47,12 +47,17 @@
"mpd_to_sci", "mpd_to_sci_size",
"mpd_iszero", "mpd_isnegative", "mpd_isinfinite", "mpd_isspecial",
"mpd_isnan", "mpd_issnan", "mpd_isqnan",
- "mpd_qcmp", "mpd_qquantize",
+ "mpd_qcmp", "mpd_qcompare", "mpd_qcompare_signal",
+ "mpd_qmin", "mpd_qmax", "mpd_qmin_mag", "mpd_qmax_mag",
+ "mpd_qnext_minus", "mpd_qnext_plus", "mpd_qnext_toward",
+ "mpd_qquantize", "mpd_qreduce",
"mpd_qplus", "mpd_qminus", "mpd_qabs",
"mpd_qadd", "mpd_qsub", "mpd_qmul", "mpd_qdiv", "mpd_qdivint",
- "mpd_qrem", "mpd_qdivmod", "mpd_qpow", "mpd_qpowmod",
+ "mpd_qrem", "mpd_qrem_near", "mpd_qdivmod", "mpd_qpow", "mpd_qpowmod",
+ "mpd_qfma",
+ "mpd_qexp", "mpd_qln", "mpd_qlog10", "mpd_qsqrt",
"mpd_qcopy_sign",
- "mpd_qround_to_int",
+ "mpd_qround_to_int", "mpd_qround_to_intx",
],
compile_extra=compile_extra,
libraries=['m'],
@@ -229,9 +234,40 @@
'mpd_isqnan', [MPD_PTR], rffi.INT)
mpd_qcmp = external(
'mpd_qcmp', [MPD_PTR, MPD_PTR, rffi.UINTP], rffi.INT)
+mpd_qcompare = external(
+ 'mpd_qcompare',
+ [MPD_PTR, MPD_PTR, MPD_PTR, MPD_CONTEXT_PTR, rffi.UINTP], lltype.Void)
+mpd_qcompare_signal = external(
+ 'mpd_qcompare_signal',
+ [MPD_PTR, MPD_PTR, MPD_PTR, MPD_CONTEXT_PTR, rffi.UINTP], lltype.Void)
+
+mpd_qmin = external(
+ 'mpd_qmin',
+ [MPD_PTR, MPD_PTR, MPD_PTR, MPD_CONTEXT_PTR, rffi.UINTP], lltype.Void)
+mpd_qmax = external(
+ 'mpd_qmax',
+ [MPD_PTR, MPD_PTR, MPD_PTR, MPD_CONTEXT_PTR, rffi.UINTP], lltype.Void)
+mpd_qmin_mag = external(
+ 'mpd_qmin_mag',
+ [MPD_PTR, MPD_PTR, MPD_PTR, MPD_CONTEXT_PTR, rffi.UINTP], lltype.Void)
+mpd_qmax_mag = external(
+ 'mpd_qmax_mag',
+ [MPD_PTR, MPD_PTR, MPD_PTR, MPD_CONTEXT_PTR, rffi.UINTP], lltype.Void)
+mpd_qnext_minus = external(
+ 'mpd_qnext_minus',
+ [MPD_PTR, MPD_PTR, MPD_CONTEXT_PTR, rffi.UINTP], lltype.Void)
+mpd_qnext_plus = external(
+ 'mpd_qnext_plus',
+ [MPD_PTR, MPD_PTR, MPD_CONTEXT_PTR, rffi.UINTP], lltype.Void)
+mpd_qnext_toward = external(
+ 'mpd_qnext_toward',
+ [MPD_PTR, MPD_PTR, MPD_PTR, MPD_CONTEXT_PTR, rffi.UINTP], lltype.Void)
mpd_qquantize = external(
- 'mpd_qquantize', [MPD_PTR, MPD_PTR, MPD_PTR, MPD_CONTEXT_PTR, rffi.UINTP],
- lltype.Void)
+ 'mpd_qquantize',
+ [MPD_PTR, MPD_PTR, MPD_PTR, MPD_CONTEXT_PTR, rffi.UINTP], lltype.Void)
+mpd_qreduce = external(
+ 'mpd_qreduce',
+ [MPD_PTR, MPD_PTR, MPD_CONTEXT_PTR, rffi.UINTP], lltype.Void)
mpd_qplus = external(
'mpd_qplus',
@@ -251,36 +287,51 @@
lltype.Void)
mpd_qsub = external(
'mpd_qsub',
- [MPD_PTR, MPD_PTR, MPD_PTR, MPD_CONTEXT_PTR, rffi.UINTP],
- lltype.Void)
+ [MPD_PTR, MPD_PTR, MPD_PTR, MPD_CONTEXT_PTR, rffi.UINTP], lltype.Void)
mpd_qmul = external(
'mpd_qmul',
- [MPD_PTR, MPD_PTR, MPD_PTR, MPD_CONTEXT_PTR, rffi.UINTP],
- lltype.Void)
+ [MPD_PTR, MPD_PTR, MPD_PTR, MPD_CONTEXT_PTR, rffi.UINTP], lltype.Void)
mpd_qdiv = external(
'mpd_qdiv',
- [MPD_PTR, MPD_PTR, MPD_PTR, MPD_CONTEXT_PTR, rffi.UINTP],
- lltype.Void)
+ [MPD_PTR, MPD_PTR, MPD_PTR, MPD_CONTEXT_PTR, rffi.UINTP], lltype.Void)
mpd_qdivint = external(
'mpd_qdivint',
- [MPD_PTR, MPD_PTR, MPD_PTR, MPD_CONTEXT_PTR, rffi.UINTP],
- lltype.Void)
+ [MPD_PTR, MPD_PTR, MPD_PTR, MPD_CONTEXT_PTR, rffi.UINTP], lltype.Void)
mpd_qrem = external(
'mpd_qrem',
- [MPD_PTR, MPD_PTR, MPD_PTR, MPD_CONTEXT_PTR, rffi.UINTP],
- lltype.Void)
+ [MPD_PTR, MPD_PTR, MPD_PTR, MPD_CONTEXT_PTR, rffi.UINTP], lltype.Void)
+mpd_qrem_near = external(
+ 'mpd_qrem_near',
+ [MPD_PTR, MPD_PTR, MPD_PTR, MPD_CONTEXT_PTR, rffi.UINTP], lltype.Void)
mpd_qdivmod = external(
'mpd_qdivmod',
[MPD_PTR, MPD_PTR, MPD_PTR, MPD_PTR, MPD_CONTEXT_PTR, rffi.UINTP],
lltype.Void)
mpd_qpow = external(
'mpd_qpow',
- [MPD_PTR, MPD_PTR, MPD_PTR, MPD_CONTEXT_PTR, rffi.UINTP],
- lltype.Void)
+ [MPD_PTR, MPD_PTR, MPD_PTR, MPD_CONTEXT_PTR, rffi.UINTP], lltype.Void)
mpd_qpowmod = external(
'mpd_qpowmod',
[MPD_PTR, MPD_PTR, MPD_PTR, MPD_PTR, MPD_CONTEXT_PTR, rffi.UINTP],
lltype.Void)
+mpd_qfma = external(
+ 'mpd_qfma',
+ [MPD_PTR, MPD_PTR, MPD_PTR, MPD_PTR, MPD_CONTEXT_PTR, rffi.UINTP],
+ lltype.Void)
+
+mpd_qexp = external(
+ 'mpd_qexp',
+ [MPD_PTR, MPD_PTR, MPD_CONTEXT_PTR, rffi.UINTP], lltype.Void)
+mpd_qln = external(
+ 'mpd_qln',
+ [MPD_PTR, MPD_PTR, MPD_CONTEXT_PTR, rffi.UINTP], lltype.Void)
+mpd_qlog10 = external(
+ 'mpd_qlog10',
+ [MPD_PTR, MPD_PTR, MPD_CONTEXT_PTR, rffi.UINTP], lltype.Void)
+mpd_qsqrt = external(
+ 'mpd_qsqrt',
+ [MPD_PTR, MPD_PTR, MPD_CONTEXT_PTR, rffi.UINTP], lltype.Void)
+
mpd_qcopy_sign = external(
'mpd_qcopy_sign',
[MPD_PTR, MPD_PTR, MPD_PTR, MPD_CONTEXT_PTR, rffi.UINTP],
@@ -289,3 +340,6 @@
mpd_qround_to_int = external(
'mpd_qround_to_int', [MPD_PTR, MPD_PTR, MPD_CONTEXT_PTR, rffi.UINTP],
lltype.Void)
+mpd_qround_to_intx = external(
+ 'mpd_qround_to_intx', [MPD_PTR, MPD_PTR, MPD_CONTEXT_PTR, rffi.UINTP],
+ lltype.Void)
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit