Skip to content

Commit

Permalink
Introduce const correctness
Browse files Browse the repository at this point in the history
For readability,
for potentially aiding some optimizing compilers,
for the sake of consistency -- with other professional C code around the world

No function changes.

The internal logic of one function altered, in p_a_inv.c
  It did modify the memory area documented as "input", which is rearly
  considered best practice,
  not at all what a caller would expect,
  could lead to subtle hard to find errors for some users of the library
  • Loading branch information
GBuella committed Jun 5, 2015
1 parent 6145edb commit d445189
Show file tree
Hide file tree
Showing 71 changed files with 241 additions and 199 deletions.
6 changes: 3 additions & 3 deletions examples/base/simple_example.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ int main(int argc, char *argv[])
{

// Stack variables
char *file = "./hello_task.elf";
char *func = "main";
const char *file = "./hello_task.elf";
const char *func = "main";
int status, i, all, nargs = 1;
char *args[nargs];
const char *args[nargs];
char argbuf[20];

// References as opaque structures
Expand Down
8 changes: 4 additions & 4 deletions include/pal_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,11 @@ p_team_t p_remove(p_team_t team, int start, int count);
int p_close(p_team_t team);

/* Loads a program from the file system into memory */
p_prog_t p_load(p_dev_t dev, char *file, char *function, int flags);
p_prog_t p_load(p_dev_t dev, const char *file, const char *function, int flags);

/* Run a program on N processors */
int p_run(p_prog_t prog, p_team_t team, int start, int count, int nargs,
char *args[], int flags);
const char *args[], int flags);

/*Execution barrier*/
int p_barrier(p_team_t team);
Expand Down Expand Up @@ -157,7 +157,7 @@ ssize_t p_gather(p_mem_t *mlist[], int mcount, void *dbuf, size_t dcount,
int disp[], int flags);

/*Specialized low level shared memory memcpy interface (non-blocking)*/
ssize_t p_memcpy(void *dst, void *src, size_t nb, int flags);
ssize_t p_memcpy(void *dst, const void *src, size_t nb, int flags);

/*
***********************************************************************
Expand Down Expand Up @@ -199,7 +199,7 @@ int p_atomic_or_u32(p_atom_t atom, uint32_t n);
int p_atomic_nand_u32(p_atom_t atom, uint32_t n);

/*atomic swap*/
int p_atomic_swap_u32(p_atom_t atom, uint32_t *input);
int p_atomic_swap_u32(p_atom_t atom, const uint32_t *input);

/*atomic compare and swap*/
int p_atomic_compswap_u32(p_atom_t atom, uint32_t *input, uint32_t expected);
Expand Down
32 changes: 16 additions & 16 deletions include/pal_dsp.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,34 +15,34 @@
*/

/*auto correlation: r[j] = sum ( x[j+k] * x[k] ), k=0..(n-j-1) */
void p_acorr_f32(float *x, float *r, int nx, int nr,
int p, p_team_t team);
void p_acorr_f32(const float *x, float *r, int nx, int nr,
int p, p_team_t team);

/*convolution: r[j] = sum ( h[k] * x[j-k), k=0..(nh-1) */
void p_conv_f32(float *x, float *h, float *r, int nr, int nh,
int p, p_team_t team);
void p_conv_f32(const float *x, const float *h, float *r, int nr, int nh,
int p, p_team_t team);

/*cross correlation: r[j] = sum ( x[j+k] * y[k]), k=0..(nx+ny-1) */
/* NOTE: current implementation requires nx >= ny */
void p_xcorr_f32(float *x, float *y, float *r, int nx, int ny,
int p, p_team_t team);
void p_xcorr_f32(const float *x, const float *y, float *r, int nx, int ny,
int p, p_team_t team);

/*FIR filter direct form: r[j] = sum ( h[k] * x [j-k]), k=0..(nh-1) */
void p_fir_f32(float *x, float *h, float *r, int nx, int nh,
int p, p_team_t team);
void p_fir_f32(const float *x, const float *h, float *r, int nx, int nh,
int p, p_team_t team);

/*FIR filter with decimation: r[j] = sum ( h[k] * x [j*D-k]), k=0..(nh-1) */
void p_firdec_f32(float *x, float *h, float *r, int nx, int nh, int df,
int p, p_team_t team);
void p_firdec_f32(const float *x, const float *h, float *r,
int nx, int nh, int df, int p, p_team_t team);

/*FIR filter with inerpolation: r[j] = sum ( h[k] * x [j*D-k]), k=0..(nh-1) */
void p_firint_f32(float *x, float *h, float *r, int nx, int nh, int ifactor,
int p, p_team_t team);
void p_firint_f32(const float *x, const float *h, float *r,
int nx, int nh, int ifactor, int p, p_team_t team);

/*FIR symmetric form: */
void p_firsym_f32(float *x, float *h, float *r, int nx, int nh,
int p, p_team_t team);
void p_firsym_f32(const float *x, const float *h, float *r, int nx, int nh,
int p, p_team_t team);

/*IIR filter: */
void p_iir_f32(float *x, float *h, float *r, int nb, int nr,
int p, p_team_t team);
void p_iir_f32(const float *x, const float *h, float *r, int nb, int nr,
int p, p_team_t team);
40 changes: 20 additions & 20 deletions include/pal_image.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,41 +18,41 @@
*/

/*2d convolution */
void p_conv2d_f32(float *x, float *m, float *r, int rows, int cols, int msize,
int p, p_team_t team);
void p_conv2d_f32(const float *x, float *m, float *r, int rows, int cols,
int msize, int p, p_team_t team);

/*2d box (i.e mean) filter(3x3) */
void p_box3x3_f32(float *x, float *r, int rows, int cols,
int p, p_team_t team);
void p_box3x3_f32(const float *x, float *r, int rows, int cols,
int p, p_team_t team);

/*2d gauss filter (3x3) */
void p_gauss3x3_f32(float *x, float *r, int rows, int cols,
int p, p_team_t team);
void p_gauss3x3_f32(const float *x, float *r, int rows, int cols,
int p, p_team_t team);

/*2d median filter (3x3) */
void p_median3x3_f32(float *x, float *r, int rows, int cols,
int p, p_team_t team);
void p_median3x3_f32(const float *x, float *r, int rows, int cols,
int p, p_team_t team);

/*2d laplace filter (3x3) */
void p_laplace3x3_f32(float *x, float *r, int rows, int cols,
int p, p_team_t team);
void p_laplace3x3_f32(const float *x, float *r, int rows, int cols,
int p, p_team_t team);

/*2d scharr filter (3x3) */
void p_scharr3x3_f32(float *x, float *r, int rows, int cols,
int p, p_team_t team);
void p_scharr3x3_f32(const float *x, float *r, int rows, int cols,
int p, p_team_t team);

/*2d prewitt filter (3x3) */
void p_prewitt3x3_f32(float *x, float *r, int rows, int cols,
int p, p_team_t team);
void p_prewitt3x3_f32(const float *x, float *r, int rows, int cols,
int p, p_team_t team);

/*2d sum of absolute differences (8x8), returns scalar */
void p_sad8x8_f32(float *x, float *m, float *r, int cols,
int p, p_team_t team);
void p_sad8x8_f32(const float *x, float *m, float *r, int cols,
int p, p_team_t team);

/*2d sum of absolute differences (16x16), returns scalar */
void p_sad16x16_f32(float *x, float *m, float *r, int cols,
int p, p_team_t team);
void p_sad16x16_f32(const float *x, float *m, float *r, int cols,
int p, p_team_t team);

/*2d sobel filter (3x3) */
void p_sobel3x3_f32(float *x, float *r, int rows, int cols,
int p, p_team_t team);
void p_sobel3x3_f32(const float *x, float *r, int rows, int cols,
int p, p_team_t team);
102 changes: 58 additions & 44 deletions include/pal_math.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,103 +101,113 @@
*/

/*integer to float conversion*/
void p_itof(int *a, float *c, int n, int p, p_team_t team);
void p_itof(const int *a, float *c, int n, int p, p_team_t team);

This comment has been minimized.

Copy link
@elfring

elfring Jun 8, 2015

Is it a bit more efficient to pass an integral value instead of a pointer?


/*float to integer conversion*/
void p_ftoi(float *a, int *c, int n, int p, p_team_t team);
void p_ftoi(const float *a, int *c, int n, int p, p_team_t team);

/*absolute value c = abs ( a ) */
void p_abs_f32(float *a, float *c, int n, int p, p_team_t team);
void p_abs_f32(const float *a, float *c, int n, int p, p_team_t team);

/*arc cosine: c = acos ( a ) */
void p_acos_f32(float *a, float *c, int n, int p, p_team_t team);
void p_acos_f32(const float *a, float *c, int n, int p, p_team_t team);

/*arc hyperbolic cosine, c = acosh ( a ) */
void p_acosh_f32(float *a, float *c, int n, int p, p_team_t team);
void p_acosh_f32(const float *a, float *c, int n, int p, p_team_t team);

/*arc sine: c = asin ( a ) */
void p_asin_f32(float *a, float *c, int n, int p, p_team_t team);
void p_asin_f32(const float *a, float *c, int n, int p, p_team_t team);

/*arc hyperbolic sine: c = asinh ( a ) */
void p_asinh_f32(float *a, float *c, int n, int p, p_team_t team);
void p_asinh_f32(const float *a, float *c, int n, int p, p_team_t team);

/*arc tanget: c = atan ( a ) */
void p_atan_f32(float *a, float *c, int n, int p, p_team_t team);
void p_atan_f32(const float *a, float *c, int n, int p, p_team_t team);

/*arc tangent of b/a: c = atan2 ( a , b) */
void p_atan2_f32(float *a, float *b, float *c, int n, int p, p_team_t team);
void p_atan2_f32(const float *a, const float *b, float *c,
int n, int p, p_team_t team);

/*arc hyperbolic tanget: c = atanh ( a ) */
void p_atanh_f32(float *a, float *c, int n, int p, p_team_t team);
void p_atanh_f32(const float *a, float *c, int n, int p, p_team_t team);

/*cubic root of a: c = cbrt ( a) */
void p_cbrt_f32(float *a, float *c, int n, int p, p_team_t team);
void p_cbrt_f32(const float *a, float *c, int n, int p, p_team_t team);

/*cosine: c = cos ( a ) */
void p_cos_f32(float *a, float *c, int n, int p, p_team_t team);
void p_cos_f32(const float *a, float *c, int n, int p, p_team_t team);

/*hyperpolic cosine: c = cosh ( a ) */
void p_cosh_f32(float *a, float *c, int n, int p, p_team_t team);
void p_cosh_f32(const float *a, float *c, int n, int p, p_team_t team);

/*division: c = a ./ b */
void p_div_f32(float *a, float *b, float *c, int n, int p, p_team_t team);
void p_div_f32(const float *a, const float *b, float *c,
int n, int p, p_team_t team);

/*exponential: c = exp ( a ) */
void p_exp_f32(float *a, float *c, int n, int p, p_team_t team);
void p_exp_f32(const float *a, float *c, int n, int p, p_team_t team);

/*inverse: c = 1 / ( a ) */
void p_inv_f32(float *a, float *c, int n, int p, p_team_t team);
void p_inv_f32(const float *a, float *c, int n, int p, p_team_t team);

/*inverse cube root: c = 1 / cbrt ( a ) */
void p_invcbrt_f32(float *a, float *c, int n, int p, p_team_t team);
void p_invcbrt_f32(const float *a, float *c, int n, int p, p_team_t team);

/*inverse square root c = 1 / sqrt ( a ) */
void p_invsqrt_f32(float *a, float *c, int n, int p, p_team_t team);
void p_invsqrt_f32(const float *a, float *c, int n, int p, p_team_t team);

/*natural logarithm: c = ln ( a ) */
void p_ln_f32(float *a, float *c, int n, int p, p_team_t team);
void p_ln_f32(const float *a, float *c, int n, int p, p_team_t team);

/*denary logarithm: c = log10 ( a ) */
void p_log10_f32(float *a, float *c, int n, int p, p_team_t team);
void p_log10_f32(const float *a, float *c, int n, int p, p_team_t team);

/*element raised to a power: c = pow ( a , b ) */
void p_pow_f32(float *a, float *b, float *c, int n, int p, p_team_t team);
void p_pow_f32(const float *a, const float *b, float *c,
int n, int p, p_team_t team);

/*sine: c = sin ( a ) */
void p_sin_f32(float *a, float *c, int n, int p, p_team_t team);
void p_sin_f32(const float *a, float *c, int n, int p, p_team_t team);

/*computes sin and cos of a: c = sin ( a ), z = cos ( a ) */
void p_sincos_f32(float *a, float *c, float *z, int n, int p, p_team_t team);
void p_sincos_f32(const float *a, float *c, float *z,
int n, int p, p_team_t team);

/*hyperbolic Sine: c = sinh ( a ) */
void p_sinh_f32(float *a, float *c, int n, int p, p_team_t team);
void p_sinh_f32(const float *a, float *c, int n, int p, p_team_t team);

/*square root c = sqrt ( a ) */
void p_sqrt_f32(float *a, float *c, int n, int p, p_team_t team);
void p_sqrt_f32(const float *a, float *c, int n, int p, p_team_t team);

/*tangent: c = tan ( a ) */
void p_tan_f32(float *a, float *c, int n, int p, p_team_t team);
void p_tan_f32(const float *a, float *c, int n, int p, p_team_t team);

/*hyperbolic tangent, c = tanh ( a ) */
void p_tanh_f32(float *a, float *c, int n, int p, p_team_t team);
void p_tanh_f32(const float *a, float *c, int n, int p, p_team_t team);

/*dot product: c = sum ( a[n-1:0] * b[n-1:0] ) */
void p_dot_f32(float *a, float *b, float *c, int n, int p, p_team_t team);
void p_dot_f32(const float *a, const float *b, float *c,
int n, int p, p_team_t team);

/*absolute difference: c = | a[n-1:0] - b[n-1:0] | */
void p_absdiff_f32(float *a, float *b, float *c, int n, int p, p_team_t team);
void p_absdiff_f32(const float *a, const float *b, float *c,
int n, int p, p_team_t team);

/*add vectors: c = a[n-1:0] + b[n-1:0] */
void p_add_f32(float *a, float *b, float *c, int n, int p, p_team_t team);
void p_add_f32(const float *a, const float *b, float *c,
int n, int p, p_team_t team);

/*subtract vectors: c = a[n-1:0] - b[n-1:0] */
void p_sub_f32(float *a, float *b, float *c, int n, int p, p_team_t team);
void p_sub_f32(const float *a, const float *b, float *c,
int n, int p, p_team_t team);

/*multiply vectors: c = a[n-1:0] - b[n-1:0] */
void p_mul_f32(float *a, float *b, float *c, int n, int p, p_team_t team);
void p_mul_f32(const float *a, const float *b, float *c,
int n, int p, p_team_t team);

/* Element wise multiply accumulate: c += a * b' */
void p_mac_f32(float *a, float *b, float *c, int n, int p, p_team_t team);
void p_mac_f32(const float *a, const float *b, float *c,
int n, int p, p_team_t team);

/*
****************************************************************
Expand All @@ -213,25 +223,27 @@ void p_mac_f32(float *a, float *b, float *c, int n, int p, p_team_t team);
*/

/*sum: c = sum ( a[n-1:0] ) */
void p_sum_f32(float *a, float *c, int n, int p, p_team_t team);
void p_sum_f32(const float *a, float *c, int n, int p, p_team_t team);

/*sum of squares: c = sum( a[n-1:0]^2 ) */
void p_sumsq_f32(float *a, float *c, int n, int p, p_team_t team);
void p_sumsq_f32(const float *a, float *c, int n, int p, p_team_t team);

/*mean: c = sum ( a[n-1:0] ) / n */
void p_mean_f32(float *a, float *c, int n, int p, p_team_t team);
void p_mean_f32(const float *a, float *c, int n, int p, p_team_t team);

/*middle value: c = median ( a[n-1:0] ) */
void p_median_f32(float *a, float *c, int n, int p, p_team_t team);
void p_median_f32(const float *a, float *c, int n, int p, p_team_t team);

/*most common number: c = mode ( a[n-1:0] ) */
void p_mode_f32(float *a, float *c, int n, int p, p_team_t team);
void p_mode_f32(const float *a, float *c, int n, int p, p_team_t team);

/*find max value and its index from input vector */
void p_max_f32(float *a, float *c, int *index, int n, int p, p_team_t team);
void p_max_f32(const float *a, float *c, int *index,
int n, int p, p_team_t team);

/*find min value and its index from input vector */
void p_min_f32(float *a, float *c, int *index, int n, int p, p_team_t team);
void p_min_f32(const float *a, float *c, int *index,
int n, int p, p_team_t team);

/*
****************************************************************
Expand All @@ -241,8 +253,8 @@ void p_min_f32(float *a, float *c, int *index, int n, int p, p_team_t team);
*/

/*sort an array*/
void p_sort_f32(float *a, float *c, int n, int p, p_team_t team);
void p_sort_u32(uint32_t *a, uint32_t *c, int n, int p, p_team_t team);
void p_sort_f32(const float *a, float *c, int n, int p, p_team_t team);
void p_sort_u32(const uint32_t *a, uint32_t *c, int n, int p, p_team_t team);

/* seed pseudo-random number generator */
void p_srand(unsigned int seed);
Expand All @@ -251,5 +263,7 @@ void p_srand(unsigned int seed);
int p_rand(void);

/*population count*/
void p_popcount_u32(uint32_t *a, uint32_t *c, int n, int p, p_team_t team);
void p_popcount_u64(uint64_t *a, uint64_t *c, int n, int p, p_team_t team);
void p_popcount_u32(const uint32_t *a, uint32_t *c,
int n, int p, p_team_t team);
void p_popcount_u64(const uint64_t *a, uint64_t *c,
int n, int p, p_team_t team);
2 changes: 1 addition & 1 deletion src/base/p_atomic_swap.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/*This is the generic version of an atomic exchange. It stores the contents of
* *val into *ptr. The original value of *ptr is copied into *ret*/

int p_atomic_swap_u32(p_atom_t atom, uint32_t *input)
int p_atomic_swap_u32(p_atom_t atom, const uint32_t *input)
{

/*PLACE CODE HERE*/
Expand Down
2 changes: 1 addition & 1 deletion src/base/p_load.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
* @return Returns a reference. Negative value indicates error.
*
*/
p_prog_t p_load(p_dev_t dev, char *file, char *function, int flags)
p_prog_t p_load(p_dev_t dev, const char *file, const char *function, int flags)
{
size_t len;
struct prog *prog;
Expand Down
2 changes: 1 addition & 1 deletion src/base/p_memcpy.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
#include "pal_base.h"
#include "pal_base_private.h"

ssize_t p_memcpy(void *dst, void *src, size_t nb, int flags)
ssize_t p_memcpy(void *dst, const void *src, size_t nb, int flags)
{
/*PLACE CODE HERE*/
return (0);
Expand Down
2 changes: 1 addition & 1 deletion src/base/p_run.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
#include "pal_base_private.h"

int p_run(p_prog_t prog, p_team_t team, int start, int size, int nargs,
char *args[], int flags)
const char *args[], int flags)
{
int err;
struct team *pteam = (struct team *) team;
Expand Down
Loading

0 comments on commit d445189

Please sign in to comment.