summaryrefslogtreecommitdiff
path: root/apps/examples/ostest/fpu.c
blob: 56cee45eed12d2762c314fe8cffe453475875ff3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/***********************************************************************
 * apps/examples/ostest/fpu.c
 *
 *   Copyright (C) 2012 Gregory Nutt. All rights reserved.
 *   Author: Gregory Nutt <gnutt@nuttx.org>
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 * 3. Neither the name NuttX nor the names of its contributors may be
 *    used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 *
 ***********************************************************************/

/***********************************************************************
 * Included Files
 ***********************************************************************/

#include <nuttx/config.h>
#include <sys/wait.h>

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sched.h>

#include "ostest.h"

/***********************************************************************
 * Pre-processor definitions
 ***********************************************************************/
/* Configuration *******************************************************/

#undef HAVE_FPU
#ifdef CONFIG_ARCH_FPU
#  if defined(CONFIG_EXAMPLES_OSTEST_FPUSIZE) && defined(CONFIG_SCHED_WAITPID) && !defined(CONFIG_DISABLE_SIGNALS)
#    define HAVE_FPU 1
#  else
#    ifndef CONFIG_EXAMPLES_OSTEST_FPUSIZE
#      warning "FPU test not built; CONFIG_EXAMPLES_OSTEST_FPUSIZE not defined"
#    endif
#    ifndef CONFIG_SCHED_WAITPID
#      warning "FPU test not built; CONFIG_SCHED_WAITPID not defined"
#    endif
#    ifdef CONFIG_DISABLE_SIGNALS
#      warning "FPU test not built; CONFIG_DISABLE_SIGNALS defined"
#    endif
#  endif
#endif

#ifdef HAVE_FPU

#ifndef CONFIG_EXAMPLES_OSTEST_FPULOOPS
#  define CONFIG_EXAMPLES_OSTEST_FPULOOPS 16
#endif

#ifndef CONFIG_EXAMPLES_OSTEST_FPUMSDELAY
#  define CONFIG_EXAMPLES_OSTEST_FPUMSDELAY 750
#endif

#ifndef CONFIG_EXAMPLES_OSTEST_FPUPRIORITY
#  define CONFIG_EXAMPLES_OSTEST_FPUPRIORITY SCHED_PRIORITY_DEFAULT
#endif

#ifndef CONFIG_EXAMPLES_OSTEST_FPUSTACKSIZE
#  define CONFIG_EXAMPLES_OSTEST_FPUSTACKSIZE 2048
#endif

/* Other defintions ****************************************************/
/* We'll keep all data using 32-bit values only to force 32-bit alignment.
 * This logic has not real notion of the underlying representation.
 */

#define FPU_WORDSIZE ((CONFIG_EXAMPLES_OSTEST_FPUSIZE+3)>>2)

#ifndef NULL
# define NULL (void*)0
#endif

/***********************************************************************
 * External Dependencies
 ***********************************************************************/
/* This test is very dependent on support provided by the chip/board-
 * layer logic.  In particular, it expects the following functions
 * to be provided:
 */

/* Given an array of size CONFIG_EXAMPLES_OSTEST_FPUSIZE, this function
 * will return the current FPU registers.
 */

extern void arch_getfpu(FAR uint32_t *fpusave);

/* Given two arrays of size CONFIG_EXAMPLES_OSTEST_FPUSIZE this
 * function will compare then an return true if they are identical.
 */

extern bool arch_cmpfpu(FAR const uint32_t *fpusave1,
                        FAR const uint32_t *fpusave2);

/***********************************************************************
 * Private Data
 ***********************************************************************/

static uint8_t g_fpuno;

/***********************************************************************
 * Private Functions
 ***********************************************************************/

static void fpu_dump(FAR uint32_t *buffer, FAR const char *msg)
{
  int i, j, k;

  printf("%s (%p):\n", msg, buffer);
  for (i = 0; i < FPU_WORDSIZE; i += 8)
    {
      printf("    %04x: ", i);
      for (j = 0; j < 8; j++)
        {
          k = i + j;

          if (k < FPU_WORDSIZE)
            {
              printf("%08x ", buffer[k]);
            }
          else
            {
              printf("\n");
              break;
            }
        }
      printf("\n");
   }
}

static int fpu_task(int argc, char *argv[])
{
  uint32_t fpusave1[FPU_WORDSIZE];
  uint32_t fpusave2[FPU_WORDSIZE];
  double val1;
  double val2;
  double val3;
  double val4;
  int id;
  int i;

  /* Which are we? */

  sched_lock();
  id = (int)(++g_fpuno);
  sched_unlock();

  /* Seed the flowing point values */

  val1 = (double)id;

  for (i = 0; i < CONFIG_EXAMPLES_OSTEST_FPULOOPS; i++)
    {
      printf("FPU#%d: pass %d\n", id, i+1);
      fflush(stdout);

      /* Set the FPU register save arrays to a known-but-illogical values so 
       * that we can verify that reading of the registers actually occurs.
       */

      memset(fpusave1, 0xff, sizeof(fpusave1));
      memset(fpusave2, 0xff, sizeof(fpusave1));

      /* Prevent context switches while we set up some stuff */

      sched_lock();

      /* Do some trivial floating point operations that should cause some
       * changes to floating point resters
       */

      val4 = 3.1415926 * val1;     /* Multiple by Pi */
      val3 = val4 + 1.61803398874; /* Add the golden ratio */
      val2 = val3 / 2.7182;        /* Divide by Euler's constant */
      val1 = val2 + 1.0;           /* Plus one */
 
      /* Sample the floating point registers */

      arch_getfpu(fpusave1);

      /* Re-read and verify the FPU registers consistently without corruption */

      arch_getfpu(fpusave2);
      if (!arch_cmpfpu(fpusave1, fpusave2))
        {
          printf("ERROR FPU#%d: fpusave1 and fpusave2 do not match\n", id);
          fpu_dump(fpusave1, "Values after math operations (fpusave1)");
          fpu_dump(fpusave2, "Values after verify re-read (fpusave2)");
          return EXIT_FAILURE;
        }

      /* Now unlock and sleep for a while -- this should result in some context switches */

      sched_unlock();
      usleep(CONFIG_EXAMPLES_OSTEST_FPUMSDELAY * 1000);

      /* Several context switches should have occurred.  Now verify that the floating
       * point registers are still correctly set.
       */

      arch_getfpu(fpusave2);
      if (!arch_cmpfpu(fpusave1, fpusave2))
        {
          printf("ERROR FPU#%d: fpusave1 and fpusave2 do not match\n", id);
          fpu_dump(fpusave1, "Values before waiting (fpusave1)");
          fpu_dump(fpusave2, "Values after waiting (fpusave2)");
          return EXIT_FAILURE;
        }
    }

  printf("FPU#%d: Succeeded\n", id);
  fflush(stdout);
  return EXIT_SUCCESS;
}
#endif /* HAVE_FPU */

/***********************************************************************
 * Private Functions
 ***********************************************************************/

void fpu_test(void)
{
#ifdef HAVE_FPU
  pid_t task1;
  pid_t task2;
  int statloc;

  /* Start two two tasks */

  g_fpuno = 0;
  printf("Starting task FPU#1\n");
  task1 = TASK_CREATE("FPU#1", CONFIG_EXAMPLES_OSTEST_FPUPRIORITY, CONFIG_EXAMPLES_OSTEST_FPUSTACKSIZE, fpu_task, NULL);
  if (task1 < 0)
    {
      printf("fpu_test: ERROR Failed to start task FPU#1\n");
    }
  else
    {
      printf("fpu_test: Started task FPU#1 at PID=%d\n", task1);
    }
  fflush(stdout);
  usleep(250);

  printf("Starting task FPU#2\n");
  task2 = TASK_CREATE("FPU#2", CONFIG_EXAMPLES_OSTEST_FPUPRIORITY, CONFIG_EXAMPLES_OSTEST_FPUSTACKSIZE, fpu_task, NULL);
  if (task2 < 0)
    {
      printf("fpu_test: ERROR Failed to start task FPU#1\n");
    }
  else
    {
      printf("fpu_test: Started task FPU#2 at PID=%d\n", task2);
    }

  /* Wait for each task to complete */

  fflush(stdout);
  (void)waitpid(task1, &statloc, 0);
  (void)waitpid(task2, &statloc, 0);

#else
  printf("fpu_test: ERROR: The FPU test is not properly configured\n");
#endif
  printf("fpu_test: Returning\n");
}