/
TypeConversionMacros.scala
239 lines (187 loc) · 8.13 KB
/
TypeConversionMacros.scala
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
/*
* Copyright 2016 Azavea
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package geotrellis.macros
import scala.reflect.macros.whitebox.Context
object TypeConversionMacros {
def b2ub_impl(c: Context)(n: c.Expr[Byte]): c.Expr[Byte] = {
import c.universe._
c.Expr(q"""{ val n = $n ; if(n == Byte.MinValue) { 0.toByte } else { n } }""")
}
def b2s_impl(c: Context)(n: c.Expr[Byte]): c.Expr[Short] = {
import c.universe._
c.Expr(q"""{ val n = $n ; if(n == Byte.MinValue) { Short.MinValue } else { n.toShort } }""")
}
def b2us_impl(c: Context)(n: c.Expr[Byte]): c.Expr[Short] = {
import c.universe._
c.Expr(q"""{ val n = $n ; if(n == Byte.MinValue) { 0.toShort } else { n.toShort } }""")
}
def b2i_impl(c: Context)(n: c.Expr[Byte]): c.Expr[Int] = {
import c.universe._
c.Expr(q"""{ val n = $n ; if(n == Byte.MinValue) { Int.MinValue } else { n.toInt } }""")
}
def b2f_impl(c: Context)(n: c.Expr[Byte]): c.Expr[Float] = {
import c.universe._
c.Expr(q"""{ val n = $n ; if(n == Byte.MinValue) { Float.NaN } else { $n.toFloat } }""")
}
def b2d_impl(c: Context)(n: c.Expr[Byte]): c.Expr[Double] = {
import c.universe._
c.Expr(q"""{ val n = $n ; if(n == Byte.MinValue) { Double.NaN } else { n.toDouble } }""")
}
def ub2b_impl(c: Context)(n: c.Expr[Byte]): c.Expr[Byte] = {
import c.universe._
c.Expr(q"""{ val n = $n ; if(n == 0.toByte) { Byte.MinValue } else { n } }""")
}
def ub2s_impl(c: Context)(n: c.Expr[Byte]): c.Expr[Short] = {
import c.universe._
c.Expr(q"""{ val n = $n ; if(n == 0.toByte) { Short.MinValue } else { (n & 0xFF).toShort } }""")
}
def ub2us_impl(c: Context)(n: c.Expr[Byte]): c.Expr[Short] = {
import c.universe._
c.Expr(q"""{ ($n & 0xFF).toShort }""")
}
def ub2i_impl(c: Context)(n: c.Expr[Byte]): c.Expr[Int] = {
import c.universe._
c.Expr(q"""{ val n = $n ; if(n == 0.toByte) { Int.MinValue } else { n & 0xFF } }""")
}
def ub2f_impl(c: Context)(n: c.Expr[Byte]): c.Expr[Float] = {
import c.universe._
c.Expr(q"""{ val n = $n ; if(n == 0.toByte) { Float.NaN } else { (n & 0xFF).toFloat } }""")
}
def ub2d_impl(c: Context)(n: c.Expr[Byte]): c.Expr[Double] = {
import c.universe._
c.Expr(q"""{ val n = $n ; if(n == 0.toByte) { Double.NaN } else { (n & 0xFF).toDouble } }""")
}
def s2b_impl(c: Context)(n: c.Expr[Short]): c.Expr[Byte] = {
import c.universe._
c.Expr(q"""{ val n = $n ; if(n == Short.MinValue) { Byte.MinValue } else { n.toByte } }""")
}
def s2ub_impl(c: Context)(n: c.Expr[Short]): c.Expr[Byte] = {
import c.universe._
c.Expr(q"""{ val n = $n ; if(n == Short.MinValue) { 0.toByte } else { n.toByte } }""")
}
def s2us_impl(c: Context)(n: c.Expr[Short]): c.Expr[Short] = {
import c.universe._
c.Expr(q"""{ val n = $n ; if(n == Short.MinValue) { 0.toShort } else { n } }""")
}
def s2i_impl(c: Context)(n: c.Expr[Short]): c.Expr[Int] = {
import c.universe._
c.Expr(q"""{ val n = $n ; if(n == Short.MinValue) { Int.MinValue } else { n.toInt } }""")
}
def s2f_impl(c: Context)(n: c.Expr[Short]): c.Expr[Float] = {
import c.universe._
c.Expr(q"""{ val n = $n ; if(n == Short.MinValue) { Float.NaN } else { n.toFloat } }""")
}
def s2d_impl(c: Context)(n: c.Expr[Short]): c.Expr[Double] = {
import c.universe._
c.Expr(q"""{ val n = $n ; if(n == Short.MinValue) { Double.NaN } else { n.toDouble } }""")
}
def us2b_impl(c: Context)(n: c.Expr[Short]): c.Expr[Byte] = {
import c.universe._
c.Expr(q"""{ val n = $n ; if(n == 0.toByte) { Byte.MinValue } else { n.toByte } }""")
}
def us2ub_impl(c: Context)(n: c.Expr[Short]): c.Expr[Byte] = {
import c.universe._
c.Expr(q"""{ $n.toByte }""")
}
def us2s_impl(c: Context)(n: c.Expr[Short]): c.Expr[Short] = {
import c.universe._
c.Expr(q"""{ val n = $n ; if(n == 0.toShort) { Short.MinValue } else { n } }""")
}
def us2i_impl(c: Context)(n: c.Expr[Short]): c.Expr[Int] = {
import c.universe._
c.Expr(q"""{ val n = $n ; if(n == 0.toShort) { Int.MinValue } else { n & 0xFFFF } }""")
}
def us2f_impl(c: Context)(n: c.Expr[Short]): c.Expr[Float] = {
import c.universe._
c.Expr(q"""{ val n = $n ; if(n == 0.toShort) { Float.NaN } else { (n & 0xFFFF).toFloat } }""")
}
def us2d_impl(c: Context)(n: c.Expr[Short]): c.Expr[Double] = {
import c.universe._
c.Expr(q"""{ val n = $n ; if(n == 0.toShort) { Double.NaN } else { (n & 0xFFFF).toDouble } }""")
}
def i2b_impl(c: Context)(n: c.Expr[Int]): c.Expr[Byte] = {
import c.universe._
c.Expr(q"""{ val n = $n ; if(n == Int.MinValue) { Byte.MinValue } else { n.toByte } }""")
}
def i2ub_impl(c: Context)(n: c.Expr[Int]): c.Expr[Byte] = {
import c.universe._
c.Expr(q"""{ val n = $n ; if(n == Int.MinValue) { 0.toByte } else { n.toByte } }""")
}
def i2s_impl(c: Context)(n: c.Expr[Int]): c.Expr[Short] = {
import c.universe._
c.Expr(q"""{ val n = $n ; if(n == Int.MinValue) { Short.MinValue } else { n.toShort } }""")
}
def i2us_impl(c: Context)(n: c.Expr[Int]): c.Expr[Short] = {
import c.universe._
c.Expr(q"""{ val n = $n ; if(n == Int.MinValue) { 0.toShort } else { n.toShort } }""")
}
def i2f_impl(c: Context)(n: c.Expr[Int]): c.Expr[Float] = {
import c.universe._
c.Expr(q"""{ val n = $n ; if(n == Int.MinValue) { Float.NaN } else { n.toFloat } }""")
}
def i2d_impl(c: Context)(n: c.Expr[Int]): c.Expr[Double] = {
import c.universe._
c.Expr(q"""{ val n = $n ; if(n == Int.MinValue) { Double.NaN } else { n.toDouble } }""")
}
def f2b_impl(c: Context)(n: c.Expr[Float]): c.Expr[Byte] = {
import c.universe._
c.Expr(q"""{ val n = $n ; if(java.lang.Float.isNaN(n)) { Byte.MinValue } else { n.toByte } }""")
}
def f2ub_impl(c: Context)(n: c.Expr[Float]): c.Expr[Byte] = {
import c.universe._
c.Expr(q"""{ val n = $n ; if(java.lang.Float.isNaN(n)) { 0.toByte } else { n.toByte } }""")
}
def f2s_impl(c: Context)(n: c.Expr[Float]): c.Expr[Short] = {
import c.universe._
c.Expr(q"""{ val n = $n ; if(java.lang.Float.isNaN(n)) { Short.MinValue } else { n.toShort } }""")
}
def f2us_impl(c: Context)(n: c.Expr[Float]): c.Expr[Short] = {
import c.universe._
c.Expr(q"""{ val n = $n ; if(java.lang.Float.isNaN(n)) { 0.toShort} else { n.toShort } }""")
}
def f2i_impl(c: Context)(n: c.Expr[Float]): c.Expr[Int] = {
import c.universe._
c.Expr(q"""{ val n = $n ; if(java.lang.Float.isNaN(n)) { Int.MinValue } else { n.toInt } }""")
}
def f2d_impl(c: Context)(n: c.Expr[Float]): c.Expr[Double] = {
import c.universe._
c.Expr(q"""$n.toDouble""")
}
def d2b_impl(c: Context)(n: c.Expr[Double]): c.Expr[Byte] = {
import c.universe._
c.Expr(q"""{ val n = $n ; if(java.lang.Double.isNaN(n)) { Byte.MinValue } else { n.toByte } }""")
}
def d2ub_impl(c: Context)(n: c.Expr[Double]): c.Expr[Byte] = {
import c.universe._
c.Expr(q"""{ val n = $n ; if(java.lang.Double.isNaN(n)) { 0.toByte } else { n.toByte } }""")
}
def d2s_impl(c: Context)(n: c.Expr[Double]): c.Expr[Short] = {
import c.universe._
c.Expr(q"""{ val n = $n ; if(java.lang.Double.isNaN(n)) { Short.MinValue } else { n.toShort } }""")
}
def d2us_impl(c: Context)(n: c.Expr[Double]): c.Expr[Short] = {
import c.universe._
c.Expr(q"""{ val n = $n ; if(java.lang.Double.isNaN(n)) { 0.toShort } else { n.toShort } }""")
}
def d2i_impl(c: Context)(n: c.Expr[Double]): c.Expr[Int] = {
import c.universe._
c.Expr(q"""{ val n = $n ; if(java.lang.Double.isNaN(n)) { Int.MinValue } else { n.toInt } }""")
}
def d2f_impl(c: Context)(n: c.Expr[Double]): c.Expr[Float] = {
import c.universe._
c.Expr(q"""$n.toFloat""")
}
}