Project Ne10
An open, optimized software library for the ARM architecture.
NE10_sample_intro.c
Go to the documentation of this file.
1 /*
2  * Copyright 2011-16 ARM Limited and Contributors.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  * * Redistributions of source code must retain the above copyright
8  * notice, this list of conditions and the following disclaimer.
9  * * Redistributions in binary form must reproduce the above copyright
10  * notice, this list of conditions and the following disclaimer in the
11  * documentation and/or other materials provided with the distribution.
12  * * Neither the name of ARM Limited nor the
13  * names of its contributors may be used to endorse or promote products
14  * derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY ARM LIMITED AND CONTRIBUTORS "AS IS" AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19  * DISCLAIMED. IN NO EVENT SHALL ARM LIMITED AND CONTRIBUTORS BE LIABLE FOR ANY
20  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 #include <stdio.h>
28 #include <stdlib.h>
29 
30 #include "NE10.h"
31 
32 #define ARR_LEN 5
33 
40 /*
41  * A simple example of using `ne10_addc_float`, an Ne10 function pointer that
42  * gets dynamically initialised to the most appropriate function for the hardware.
43  */
44 void test_add_dynamic(void)
45 {
46  ne10_float32_t src[ARR_LEN]; // A source array of scalar floats
47  ne10_float32_t cst; // A constant scalar to add to the elements in `src`
48  ne10_float32_t dst[ARR_LEN]; // A destination array for the results of the addition
49 
50  // Generate test input values for `src` and `cst` using `rand()`
51  for (int i = 0; i < ARR_LEN; i++)
52  {
53  src[i] = (ne10_float32_t)rand() / RAND_MAX * 5.0f;
54  }
55  cst = (ne10_float32_t)rand() / RAND_MAX * 5.0f;
56 
57  // Perform the operation! This will use the NEON-optimised version of the function
58  // if NEON hardware has been detected, or will otherwise fall back to the C version.
59  ne10_addc_float(dst, src, cst, ARR_LEN);
60 
61  // Display the results
62  printf("test_intro[test_add_dynamic]:\n");
63  for (int i = 0; i < ARR_LEN; i++)
64  {
65  printf("\tne10_addc_float:\t%f + %f = %f\n", src[i], cst, dst[i]);
66  }
67 }
68 
69 /*
70  * A simple example of calling the C and NEON specific versions of Ne10 functions
71  * directly -- in this case, `ne10_addc_float_c` and `ne10_addc_float_neon`.
72  */
73 void test_add_static(void)
74 {
76  ne10_float32_t cst;
77  ne10_float32_t dst_c[ARR_LEN];
78  ne10_float32_t dst_neon[ARR_LEN];
79 
80  for (int i = 0; i < ARR_LEN; i++)
81  {
82  src[i] = (ne10_float32_t)rand() / RAND_MAX * 5.0f;
83  }
84  cst = (ne10_float32_t)rand() / RAND_MAX * 5.0f;
85 
86  ne10_addc_float_c(dst_c, src, cst, ARR_LEN);
87  ne10_addc_float_neon(dst_neon, src, cst, ARR_LEN);
88 
89  printf("test_intro[test_add_static]:\n");
90  for (int i = 0; i < ARR_LEN; i++)
91  {
92  printf("\tne10_addc_float_c:\t%f + %f = %f\n", src[i], cst, dst_c[i]);
93  printf("\tne10_addc_float_neon:\t%f + %f = %f\n", src[i], cst, dst_neon[i]);
94  }
95 }
96 
97 /*
98  * The entry point of this sample program (like `main` in regular C).
99  */
101 {
102  // Initialise Ne10, using hardware auto-detection to set function pointers such as
103  // `ne10_addc_float` to point to the Ne10 code best optimised for this machine. (There
104  // is no need to do this if the C or NEON versions of all Ne10 functions are called
105  // directly rather than through function pointers.)
106  if (ne10_init() != NE10_OK)
107  {
108  fprintf(stderr, "Failed to initialise Ne10.\n");
109  return 1;
110  }
111 
112  test_add_dynamic();
113  test_add_static();
114 
115  return 0;
116 }
float ne10_float32_t
Definition: NE10_types.h:80
ne10_result_t ne10_addc_float_neon(ne10_float32_t *dst, ne10_float32_t *src, const ne10_float32_t cst, ne10_uint32_t count) asm("ne10_addc_float_neon")
Specific implementation of ne10_addc_float using NEON intrinsics.
ne10_result_t ne10_init(void)
Definition: NE10_init.c:44
int intro_sample_main(void)
ne10_result_t ne10_addc_float_c(ne10_float32_t *dst, ne10_float32_t *src, const ne10_float32_t cst, ne10_uint32_t count)
Specific implementation of ne10_addc_float using plain C code.
Definition: NE10_addc.c:37
ne10_result_t(* ne10_addc_float)(ne10_float32_t *dst, ne10_float32_t *src, const ne10_float32_t cst, ne10_uint32_t count)
Adds a constant scalar value to all elements of an input array, storing the results in an output arra...
#define NE10_OK
Definition: NE10_types.h:65
#define ARR_LEN