Merge pull request #2069 from AleoHQ/indent-examples

[examples] improve indentation
This commit is contained in:
Collin Chin 2022-09-17 16:12:31 +02:00 committed by GitHub
commit 491bc2271d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 154 additions and 147 deletions

View File

@ -5,10 +5,10 @@ function main(public x: u32) -> u8 {
x = !x & x.sub_wrapped(1u32);
let n: u8 = 0u8;
for i:u8 in 0u8..32u8 {
if x != 0u32 {
n += 1u8;
x = x >> 1u8;
}
if x != 0u32 {
n += 1u8;
x = x >> 1u8;
}
}
return n;
}

View File

@ -2,9 +2,9 @@
// From Hacker's Delight 2nd ed. figure 5-25
@program
function main(public x: u32) -> u8 {
// 0x0450FBAF = 72416175
x = (x & 0u32.sub_wrapped(x)).mul_wrapped(72416175u32);
return sealsTableLookup(x >> 26u8);
// 0x0450FBAF = 72416175
x = (x & 0u32.sub_wrapped(x)).mul_wrapped(72416175u32);
return sealsTableLookup(x >> 26u8);
}
// Right now we do not have any structure that allows
@ -16,76 +16,76 @@ function main(public x: u32) -> u8 {
function sealsTableLookup(i: u32) -> u8 {
if i == 0u32 {return 32u8;} else
if i == 1u32 {return 0u8;} else
if i == 2u32 {return 1u8;} else
if i == 3u32 {return 12u8;} else
if i == 4u32 {return 2u8;} else
if i == 5u32 {return 6u8;} else
if i == 6u32 {return 0u8;} else // unused
if i == 7u32 {return 13u8;} else
if i == 0u32 {return 32u8;} else
if i == 1u32 {return 0u8;} else
if i == 2u32 {return 1u8;} else
if i == 3u32 {return 12u8;} else
if i == 4u32 {return 2u8;} else
if i == 5u32 {return 6u8;} else
if i == 6u32 {return 0u8;} else // unused
if i == 7u32 {return 13u8;} else
if i == 8u32 {return 3u8;} else
if i == 9u32 {return 0u8;} else // unused
if i == 10u32 {return 7u8;} else
if i == 11u32 {return 0u8;} else // unused
if i == 12u32 {return 0u8;} else // unused
if i == 13u32 {return 0u8;} else // unused
if i == 14u32 {return 0u8;} else // unused
if i == 15u32 {return 14u8;} else
if i == 8u32 {return 3u8;} else
if i == 9u32 {return 0u8;} else // unused
if i == 10u32 {return 7u8;} else
if i == 11u32 {return 0u8;} else // unused
if i == 12u32 {return 0u8;} else // unused
if i == 13u32 {return 0u8;} else // unused
if i == 14u32 {return 0u8;} else // unused
if i == 15u32 {return 14u8;} else
if i == 16u32 {return 10u8;} else
if i == 17u32 {return 4u8;} else
if i == 18u32 {return 0u8;} else // unused
if i == 19u32 {return 0u8;} else // unused
if i == 20u32 {return 8u8;} else
if i == 21u32 {return 0u8;} else // unused
if i == 22u32 {return 0u8;} else // unused
if i == 23u32 {return 25u8;} else
if i == 16u32 {return 10u8;} else
if i == 17u32 {return 4u8;} else
if i == 18u32 {return 0u8;} else // unused
if i == 19u32 {return 0u8;} else // unused
if i == 20u32 {return 8u8;} else
if i == 21u32 {return 0u8;} else // unused
if i == 22u32 {return 0u8;} else // unused
if i == 23u32 {return 25u8;} else
if i == 24u32 {return 0u8;} else // unused
if i == 25u32 {return 0u8;} else // unused
if i == 26u32 {return 0u8;} else // unused
if i == 27u32 {return 0u8;} else // unused
if i == 28u32 {return 0u8;} else // unused
if i == 29u32 {return 21u8;} else
if i == 30u32 {return 27u8;} else
if i == 31u32 {return 15u8;} else
if i == 24u32 {return 0u8;} else // unused
if i == 25u32 {return 0u8;} else // unused
if i == 26u32 {return 0u8;} else // unused
if i == 27u32 {return 0u8;} else // unused
if i == 28u32 {return 0u8;} else // unused
if i == 29u32 {return 21u8;} else
if i == 30u32 {return 27u8;} else
if i == 31u32 {return 15u8;} else
if i == 32u32 {return 31u8;} else
if i == 33u32 {return 11u8;} else
if i == 34u32 {return 5u8;} else
if i == 35u32 {return 0u8;} else // unused
if i == 36u32 {return 0u8;} else // unused
if i == 37u32 {return 0u8;} else // unused
if i == 38u32 {return 0u8;} else // unused
if i == 39u32 {return 0u8;} else // unused
if i == 32u32 {return 31u8;} else
if i == 33u32 {return 11u8;} else
if i == 34u32 {return 5u8;} else
if i == 35u32 {return 0u8;} else // unused
if i == 36u32 {return 0u8;} else // unused
if i == 37u32 {return 0u8;} else // unused
if i == 38u32 {return 0u8;} else // unused
if i == 39u32 {return 0u8;} else // unused
if i == 40u32 {return 9u8;} else
if i == 41u32 {return 0u8;} else // unused
if i == 42u32 {return 0u8;} else // unused
if i == 43u32 {return 24u8;} else
if i == 44u32 {return 0u8;} else // unused
if i == 45u32 {return 0u8;} else // unused
if i == 46u32 {return 20u8;} else
if i == 47u32 {return 26u8;} else
if i == 40u32 {return 9u8;} else
if i == 41u32 {return 0u8;} else // unused
if i == 42u32 {return 0u8;} else // unused
if i == 43u32 {return 24u8;} else
if i == 44u32 {return 0u8;} else // unused
if i == 45u32 {return 0u8;} else // unused
if i == 46u32 {return 20u8;} else
if i == 47u32 {return 26u8;} else
if i == 48u32 {return 30u8;} else
if i == 49u32 {return 0u8;} else // unused
if i == 50u32 {return 0u8;} else // unused
if i == 51u32 {return 0u8;} else // unused
if i == 52u32 {return 0u8;} else // unused
if i == 53u32 {return 23u8;} else
if i == 54u32 {return 0u8;} else // unused
if i == 55u32 {return 19u8;} else
if i == 48u32 {return 30u8;} else
if i == 49u32 {return 0u8;} else // unused
if i == 50u32 {return 0u8;} else // unused
if i == 51u32 {return 0u8;} else // unused
if i == 52u32 {return 0u8;} else // unused
if i == 53u32 {return 23u8;} else
if i == 54u32 {return 0u8;} else // unused
if i == 55u32 {return 19u8;} else
if i == 56u32 {return 29u8;} else
if i == 57u32 {return 0u8;} else // unused
if i == 58u32 {return 22u8;} else
if i == 59u32 {return 18u8;} else
if i == 60u32 {return 28u8;} else
if i == 61u32 {return 17u8;} else
if i == 62u32 {return 16u8;} else
if i == 63u32 {return 0u8;} else // unused
{return 0u8;} // unused
if i == 56u32 {return 29u8;} else
if i == 57u32 {return 0u8;} else // unused
if i == 58u32 {return 22u8;} else
if i == 59u32 {return 18u8;} else
if i == 60u32 {return 28u8;} else
if i == 61u32 {return 17u8;} else
if i == 62u32 {return 16u8;} else
if i == 63u32 {return 0u8;} else // unused
{return 0u8;} // unused
}

View File

@ -4,66 +4,72 @@
@program
function main(public x: u32) -> u8 {
if (x & 65535u32 != 0u32) {
if (x & 255u32 != 0u32) {
if (x & 15u32 != 0u32) {
if (x & 3u32 != 0u32) {
if (x & 1u32 != 0u32) {return 0u8;}
else {return 1u8;} }
else {if (x & 4u32 != 0u32) {return 2u8;}
else {return 3u8;} } }
else // low 4 bits are 0 but low 8 bits are not zero
{if (x & 48u32 != 0u32) { // 48 = 0011 0000
if (x & 16u32 != 0u32) {return 4u8;}
else {return 5u8;} }
else {if (x & 64u32 != 0u32) {return 6u8;}
else {return 7u8;} } } }
else // low 8 bits are 0 but low 16 bits are not zero
{// 3840 = 00001111 00000000
if (x & 3840u32 != 0u32) {
// 768 = 00000011 00000000
if (x & 768u32 != 0u32) {
if (x & 256u32 != 0u32) {return 8u8;}
else {return 9u8;} }
else // 1024 = 00000100 00000000
{if (x & 1024u32 != 0u32) {return 10u8;}
else {return 11u8;} } }
else // low 12 bits are 0 but low 16 bits are not zero
{// 12288 = 0011 0000 0000 0000
if (x & 12288u32 != 0u32) {
// 4096 = 0001 0000 0000 0000
if (x & 4096u32 != 0u32) {return 12u8;}
else {return 13u8;} }
else // low 14 bits are 0 but low 16 bits are not zero
// 16384 = 0100 0000 0000 0000
{if (x & 16384u32 != 0u32) {return 14u8;}
else {return 15u8;} } } } }
else // low 16 bits are zero, now look at high 16 bits
{// Simply by multiplying the previous constants by 65536
if (x & 255u32 != 0u32) {
if (x & 15u32 != 0u32) {
if (x & 3u32 != 0u32) {
if (x & 1u32 != 0u32) {return 0u8;}
else {return 1u8;} }
else {
if (x & 4u32 != 0u32) {return 2u8;}
else {return 3u8;} } }
else { // low 4 bits are 0 but low 8 bits are not zero
if (x & 48u32 != 0u32) { // 48 = 0011 0000
if (x & 16u32 != 0u32) {return 4u8;}
else {return 5u8;} }
else {if (x & 64u32 != 0u32) {return 6u8;}
else {return 7u8;} } } }
else { // low 8 bits are 0 but low 16 bits are not zero
// 3840 = 00001111 00000000
if (x & 3840u32 != 0u32) {
// 768 = 00000011 00000000
if (x & 768u32 != 0u32) {
if (x & 256u32 != 0u32) {return 8u8;}
else {return 9u8;} }
else { // 1024 = 00000100 00000000
if (x & 1024u32 != 0u32) {return 10u8;}
else {return 11u8;} } }
else { // low 12 bits are 0 but low 16 bits are not zero
// 12288 = 0011 0000 0000 0000
if (x & 12288u32 != 0u32) {
// 4096 = 0001 0000 0000 0000
if (x & 4096u32 != 0u32) {return 12u8;}
else {return 13u8;} }
else { // low 14 bits are 0 but low 16 bits are not zero
// 16384 = 0100 0000 0000 0000
if (x & 16384u32 != 0u32) {return 14u8;}
else {return 15u8;} } } } }
else { // low 16 bits are zero, now look at high 16 bits
// Simply by multiplying the previous constants by 65536
if (x & (255u32 * 65536u32) != 0u32) {
if (x & (15u32 * 65536u32) != 0u32) {
if (x & (3u32 * 65536u32) != 0u32) {
if (x & (1u32 * 65536u32) != 0u32) {return 16u8;}
else {return 17u8;} }
else {if (x & (4u32 * 65536u32) != 0u32) {return 18u8;}
else {return 19u8;} } }
else
{if (x & (48u32 * 65536u32) != 0u32) {
if (x & (16u32 * 65536u32) != 0u32) {return 20u8;}
else {return 21u8;} }
else {if (x & (64u32 * 65536u32) != 0u32) {return 22u8;}
else {return 23u8;} } } }
else
{if (x & (3840u32 * 65536u32) != 0u32) {
if (x & (768u32 * 65536u32) != 0u32) {
if (x & (256u32 * 65536u32) != 0u32) {return 24u8;}
else {return 25u8;} }
else {if (x & (1024u32 * 65536u32) != 0u32) {return 26u8;}
else {return 27u8;} } }
else
{if (x & (12288u32 * 65536u32) != 0u32) {
if (x & (4096u32 * 65536u32) != 0u32) {return 28u8;}
else {return 29u8;} }
else {if (x & (16384u32 * 65536u32) != 0u32) {return 30u8;}
else {if (x != 0u32) {return 31u8;}
if (x & (15u32 * 65536u32) != 0u32) {
if (x & (3u32 * 65536u32) != 0u32) {
if (x & (1u32 * 65536u32) != 0u32) {return 16u8;}
else {return 17u8;} }
else {
if (x & (4u32 * 65536u32) != 0u32) {return 18u8;}
else {return 19u8;} } }
else {
if (x & (48u32 * 65536u32) != 0u32) {
if (x & (16u32 * 65536u32) != 0u32) {return 20u8;}
else {return 21u8;} }
else {
if (x & (64u32 * 65536u32) != 0u32) {return 22u8;}
else {return 23u8;} } } }
else {
if (x & (3840u32 * 65536u32) != 0u32) {
if (x & (768u32 * 65536u32) != 0u32) {
if (x & (256u32 * 65536u32) != 0u32) {return 24u8;}
else {return 25u8;} }
else {
if (x & (1024u32 * 65536u32) != 0u32) {return 26u8;}
else {return 27u8;} } }
else {
if (x & (12288u32 * 65536u32) != 0u32) {
if (x & (4096u32 * 65536u32) != 0u32) {return 28u8;}
else {return 29u8;} }
else {
if (x & (16384u32 * 65536u32) != 0u32) {return 30u8;}
else {
if (x != 0u32) {return 31u8;}
else {return 32u8;} } } } } }
}

View File

@ -1,26 +1,27 @@
// The 'twoadicity' main function.
// This function calculates the number of powers of two ("twoadicity")
// in the prime factorization of the input number `n`.
@program
function main(public n: field) -> u8 {
let remaining_n: field = n;
let powers_of_two: u8 = 0u8;
// Since field ints are 253 bits or fewer,
// any number in the field will have at most 252 powers of two in its prime factoring.
for i:u8 in 0u8..252u8 {
if is_even_and_nonzero(remaining_n) {
remaining_n = remaining_n / 2field;
powers_of_two = powers_of_two + 1u8;
}
}
return powers_of_two;
let remaining_n: field = n;
let powers_of_two: u8 = 0u8;
// Since field ints are 253 bits or fewer, any number in the field
// will have at most 252 powers of two in its prime factoring.
for i:u8 in 0u8..252u8 {
if is_even_and_nonzero(remaining_n) {
remaining_n = remaining_n / 2field;
powers_of_two = powers_of_two + 1u8;
}
}
return powers_of_two;
}
/* We define the is_even predicate on fields as follows.
If n is even and nonzero, clearly n/2 < n.
If n is odd, n-p is a field-equivalent negative number that is even,
and (n-p)/2 is a field-equivalent negative number closer to 0, greater than n-p.
If n is odd, n-p is a field-equivalent negative number that is even, and
(n-p)/2 is a field-equivalent negative number closer to 0, greater than n-p.
If we add p to both of these negative numbers, we have
n/2 = (n-p)/2 + p = (n+p)/2 is greater than n and still less than p.
*/
function is_even_and_nonzero (n: field) -> bool {
return n / 2field < n;
return n / 2field < n;
}