Debian Codegraph @

src/traits.hpp

raw symbols SCIP
1 /* -*- coding: utf-8;mode:c++;c-file-style:"stroustrup" -*- */
2 /*
3 Licensed under the terms of the BSD 3-Clause
4 (see plotpy/__init__.py for details)
5
6
7 Two lines of the following code are distributed under LGPL license terms and
8 with a different copyright. These two lines are the Visual Studio x86_64
9 (_M_X64) inline versions of lrint() and lrintf() functions, and were adapted
10 from fast_convert.h (SpanDSP), which is:
11
12 Copyright © 2009 Steve Underwood
13 All rights reserved.
14
15 This program is free software; you can redistribute it and/or modify
16 it under the terms of the GNU Lesser General Public License version 2.1,
17 as published by the Free Software Foundation.
18
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU Lesser General Public License for more details.
23
24 */
25 #ifndef __TRAITS_HPP__
26 #define __TRAITS_HPP__
27
28 #include <math.h>
29
30 /* this class (real_trait) is used
31 to make the code somewhat independent of
32 the real type used (float or double).
33 we don't provide a template parameter
34 on the Point* class to avoid clutter but
35 changing a simple typedef is all it takes
36 to switch from float to double type
37 */
38
39 /* MSVC does not have lrint/lrintf */
40 #ifdef _MSC_VER
41 #if _MSC_VER < 1900 // (Visual Studio 2015 version 14.0)
42
43 #include <intrin.h>
44 __inline long int lrint(double f)
45 {
46 #ifdef _M_X64
47 return (long int)_mm_cvtsd_si64x(_mm_loadu_pd((const double *)&f));
48 #else
49 int i;
50 _asm
51 {
52 fld f
53 fistp i
54 }
55 return i;
56 #endif
57 }
58
59 __inline long int lrintf(float f)
60 {
61 #ifdef _M_X64
62 return _mm_cvt_ss2si(_mm_load_ss((const float *)&f));
63 #else
64 int i;
65 _asm
66 {
67 fld f
68 fistp i
69 }
70 return i;
71 #endif
72 }
73 #endif
74 #endif
75
76 typedef int fixed;
77
78 template <typename T>
79 struct num_trait
80 {
81 typedef T value_type;
82 typedef long large_type;
83 static int toint(value_type v) { return (int)v; }
84 static value_type fromdouble(double v) { return (value_type)v; }
85
86 static const bool is_integer = true;
87 };
88
89 template <>
90 struct num_trait<float>
91 {
92 typedef float value_type;
93 typedef float large_type;
94 static int toint(value_type v) { return lrintf(v); }
95 static value_type fromdouble(double v) { return (value_type)v; }
96
97 static const bool is_integer = false;
98 };
99
100 template <>
101 struct num_trait<double>
102 {
103 typedef double value_type;
104 typedef double large_type;
105 static int toint(value_type v) { return lrint(v); }
106 static value_type fromdouble(double v) { return (value_type)v; }
107
108 static const bool is_integer = false;
109 };
110
111 template <>
112 struct num_trait<fixed>
113 {
114 typedef fixed value_type;
115 typedef fixed large_type;
116 static int toint(value_type v) { return (v >> 15); }
117 static value_type fromdouble(double v) { return lrint(v * 32768.); }
118
119 static const bool is_integer = false;
120 };
121
122 template <class A>
123 static void dispatch_array(int npy_type, A &algo)
124 {
125 switch (npy_type)
126 {
127 case NPY_FLOAT32:
128 algo.template run<npy_float32>();
129 break;
130 case NPY_FLOAT64:
131 algo.template run<npy_float64>();
132 break;
133 case NPY_UINT64:
134 algo.template run<npy_uint64>();
135 break;
136 case NPY_INT64:
137 algo.template run<npy_int64>();
138 break;
139 case NPY_UINT32:
140 algo.template run<npy_uint32>();
141 break;
142 case NPY_INT32:
143 algo.template run<npy_int32>();
144 break;
145 case NPY_UINT16:
146 algo.template run<npy_uint16>();
147 break;
148 case NPY_INT16:
149 algo.template run<npy_int16>();
150 break;
151 case NPY_UINT8:
152 algo.template run<npy_uint8>();
153 break;
154 case NPY_INT8:
155 algo.template run<npy_int8>();
156 break;
157 case NPY_BOOL:
158 algo.template run<npy_bool>();
159 break;
160 }
161 }
162 #endif