urbit/jets/e/rd.c

374 lines
6.1 KiB
C
Raw Normal View History

2015-07-29 18:59:21 +03:00
/* j/e/rd.c
**
*/
#include "all.h"
2015-07-29 06:56:02 +03:00
#include "softfloat.h"
2014-09-04 07:10:43 +04:00
2015-07-29 06:56:02 +03:00
#define DOUBNAN 0x7ff8000000000000
2015-07-29 06:56:02 +03:00
union doub {
float64_t d;
c3_d c;
};
/* functions
*/
2015-07-29 06:56:02 +03:00
static inline c3_t
_nan_test(float64_t a)
{
return !f64_eq(a, a);
}
static inline float64_t
_nan_unify(float64_t a)
{
2015-07-29 23:43:55 +03:00
if ( _nan_test(a) )
2015-07-29 06:56:02 +03:00
{
*(c3_d*)(&a) = DOUBNAN;
}
return a;
}
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 06:56:02 +03:00
/* add
2014-06-04 23:16:15 +04:00
*/
2014-09-06 00:13:24 +04:00
u3_noun
2015-08-04 06:00:09 +03:00
u3qer_add(u3_atom a, u3_atom b, u3_atom r)
2014-06-04 23:16:15 +04:00
{
2015-07-29 06:56:02 +03:00
union doub c, d, e;
2015-08-04 06:00:09 +03:00
_set_rounding(r);
2015-07-29 06:56:02 +03:00
c.c = u3r_chub(0, a);
d.c = u3r_chub(0, b);
e.d = _nan_unify(f64_add(c.d, d.d));
2014-06-05 06:46:12 +04:00
2015-07-29 06:56:02 +03:00
return u3i_chubs(1, &e.c);
2014-06-04 23:16:15 +04:00
}
2014-09-06 00:13:24 +04:00
u3_noun
2015-07-29 06:56:02 +03:00
u3wer_add(u3_noun cor)
2014-06-04 23:16:15 +04:00
{
2015-07-29 06:56:02 +03:00
u3_noun a, b;
2014-06-04 23:16:15 +04:00
2015-07-29 06:56:02 +03:00
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 u3qer_add(a, b, u3x_at(30, cor));
2015-07-29 06:56:02 +03:00
}
}
/* sub
*/
u3_noun
2015-08-04 06:00:09 +03:00
u3qer_sub(u3_atom a, u3_atom b, u3_atom r)
2015-07-29 06:56:02 +03:00
{
union doub c, d, e;
2015-08-04 06:00:09 +03:00
_set_rounding(r);
2015-07-29 06:56:02 +03:00
c.c = u3r_chub(0, a);
d.c = u3r_chub(0, b);
e.d = _nan_unify(f64_sub(c.d, d.d));
return u3i_chubs(1, &e.c);
}
u3_noun
u3wer_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) )
{
2014-11-06 03:20:01 +03:00
return u3m_bail(c3__exit);
2014-06-04 23:16:15 +04:00
}
else {
2015-08-04 06:00:09 +03:00
return u3qer_sub(a, b, u3x_at(30, cor));
2014-06-04 23:16:15 +04:00
}
}
/* mul
*/
2014-09-06 00:13:24 +04:00
u3_noun
2015-08-04 06:00:09 +03:00
u3qer_mul(u3_atom a, u3_atom b, u3_atom r)
{
union doub c, d, e;
2015-08-04 06:00:09 +03:00
_set_rounding(r);
2014-11-06 03:20:01 +03:00
c.c = u3r_chub(0, a);
d.c = u3r_chub(0, b);
2015-07-29 06:56:02 +03:00
e.d = _nan_unify(f64_mul(c.d, d.d));
2014-06-05 06:46:12 +04:00
2014-11-06 03:20:01 +03:00
return u3i_chubs(1, &e.c);
}
2014-09-06 00:13:24 +04:00
u3_noun
2014-11-06 22:13:57 +03:00
u3wer_mul(u3_noun cor)
{
2014-09-06 00:13:24 +04:00
u3_noun a, b;
2014-11-18 00:56:51 +03:00
if ( c3n == u3r_mean(cor, u3x_sam_2, &a, u3x_sam_3, &b, 0) ||
2014-11-05 04:18:47 +03:00
c3n == u3ud(a) ||
c3n == u3ud(b) )
{
2014-11-06 03:20:01 +03:00
return u3m_bail(c3__exit);
}
else {
2015-08-04 06:00:09 +03:00
return u3qer_mul(a, b, u3x_at(30, cor));
}
}
/* div
*/
2014-09-06 00:13:24 +04:00
u3_noun
2015-08-04 06:00:09 +03:00
u3qer_div(u3_atom a, u3_atom b, u3_atom r)
{
union doub c, d, e;
2015-08-04 06:00:09 +03:00
_set_rounding(r);
2014-11-06 03:20:01 +03:00
c.c = u3r_chub(0, a);
d.c = u3r_chub(0, b);
2015-07-29 06:56:02 +03:00
e.d = _nan_unify(f64_div(c.d, d.d));
2014-06-05 06:46:12 +04:00
2014-11-06 03:20:01 +03:00
return u3i_chubs(1, &e.c);
}
2014-09-06 00:13:24 +04:00
u3_noun
2014-11-06 22:13:57 +03:00
u3wer_div(u3_noun cor)
{
2014-09-06 00:13:24 +04:00
u3_noun a, b;
2014-11-18 00:56:51 +03:00
if ( c3n == u3r_mean(cor, u3x_sam_2, &a, u3x_sam_3, &b, 0) ||
2014-11-05 04:18:47 +03:00
c3n == u3ud(a) ||
c3n == u3ud(b) )
{
2014-11-06 03:20:01 +03:00
return u3m_bail(c3__exit);
}
else {
2015-08-04 06:00:09 +03:00
return u3qer_div(a, b, u3x_at(30, cor));
}
}
2015-07-29 06:56:02 +03:00
/* sqt
*/
2014-09-06 00:13:24 +04:00
u3_noun
2015-08-04 06:00:09 +03:00
u3qer_sqt(u3_atom a, u3_atom r)
{
2015-07-29 06:56:02 +03:00
union doub c, d;
2015-08-04 06:00:09 +03:00
_set_rounding(r);
2014-11-06 03:20:01 +03:00
c.c = u3r_chub(0, a);
2015-07-29 06:56:02 +03:00
d.d = _nan_unify(f64_sqrt(c.d));
2014-06-05 06:46:12 +04:00
2015-07-29 06:56:02 +03:00
return u3i_chubs(1, &d.c);
}
2014-09-06 00:13:24 +04:00
u3_noun
2015-07-29 06:56:02 +03:00
u3wer_sqt(u3_noun cor)
{
2015-07-29 06:56:02 +03:00
u3_noun a;
2015-07-29 06:56:02 +03:00
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 u3qer_sqt(a, u3x_at(30, cor));
2015-07-29 06:56:02 +03:00
}
}
/* fma
*/
u3_noun
2015-08-04 06:00:09 +03:00
u3qer_fma(u3_atom a, u3_atom b, u3_atom c, u3_atom r)
2015-07-29 06:56:02 +03:00
{
union doub d, e, f, g;
2015-08-04 06:00:09 +03:00
_set_rounding(r);
2015-07-29 06:56:02 +03:00
d.c = u3r_chub(0, a);
e.c = u3r_chub(0, b);
f.c = u3r_chub(0, c);
g.d = _nan_unify(f64_mulAdd(d.d, e.d, f.d));
return u3i_chubs(1, &g.c);
}
u3_noun
u3wer_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) ||
2014-11-05 04:18:47 +03:00
c3n == u3ud(a) ||
2015-07-29 06:56:02 +03:00
c3n == u3ud(b) ||
c3n == u3ud(c) )
{
2014-11-06 03:20:01 +03:00
return u3m_bail(c3__exit);
}
else {
2015-08-04 06:00:09 +03:00
return u3qer_fma(a, b, c, u3x_at(30, cor));
}
}
2015-07-29 06:56:02 +03:00
/* lth
2014-06-02 23:13:58 +04:00
*/
2014-09-06 00:13:24 +04:00
u3_noun
2015-07-29 06:56:02 +03:00
u3qer_lth(u3_atom a, u3_atom b)
2014-06-02 23:13:58 +04:00
{
2015-07-29 06:56:02 +03:00
union doub c, d;
2014-11-06 03:20:01 +03:00
c.c = u3r_chub(0, a);
d.c = u3r_chub(0, b);
2014-06-05 06:46:12 +04:00
2015-07-29 06:56:02 +03:00
return __(f64_lt(c.d, d.d));
2014-06-02 23:13:58 +04:00
}
2014-09-06 00:13:24 +04:00
u3_noun
2015-07-29 06:56:02 +03:00
u3wer_lth(u3_noun cor)
2014-06-02 23:13:58 +04:00
{
2014-09-06 00:13:24 +04:00
u3_noun a, b;
2014-06-02 23:13:58 +04:00
2014-11-18 00:56:51 +03:00
if ( c3n == u3r_mean(cor, u3x_sam_2, &a, u3x_sam_3, &b, 0) ||
2014-11-05 04:18:47 +03:00
c3n == u3ud(a) ||
c3n == u3ud(b) )
2014-06-02 23:13:58 +04:00
{
2014-11-06 03:20:01 +03:00
return u3m_bail(c3__exit);
2014-06-02 23:13:58 +04:00
}
else {
2015-07-29 06:56:02 +03:00
return u3qer_lth(a, b);
2014-06-02 23:13:58 +04:00
}
}
/* lte
*/
2014-09-06 00:13:24 +04:00
u3_noun
2014-11-06 22:13:57 +03:00
u3qer_lte(u3_atom a, u3_atom b)
{
union doub c, d;
2014-11-06 03:20:01 +03:00
c.c = u3r_chub(0, a);
d.c = u3r_chub(0, b);
2014-06-05 06:46:12 +04:00
2015-07-29 06:56:02 +03:00
return __(f64_le(c.d, d.d));
}
2014-09-06 00:13:24 +04:00
u3_noun
2014-11-06 22:13:57 +03:00
u3wer_lte(u3_noun cor)
{
2014-09-06 00:13:24 +04:00
u3_noun a, b;
2014-11-18 00:56:51 +03:00
if ( c3n == u3r_mean(cor, u3x_sam_2, &a, u3x_sam_3, &b, 0) ||
2014-11-05 04:18:47 +03:00
c3n == u3ud(a) ||
c3n == u3ud(b) )
{
2014-11-06 03:20:01 +03:00
return u3m_bail(c3__exit);
}
else {
2014-11-06 22:13:57 +03:00
return u3qer_lte(a, b);
}
}
2015-07-29 06:56:02 +03:00
/* equ
*/
2014-09-06 00:13:24 +04:00
u3_noun
2015-07-29 06:56:02 +03:00
u3qer_equ(u3_atom a, u3_atom b)
{
union doub c, d;
2014-11-06 03:20:01 +03:00
c.c = u3r_chub(0, a);
d.c = u3r_chub(0, b);
2014-06-05 06:46:12 +04:00
2015-07-29 06:56:02 +03:00
return __(f64_eq(c.d, d.d));
}
2014-09-06 00:13:24 +04:00
u3_noun
2015-07-29 06:56:02 +03:00
u3wer_equ(u3_noun cor)
{
2014-09-06 00:13:24 +04:00
u3_noun a, b;
2014-11-18 00:56:51 +03:00
if ( c3n == u3r_mean(cor, u3x_sam_2, &a, u3x_sam_3, &b, 0) ||
2014-11-05 04:18:47 +03:00
c3n == u3ud(a) ||
c3n == u3ud(b) )
{
2014-11-06 03:20:01 +03:00
return u3m_bail(c3__exit);
}
else {
2015-07-29 06:56:02 +03:00
return u3qer_equ(a, b);
}
}
/* gte
*/
2014-09-06 00:13:24 +04:00
u3_noun
2014-11-06 22:13:57 +03:00
u3qer_gte(u3_atom a, u3_atom b)
{
union doub c, d;
2014-11-06 03:20:01 +03:00
c.c = u3r_chub(0, a);
d.c = u3r_chub(0, b);
2014-06-05 06:46:12 +04:00
2015-07-29 06:56:02 +03:00
return __(f64_le(d.d, c.d));
}
2014-09-06 00:13:24 +04:00
u3_noun
2014-11-06 22:13:57 +03:00
u3wer_gte(u3_noun cor)
{
2014-09-06 00:13:24 +04:00
u3_noun a, b;
2014-11-18 00:56:51 +03:00
if ( c3n == u3r_mean(cor, u3x_sam_2, &a, u3x_sam_3, &b, 0) ||
2014-11-05 04:18:47 +03:00
c3n == u3ud(a) ||
c3n == u3ud(b) )
{
2014-11-06 03:20:01 +03:00
return u3m_bail(c3__exit);
}
else {
2014-11-06 22:13:57 +03:00
return u3qer_gte(a, b);
}
}
/* gth
*/
2014-09-06 00:13:24 +04:00
u3_noun
2014-11-06 22:13:57 +03:00
u3qer_gth(u3_atom a, u3_atom b)
{
union doub c, d;
2014-11-06 03:20:01 +03:00
c.c = u3r_chub(0, a);
d.c = u3r_chub(0, b);
2014-06-05 06:46:12 +04:00
2015-07-29 06:56:02 +03:00
return __(f64_lt(d.d, c.d));
}
2014-09-06 00:13:24 +04:00
u3_noun
2014-11-06 22:13:57 +03:00
u3wer_gth(u3_noun cor)
{
2014-09-06 00:13:24 +04:00
u3_noun a, b;
2014-11-18 00:56:51 +03:00
if ( c3n == u3r_mean(cor, u3x_sam_2, &a, u3x_sam_3, &b, 0) ||
2014-11-05 04:18:47 +03:00
c3n == u3ud(a) ||
c3n == u3ud(b) )
{
2014-11-06 03:20:01 +03:00
return u3m_bail(c3__exit);
}
else {
2014-11-06 22:13:57 +03:00
return u3qer_gth(a, b);
}
}