Skip to content

Commit

Permalink
u3: inline cell deconstruction (#536)
Browse files Browse the repository at this point in the history
This PR converts `u3r_cell()`/trel/&c inline functions and
`u3x_cell()`/trel/&c to macros. This is mostly just a straightforward
optimization of frequently used deconstructors, as prelude to optimizing
all the jet-interface (`u3w*`) functions.

In the case of `u3x_atom()` the previously-defined macro repeated its
argument, which is a bad pattern as that "argument" is often a call to
another function. It has now been switched to an inline function.
  • Loading branch information
joemfb authored Oct 13, 2023
2 parents 9bdc1af + 7149079 commit 770d094
Show file tree
Hide file tree
Showing 4 changed files with 198 additions and 335 deletions.
134 changes: 29 additions & 105 deletions pkg/noun/retrieve.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,35 @@
#include "trace.h"
#include "xtract.h"


// declarations of inline functions
//
c3_o
u3r_cell(u3_noun a, u3_noun* b, u3_noun* c);
c3_o
u3r_trel(u3_noun a, u3_noun* b, u3_noun* c, u3_noun* d);
c3_o
u3r_qual(u3_noun a,
u3_noun* b,
u3_noun* c,
u3_noun* d,
u3_noun* e);
c3_o
u3r_quil(u3_noun a,
u3_noun* b,
u3_noun* c,
u3_noun* d,
u3_noun* e,
u3_noun* f);
c3_o
u3r_hext(u3_noun a,
u3_noun* b,
u3_noun* c,
u3_noun* d,
u3_noun* e,
u3_noun* f,
u3_noun* g);

/* _frag_word(): fast fragment/branch prediction for top word.
*/
static u3_weak
Expand Down Expand Up @@ -833,27 +862,6 @@ u3r_bite(u3_noun bite, u3_atom* bloq, u3_atom *step)
}
}

/* u3r_cell():
**
** Factor (a) as a cell (b c).
*/
c3_o
u3r_cell(u3_noun a,
u3_noun* b,
u3_noun* c)
{
u3_assert(u3_none != a);

if ( _(u3a_is_atom(a)) ) {
return c3n;
}
else {
if ( b ) *b = u3a_h(a);
if ( c ) *c = u3a_t(a);
return c3y;
}
}

/* u3r_p():
**
** & [0] if [a] is of the form [b *c].
Expand Down Expand Up @@ -937,90 +945,6 @@ u3r_pqrs(u3_noun a,
else return c3n;
}

/* u3r_trel():
**
** Factor (a) as a trel (b c d).
*/
c3_o
u3r_trel(u3_noun a,
u3_noun *b,
u3_noun *c,
u3_noun *d)
{
u3_noun guf;

if ( (c3y == u3r_cell(a, b, &guf)) &&
(c3y == u3r_cell(guf, c, d)) ) {
return c3y;
}
else {
return c3n;
}
}

/* u3r_qual():
**
** Factor (a) as a qual (b c d e).
*/
c3_o
u3r_qual(u3_noun a,
u3_noun* b,
u3_noun* c,
u3_noun* d,
u3_noun* e)
{
u3_noun guf;

if ( (c3y == u3r_cell(a, b, &guf)) &&
(c3y == u3r_trel(guf, c, d, e)) ) {
return c3y;
}
else return c3n;
}

/* u3r_quil():
**
** Factor (a) as a quil (b c d e f).
*/
c3_o
u3r_quil(u3_noun a,
u3_noun* b,
u3_noun* c,
u3_noun* d,
u3_noun* e,
u3_noun* f)
{
u3_noun guf;

if ( (c3y == u3r_cell(a, b, &guf)) &&
(c3y == u3r_qual(guf, c, d, e, f)) ) {
return c3y;
}
else return c3n;
}

/* u3r_hext():
**
** Factor (a) as a hext (b c d e f g)
*/
c3_o
u3r_hext(u3_noun a,
u3_noun* b,
u3_noun* c,
u3_noun* d,
u3_noun* e,
u3_noun* f,
u3_noun* g)
{
u3_noun guf;

if ( (c3y == u3r_cell(a, b, &guf)) &&
(c3y == u3r_quil(guf, c, d, e, f, g)) ) {
return c3y;
}
else return c3n;
}

/* u3r_met():
**
** Return the size of (b) in bits, rounded up to
Expand Down
151 changes: 96 additions & 55 deletions pkg/noun/retrieve.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,107 @@
#define U3_RETRIEVE_H

#include "c3.h"
#include "allocate.h"
#include "error.h"
#include "gmp.h"
#include "types.h"

/** u3r_*: read without ever crashing.
**/

/* u3r_cell(): factor (a) as a cell (b c).
*/
inline c3_o
u3r_cell(u3_noun a, u3_noun* b, u3_noun* c)
{
u3a_cell* cel_u;

u3_assert(u3_none != a);

if ( c3y == u3a_is_cell(a) ) {
cel_u = u3a_to_ptr(a);
if ( b ) *b = cel_u->hed;
if ( c ) *c = cel_u->tel;
return c3y;
}
else {
return c3n;
}
}

/* u3r_trel(): factor (a) as a trel (b c d).
*/
inline c3_o
u3r_trel(u3_noun a, u3_noun *b, u3_noun *c, u3_noun *d)
{
u3_noun guf;

if ( (c3y == u3r_cell(a, b, &guf)) &&
(c3y == u3r_cell(guf, c, d)) ) {
return c3y;
}
else {
return c3n;
}
}

/* u3r_qual(): factor (a) as a qual (b c d e).
*/
inline c3_o
u3r_qual(u3_noun a,
u3_noun* b,
u3_noun* c,
u3_noun* d,
u3_noun* e)
{
u3_noun guf;

if ( (c3y == u3r_cell(a, b, &guf)) &&
(c3y == u3r_trel(guf, c, d, e)) ) {
return c3y;
}
else return c3n;
}

/* u3r_quil(): factor (a) as a quil (b c d e f).
*/
inline c3_o
u3r_quil(u3_noun a,
u3_noun* b,
u3_noun* c,
u3_noun* d,
u3_noun* e,
u3_noun* f)
{
u3_noun guf;

if ( (c3y == u3r_cell(a, b, &guf)) &&
(c3y == u3r_qual(guf, c, d, e, f)) ) {
return c3y;
}
else return c3n;
}

/* u3r_hext(): factor (a) as a hext (b c d e f g)
*/
inline c3_o
u3r_hext(u3_noun a,
u3_noun* b,
u3_noun* c,
u3_noun* d,
u3_noun* e,
u3_noun* f,
u3_noun* g)
{
u3_noun guf;

if ( (c3y == u3r_cell(a, b, &guf)) &&
(c3y == u3r_quil(guf, c, d, e, f, g)) ) {
return c3y;
}
else return c3n;
}

/* u3r_at(): fragment `a` of `b`, or u3_none.
*/
u3_weak
Expand Down Expand Up @@ -183,61 +279,6 @@
c3_o
u3r_bite(u3_noun bite, u3_atom* bloq, u3_atom *step);

/* u3r_cell():
**
** Divide `a` as a cell `[b c]`.
*/
c3_o
u3r_cell(u3_noun a,
u3_noun* b,
u3_noun* c);

/* u3r_trel():
**
** Divide `a` as a trel `[b c d]`.
*/
c3_o
u3r_trel(u3_noun a,
u3_noun* b,
u3_noun* c,
u3_noun* d);

/* u3r_qual():
**
** Divide (a) as a qual [b c d e].
*/
c3_o
u3r_qual(u3_noun a,
u3_noun* b,
u3_noun* c,
u3_noun* d,
u3_noun* e);

/* u3r_quil():
**
** Divide (a) as a quil [b c d e f].
*/
c3_o
u3r_quil(u3_noun a,
u3_noun* b,
u3_noun* c,
u3_noun* d,
u3_noun* e,
u3_noun* f);

/* u3r_hext():
**
** Divide (a) as a hext [b c d e f g].
*/
c3_o
u3r_hext(u3_noun a,
u3_noun* b,
u3_noun* c,
u3_noun* d,
u3_noun* e,
u3_noun* f,
u3_noun* g);

/* u3r_p():
**
** & [0] if [a] is of the form [b *c].
Expand Down
Loading

0 comments on commit 770d094

Please sign in to comment.