shrub/jets/e/rq.c

447 lines
7.3 KiB
C
Raw Normal View History

2015-07-29 19:53:45 +03:00
/* j/e/rq.c
**
*/
#include "all.h"
#include <softfloat.h>
2015-07-29 19:53:45 +03:00
#define QUADNAN 0x7fff800000000000
union quad {
float128_t* q;
c3_w* c;
};
/* functions
*/
static inline c3_t
_nan_test(float128_t* a)
{
return !f128M_eq(a, a);
}
static inline void
_nan_unify(float128_t* a)
{
2015-07-29 23:43:55 +03:00
if ( _nan_test(a) )
2015-07-29 19:53:45 +03:00
{
2015-07-29 23:54:33 +03:00
*( (c3_d*)a) = 0;
2015-07-29 19:53:45 +03:00
*(((c3_d*)a)+1) = QUADNAN;
}
}
2015-08-04 06:00:09 +03:00
static inline void
_set_rounding(c3_w a)
{
switch ( a )
{
default:
u3m_bail(c3__fail);
break;
case c3__n:
softfloat_roundingMode = softfloat_round_near_even;
break;
case c3__z:
softfloat_roundingMode = softfloat_round_minMag;
break;
case c3__u:
softfloat_roundingMode = softfloat_round_max;
break;
case c3__d:
softfloat_roundingMode = softfloat_round_min;
break;
}
}
2015-07-29 19:53:45 +03:00
/* add
*/
u3_noun
2015-08-28 17:14:14 +03:00
u3qeq_add(u3_atom a,
u3_atom b,
u3_atom r)
2015-07-29 19:53:45 +03:00
{
union quad c, d, e;
2015-08-04 06:00:09 +03:00
_set_rounding(r);
2015-07-29 23:54:33 +03:00
c.c = alloca(16);
d.c = alloca(16);
e.c = alloca(16);
2015-07-29 19:53:45 +03:00
u3r_words(0, 4, c.c, a);
u3r_words(0, 4, d.c, b);
f128M_add(c.q, d.q, e.q);
_nan_unify(e.q);
u3_atom f = u3i_words(4, e.c);
return f;
}
u3_noun
u3weq_add(u3_noun cor)
{
u3_noun a, b;
if ( c3n == u3r_mean(cor, u3x_sam_2, &a, u3x_sam_3, &b, 0) ||
c3n == u3ud(a) ||
c3n == u3ud(b) )
{
return u3m_bail(c3__exit);
}
else {
2015-08-04 06:00:09 +03:00
return u3qeq_add(a, b, u3x_at(30, cor));
2015-07-29 19:53:45 +03:00
}
}
/* sub
*/
u3_noun
2015-08-28 17:14:14 +03:00
u3qeq_sub(u3_atom a,
u3_atom b,
u3_atom r)
2015-07-29 19:53:45 +03:00
{
union quad c, d, e;
2015-08-04 06:00:09 +03:00
_set_rounding(r);
2015-07-29 23:54:33 +03:00
c.c = alloca(16);
d.c = alloca(16);
e.c = alloca(16);
2015-07-29 19:53:45 +03:00
u3r_words(0, 4, c.c, a);
u3r_words(0, 4, d.c, b);
f128M_sub(c.q, d.q, e.q);
_nan_unify(e.q);
u3_atom f = u3i_words(4, e.c);
return f;
}
u3_noun
u3weq_sub(u3_noun cor)
{
u3_noun a, b;
if ( c3n == u3r_mean(cor, u3x_sam_2, &a, u3x_sam_3, &b, 0) ||
c3n == u3ud(a) ||
c3n == u3ud(b) )
{
return u3m_bail(c3__exit);
}
else {
2015-08-04 06:00:09 +03:00
return u3qeq_sub(a, b, u3x_at(30, cor));
2015-07-29 19:53:45 +03:00
}
}
/* mul
*/
u3_noun
2015-08-28 17:14:14 +03:00
u3qeq_mul(u3_atom a,
u3_atom b,
u3_atom r)
2015-07-29 19:53:45 +03:00
{
union quad c, d, e;
2015-08-04 06:00:09 +03:00
_set_rounding(r);
2015-07-29 23:54:33 +03:00
c.c = alloca(16);
d.c = alloca(16);
e.c = alloca(16);
2015-07-29 19:53:45 +03:00
u3r_words(0, 4, c.c, a);
u3r_words(0, 4, d.c, b);
f128M_mul(c.q, d.q, e.q);
_nan_unify(e.q);
u3_atom f = u3i_words(4, e.c);
return f;
}
u3_noun
u3weq_mul(u3_noun cor)
{
u3_noun a, b;
if ( c3n == u3r_mean(cor, u3x_sam_2, &a, u3x_sam_3, &b, 0) ||
c3n == u3ud(a) ||
c3n == u3ud(b) )
{
return u3m_bail(c3__exit);
}
else {
2015-08-04 06:00:09 +03:00
return u3qeq_mul(a, b, u3x_at(30, cor));
2015-07-29 19:53:45 +03:00
}
}
/* div
*/
u3_noun
2015-08-28 17:14:14 +03:00
u3qeq_div(u3_atom a,
u3_atom b,
u3_atom r)
2015-07-29 19:53:45 +03:00
{
union quad c, d, e;
2015-08-04 06:00:09 +03:00
_set_rounding(r);
2015-07-29 23:54:33 +03:00
c.c = alloca(16);
d.c = alloca(16);
e.c = alloca(16);
2015-07-29 19:53:45 +03:00
u3r_words(0, 4, c.c, a);
u3r_words(0, 4, d.c, b);
f128M_div(c.q, d.q, e.q);
_nan_unify(e.q);
u3_atom f = u3i_words(4, e.c);
return f;
}
u3_noun
u3weq_div(u3_noun cor)
{
u3_noun a, b;
if ( c3n == u3r_mean(cor, u3x_sam_2, &a, u3x_sam_3, &b, 0) ||
c3n == u3ud(a) ||
c3n == u3ud(b) )
{
return u3m_bail(c3__exit);
}
else {
2015-08-04 06:00:09 +03:00
return u3qeq_div(a, b, u3x_at(30, cor));
2015-07-29 19:53:45 +03:00
}
}
/* sqt
*/
u3_noun
2015-08-28 17:14:14 +03:00
u3qeq_sqt(u3_atom a,
u3_atom r)
2015-07-29 19:53:45 +03:00
{
union quad c, d;
2015-08-04 06:00:09 +03:00
_set_rounding(r);
2015-07-29 23:54:33 +03:00
c.c = alloca(16);
d.c = alloca(16);
2015-07-29 19:53:45 +03:00
u3r_words(0, 4, c.c, a);
f128M_sqrt(c.q, d.q);
_nan_unify(d.q);
u3_atom e = u3i_words(4, d.c);
return e;
}
u3_noun
u3weq_sqt(u3_noun cor)
{
u3_noun a;
if ( c3n == (a = u3r_at(u3x_sam, cor)) ||
c3n == u3ud(a) )
{
return u3m_bail(c3__exit);
}
else {
2015-08-04 06:00:09 +03:00
return u3qeq_sqt(a, u3x_at(30, cor));
2015-07-29 19:53:45 +03:00
}
}
/* fma
*/
u3_noun
2015-08-28 17:14:14 +03:00
u3qeq_fma(u3_atom a,
u3_atom b,
u3_atom c,
u3_atom r)
2015-07-29 19:53:45 +03:00
{
union quad d, e, f, g;
2015-08-04 06:00:09 +03:00
_set_rounding(r);
2015-07-29 23:54:33 +03:00
d.c = alloca(16);
e.c = alloca(16);
f.c = alloca(16);
g.c = alloca(16);
2015-07-29 19:53:45 +03:00
u3r_words(0, 4, d.c, a);
u3r_words(0, 4, e.c, b);
u3r_words(0, 4, f.c, c);
f128M_mulAdd(d.q, e.q, f.q, g.q);
_nan_unify(g.q);
u3_atom h = u3i_words(4, g.c);
return h;
}
u3_noun
u3weq_fma(u3_noun cor)
{
u3_noun a, b, c;
if ( c3n == u3r_mean(cor, u3x_sam_2, &a, u3x_sam_6, &b, u3x_sam_7, &c, 0) ||
c3n == u3ud(a) ||
c3n == u3ud(b) ||
c3n == u3ud(c) )
{
return u3m_bail(c3__exit);
}
else {
2015-08-04 06:00:09 +03:00
return u3qeq_fma(a, b, c, u3x_at(30, cor));
2015-07-29 19:53:45 +03:00
}
}
/* lth
*/
u3_noun
2015-08-28 17:14:14 +03:00
u3qeq_lth(u3_atom a,
u3_atom b)
2015-07-29 19:53:45 +03:00
{
union quad c, d;
2015-07-29 23:54:33 +03:00
c.c = alloca(16);
d.c = alloca(16);
2015-07-29 19:53:45 +03:00
u3r_words(0, 4, c.c, a);
u3r_words(0, 4, d.c, b);
c3_o e = __(f128M_lt(c.q, d.q));
return e;
}
u3_noun
u3weq_lth(u3_noun cor)
{
u3_noun a, b;
if ( c3n == u3r_mean(cor, u3x_sam_2, &a, u3x_sam_3, &b, 0) ||
c3n == u3ud(a) ||
c3n == u3ud(b) )
{
return u3m_bail(c3__exit);
}
else {
return u3qeq_lth(a, b);
}
}
/* lte
*/
u3_noun
2015-08-28 17:14:14 +03:00
u3qeq_lte(u3_atom a,
u3_atom b)
2015-07-29 19:53:45 +03:00
{
union quad c, d;
2015-07-29 23:54:33 +03:00
c.c = alloca(16);
d.c = alloca(16);
2015-07-29 19:53:45 +03:00
u3r_words(0, 4, c.c, a);
u3r_words(0, 4, d.c, b);
c3_o e = __(f128M_le(c.q, d.q));
return e;
}
u3_noun
u3weq_lte(u3_noun cor)
{
u3_noun a, b;
if ( c3n == u3r_mean(cor, u3x_sam_2, &a, u3x_sam_3, &b, 0) ||
c3n == u3ud(a) ||
c3n == u3ud(b) )
{
return u3m_bail(c3__exit);
}
else {
return u3qeq_lte(a, b);
}
}
/* equ
*/
u3_noun
2015-08-28 17:14:14 +03:00
u3qeq_equ(u3_atom a,
u3_atom b)
2015-07-29 19:53:45 +03:00
{
union quad c, d;
2015-07-29 23:54:33 +03:00
c.c = alloca(16);
d.c = alloca(16);
2015-07-29 19:53:45 +03:00
u3r_words(0, 4, c.c, a);
u3r_words(0, 4, d.c, b);
c3_o e = __(f128M_eq(c.q, d.q));
return e;
}
u3_noun
u3weq_equ(u3_noun cor)
{
u3_noun a, b;
if ( c3n == u3r_mean(cor, u3x_sam_2, &a, u3x_sam_3, &b, 0) ||
c3n == u3ud(a) ||
c3n == u3ud(b) )
{
return u3m_bail(c3__exit);
}
else {
return u3qeq_equ(a, b);
}
}
/* gte
*/
u3_noun
2015-08-28 17:14:14 +03:00
u3qeq_gte(u3_atom a,
u3_atom b)
2015-07-29 19:53:45 +03:00
{
union quad c, d;
2015-07-29 23:54:33 +03:00
c.c = alloca(16);
d.c = alloca(16);
2015-07-29 19:53:45 +03:00
u3r_words(0, 4, c.c, a);
u3r_words(0, 4, d.c, b);
c3_o e = __(f128M_le(d.q, c.q));
return e;
}
u3_noun
u3weq_gte(u3_noun cor)
{
u3_noun a, b;
if ( c3n == u3r_mean(cor, u3x_sam_2, &a, u3x_sam_3, &b, 0) ||
c3n == u3ud(a) ||
c3n == u3ud(b) )
{
return u3m_bail(c3__exit);
}
else {
return u3qeq_gte(a, b);
}
}
/* gth
*/
u3_noun
2015-08-28 17:14:14 +03:00
u3qeq_gth(u3_atom a,
u3_atom b)
2015-07-29 19:53:45 +03:00
{
union quad c, d;
2015-07-29 23:54:33 +03:00
c.c = alloca(16);
d.c = alloca(16);
2015-07-29 19:53:45 +03:00
u3r_words(0, 4, c.c, a);
u3r_words(0, 4, d.c, b);
c3_o e = __(f128M_lt(d.q, c.q));
return e;
}
u3_noun
u3weq_gth(u3_noun cor)
{
u3_noun a, b;
if ( c3n == u3r_mean(cor, u3x_sam_2, &a, u3x_sam_3, &b, 0) ||
c3n == u3ud(a) ||
c3n == u3ud(b) )
{
return u3m_bail(c3__exit);
}
else {
return u3qeq_gth(a, b);
}
}