Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add crc32 jet #653

Merged
merged 8 commits into from
Jun 26, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pkg/noun/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ vere_library(
"@sigsegv",
"@softfloat",
"@urcrypt",
"@zlib",
] + select({
"@platforms//os:macos": ["//pkg/noun/platform/darwin"],
"@platforms//os:linux": ["//pkg/noun/platform/linux"],
Expand Down
56 changes: 56 additions & 0 deletions pkg/noun/jets/e/crc32.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/// @file

#include <stdio.h>
#include <allocate.h>
#include "zlib.h"

#include "jets/w.h"

#include "noun.h"

u3_noun
u3qe_crc32(u3_noun input_octs)
{
u3_atom head = u3h(input_octs);
u3_atom tail = u3t(input_octs);
c3_w tel_w = u3r_met(3, tail);
c3_w hed_w = u3r_met(3, head);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hed_w should be the value of head as a c3_w, not its byte-width.

c3_w hed_w;
if ( c3n == u3r_safe_word(head, &hed_w) ) {
  return u3m_bail(c3__fail);
}

c3_y* input;

if (c3y == u3a_is_cat(tail)) {
input = &tail;
}
else {
u3a_atom* vat_u = u3a_to_ptr(tail);
input = (c3_y*)vat_u->buf_w;
}

if ( tel_w > hed_w ) {
return u3m_error("subtract-underflow");
}

c3_w led_w = hed_w - tel_w;
c3_w crc_w = 0;

crc_w = crc32(crc_w, input, tel_w);

while ( led_w > 0 ) {
c3_y byt_y = 0;
crc_w = crc32(crc_w, &byt_y, 1);
led_w--;
}

return u3i_word(crc_w);
}

u3_noun
u3we_crc32(u3_noun cor)
{
u3_noun a = u3r_at(u3x_sam, cor);

if ( (u3du(a) == c3y) && (u3ud(u3h(a)) == c3y) && (u3ud(u3t(a)) == c3y) ) {
return u3qe_crc32(a);
} else {
return u3m_bail(c3__exit);
}
}
7 changes: 7 additions & 0 deletions pkg/noun/jets/tree.c
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,12 @@ static c3_c* _140_hex_lune_ha[] = {
0
};


static u3j_harm _137_hex__crc32_a[] = {{".2", u3we_crc32}, {}};

static u3j_core _137_hex__crc_d[] = {{"crc32", 7, _137_hex__crc32_a, 0, no_hashes }, {}};


static u3j_harm _140_hex_coed__ed_puck_a[] = {{".2", u3wee_puck}, {}};
static c3_c* _140_hex_coed__ed_puck_ha[] = {
"1bc694675842345c50b0e20a2193bb5bcbb42f163fc832431a3d1822a81e4c98",
Expand Down Expand Up @@ -2346,6 +2352,7 @@ static u3j_core _138_hex_d[] =
{ "leer", 63, _140_hex_leer_a, 0, no_hashes },
{ "loss", 63, _140_hex_loss_a, 0, no_hashes },
{ "lune", 127, _140_hex_lune_a, 0, no_hashes },
{ "crc", 31, 0, _137_hex__crc_d, no_hashes },

{ "coed", 63, 0, _140_hex_coed_d, no_hashes },
{ "aes", 31, 0, _140_hex_aes_d, no_hashes },
Expand Down
2 changes: 2 additions & 0 deletions pkg/noun/jets/w.h
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,8 @@
u3_noun u3wes_gte(u3_noun);
u3_noun u3wes_gth(u3_noun);

u3_noun u3we_crc32(u3_noun);

/** Tier 6.
**/
u3_noun u3wf_bull(u3_noun);
Expand Down
Loading