Derive-C
Loading...
Searching...
No Matches
prime_sieve.c File Reference
#include <limits.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <derive-c/prelude.h>
#include <derive-c/alloc/hybridstatic/template.h>
#include <derive-c/container/vector/dynamic/template.h>

Go to the source code of this file.

Macros

#define MAX_UP_TO   100000
#define CAPACITY   300000
#define NAME   bump_alloc
#define ITEM   bool
#define ALLOC   bump_alloc
#define NAME   sieve_vec

Functions

static size_t sqrt_size_t (size_t n)
static void display (sieve_vec const *sieve, DC_LOGGER *log)
static void compute (sieve_vec *sieve, DC_LOGGER *log)
static void example_prime_sieve (DC_LOGGER *parent)
int main ()

Macro Definition Documentation

◆ ALLOC

#define ALLOC   bump_alloc

Definition at line 22 of file prime_sieve.c.

◆ CAPACITY

#define CAPACITY   300000

Definition at line 17 of file prime_sieve.c.

◆ ITEM

#define ITEM   bool

Definition at line 21 of file prime_sieve.c.

◆ MAX_UP_TO

#define MAX_UP_TO   100000
Examples
complex/prime_sieve.c.

Definition at line 14 of file prime_sieve.c.

◆ NAME [1/2]

#define NAME   sieve_vec

Definition at line 18 of file prime_sieve.c.

◆ NAME [2/2]

#define NAME   bump_alloc

Definition at line 18 of file prime_sieve.c.

Function Documentation

◆ compute()

void compute ( sieve_vec * sieve,
DC_LOGGER * log )
static
Examples
complex/prime_sieve.c.

Definition at line 69 of file prime_sieve.c.

69 {
70 size_t size = sieve_vec_size(sieve);
71 size_t sqrt = sqrt_size_t(size);
72 DC_LOG(*log, DC_INFO, "sieve size: %zu, sqrt: %zu", size, sqrt);
73 for (size_t factor = 2; factor <= sqrt; factor++) {
74 for (size_t index = factor * 2; index < size; index += factor) {
75 DC_LOG(*log, DC_DEBUG, "marking %zu as not prime (factor: %zu)", index, factor);
76 *sieve_vec_write(sieve, index) = true;
77 }
78 }
79}
static DC_PUBLIC size_t size(SELF const *self)
Definition template.h:252
#define DC_DEBUG(DEBUG_FN, DEBUG_PTR)
Definition dump.h:92
static size_t sqrt_size_t(size_t n)
Definition prime_sieve.c:26
#define DC_LOG(...)
Definition prelude.h:20
@ DC_INFO
Definition trait.h:8

◆ display()

void display ( sieve_vec const * sieve,
DC_LOGGER * log )
static
Examples
complex/prime_sieve.c.

Definition at line 53 of file prime_sieve.c.

53 {
54 sieve_vec_iter_const iter = sieve_vec_get_iter_const(sieve);
55
56 sieve_vec_iter_const_next(&iter); // skip 0
57 sieve_vec_iter_const_next(&iter); // skip 1
58 size_t index = 2;
59
60 bool const* is_not_prime;
61 while ((is_not_prime = sieve_vec_iter_const_next(&iter))) {
62 if (!*is_not_prime) {
63 DC_LOG(*log, DC_INFO, "%zu is prime", index);
64 }
65 index++;
66 }
67}

◆ example_prime_sieve()

void example_prime_sieve ( DC_LOGGER * parent)
static
Examples
complex/prime_sieve.c.

Definition at line 81 of file prime_sieve.c.

81 {
82 DC_SCOPED(DC_LOGGER) log = DC_LOGGER_NEW(parent, "%s", __func__);
83
85 DC_LOG(log, DC_INFO, "cpu features: %s", DC_DEBUG(dc_cpu_features_debug, &features));
86
87 size_t up_to = 28;
88 DC_ASSERT(up_to < MAX_UP_TO);
89 DC_LOG(log, DC_INFO, "listing primes up to: %zu", up_to);
90
91 bump_alloc_buffer buf;
92 bump_alloc alloc = bump_alloc_new(&buf, stdalloc_get_ref());
93 sieve_vec values = sieve_vec_new_with_defaults(up_to, false, &alloc);
94
95 compute(&values, &log);
96 display(&values, &log);
97
98 sieve_vec_delete(&values);
99}
static dc_cpu_features dc_cpu_features_get()
static DC_PUBLIC void dc_cpu_features_debug(dc_cpu_features const *self, dc_debug_fmt fmt, FILE *stream)
#define DC_LOGGER
Definition file.h:168
#define DC_ASSERT(expr,...)
Definition panic.h:37
static void compute(sieve_vec *sieve, DC_LOGGER *log)
Definition prime_sieve.c:69
static void display(sieve_vec const *sieve, DC_LOGGER *log)
Definition prime_sieve.c:53
#define MAX_UP_TO
Definition prime_sieve.c:14
#define DC_SCOPED(type,...)
RAII in C. Call the destructor when the variable goes out of scope.
Definition scope.h:5
#define DC_LOGGER_NEW(...)
Definition prelude.h:21

◆ main()

int main ( )

Definition at line 101 of file prime_sieve.c.

101 {
103 root = NS(DC_LOGGER,
104 new_global)((NS(DC_LOGGER, global_config)){.stream = stdout, .ansi_colours = true},
105 (dc_log_id){"prime_sieve"});
106
107 example_prime_sieve(&root);
108 return 0;
109}
#define NS(pre, post)
Definition namespace.h:14
static void example_prime_sieve(DC_LOGGER *parent)
Definition prime_sieve.c:81

◆ sqrt_size_t()

size_t sqrt_size_t ( size_t n)
static
Examples
complex/prime_sieve.c.

Definition at line 26 of file prime_sieve.c.

26 {
27 if (n == 0 || n == 1) {
28 return n;
29 }
30 size_t left = 1;
31 size_t right = n;
32 size_t mid;
33 size_t result = 0;
34 while (left <= right) {
35 mid = left + (right - left) / 2;
36 size_t square = mid * mid;
37
38 if (square == n) {
39 return mid;
40 }
41
42 if (square < n) {
43 result = mid; // Store the last valid result
44 left = mid + 1;
45 } else {
46 right = mid - 1;
47 }
48 }
49
50 return result;
51}