[ / / / / / / / / / / / / / ] [ dir / asmr / f / fur / htg / kc / pdfs / sw / zoo ][Options][ watchlist ]

/tech/ - Technology

You can now write text to your AI-generated image at https://aiproto.com It is currently free to use for Proto members.
Name
Email
Subject
Comment *
File
Select/drop/paste files here
Password (Randomized for file and post deletion; you may also set your own.)
* = required field[▶ Show post options & limits]
Confused? See the FAQ.
Expand all images

[–]

 No.897820>>897836 >>897839 >>897853 >>897973 >>897988 >>898035 [Watch Thread][Show All Posts]

ITT: We critique each others fizzbuzzes


#include <stdint.h>
#include <stdio.h>


#ifdef __GNUC__
#define HOT_INLINED __attribute__ ((always_inline)) inline \
__attribute__ ((hot))
#else
#define HOT_INLINED inline
#endif


static const char *LOOKUP_TABLE[] = {
"FIZZBUZZ", NULL, NULL, "FIZZ", NULL,
"BUZZ", "FIZZ", NULL, NULL, "FIZZ",
"BUZZ", NULL, "FIZZ", NULL, NULL
};


typedef struct s_div10_t
{
const uint32_t quot;
const uint32_t rem;
}
div10_t;


static HOT_INLINED div10_t div10(uint32_t value)
{
uint32_t quot = (value >> 1) + (value >> 2);

quot = quot + (quot >> 4);
quot = quot + (quot >> 8);
quot = quot + (quot >> 16);

quot = quot >> 3;

uint32_t rem = value - ((quot << 3) + (quot << 1));

quot = quot + ((rem + 6) >> 4);

if (9 < rem) rem = rem - 10;

return (div10_t) { .quot = quot, .rem = rem };
}


static HOT_INLINED uint32_t mod15(uint32_t value)
{
value = (value >> 16) + (value & 0xFFFF);
value = (value >> 8) + (value & 0xFF);
value = (value >> 4) + (value & 0xF);

if (value < 15) return value;
if (value < 30) return value - 15;
if (value < 45) return value - 30;

return value - 45;
}


static HOT_INLINED void printu(uint32_t value)
{
if (0 == value)
{
putchar_unlocked('0');
putchar_unlocked('\n');
}
else
{
char buffer[11] = { 0 };

char *iter = &buffer[9];

buffer[10] = '\n';

while (value)
{
const div10_t div10_r = div10(value);
value = div10_r.quot;
*(iter--) = div10_r.rem + '0';
}

while (*iter != '\n') putchar_unlocked(*(++iter));
}
}


static HOT_INLINED void fastbuzz(uint32_t n)
{
const char *result = LOOKUP_TABLE[mod15(n)];

if (NULL != result)
{
puts(result);
}
else
{
printu(n);
}
}


int main()
{
for (uint32_t i = 1; i < 100; i++)
{
fastbuzz(i);
}

return 0;
}

 No.897836

>>897820 (OP)

is the assembly to this fewer lines than the source?

I looked at the assembly to this with -Os, and the answer is no, barely, the straight assembly is 134 lines while this is 112


 No.897839>>897988

reposting my fizzbuzz from another thread: >>886233

use std::cmp::min;

const FNS: [fn(usize) -> FizzBuzz; 15] = [
FizzBuzz::fizzbuzz,
FizzBuzz::number,
FizzBuzz::number,
FizzBuzz::fizz,
FizzBuzz::number,
FizzBuzz::buzz,
FizzBuzz::fizz,
FizzBuzz::number,
FizzBuzz::number,
FizzBuzz::fizz,
FizzBuzz::buzz,
FizzBuzz::number,
FizzBuzz::fizz,
FizzBuzz::number,
FizzBuzz::number
];

#[derive(Debug, Eq, PartialEq)]
enum FizzBuzz {
FizzBuzz,
Fizz,
Buzz,
Number(usize)
}

impl FizzBuzz {
fn fizzbuzz(n: usize) -> Self {
debug_assert!(n % 15 == 0);
FizzBuzz::FizzBuzz
}

fn fizz(n: usize) -> Self {
debug_assert!(n % 3 == 0 && n % 5 != 0);
FizzBuzz::Fizz
}

fn buzz(n: usize) -> Self {
debug_assert!(n % 3 != 0 && n % 5 == 0);
FizzBuzz::Buzz
}

fn number(n: usize) -> Self {
debug_assert!(n % 3 != 0 && n % 5 != 0);
FizzBuzz::Number(n)
}
}

fn main() {
fizzbuzz(|v| println!("{:?}", v), 1, 100);
}

fn fizzbuzz<F: FnMut(FizzBuzz)>(mut f: F, mut start: usize, end: usize) {
assert!(end >= start);

let rem = start % 15;
if rem != 0 {
for i in rem..rem + min(15 - rem, end - start) {
f(FNS[i](start - rem + i));
}

start += 15 - rem;
}

debug_assert!(start % 15 == 0);
debug_assert!(end >= start);
while (end - start) >= 15 {
f(FizzBuzz::fizzbuzz(start));
f(FizzBuzz::number(start + 1));
f(FizzBuzz::number(start + 2));
f(FizzBuzz::fizz(start + 3));
f(FizzBuzz::number(start + 4));
f(FizzBuzz::buzz(start + 5));
f(FizzBuzz::fizz(start + 6));
f(FizzBuzz::number(start + 7));
f(FizzBuzz::number(start + 8));
f(FizzBuzz::fizz(start + 9));
f(FizzBuzz::buzz(start + 10));
f(FizzBuzz::number(start + 11));
f(FizzBuzz::fizz(start + 12));
f(FizzBuzz::number(start + 13));
f(FizzBuzz::number(start + 14));

start += 15;
}

debug_assert!(start % 15 == 0);
debug_assert!(end >= start);
for i in 0..end - start {
f(FNS[i](start + i));
}
}

>>897820 (OP)

>mod15

>div10

retard


 No.897842>>897850 >>897994

File (hide): 3ac406e977b0214⋯.png (573.14 KB, 750x1000, 3:4, rms.png) (h) (u)

Has anyone determined the least bloated FizzBuzz possible?


 No.897850

>>897842

pen and a pad of fuckin paper


 No.897853

>>897820 (OP)

tl;dr

bloat

0/10

also this task is too trivial to discuss, there's really nothing interesting, unless you are brainlet or are trying to be funny and need ideas for a pull request to https://github.com/EnterpriseQualityCoding/FizzBuzzEnterpriseEdition


 No.897856


 No.897858>>897988


for i in range(100):
if i == 1:
print 1
elif i == 2:
print 2
elif i == 3:
print "fizz"
elif i == 4:
print 4
elif i == 5:
print "buzz"
elif i == 6
print "fizz"
elif i == 7
print 7
...


 No.897860>>897988 >>898004

butt ugly GAS syntax

.section .data
fizz: .ascii "fizz\n"
buzz: .ascii "buzz\n"
fizzbuzz: .ascii "fizzbuzz\n"
buf: .skip 20
nl: .ascii "\n"


.section .text
.global _start
_start:
mov $1, %r15

1:
/* mod 15 */
mov %r15, %rax
mov $15, %rbx
xor %rdx, %rdx
div %rbx
cmp $0, %rdx
jne 2f

mov $4, %rax
mov $1, %rbx
mov $9, %rdx
mov $fizzbuzz, %rcx
int $0x80
jmp 6f

2:
/* mod 3 */
mov %r15, %rax
mov $3, %rbx
xor %rdx, %rdx
div %rbx
cmp $0, %rdx
jne 3f

mov $4, %rax
mov $1, %rbx
mov $5, %rdx
mov $fizz, %rcx
int $0x80
jmp 6f

3:
/* mod 5 */
mov %r15, %rax
mov $5, %rbx
xor %rdx, %rdx
div %rbx
cmp $0, %rdx
jne 4f

mov $4, %rax
mov $1, %rbx
mov $5, %rdx
mov $buzz, %rcx
int $0x80
jmp 6f

4:
/* number to buffer */
mov %r15, %rax
mov $buf, %r14
add $20, %r14

5:
dec %r14
mov $10, %rbx
xor %rdx, %rdx
div %rbx

add $'0', %rdx
mov %dl, (%r14)

cmp $0, %rax
jne 5b

mov $4, %rax
mov $1, %rbx
mov $nl, %rdx
sub %r14, %rdx
inc %rdx
mov %r14, %rcx
int $0x80

6:
/* jump to 1 if counter != 101 */
inc %r15
cmp $101, %r15
jne 1b

/* exit */
mov $1, %rax
mov $0, %rbx
int $0x80

Save as fizzbuzz.S and compile with `as -o fizzbuzz.o fizzbuzz.S; ld -o fizzbuzz fizzbuzz.o`


 No.897973>>897987

>>897820 (OP)

>ITT: We critique each others fizzbuzzes

Your fizz-buzz violates KISS principle.


 No.897987

>>897973

KISS principle is for Java pajeets who need interfaces and inheritance


 No.897988>>897990

>>897820 (OP)

>>897839

>>897858

>>897860

>Being unemployed

/**
* Plays FizzBuzz up to an arbitrary high given integer.
* <p><b>FizzBuzz</b> is a game where the goal is to count up to an integer
* <i>n</i> while replacing the multiples of 3 with <i>Fizz</i>, the mutliples
* of 5 with <i>Buzz</i> and the multiples of both 3 and 5 with
* <i>Fizzbuzz</i>. It is a common entry-level problem for gaujing a software
* engineer's expertise and methodological rigor.</p>
* @author Dinesh Ratrikasha
* @version 1.0.0
* @since 2018-04-07
*/
public class FizzBuzz {
/**
* Checks whether an integer is a multiple of another.
* @param n The integer to be tested
* @param q The divisor to be tested
* @return {@code true} if {@code n} is a multiple of {@code q}, {@code false} otherwise
*/
private static boolean isMultiple(final int n, final int q) {
// The integer n is a multiple of q if and only if n = 0 (mod q)
return n % q == 0;
}

/**
* Prints the appropriate FizzBuzz message for a given integer.
* @param n The number of the message
*/
private static void printFizzBuzzMessage(final int n) {
// Since 3 and 5 are co-prime, (3 | n and 5 | n) iff 15 | n
if (isMultiple(n, 15))
System.out.println("Fizzbuzz");
else if (isMultiple(n, 3))
System.out.println("Fizz");
else if (isMultiple(n, 5))
System.out.println("Buzz");
else
System.out.println(n);
}

/**
* Program starting point.
* @param args Command-line arguments, expected to be of length 1
* @throws SecurityException if a call to {@link System#exit} fails (unlikely)
*/
public static void main(String[] args) {
if (args.length == 1) {
try {
final int max = Integer.parseInt(args[0]);
for (int i = 1; i <= max; ++i)
printFizzBuzzMessage(i);
} catch (NumberFormatException e) {
System.err.println("Error parsing input value: " + e.toString());
System.exit(1);
}
} else {
final String name = System.getProperty("sun.java.command");
System.err.println("usage: java " + name + " n");
System.exit(1);
}
}
}


 No.897990

>>897988

>>Being unemployed

>Working on saturday

>Dinesh Ratrikasha

>Java

...


 No.897992>>898758

File (hide): 7c99cad7f46835c⋯.png (34.22 KB, 621x942, 207:314, hurr_fizzbuzz.png) (h) (u)


 No.897994

>>897842

Yes we have.


: fizz 3 mod 0= if ." fizz" -1 else 0 then ;
: buzz 5 mod 0= if ." buzz" -1 else 0 then ;
: else -rot or 0= if . then ;

: fb 100 0 do i fizz i buzz i else cr loop ;

Alt:


: fizz ( n -- ) drop ." Fizz" ;
: buzz ( n -- ) drop ." Buzz" ;
: fb ( n -- ) drop ." FizzBuzz" ;
: vector create does> ( n -- )
over 15 mod cells + @ execute ;
vector .fizzbuzz
' fb , ' . , ' . ,
' fizz , ' . , ' buzz ,
' fizz , ' . , ' . ,
' fizz , ' buzz , ' . ,
' fizz , ' . , ' . ,


 No.898004

>>897860

>butt ugly GAS syntax

>not using NASM

>int 0x80 in 64-bit mode

2/10 would not hire


 No.898024>>898036 >>898838

AWK:

seq 100 | awk '!($1%3){printf"Fizz"}!($1%5){printf"Buzz"}{if(!(!($1%3)||!($1%5)))print;else printf"\n"}'

sed:

seq 100 | sed '3~3s/.*/Fizz/;5~5s/.*/Buzz/;15~15s/.*/FizzBuzz/'

Can anyone make it even shorter?


 No.898035

>>897820 (OP)

>*LOOKUP_TABLE[] =

Invalid solution. You are supposed to determine conditions for fizz and buzz as you go, and not just regurgitate a premade table every 15 steps.


 No.898036>>898127

>>898024

>Can anyone make it even shorter?

Yes. Given that the universe is doomed to perish eventually anyway, 0x90 (NOP) is equivalent to any program in the long run.


 No.898039

generator () {
echo ${1:-1}
generator $((${1:-1} + 1))
}

divides() {
[ $(($1 % $2)) = 0 ]
}

enhancer () {
echo -n $1
divides $1 3 && echo -n ' 'fizz
divides $1 5 && echo -n ' 'buzz
echo
}

printer () {
echo -n $1'\r'
echo -n $2
echo $3
}

per_line () {
while read line
do
$1 $line
done
}

generator | per_line enhancer | per_line printer


 No.898127

>>898036

yelling "nigger" at the wall and observing the output of the wall is equivalent to any program in the long run


 No.898662>>898706 >>898753 >>898791 >>899697

#include <stdio.h>

struct potential {
int divisor;
char *word;
};

struct potential potentials[] = {
(struct potential){3, "fizz"},
(struct potential){5, "buzz"},
(struct potential){15, "fizzbuzz"}
};

void u(int num)
{
int length;
char *output = 0;
length = sizeof potentials / sizeof (struct potential);
for (int i = 0; i < length; i++)
if (!(num % potentials[i].divisor))
output = potentials[i].word;
if (output)
puts(output);
else
printf("%d\n", num);
u(num + 1);
}

main [-1u]= {
1
};


 No.898706>>898709

>>898662

?????????????


 No.898709

>>898706

Just compile it. It's C.


 No.898753>>898764

File (hide): ba2bcfa5772c090⋯.jpeg (4.81 KB, 300x168, 25:14, index.jpeg) (h) (u)

>>898662

<main [-1u]= {

< 1

<};

What is this fuckness...?


 No.898758

>>897992

Looks like code "refactored" by a disgruntled employee one day before he left


 No.898764>>898789

>>898753

Compile it and find out.


 No.898785>>898791

File (hide): d800c46e03e0a71⋯.jpg (136.21 KB, 1280x720, 16:9, 15655180.jpg) (h) (u)

for 1..50 {
if !it%3 printf("fizz\n");
else if !it%5 printf("buzz\n");
else if !it%15 printf("fizzbuzz\n");
else printf("%\n", it);
}


 No.898789>>898794

>>898764

Even if it somehow compiles and works, that still won't explain what that ayyyylmao-tier piece of syntax is supposed to represent


 No.898791>>898795

>>898662

>>898785

>defining "fizzbuzz" separately from "fizz"/"buzz"

fucking bloat


 No.898794>>898807

>>898789

It's declaring an array with the name main and a size of -1u. -1u is unsigned -1, or 4294967295. It's implicitly typed int, which means the array is 16 GB large on most architectures. The first element is set to 1, all the others are set to 0. All of that is dumped in the binary, so the file will be 16 GB+ and the compiler won't be very responsive. There's no main function, but because the symbol is called "main" the linker will accept it. The executable will be created but won't work.


 No.898795>>899400

>>898791

I'd rather take a bloated program than a slow program.


 No.898807

File (hide): e2579dd7131b986⋯.png (44.68 KB, 149x102, 149:102, notcheatedthistime.png) (h) (u)

>>898794

So I was in the right staying sceptical of actually attempting to compile it.


 No.898838>>899693

File (hide): d04c59d776f8fa6⋯.jpg (359.19 KB, 1130x1194, 565:597, 1520512904039.jpg) (h) (u)

>>898024

seq 100|sed 5~5cBuzz|sed 3~3s/[^B]*/Fizz/


 No.899400>>899557

>>898795

>t. GNU


 No.899557

>>899400

GNU isn't perfect but it sure is better than traditional inefficient arbitrarily-limited Unix.


 No.899602


while($counter++,$num++ -lt 100){
$mod3 = $num % 3
$mod5 = $num % 5
if($mod3 -eq 0 -and $mod5 -eq 0){
Write-Output FizzBuzz
}elseif($mod5 -eq 0){
Write-Output Buzz
}elseif($mod3 -eq 0){
Write-Output Fizz
}else{
Write-Output $num
}
}


 No.899693

>>898838

sauce? are there n00dez?


 No.899697>>899727 >>899730

>>898662

>main [-1u]= { 1 };

Care to explain this bit? It really confuses me.


 No.899727>>899728 >>899730

>>899697

The suffix "u" makes the compiler read the integer as an unsigned integer. Because it is almost guaranteed that the computer uses two-complement to represent integers, signed -1 is equivalent to the maximum permitted unsigned integer, i.e. 1111 1111 1111 1111...

So, he's creating an array of UINT_MAX which contains the number 1 everywhere.


 No.899728

>>899727

Uhm that's not the screencap I wanted to post.


 No.899730

>>899697

>>899727

Now, that's not the real trick he's doing. By creating an array called main, he tricks the linker into thinking the contained data is in fact the main() function. I saw this hack implemented by simply declaring a const char array that contained the bytes of a classic "hello, world!" program. Because they were declared as const, they were also executable (since memory cells are either writable or executable in modern computers). That way, it was effectively possible to write a C program without ever declaring a main() function.

Why it would work without declaring the type of the array, or the array as constant I do not know.


 No.899747

FizzBuzz hardware edition:

library IEEE;
use IEEE.std_logic_1164.all;
use IEEE.numeric_std.all;

entity fizzbuzz is
port (clk : in std_logic; fizz, buzz : out std_logic);
end fizzbuzz;

architecture behaviour of fizzbuzz is
signal cnt : unsigned(6 downto 0) := "0000001";
signal cnt3, cnt5 : unsigned(2 downto 0) := "001";
begin
p1: process(clk)
begin
if rising_edge(clk) then
cnt <= cnt + 1;
cnt3 <= cnt3 + 1;
cnt5 <= cnt5 + 1;

if cnt3 = "011" then cnt3 <= "001"; end if;
if cnt5 = "101" then cnt5 <= "001"; end if;
end if;
end process;

fizz <= '1' when cnt3 = "011" else '0';
buzz <= '1' when cnt5 = "101" else '0';
end behaviour;




[Return][Go to top][Catalog][Screencap][Nerve Center][Cancer][Update] ( Scroll to new posts) ( Auto) 5
42 replies | 9 images | Page ?
[Post a Reply]
[ / / / / / / / / / / / / / ] [ dir / asmr / f / fur / htg / kc / pdfs / sw / zoo ][ watchlist ]