forked from couchbaselabs/gojsonsm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fastval_math.go
578 lines (520 loc) · 13.6 KB
/
fastval_math.go
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
package gojsonsm
import (
"math"
)
// floatRound implements math.Round for Go versions older than
// 1.10 which did not have the function... Wat?
func floatMathRound(val float64) float64 {
if val < 0 {
return float64(int(val - 0.5))
}
return float64(int(val + 0.5))
}
var mathDegreeFunc func(float64) float64 = func(rad float64) float64 {
return rad * 180 / math.Pi
}
var mathRadiansFunc func(float64) float64 = func(deg float64) float64 {
return deg * math.Pi / 180
}
func FastValMathRound(val FastVal) FastVal {
if val.IsFloat() {
originalValue, valid := val.AsFloat()
if !valid {
return NewInvalidFastVal()
}
roundedValue := floatMathRound(originalValue)
return NewFloatFastVal(roundedValue)
} else if val.IsInt() || val.IsUInt() {
// These values are already rounded, no need for any work
return val
}
return NewInvalidFastVal()
}
func FastValMathAbs(val FastVal) FastVal {
if val.IsUInt() {
// Not gonna do abs on an uint
return val
} else {
floatVal, valid := val.AsFloat()
if !valid {
return NewInvalidFastVal()
}
if val.IsFloat() {
return NewFloatFastVal(math.Abs(floatVal))
} else if val.IsInt() {
return NewIntFastVal(int64(math.Abs(floatVal)))
}
}
return NewInvalidFastVal()
}
type intToIntOp func(int64) int64
type int2ToIntOp func(int64, int64) int64
type uint2ToUintOp func(uint64, uint64) uint64
type floatToFloatOp func(float64) float64
type float2ToFloatOp func(float64, float64) float64
func fastValMathAdd(a, b float64) float64 {
return a + b
}
func fastValMathAddInt(a, b int64) int64 {
return a + b
}
func fastValMathAddUint(a, b uint64) uint64 {
return a + b
}
func fastValMathSub(a, b float64) float64 {
return a - b
}
func fastValMathSubInt(a, b int64) int64 {
return a - b
}
func fastValMathSubUint(a, b uint64) uint64 {
return a - b
}
func fastValMathMult(a, b float64) float64 {
return a * b
}
func fastValMathMultInt(a, b int64) int64 {
return a * b
}
func fastValMathMultUint(a, b uint64) uint64 {
return a * b
}
func fastValMathDiv(a, b float64) float64 {
return a / b
}
func fastValMathDivInt(a, b int64) int64 {
return a / b
}
func fastValMathDivUint(a, b uint64) uint64 {
return a / b
}
func fastValMathMod(a, b int64) int64 {
return a % b
}
func fastValMathModUint(a, b uint64) uint64 {
return a % b
}
func fastValNegate(a float64) float64 {
return -1.0 * a
}
func fastValNegateInt(a int64) int64 {
return -1 * a
}
func genericFastVal2IntsOp(val, val1 FastVal, op int2ToIntOp) FastVal {
valInt, valid := val.AsInt()
val1Int, valid2 := val1.AsInt()
if !val.IsNumeric() || !val1.IsNumeric() || !valid || !valid2 {
return NewInvalidFastVal()
}
return NewIntFastVal(op(valInt, val1Int))
}
func genericFastVal2UintsOp(val, val1 FastVal, op uint2ToUintOp) FastVal {
valUint, valid := val.AsUint()
val1Uint, valid2 := val1.AsUint()
if !val.IsNumeric() || !val1.IsNumeric() || !valid || !valid2 {
return NewInvalidFastVal()
}
return NewUintFastVal(op(valUint, val1Uint))
}
func genericFastValIntOp(val FastVal, op intToIntOp) FastVal {
intVal, valid := val.AsInt()
if valid && val.IsNumeric() {
return NewIntFastVal(op(intVal))
}
return NewInvalidFastVal()
}
func genericFastValFloatOp(val FastVal, op floatToFloatOp) FastVal {
valFloat, valid := val.AsFloat()
if valid && val.IsNumeric() {
return NewFloatFastVal(op(valFloat))
}
return NewInvalidFastVal()
}
func genericFastVal2FloatsOp(val, val1 FastVal, op float2ToFloatOp) FastVal {
valFloat, valid := val.AsFloat()
val1Float, valid2 := val1.AsFloat()
if !val.IsNumeric() || !val1.IsNumeric() || !valid || !valid2 {
return NewInvalidFastVal()
}
return NewFloatFastVal(op(valFloat, val1Float))
}
func FastValMathSqrt(val FastVal) FastVal {
return genericFastValFloatOp(val, math.Sqrt)
}
func FastValMathAcos(val FastVal) FastVal {
return genericFastValFloatOp(val, math.Acos)
}
func FastValMathAsin(val FastVal) FastVal {
return genericFastValFloatOp(val, math.Asin)
}
func FastValMathAtan(val FastVal) FastVal {
return genericFastValFloatOp(val, math.Atan)
}
func FastValMathCos(val FastVal) FastVal {
return genericFastValFloatOp(val, math.Cos)
}
func FastValMathSin(val FastVal) FastVal {
return genericFastValFloatOp(val, math.Sin)
}
func FastValMathTan(val FastVal) FastVal {
return genericFastValFloatOp(val, math.Tan)
}
func FastValMathExp(val FastVal) FastVal {
return genericFastValFloatOp(val, math.Exp)
}
func FastValMathLn(val FastVal) FastVal {
return genericFastValFloatOp(val, math.Log)
}
func FastValMathLog(val FastVal) FastVal {
return genericFastValFloatOp(val, math.Log10)
}
func FastValMathCeil(val FastVal) FastVal {
return genericFastValFloatOp(val, math.Ceil)
}
func FastValMathFloor(val FastVal) FastVal {
return genericFastValFloatOp(val, math.Floor)
}
func FastValMathPow(val, val1 FastVal) FastVal {
return genericFastVal2FloatsOp(val, val1, math.Pow)
}
func FastValMathAtan2(val, val1 FastVal) FastVal {
return genericFastVal2FloatsOp(val, val1, math.Atan2)
}
func FastValMathDegrees(val FastVal) FastVal {
return genericFastValFloatOp(val, mathDegreeFunc)
}
func FastValMathRadians(val FastVal) FastVal {
return genericFastValFloatOp(val, mathRadiansFunc)
}
func fastValMathAddMultSharedLogic(val, val1 FastVal, intOp int2ToIntOp, uintOp uint2ToUintOp, floatOp float2ToFloatOp) FastVal {
switch val.dataType {
case UintValue:
fallthrough
case JsonUintValue:
switch val1.dataType {
case UintValue:
fallthrough
case JsonUintValue:
return genericFastVal2UintsOp(val, val1, uintOp)
case IntValue:
fallthrough
case JsonIntValue:
intCheck, valid := val1.AsInt()
if !valid {
return NewInvalidFastVal()
}
if intCheck >= 0 {
return genericFastVal2UintsOp(val, val1, uintOp)
} else {
uintCheck, valid1 := val.AsUint()
if !valid1 || uintCheck > math.MaxInt64 {
return NewInvalidFastVal()
}
return genericFastVal2IntsOp(val, val1, intOp)
}
case FloatValue:
fallthrough
case JsonFloatValue:
return genericFastVal2FloatsOp(val, val1, floatOp)
default:
return NewInvalidFastVal()
}
case IntValue:
fallthrough
case JsonIntValue:
switch val1.dataType {
case UintValue:
fallthrough
case JsonUintValue:
intCheck, valid := val.AsInt()
if !valid {
return NewInvalidFastVal()
}
if intCheck >= 0 {
return genericFastVal2UintsOp(val, val1, uintOp)
} else {
uint1Check, valid1 := val1.AsUint()
if !valid1 || uint1Check > math.MaxInt64 {
return NewInvalidFastVal()
}
return genericFastVal2IntsOp(val, val1, intOp)
}
case IntValue:
fallthrough
case JsonIntValue:
return genericFastVal2IntsOp(val, val1, intOp)
case FloatValue:
fallthrough
case JsonFloatValue:
return genericFastVal2FloatsOp(val, val1, floatOp)
default:
return NewInvalidFastVal()
}
case FloatValue:
fallthrough
case JsonFloatValue:
return genericFastVal2FloatsOp(val, val1, floatOp)
default:
return NewInvalidFastVal()
}
}
func FastValMathAdd(val, val1 FastVal) FastVal {
return fastValMathAddMultSharedLogic(val, val1, fastValMathAddInt, fastValMathAddUint, fastValMathAdd)
}
func FastValMathMul(val, val1 FastVal) FastVal {
return fastValMathAddMultSharedLogic(val, val1, fastValMathMultInt, fastValMathMultUint, fastValMathMult)
}
func FastValMathSub(val, val1 FastVal) FastVal {
switch val.dataType {
case UintValue:
fallthrough
case JsonUintValue:
switch val1.dataType {
case UintValue:
fallthrough
case JsonUintValue:
uintCheck, valid := val.AsUint()
uint1Check, valid1 := val1.AsUint()
if !valid || !valid1 {
return NewInvalidFastVal()
}
if uintCheck >= uint1Check {
return genericFastVal2UintsOp(val, val1, fastValMathSubUint)
} else {
// Negative result
return genericFastVal2IntsOp(val, val1, fastValMathSubInt)
}
case IntValue:
fallthrough
case JsonIntValue:
uintCheck, valid := val.AsUint()
int1Check, valid1 := val1.AsInt()
if !valid || !valid1 {
return NewInvalidFastVal()
}
if int1Check >= 0 {
int1AsUint, valid := val1.AsUint()
if !valid {
return NewInvalidFastVal()
}
if int1AsUint > uintCheck {
// Result will be negative
return genericFastVal2IntsOp(val, val1, fastValMathSubInt)
} else {
return genericFastVal2UintsOp(val, val1, fastValMathSubUint)
}
} else {
// Subtracting a neg int == adding int to uint to be prevent potential overflow
positiveIntFastVal := NewIntFastVal(fastValNegateInt(int1Check))
return genericFastVal2UintsOp(val, positiveIntFastVal, fastValMathAddUint)
}
case FloatValue:
fallthrough
case JsonFloatValue:
return genericFastVal2FloatsOp(val, val1, fastValMathSub)
default:
return NewInvalidFastVal()
}
case IntValue:
fallthrough
case JsonIntValue:
switch val1.dataType {
case UintValue:
fallthrough
case JsonUintValue:
intCheck, valid := val.AsInt()
uint1Check, valid1 := val1.AsUint()
if !valid || !valid1 {
return NewInvalidFastVal()
}
if uint1Check > math.MaxInt64 {
// Instead of invalid - best effort and let float take care of it
return genericFastVal2FloatsOp(val, val1, fastValMathSub)
}
if intCheck >= 0 {
uint1AsInt, _ := val1.AsInt()
if intCheck > uint1AsInt {
// positive result
return genericFastVal2UintsOp(val, val1, fastValMathSubUint)
} else {
return genericFastVal2IntsOp(val, val1, fastValMathSubInt)
}
} else {
// Result will be negative
return genericFastVal2IntsOp(val, val1, fastValMathSubInt)
}
case IntValue:
fallthrough
case JsonIntValue:
return genericFastVal2IntsOp(val, val1, fastValMathSubInt)
case FloatValue:
fallthrough
case JsonFloatValue:
return genericFastVal2FloatsOp(val, val1, fastValMathSub)
default:
return NewInvalidFastVal()
}
case FloatValue:
fallthrough
case JsonFloatValue:
return genericFastVal2FloatsOp(val, val1, fastValMathSub)
default:
return NewInvalidFastVal()
}
}
func FastValMathDiv(val, val1 FastVal) FastVal {
switch val.dataType {
case UintValue:
fallthrough
case JsonUintValue:
switch val1.dataType {
case UintValue:
fallthrough
case JsonUintValue:
val1Check, valid := val1.AsUint()
if !valid || val1Check == 0 {
return NewInvalidFastVal()
}
return genericFastVal2UintsOp(val, val1, fastValMathDivUint)
case IntValue:
fallthrough
case JsonIntValue:
int1Check, valid1 := val1.AsInt()
if !valid1 || int1Check == 0 {
return NewInvalidFastVal()
} else if int1Check > 0 {
return genericFastVal2UintsOp(val, val1, fastValMathDivUint)
} else {
return genericFastVal2IntsOp(val, val1, fastValMathDivInt)
}
case FloatValue:
fallthrough
case JsonFloatValue:
return genericFastVal2FloatsOp(val, val1, fastValMathDiv)
default:
return NewInvalidFastVal()
}
case IntValue:
fallthrough
case JsonIntValue:
intCheck, valid := val.AsInt()
if !valid {
return NewInvalidFastVal()
}
switch val1.dataType {
case UintValue:
fallthrough
case JsonUintValue:
uint1Check, valid1 := val1.AsUint()
if !valid1 || intCheck < 0 && uint1Check > math.MaxInt64 {
// Cannot convert to int type
return genericFastVal2FloatsOp(val, val1, fastValMathDiv)
} else {
if intCheck >= 0 {
return genericFastVal2UintsOp(val, val1, fastValMathDivUint)
} else {
return genericFastVal2IntsOp(val, val1, fastValMathDivInt)
}
}
case IntValue:
fallthrough
case JsonIntValue:
return genericFastVal2IntsOp(val, val1, fastValMathDivInt)
case FloatValue:
fallthrough
case JsonFloatValue:
return genericFastVal2FloatsOp(val, val1, fastValMathDiv)
default:
return NewInvalidFastVal()
}
case FloatValue:
fallthrough
case JsonFloatValue:
return genericFastVal2FloatsOp(val, val1, fastValMathDiv)
default:
return NewInvalidFastVal()
}
}
func FastValMathMod(val, val1 FastVal) FastVal {
switch val.dataType {
case UintValue:
fallthrough
case JsonUintValue:
switch val1.dataType {
case UintValue:
fallthrough
case JsonUintValue:
val1Check, valid := val1.AsUint()
if !valid || val1Check == 0 {
return NewInvalidFastVal()
}
return genericFastVal2UintsOp(val, val1, fastValMathModUint)
case IntValue:
fallthrough
case JsonIntValue:
int1Check, valid1 := val1.AsInt()
if !valid1 || int1Check == 0 {
return NewInvalidFastVal()
} else if int1Check > 0 {
return genericFastVal2UintsOp(val, val1, fastValMathModUint)
} else {
return genericFastVal2IntsOp(val, val1, fastValMathMod)
}
default:
return NewInvalidFastVal()
}
case IntValue:
fallthrough
case JsonIntValue:
intCheck, valid := val.AsInt()
if !valid {
return NewInvalidFastVal()
}
switch val1.dataType {
case UintValue:
fallthrough
case JsonUintValue:
uint1Check, valid1 := val1.AsUint()
if !valid1 || intCheck < 0 && uint1Check > math.MaxInt64 {
// Cannot convert to int type
return NewInvalidFastVal()
} else {
if intCheck >= 0 {
return genericFastVal2UintsOp(val, val1, fastValMathModUint)
} else {
return genericFastVal2IntsOp(val, val1, fastValMathMod)
}
}
case IntValue:
fallthrough
case JsonIntValue:
return genericFastVal2IntsOp(val, val1, fastValMathMod)
default:
return NewInvalidFastVal()
}
default:
return NewInvalidFastVal()
}
}
func FastValMathNeg(val FastVal) FastVal {
switch val.dataType {
case UintValue:
fallthrough
case JsonUintValue:
checkUint, _ := val.AsUint()
if checkUint > math.MaxInt64 {
return NewInvalidFastVal()
}
fallthrough
case IntValue:
fallthrough
case JsonIntValue:
return genericFastValIntOp(val, fastValNegateInt)
case FloatValue:
fallthrough
case JsonFloatValue:
return genericFastValFloatOp(val, fastValNegate)
default:
return NewInvalidFastVal()
}
}