Skip to content

Remove x86intrin.h dependency from fd_util.h #4997

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

Merged
merged 3 commits into from
May 2, 2025
Merged
Show file tree
Hide file tree
Changes from all 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 src/app/shared/fd_config_parse.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "fd_config_private.h"
#include "../../util/pod/fd_pod.h"

/* Pod query utils ****************************************************/

Expand Down
4 changes: 4 additions & 0 deletions src/ballet/bigint/fd_uint256_mul.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@
#define OPTIMIZE
#endif

#if FD_HAS_X86
#include <x86intrin.h>
#endif

/* Utility functions for fd_uint256_mul_mod_p.
Implementation is based on uint128.
The implementations WITHOUT uint128 are just for completeness, we
Expand Down
1 change: 1 addition & 0 deletions src/ballet/toml/fd_toml.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "fd_toml.h"
#include "../../util/fd_util.h"
#include <ctype.h>
#include <math.h>
#include <time.h>

/* Implementation note:
Expand Down
1 change: 1 addition & 0 deletions src/disco/metrics/fd_metrics.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "generated/fd_metrics_all.h"

#include "../../tango/tempo/fd_tempo.h"
#include "../../util/hist/fd_histf.h"

/* fd_metrics mostly defines way of laying out metrics in shared
memory so that a producer and consumer can agree on where they
Expand Down
1 change: 1 addition & 0 deletions src/disco/pack/fd_chkdup.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

#ifndef FD_CHKDUP_IMPL
# if FD_HAS_AVX512
# include "../../util/simd/fd_avx.h"
# define FD_CHKDUP_IMPL 2
# elif FD_HAS_AVX
# define FD_CHKDUP_IMPL 1
Expand Down
3 changes: 3 additions & 0 deletions src/disco/pack/fd_pack_rebate_sum.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
#include "fd_pack_rebate_sum.h"
#include "fd_pack.h"
#if FD_HAS_AVX
#include "../../util/simd/fd_avx.h"
#endif

static const fd_acct_addr_t null_addr = { 0 };

Expand Down
1 change: 1 addition & 0 deletions src/disco/pack/test_chkdup.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#define FD_UNALIGNED_ACCESS_STYLE 0
#include "fd_chkdup.h"
#include <math.h>

#define P 4294967291UL /* A prime that fits in a uint with 2 as a primitive root */

Expand Down
1 change: 1 addition & 0 deletions src/discof/gossip/fd_gossip_tile.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "../../flamenco/gossip/fd_gossip.h"
#include "../../flamenco/runtime/fd_system_ids.h"
#include "../../flamenco/runtime/fd_runtime.h"
#include "../../util/pod/fd_pod.h"
#include "../../util/net/fd_ip4.h"
#include "../../util/net/fd_udp.h"
#include "../../util/net/fd_net_headers.h"
Expand Down
1 change: 1 addition & 0 deletions src/discof/poh/fd_poh.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include "../../disco/tiles.h"
#include "../../disco/shred/fd_stake_ci.h"
#include "../../util/hist/fd_histf.h"

/* Common library functions for the Proof of History tile. */

Expand Down
4 changes: 4 additions & 0 deletions src/discoh/resolv/fd_resolv_tile.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
#include "../../flamenco/runtime/fd_system_ids.h"
#include "../../flamenco/runtime/fd_system_ids_pp.h"

#if FD_HAS_AVX
#include "../../util/simd/fd_avx.h"
#endif

#define FD_RESOLV_IN_KIND_FRAGMENT (0)
#define FD_RESOLV_IN_KIND_BANK (1)

Expand Down
24 changes: 0 additions & 24 deletions src/flamenco/fd_flamenco_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -119,30 +119,6 @@ fd_acct_addr_cstr( char cstr[ static FD_BASE58_ENCODED_32_SZ ],
return fd_base58_encode_32( addr, NULL, cstr );
}

/* fd_pod utils */

FD_FN_UNUSED static fd_pubkey_t *
fd_pod_query_pubkey( uchar const * pod,
char const * path,
fd_pubkey_t * val ) {

ulong bufsz = 0UL;
void const * buf = fd_pod_query_buf( pod, path, &bufsz );

if( FD_UNLIKELY( (!buf) | (bufsz!=sizeof(fd_pubkey_t)) ) )
return NULL;

memcpy( val->uc, buf, sizeof(fd_pubkey_t) );
return val;
}

static inline ulong
fd_pod_insert_pubkey( uchar * pod,
char const * path,
fd_pubkey_t const * val ) {
return fd_pod_insert_buf( pod, path, val->uc, sizeof(fd_pubkey_t) );
}

FD_PROTOTYPES_END

#endif /* HEADER_fd_src_flamenco_fd_flamenco_base_h */
1 change: 1 addition & 0 deletions src/flamenco/gossip/fd_gossip.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "fd_gossip.h"
#include "../../ballet/base58/fd_base58.h"
#include "../../disco/keyguard/fd_keyguard.h"
#include <math.h>

/* Maximum size of a network packet */
#define PACKET_DATA_SIZE 1232
Expand Down
1 change: 1 addition & 0 deletions src/flamenco/runtime/fd_acc_mgr.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "../fd_flamenco_base.h"
#include "../../ballet/txn/fd_txn.h"
#include "fd_txn_account.h"
#include "../../util/simd/fd_avx.h"

/* FD_ACC_MGR_{SUCCESS,ERR{...}} are account management specific error codes.
To be stored in an int. */
Expand Down
3 changes: 1 addition & 2 deletions src/flamenco/runtime/fd_executor.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,11 @@
#include "../../ballet/base58/fd_base58.h"
#include "../../disco/pack/fd_pack.h"
#include "../../disco/pack/fd_pack_cost.h"
#include "../../ballet/sbpf/fd_sbpf_loader.h"

#include "../../util/bits/fd_uwide.h"

#include <assert.h>
#include <errno.h>
#include <math.h>
#include <stdio.h> /* snprintf(3) */
#include <fcntl.h> /* openat(2) */
#include <unistd.h> /* write(3) */
Expand Down
1 change: 1 addition & 0 deletions src/funk/bench_funk_index.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "fd_funk.h"
#include "fd_funk_base.h"
#include <math.h>

#define FUNK_TAG 1UL

Expand Down
15 changes: 8 additions & 7 deletions src/tango/tempo/fd_tempo.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "../fd_tango.h"
#include "../../util/math/fd_stat.h"

#if FD_HAS_DOUBLE

Expand All @@ -24,10 +25,10 @@ fd_tempo_wallclock_model( double * opt_tau ) {
jitter. */

ulong iter = 0UL;
for(;;) {
for(;;) {
# define TRIAL_CNT 512UL
# define TRIM_CNT 64UL
double trial[ TRIAL_CNT ];
double trial[ TRIAL_CNT ];
for( ulong trial_idx=0UL; trial_idx<TRIAL_CNT; trial_idx++ ) {
FD_COMPILER_MFENCE();
long tic = fd_log_wallclock();
Expand Down Expand Up @@ -67,10 +68,10 @@ fd_tempo_tickcount_model( double * opt_tau ) {
/* Same as the above but for fd_tickcount(). */

ulong iter = 0UL;
for(;;) {
for(;;) {
# define TRIAL_CNT 512UL
# define TRIM_CNT 64UL
double trial[ TRIAL_CNT ];
double trial[ TRIAL_CNT ];
for( ulong trial_idx=0UL; trial_idx<TRIAL_CNT; trial_idx++ ) {
FD_COMPILER_MFENCE();
long tic = fd_tickcount();
Expand Down Expand Up @@ -131,10 +132,10 @@ fd_tempo_tick_per_ns( double * opt_sigma ) {
well modeled as normal. */

ulong iter = 0UL;
for(;;) {
for(;;) {
# define TRIAL_CNT 32UL
# define TRIM_CNT 4UL
double trial[ TRIAL_CNT ];
double trial[ TRIAL_CNT ];
for( ulong trial_idx=0UL; trial_idx<TRIAL_CNT; trial_idx++ ) {
long then; long toc; fd_tempo_observe_pair( &then, &toc );
fd_log_sleep( 16777216L ); /* ~16.8 ms */
Expand Down Expand Up @@ -199,7 +200,7 @@ fd_tempo_observe_pair( long * opt_now,
tickcount because that is typically the lower overhead, more
deterministic one and less likely to get jerked around behind our
back.

Theoretically, this exploits how the minimum of a shifted
exponential random variable converges. Since the time to read
the various clocks is expected to be reasonably modeled as a
Expand Down
6 changes: 3 additions & 3 deletions src/util/fd_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@
//#include "tile/fd_tile.h" /* includes shmem/fd_shmem.h */
//#include "scratch/fd_scratch.h" /* includes tile/fd_tile.h sanitize/fd_sanitize.h valloc/fd_valloc.h */
//#include "tpool/fd_tpool.h" /* includes tile/fd_tile.h scratch/fd_scratch.h */
//#include "wksp/fd_wksp.h" /* includes pod/fd_pod.h tpool/fd_tpool.h checkpt/fd_checkpt.h sanitize/fd_sanitize.h */
//#include "wksp/fd_wksp.h" /* tpool/fd_tpool.h checkpt/fd_checkpt.h sanitize/fd_sanitize.h */
#include "alloc/fd_alloc.h" /* includes wksp/fd_wksp.h valloc/fd_valloc.h */
#include "rng/fd_rng.h" /* includes bits/fd_bits.h */

/* FIXME: Should these be optional APIs? */
#include "sandbox/fd_sandbox.h" /* includes fd_util_base.h */
#include "math/fd_stat.h" /* includes bits/fd_bits.h */
//#include "math/fd_stat.h" /* includes bits/fd_bits.h */
#include "bits/fd_sat.h" /* includes bits/fd_bits.h */
#include "hist/fd_histf.h" /* includes log/fd_log.h */
//#include "hist/fd_histf.h" /* includes log/fd_log.h */

/* Additional fd_util APIs that are not included by default */

Expand Down
1 change: 1 addition & 0 deletions src/util/math/test_stat.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "../fd_util.h"
#include "../../util/math/fd_stat.h"

/* This are uniform in [-0.5,+0.5] */

Expand Down
1 change: 1 addition & 0 deletions src/util/pod/fd_pod_ctl.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "../fd_util.h"
#include "../../util/pod/fd_pod.h"

#if FD_HAS_HOSTED

Expand Down
1 change: 1 addition & 0 deletions src/util/pod/test_pod.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "../fd_util.h"
#include "../../util/pod/fd_pod.h"

uchar mem[ 16384 ];

Expand Down
72 changes: 3 additions & 69 deletions src/util/rng/fd_rng.h
Original file line number Diff line number Diff line change
Expand Up @@ -450,15 +450,15 @@ double fd_rng_double_norm ( fd_rng_t * rng );
ETC) */

/* fd_rng_secure reads random bytes from a cryptographically secure
source provided by the platform. Features /dev/urandom like entropy.
source provided by the platform. Features /dev/urandom like entropy.

On success, returns d and guarantees that [d,d+sz) is filled with
On success, returns d and guarantees that [d,d+sz) is filled with
unguessable random bytes. On failure, returns NULL and prints reason
for failure to warning log.

(!!!) This operation may fail if no secure RNG is available or the
RNG failed for some reason. Always check the return code.

Currently available on Linux, FreeBSD, and macOS.
On Linux and FreeBSD uses getrandom(2).
On macOS uses CommonCrypto's CommonRandom. */
Expand All @@ -470,70 +470,4 @@ fd_rng_secure( void * d,

FD_PROTOTYPES_END

#if FD_HAS_X86

#include <immintrin.h> /* FIXME: HMMM */

/* rdrand reads sz cryptographically secure bytes using the RDRAND x86
instruction. Returns 1 if the read succeeded, 0 on failure.

Intel architecture manual section 7.3.17.1 (December 2023):

The RDRAND instruction returns a random number. All Intel
processors that support the RDRAND instruction indicate the
availability of the RDRAND instruction via reporting
CPUID.01H:ECX.RDRAND[bit 30] = 1.

RDRAND returns random numbers that are supplied by a
cryptographically secure, deterministic] random bit generator DRBG.
The DRBG is designed to meet the NIST SP 800-90A standard. The DRBG
is re-seeded frequently from an on-chip non-deterministic entropy
source to guarantee data returned by RDRAND is statistically
uniform, nonperiodic and non-deterministic.

In order for the hardware design to meet its security goals, the
random number generator continuously tests itself and the random
data it is generating. Runtime failures in the random number
generator circuitry or statistically anomalous data occurring by
chance will be detected by the self test hardware and flag the
resulting data as being bad. In such extremely rare cases, the
RDRAND instruction will return no data instead of bad data.

Under heavy load, with multiple cores executing RDRAND in parallel,
it is possible, though unlikely, for the demand of random numbers
by software processes/threads to exceed the rate at which the
random number generator hardware can supply them. This will lead to
the RDRAND instruction returning no data transitorily. */

FD_PROTOTYPES_BEGIN

__attribute__((warn_unused_result)) static inline int
fd_rdrand( uchar * dst,
ulong sz ) {

uchar * cur = dst;
ulong align_sz = sz & ~0x7UL;
while( cur < dst + align_sz ) {
unsigned long long slice;
if( FD_UNLIKELY( !_rdrand64_step( &slice ) ) )
return 0;
FD_STORE( ulong, dst, slice );
cur += 8UL;
}
if( FD_UNLIKELY( cur < dst ) ) {
unsigned long long slice;
if( FD_UNLIKELY( !_rdrand64_step( &slice ) ) )
return 0;
ulong tail = (ulong)( cur - dst );
fd_memcpy( cur, &slice, tail );
cur += tail;
}

return 1;
}

FD_PROTOTYPES_END

#endif /* FD_HAS_X86 */

#endif /* HEADER_fd_src_rng_fd_rng_h */
31 changes: 0 additions & 31 deletions src/util/rng/test_rng.c
Original file line number Diff line number Diff line change
Expand Up @@ -289,37 +289,6 @@ main( int argc,

FD_TEST( fd_rng_delete( shrng )==_rng );

# if FD_HAS_X86

FD_LOG_NOTICE(( "Testing RDRAND" ));

sum_pop = 0L;
sum_pop2 = 0L;
min_pop = INT_MAX;
max_pop = INT_MIN;

long iter = (1L<<22);
ctr = 0;
for( long i=0; i<iter; i++ ) {
if( !ctr ) { FD_LOG_NOTICE(( "Completed %li iterations", i )); ctr = 1000000; }
ctr--;

uchar seq[8];
for(;;)
if( FD_LIKELY( fd_rdrand( seq, 8UL ) ) )
break;
int pop = fd_ulong_popcnt( FD_LOAD( ulong, seq ) );
sum_pop += (long) pop;
sum_pop2 += (long)(pop*pop);
min_pop = pop<min_pop ? pop : min_pop;
max_pop = pop>max_pop ? pop : max_pop;
}
avg_pop = ((float)sum_pop ) / ((float)iter);
rms_pop = sqrtf( (((float)sum_pop2) / ((float)iter)) - avg_pop*avg_pop );
FD_LOG_NOTICE(( "rdrand popcount stats: %.3f +/- %.3f [%i,%i]", (double)avg_pop, (double)rms_pop, min_pop, max_pop ));

# endif /* FD_HAS_X86 */

# if defined(__linux__) || defined(__FreeBSD__) || defined(__APPLE__)
test_rng_secure();
# endif /* defined(...) */
Expand Down
1 change: 0 additions & 1 deletion src/util/wksp/fd_wksp.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#ifndef HEADER_fd_src_util_wksp_fd_wksp_h
#define HEADER_fd_src_util_wksp_fd_wksp_h

#include "../pod/fd_pod.h"
#include "../tpool/fd_tpool.h"
#include "../checkpt/fd_checkpt.h"
#include "../sanitize/fd_sanitize.h"
Expand Down
1 change: 1 addition & 0 deletions src/util/wksp/fd_wksp_helper.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "fd_wksp_private.h"
#include "../pod/fd_pod.h"

/* fd_wksp_private_{join,leave}_func are used to automagically handle
the first join / last leave by the fd_wksp_attach / fd_wksp_detach. */
Expand Down
1 change: 1 addition & 0 deletions src/waltz/quic/fd_quic.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@

#include "../aio/fd_aio.h"
#include "../tls/fd_tls.h"
#include "../../util/hist/fd_histf.h"

/* FD_QUIC_API marks public API declarations. No-op for now. */
#define FD_QUIC_API
Expand Down
Loading
Loading