Joey Armstrong | a6af152 | 2023-01-17 16:06:16 -0500 | [diff] [blame] | 1 | // Copyright The OpenTelemetry Authors |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package internal |
| 16 | |
| 17 | import ( |
| 18 | "math" |
| 19 | "unsafe" |
| 20 | ) |
| 21 | |
| 22 | func BoolToRaw(b bool) uint64 { |
| 23 | if b { |
| 24 | return 1 |
| 25 | } |
| 26 | return 0 |
| 27 | } |
| 28 | |
| 29 | func RawToBool(r uint64) bool { |
| 30 | return r != 0 |
| 31 | } |
| 32 | |
| 33 | func Int64ToRaw(i int64) uint64 { |
| 34 | return uint64(i) |
| 35 | } |
| 36 | |
| 37 | func RawToInt64(r uint64) int64 { |
| 38 | return int64(r) |
| 39 | } |
| 40 | |
| 41 | func Uint64ToRaw(u uint64) uint64 { |
| 42 | return u |
| 43 | } |
| 44 | |
| 45 | func RawToUint64(r uint64) uint64 { |
| 46 | return r |
| 47 | } |
| 48 | |
| 49 | func Float64ToRaw(f float64) uint64 { |
| 50 | return math.Float64bits(f) |
| 51 | } |
| 52 | |
| 53 | func RawToFloat64(r uint64) float64 { |
| 54 | return math.Float64frombits(r) |
| 55 | } |
| 56 | |
| 57 | func Int32ToRaw(i int32) uint64 { |
| 58 | return uint64(i) |
| 59 | } |
| 60 | |
| 61 | func RawToInt32(r uint64) int32 { |
| 62 | return int32(r) |
| 63 | } |
| 64 | |
| 65 | func Uint32ToRaw(u uint32) uint64 { |
| 66 | return uint64(u) |
| 67 | } |
| 68 | |
| 69 | func RawToUint32(r uint64) uint32 { |
| 70 | return uint32(r) |
| 71 | } |
| 72 | |
| 73 | func Float32ToRaw(f float32) uint64 { |
| 74 | return Uint32ToRaw(math.Float32bits(f)) |
| 75 | } |
| 76 | |
| 77 | func RawToFloat32(r uint64) float32 { |
| 78 | return math.Float32frombits(RawToUint32(r)) |
| 79 | } |
| 80 | |
| 81 | func RawPtrToFloat64Ptr(r *uint64) *float64 { |
| 82 | return (*float64)(unsafe.Pointer(r)) |
| 83 | } |
| 84 | |
| 85 | func RawPtrToInt64Ptr(r *uint64) *int64 { |
| 86 | return (*int64)(unsafe.Pointer(r)) |
| 87 | } |
| 88 | |
| 89 | func RawPtrToUint64Ptr(r *uint64) *uint64 { |
| 90 | return r |
| 91 | } |