<commit_before>// We're not sure where the android headers might be, so we just extern declare all the stuff we need here

// cfloat should be safe. It's just constants and whatnot.
#include <cfloat>

typedef unsigned size_t;
#include "buffer_t.h"

extern "C" {

  extern void *malloc(size_t);
  extern void free(void *);
  extern void exit(int);
  extern char *getenv(const char *);
  extern int atoi(const char *);

#define NULL 0
typedef struct {
    uint32_t flags;
    void * stack_base;
    size_t stack_size;
    size_t guard_size;
    int32_t sched_policy;
    int32_t sched_priority;
} pthread_attr_t;
typedef long pthread_t;
typedef struct {
    int volatile value;
} pthread_cond_t;
typedef long pthread_condattr_t;
typedef struct {
    int volatile value;
} pthread_mutex_t;
typedef long pthread_mutexattr_t;
extern int pthread_create(pthread_t *thread, pthread_attr_t const * attr,
                          void *(*start_routine)(void *), void * arg);
extern int pthread_cond_init(pthread_cond_t *cond, const pthread_condattr_t *attr);
extern int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex);
extern int pthread_cond_broadcast(pthread_cond_t *cond);
extern int pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *attr);
extern int pthread_mutex_lock(pthread_mutex_t *mutex);
extern int pthread_mutex_unlock(pthread_mutex_t *mutex);

extern float logf(float);
extern float expf(float);
extern float cosf(float);
extern float sinf(float);
extern float floorf(float);
extern float ceilf(float);
extern float sqrtf(float);
extern float roundf(float);
extern float powf(float, float);

extern double log(double);
extern double exp(double);
extern double cos(double);
extern double sin(double);
extern double floor(double);
extern double ceil(double);
extern double sqrt(double);
extern double round(double);
extern double pow(double, double);

#define PTR_OFFSET(ptr,offset)	((void*)((char*)(ptr) + (offset)))

// The functions below are all weak or inline so that you can link multiple
// halide modules without name conflicts. 

#define WEAK __attribute__((weak))
#define INLINE inline __attribute__((used)) __attribute__((always_inline)) __attribute__((nothrow))

WEAK buffer_t *force_include_buffer_t(buffer_t *b) {
  return b;
}

// This only gets defined if it's not already defined by an including module, e.g. PTX.
// This also serves to forcibly include the buffer_t struct definition
#ifndef _COPY_TO_HOST
#define _COPY_TO_HOST
WEAK void __copy_to_host(buffer_t* buf) { /* NOP */ }
#endif //_COPY_TO_HOST

static void *(*halide_custom_malloc)(size_t) = NULL;
static void (*halide_custom_free)(void *) = NULL;
WEAK void set_custom_allocator(void *(*cust_malloc)(size_t), void (*cust_free)(void *)) {
    halide_custom_malloc = cust_malloc;
    halide_custom_free = cust_free;
}

WEAK void *fast_malloc(size_t x) {
    if (halide_custom_malloc) {
        return halide_custom_malloc(x);
    } else {
        void *orig = malloc(x+32);
        // Round up to next multiple of 32. Should add at least 8 bytes so we can fit the original pointer.
        void *ptr = (void *)((((size_t)orig + 32) >> 5) << 5);
        ((void **)ptr)[-1] = orig;
        return ptr;
    }
}

WEAK void fast_free(void *ptr) {
    if (halide_custom_free) {
        halide_custom_free(ptr);
    } else {
        free(((void**)ptr)[-1]);
    }
}

WEAK void *safe_malloc(size_t x) {
    return fast_malloc(x);
}

WEAK void safe_free(void *ptr) {
    return fast_free(ptr);
}

static void (*halide_error_handler)(char *) = NULL;

extern void __android_log_print(int, const char *, const char *, ...);

WEAK void halide_error(char *msg) {
    if (halide_error_handler) (*halide_error_handler)(msg);
    else {
        __android_log_print(7, "halide", "Error: %s\n", msg);
        exit(1);
    }
}

WEAK void set_error_handler(void (*handler)(char *)) {
    halide_error_handler = handler;
}

struct work {
    void (*f)(int, uint8_t *);
    int next, max;
    uint8_t *closure;
    int id;
    int active_workers;
};
<commit_msg>Added hlprintf to the android stdlib<commit_after>// We're not sure where the android headers might be, so we just extern declare all the stuff we need here

// cfloat should be safe. It's just constants and whatnot.
#include <cfloat>

typedef unsigned size_t;
#include "buffer_t.h"

extern "C" {

  extern void *malloc(size_t);
  extern void free(void *);
  extern void exit(int);
  extern char *getenv(const char *);
  extern int atoi(const char *);

#define NULL 0
typedef struct {
    uint32_t flags;
    void * stack_base;
    size_t stack_size;
    size_t guard_size;
    int32_t sched_policy;
    int32_t sched_priority;
} pthread_attr_t;
typedef long pthread_t;
typedef struct {
    int volatile value;
} pthread_cond_t;
typedef long pthread_condattr_t;
typedef struct {
    int volatile value;
} pthread_mutex_t;
typedef long pthread_mutexattr_t;
extern int pthread_create(pthread_t *thread, pthread_attr_t const * attr,
                          void *(*start_routine)(void *), void * arg);
extern int pthread_cond_init(pthread_cond_t *cond, const pthread_condattr_t *attr);
extern int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex);
extern int pthread_cond_broadcast(pthread_cond_t *cond);
extern int pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *attr);
extern int pthread_mutex_lock(pthread_mutex_t *mutex);
extern int pthread_mutex_unlock(pthread_mutex_t *mutex);

extern float logf(float);
extern float expf(float);
extern float cosf(float);
extern float sinf(float);
extern float floorf(float);
extern float ceilf(float);
extern float sqrtf(float);
extern float roundf(float);
extern float powf(float, float);

extern double log(double);
extern double exp(double);
extern double cos(double);
extern double sin(double);
extern double floor(double);
extern double ceil(double);
extern double sqrt(double);
extern double round(double);
extern double pow(double, double);

#define PTR_OFFSET(ptr,offset)	((void*)((char*)(ptr) + (offset)))

// The functions below are all weak or inline so that you can link multiple
// halide modules without name conflicts. 

#define WEAK __attribute__((weak))
#define INLINE inline __attribute__((used)) __attribute__((always_inline)) __attribute__((nothrow))

WEAK buffer_t *force_include_buffer_t(buffer_t *b) {
  return b;
}

// This only gets defined if it's not already defined by an including module, e.g. PTX.
// This also serves to forcibly include the buffer_t struct definition
#ifndef _COPY_TO_HOST
#define _COPY_TO_HOST
WEAK void __copy_to_host(buffer_t* buf) { /* NOP */ }
#endif //_COPY_TO_HOST

static void *(*halide_custom_malloc)(size_t) = NULL;
static void (*halide_custom_free)(void *) = NULL;
WEAK void set_custom_allocator(void *(*cust_malloc)(size_t), void (*cust_free)(void *)) {
    halide_custom_malloc = cust_malloc;
    halide_custom_free = cust_free;
}

WEAK void *fast_malloc(size_t x) {
    if (halide_custom_malloc) {
        return halide_custom_malloc(x);
    } else {
        void *orig = malloc(x+32);
        // Round up to next multiple of 32. Should add at least 8 bytes so we can fit the original pointer.
        void *ptr = (void *)((((size_t)orig + 32) >> 5) << 5);
        ((void **)ptr)[-1] = orig;
        return ptr;
    }
}

WEAK void fast_free(void *ptr) {
    if (halide_custom_free) {
        halide_custom_free(ptr);
    } else {
        free(((void**)ptr)[-1]);
    }
}

WEAK void *safe_malloc(size_t x) {
    return fast_malloc(x);
}

WEAK void safe_free(void *ptr) {
    return fast_free(ptr);
}

static void (*halide_error_handler)(char *) = NULL;

#include <stdarg.h>
extern void __android_log_print(int, const char *, const char *, ...);
extern void __android_log_vprint(int, const char *, const char *, va_list);

WEAK int hlprintf(const char * fmt, ...) {
    va_list args;
    va_start(args,fmt);
    __android_log_vprint(7, "halide", fmt, args);
    va_end(args);
    return 0;
}

WEAK void halide_error(char *msg) {
    if (halide_error_handler) (*halide_error_handler)(msg);
    else {
        __android_log_print(7, "halide", "Error: %s\n", msg);
        exit(1);
    }
}

WEAK void set_error_handler(void (*handler)(char *)) {
    halide_error_handler = handler;
}

struct work {
    void (*f)(int, uint8_t *);
    int next, max;
    uint8_t *closure;
    int id;
    int active_workers;
};

{'fraction_non_alphanumeric': 0.11218292247231154, 'fraction_numerical': 0.005478146957246636, 'mean_word_length': 4.369565217391305, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 10, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2000995', 'n_tokens_mistral': 2948, 'n_tokens_neox': 2802, 'n_words': 822}