File: nan.cl

package info (click to toggle)
pocl 1.6-5
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 17,816 kB
  • sloc: lisp: 135,476; ansic: 64,403; cpp: 32,196; vhdl: 1,040; sh: 382; python: 336; makefile: 151; pascal: 140; java: 72; xml: 49
file content (55 lines) | stat: -rw-r--r-- 2,591 bytes parent folder | download | duplicates (5)
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
/* OpenCL built-in library: nan()

   Copyright (c) 2011 Erik Schnetter <eschnetter@perimeterinstitute.ca>
                      Perimeter Institute for Theoretical Physics
   
   Permission is hereby granted, free of charge, to any person obtaining a copy
   of this software and associated documentation files (the "Software"), to deal
   in the Software without restriction, including without limitation the rights
   to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
   copies of the Software, and to permit persons to whom the Software is
   furnished to do so, subject to the following conditions:
   
   The above copyright notice and this permission notice shall be included in
   all copies or substantial portions of the Software.
   
   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
   IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
   AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
   LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
   OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
   THE SOFTWARE.
*/

#include "templates.h"

/* Fall-back implementation which ignores the nancode */
// DEFINE_EXPR_V_U(nan, (vtype)NAN)

DEFINE_EXPR_V_U(nan,
                ({
                  utype nancode = a;
                  // number of bits in the mantissa
                  sutype mant_dig =
                    TYPED_CONST(sutype,
                                HALF_MANT_DIG, FLT_MANT_DIG, DBL_MANT_DIG) - 1;
                  // mask for the mantissa
                  sutype nan_mant_mask = (1 << mant_dig) - 1;
                  // mask out bits that can't be stored in the mantissa
                  // this also ensures the sign bit is zero,
                  // i.e. that the nan is quiet
                  nancode &= nan_mant_mask;
                  // ensure the nancode is not zero
                  nancode = nancode ? nancode : (sutype)1;
                  // create the exponent
                  sutype nan_exp =
                    TYPED_CONST(sutype,
                                HALF_MAX_EXP - HALF_MIN_EXP,
                                FLT_MAX_EXP - FLT_MIN_EXP,
                                DBL_MAX_EXP - DBL_MIN_EXP) + 2;
                  nan_exp <<= mant_dig;
                  // combine exponent and nancode
                  utype val = nan_exp | nancode;
                  *(vtype*)&val;
                }))