Skip to content

Commit

Permalink
Improve type casting
Browse files Browse the repository at this point in the history
  • Loading branch information
yitzchak committed Sep 25, 2024
1 parent d26c225 commit 7b239cb
Show file tree
Hide file tree
Showing 10 changed files with 113 additions and 97 deletions.
2 changes: 1 addition & 1 deletion include/clasp/core/array_long_float.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class SimpleVector_long_float_O : public specialized_SimpleVector_long_float {
size_t length() const override { return this->_Data.length(); };

static value_type default_initial_element(void) { return long_float_t{0.0}; }
static value_type from_object(T_sp obj) { return core::Number_O::as_long_float(obj); };
static value_type from_object(T_sp obj) { return core::Number_O::as_long_float(obj.as<Number_O>()); };
static T_sp to_object(const value_type& v) { return core::LongFloat_O::create(v); };

SimpleVector_long_float_O(size_t length, value_type initialElement = value_type(), bool initialElementSupplied = false,
Expand Down
2 changes: 1 addition & 1 deletion include/clasp/core/array_short_float.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class SimpleVector_short_float_O : public specialized_SimpleVector_short_float {
typedef specialized_SimpleVector_short_float TemplatedBase;

static value_type default_initial_element(void) { return short_float_t{0.0}; }
static value_type from_object(T_sp obj) { return core::Number_O::as_short_float(obj); };
static value_type from_object(T_sp obj) { return core::Number_O::as_short_float(obj.as<Number_O>()); };
static T_sp to_object(const value_type& v) { return core::ShortFloat_O::create(v); };

SimpleVector_short_float_O(size_t length, value_type initialElement = value_type(), bool initialElementSupplied = false,
Expand Down
8 changes: 4 additions & 4 deletions include/clasp/core/bignum.h
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,9 @@ class Bignum_O : public Integer_O {

template <std::floating_point Float> static Bignum_sp create(Float v) {
auto q = float_convert<Float>::float_to_quadruple(v);
Bignum_sp b = clasp_ash(create(q.significand), q.exponent);
Bignum_sp b = gc::As_unsafe<Bignum_sp>(clasp_ash(create(q.significand), q.exponent)) ;
if (q.sign < 0)
return clasp_negate(b);
return gc::As_unsafe<Bignum_sp>(clasp_negate(b));

return b;
}
Expand Down Expand Up @@ -231,9 +231,9 @@ Integer_sp bignum_result(mp_size_t, const mp_limb_t*);
Bignum_sp core__mul_fixnums(Fixnum, Fixnum);
Bignum_sp core__next_lshift(Bignum_sp, Fixnum);
Integer_sp core__next_rshift(Bignum_sp, Fixnum);
T_mv core__next_truncate(Bignum_sp, Bignum_sp);
Number_mv core__next_truncate(Bignum_sp, Bignum_sp);
Integer_sp fix_divided_by_next(Fixnum, Bignum_sp);
T_mv core__next_ftruncate(Bignum_sp, Fixnum);
Number_mv core__next_ftruncate(Bignum_sp, Fixnum);
Integer_sp core__next_gcd(Bignum_sp, Bignum_sp);
Integer_sp core__next_fgcd(Bignum_sp, Fixnum);
int core__next_compare(Bignum_sp, Bignum_sp);
Expand Down
59 changes: 40 additions & 19 deletions include/clasp/core/numbers.h
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,8 @@ class Number_O : public General_O {
virtual bool equal(T_sp obj) const override;
virtual bool equalp(T_sp obj) const override;

virtual Number_sp realpart_() const { SUBIMP(); };
virtual Number_sp imagpart_() const { SUBIMP(); };
virtual Real_sp realpart_() const { SUBIMP(); };
virtual Real_sp imagpart_() const { SUBIMP(); };

// log(x) (i.e. natural log)
virtual Number_sp log1_() const { SUBIMP(); };
Expand Down Expand Up @@ -281,8 +281,8 @@ class Number_O : public General_O {
return x->as_long_float_();
}

inline static Number_sp realpart(const Number_sp x);
inline static Number_sp imagpart(const Number_sp x);
inline static Real_sp realpart(const Number_sp x);
inline static Real_sp imagpart(const Number_sp x);

inline static Number_sp negate(const Number_sp x);
inline static Number_sp signum(const Number_sp num);
Expand Down Expand Up @@ -319,24 +319,46 @@ class Number_O : public General_O {
};

inline Number_sp operator-(const Number_sp x) { return Number_O::negate(x); }
inline Integer_sp operator-(const Integer_sp x) { return gc::As_unsafe<Integer_sp>(Number_O::negate(x)); }
inline Real_sp operator-(const Real_sp x) { return gc::As_unsafe<Real_sp>(Number_O::negate(x)); }

inline Number_sp operator+(const Number_sp x, const Number_sp y) { return Number_O::add_nn(x, y); }
inline Integer_sp operator+(const Integer_sp x, const Integer_sp y) { return gc::As_unsafe<Integer_sp>(Number_O::add_nn(x, y)); }
inline Real_sp operator+(const Real_sp x, const Real_sp y) { return gc::As_unsafe<Real_sp>(Number_O::add_nn(x, y)); }

inline Number_sp operator-(const Number_sp x, const Number_sp y) { return Number_O::sub_nn(x, y); }
inline Integer_sp operator-(const Integer_sp x, const Integer_sp y) { return gc::As_unsafe<Integer_sp>(Number_O::sub_nn(x, y)); }
inline Real_sp operator-(const Real_sp x, const Real_sp y) { return gc::As_unsafe<Real_sp>(Number_O::sub_nn(x, y)); }

inline Number_sp operator*(const Number_sp x, const Number_sp y) { return Number_O::mul_nn(x, y); }
inline Integer_sp operator*(const Integer_sp x, const Integer_sp y) { return gc::As_unsafe<Integer_sp>(Number_O::mul_nn(x, y)); }
inline Real_sp operator*(const Real_sp x, const Real_sp y) { return gc::As_unsafe<Real_sp>(Number_O::mul_nn(x, y)); }

inline Number_sp operator/(const Number_sp x, const Number_sp y) { return Number_O::div_nn(x, y); }
inline Rational_sp operator/(const Integer_sp x, const Integer_sp y) { return gc::As_unsafe<Rational_sp>(Number_O::div_nn(x, y)); }
inline Real_sp operator/(const Real_sp x, const Real_sp y) { return gc::As_unsafe<Real_sp>(Number_O::div_nn(x, y)); }

inline Number_sp operator+=(Number_sp& x, const Number_sp y) { return x = Number_O::add_nn(x, y); };
inline Integer_sp operator+=(Integer_sp& x, const Integer_sp y) { return x = gc::As_unsafe<Integer_sp>(Number_O::add_nn(x, y)); };
inline Real_sp operator+=(Real_sp& x, const Real_sp y) { return x = gc::As_unsafe<Real_sp>(Number_O::add_nn(x, y)); };

inline Number_sp operator-=(Number_sp& x, const Number_sp y) { return x = Number_O::sub_nn(x, y); };
inline Integer_sp operator-=(Integer_sp& x, const Integer_sp y) { return x = gc::As_unsafe<Integer_sp>(Number_O::sub_nn(x, y)); };
inline Real_sp operator-=(Real_sp& x, const Real_sp y) { return x = gc::As_unsafe<Real_sp>(Number_O::sub_nn(x, y)); };

inline Number_sp operator*=(Number_sp& x, const Number_sp y) { return x = Number_O::mul_nn(x, y); };
inline Integer_sp operator*=(Integer_sp& x, const Integer_sp y) { return x = gc::As_unsafe<Integer_sp>(Number_O::mul_nn(x, y)); };
inline Real_sp operator*=(Real_sp& x, const Real_sp y) { return x = gc::As_unsafe<Real_sp>(Number_O::mul_nn(x, y)); };

inline Number_sp operator/=(Number_sp& x, const Number_sp y) { return x = Number_O::div_nn(x, y); };

SMART(Real);
class Real_O : public Number_O {
LISP_ABSTRACT_CLASS(core, ClPkg, Real_O, "real", Number_O);

public:
Number_sp realpart_() const override { return asSmartPtr(); }
Number_sp imagpart_() const override { return clasp_make_fixnum(0); }
Real_sp realpart_() const override { return asSmartPtr(); }
Real_sp imagpart_() const override { return clasp_make_fixnum(0); }

virtual double_float_t as_double_float_() const override { SUBIMP(); };

Expand Down Expand Up @@ -591,7 +613,7 @@ class DoubleFloat_O : public Float_O {
void set(double val) { this->_Value = val; };
double get() const { return this->_Value; };

Number_sp imagpart_() const override;
Real_sp imagpart_() const override;

Number_sp signum_() const override;
Number_sp abs_() const override { return DoubleFloat_O::create(std::abs(this->_Value)); };
Expand Down Expand Up @@ -681,7 +703,7 @@ class LongFloat_O : public Float_O {
void set(long_float_t val) { this->_Value = val; };
long_float_t get() const { return this->_Value; };

Number_sp imagpart_() const override;
Real_sp imagpart_() const override;

Number_sp signum_() const override;
Number_sp abs_() const override { return LongFloat_O::create(std::abs(this->_Value)); };
Expand Down Expand Up @@ -770,8 +792,8 @@ class Complex_O : public Number_O {
// virtual Number_sp copy() const;
string __repr__() const override;

Number_sp realpart_() const override { return _real; }
Number_sp imagpart_() const override { return _imaginary; }
Real_sp realpart_() const override { return _real; }
Real_sp imagpart_() const override { return _imaginary; }

Number_sp signum_() const override;
Number_sp abs_() const override;
Expand Down Expand Up @@ -966,18 +988,18 @@ template <std::floating_point Float> inline Rational_sp float_to_rational(Float
if (q.exponent < 0) {
n /= clasp_ash(clasp_make_fixnum(1), -q.exponent);
} else if (q.exponent > 0) {
n = clasp_ash(n, q.exponent);
n = clasp_ash(n.as_unsafe<Integer_O>(), q.exponent);
}

if (q.sign < 0)
return clasp_negate(n);
return clasp_negate(n).as_unsafe<Rational_O>();

return n;
return n.as_unsafe<Rational_O>();
}

inline Rational_sp Rational_O::coerce(const Real_sp x) {
if (x.fixnump())
return x;
return x.as_unsafe<Rational_O>();
#ifdef CLASP_SHORT_FLOAT
if (x.short_floatp())
return float_to_rational(x.unsafe_short_float());
Expand All @@ -987,7 +1009,6 @@ inline Rational_sp Rational_O::coerce(const Real_sp x) {
return gc::As_unsafe<Number_sp>(x)->as_rational_();
}


Number_sp cl__expt(Number_sp x, Number_sp y);
Real_sp cl__mod(Real_sp, Real_sp);

Expand Down Expand Up @@ -1423,18 +1444,18 @@ inline Number_sp Number_O::tanh(Number_sp x) {
return x->tanh_();
}

inline Number_sp Number_O::realpart(const Number_sp x) {
inline Real_sp Number_O::realpart(const Number_sp x) {
#ifdef CLASP_SHORT_FLOAT
if (x.fixnump() || x.single_floatp() || x.short_floatp())
return x;
return x.as_unsafe<Real_O>();
#else
if (x.fixnump() || x.single_floatp())
return x;
return x.as_unsafe<Real_O>();
#endif
return x->realpart_();
}

inline Number_sp Number_O::imagpart(const Number_sp x) {
inline Real_sp Number_O::imagpart(const Number_sp x) {
if (x.fixnump())
return clasp_make_fixnum(0);
#ifdef CLASP_SHORT_FLOAT
Expand Down
4 changes: 2 additions & 2 deletions src/core/bignum.cc
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,7 @@ CL_DEFUN Bignum_sp core__mul_fixnums(Fixnum left, Fixnum right) {
}

DOCGROUP(clasp);
CL_DEFUN T_mv core__next_truncate(Bignum_sp dividend, Bignum_sp divisor) {
CL_DEFUN Number_mv core__next_truncate(Bignum_sp dividend, Bignum_sp divisor) {
ASSERT(dividend != divisor); // "No overlap is permitted between arguments"
mp_size_t dividend_length = dividend->length();
mp_size_t divisor_length = divisor->length();
Expand Down Expand Up @@ -436,7 +436,7 @@ CL_DEFUN T_mv core__next_truncate(Bignum_sp dividend, Bignum_sp divisor) {
// Truncating a fixnum by a bignum will always get you zero
// so there's no function for that.
DOCGROUP(clasp);
CL_DEFUN T_mv core__next_ftruncate(Bignum_sp dividend, Fixnum divisor) {
CL_DEFUN Number_mv core__next_ftruncate(Bignum_sp dividend, Fixnum divisor) {
if (divisor == 0)
ERROR_DIVISION_BY_ZERO(dividend, clasp_make_fixnum(divisor));
mp_limb_t positive_divisor = std::abs(divisor);
Expand Down
2 changes: 1 addition & 1 deletion src/core/float_to_string.cc
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ T_sp core_float_to_string_free(Float_sp number, Number_sp e_min, Number_sp e_max
insert_char(buffer, base++, '-');
}
/* Do we have to print in exponent notation? */
if (clasp_lowereq(exp, e_min) || clasp_lowereq(e_max, exp)) {
if (clasp_lowereq(exp, e_min.as<Real_O>()) || clasp_lowereq(e_max.as<Real_O>(), exp)) {
insert_char(buffer, base + 1, '.');
if (gc::As<StrNs_sp>(buffer)->fillPointer() == base + 2)
buffer->vectorPushExtend(clasp_make_character('0'));
Expand Down
4 changes: 2 additions & 2 deletions src/core/num_arith.cc
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ namespace core {
Integer_sp clasp_integer_divide(Integer_sp x, Integer_sp y) {
Bignum_sp bx = x.asOrNull<Bignum_O>(), by = y.asOrNull<Bignum_O>();
if (bx && by)
return core__next_truncate(bx, by);
return core__next_truncate(bx, by).as_unsafe<Integer_O>();
if (bx)
return core__next_ftruncate(bx, y.unsafe_fixnum());
return core__next_ftruncate(bx, y.unsafe_fixnum()).as_unsafe<Integer_O>();
if (by)
return fix_divided_by_next(x.unsafe_fixnum(), by);

Expand Down
34 changes: 17 additions & 17 deletions src/core/num_co.cc
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ DOCGROUP(clasp);
CL_DEFUN Float_sp cl__float(Real_sp x, T_sp y, bool yp) {
if (!yp) {
if (x.isA<Float_O>())
return x;
return x.as_unsafe<Float_O>();
return SingleFloat_dummy_O::coerce(x);
}

Expand Down Expand Up @@ -143,7 +143,7 @@ CL_DEFUN Integer_sp cl__numerator(Rational_sp x) {
if (rx)
return rx->numerator();
if (x.fixnump())
return x;
return x.as_unsafe<Integer_O>();

return x.as<Integer_O>();
}
Expand Down Expand Up @@ -178,19 +178,19 @@ static void clasp_truncate(Real_sp dividend, Real_sp divisor, Integer_sp& quotie
Real_sp subr;
clasp_truncate(rdividend->numerator() * rdivisor->denominator(), rdivisor->numerator() * rdividend->denominator(), quotient,
subr);
remainder = Rational_O::create(subr, rdividend->denominator() * rdivisor->denominator());
remainder = Rational_O::create(subr.as<Integer_O>(), rdividend->denominator() * rdivisor->denominator());
return;
}
if (rdividend) {
Real_sp subr;
clasp_truncate(rdividend->numerator(), rdividend->denominator() * divisor, quotient, subr);
remainder = subr / rdividend->denominator();
clasp_truncate(rdividend->numerator(), rdividend->denominator().as_unsafe<Real_O>() * divisor, quotient, subr);
remainder = subr / rdividend->denominator().as_unsafe<Real_O>();
return;
}
if (rdivisor && dividend.isA<Integer_O>()) {
Real_sp subr;
clasp_truncate(dividend * rdivisor->denominator(), rdivisor->numerator(), quotient, subr);
remainder = Rational_O::create(subr, rdivisor->denominator());
clasp_truncate(dividend * rdivisor->denominator().as_unsafe<Real_O>(), rdivisor->numerator(), quotient, subr);
remainder = Rational_O::create(subr.as<Integer_O>(), rdivisor->denominator());
return;
}

Expand Down Expand Up @@ -220,14 +220,14 @@ static void clasp_truncate(Real_sp dividend, Real_sp divisor, Integer_sp& quotie

Bignum_sp bdividend = dividend.asOrNull<Bignum_O>(), bdivisor = divisor.asOrNull<Bignum_O>();
if (bdividend && bdivisor) {
T_mv mvr = core__next_truncate(bdividend, bdivisor);
quotient = mvr;
Number_mv mvr = core__next_truncate(bdividend, bdivisor);
quotient = mvr.as<Integer_O>();
MultipleValues& mvn = core::lisp_multipleValues();
remainder = mvn.valueGet(1, mvr.number_of_values());
remainder = mvn.valueGet(1, mvr.number_of_values()).as<Real_O>();
return;
}
if (bdividend) {
T_mv rmv = core__next_ftruncate(bdividend, divisor.unsafe_fixnum());
Number_mv rmv = core__next_ftruncate(bdividend, divisor.unsafe_fixnum());
quotient = gc::As_unsafe<Integer_sp>(rmv);
MultipleValues& mvn = core::lisp_multipleValues();
remainder = gc::As_unsafe<Integer_sp>(mvn.valueGet(1, rmv.number_of_values()));
Expand Down Expand Up @@ -289,7 +289,7 @@ Real_mv clasp_floor1(Real_sp x) {
Integer_sp v0;
Real_sp tv1;
clasp_floor(rx->numerator(), rx->denominator(), v0, tv1);
return Values(v0, Ratio_O::create(tv1, rx->denominator()));
return Values(v0, Ratio_O::create(tv1.as_unsafe<Integer_O>(), rx->denominator()));
}

#ifdef CLASP_LONG_FLOAT
Expand Down Expand Up @@ -366,7 +366,7 @@ Real_mv clasp_ceiling1(Real_sp x) {
Integer_sp v0;
Real_sp tv1;
clasp_ceiling(rx->numerator(), rx->denominator(), v0, tv1);
return Values(v0, Ratio_O::create(tv1, rx->denominator()));
return Values(v0, Ratio_O::create(tv1.as_unsafe<Integer_O>(), rx->denominator()));
}

#ifdef CLASP_LONG_FLOAT
Expand Down Expand Up @@ -430,7 +430,7 @@ Real_mv clasp_truncate1(Real_sp x) {
Integer_sp v0;
Real_sp tv1;
clasp_truncate(rx->numerator(), rx->denominator(), v0, tv1);
return Values(v0, Ratio_O::create(tv1, rx->denominator()));
return Values(v0, Ratio_O::create(tv1.as_unsafe<Integer_O>(), rx->denominator()));
}

#ifdef CLASP_LONG_FLOAT
Expand Down Expand Up @@ -538,7 +538,7 @@ Real_mv clasp_round1(Real_sp x) {
Integer_sp v0;
Real_sp tv1;
clasp_round(rx->numerator(), rx->denominator(), v0, tv1);
return Values(v0, Ratio_O::create(tv1, rx->denominator()));
return Values(v0, Ratio_O::create(tv1.as_unsafe<Integer_O>(), rx->denominator()));
}

#ifdef CLASP_LONG_FLOAT
Expand Down Expand Up @@ -827,10 +827,10 @@ CL_DOCSTRING(R"dx(complex)dx");
DOCGROUP(clasp);
CL_DEFUN Complex_sp cl__complex(Real_sp r, Real_sp i) { return gc::As_unsafe<Complex_sp>(clasp_make_complex(r, i)); }

Number_sp DoubleFloat_O::imagpart_() const { return create(std::copysign(double_float_t{0.0}, _Value)); }
Real_sp DoubleFloat_O::imagpart_() const { return create(std::copysign(double_float_t{0.0}, _Value)); }

#ifdef CLASP_LONG_FLOAT
Number_sp LongFloat_O::imagpart_() const { return create(std::copysign(long_float_t{0.0}, _Value)); }
Real_sp LongFloat_O::imagpart_() const { return create(std::copysign(long_float_t{0.0}, _Value)); }
#endif

CL_LAMBDA(x);
Expand Down
Loading

0 comments on commit 7b239cb

Please sign in to comment.