word.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. // Copyright 2016-2017 VMware, Inc. All Rights Reserved.
  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. package bdoor
  15. import "unsafe"
  16. type UInt32 struct {
  17. High uint16
  18. Low uint16
  19. }
  20. func (u *UInt32) Word() uint32 {
  21. return uint32(u.High)<<16 + uint32(u.Low)
  22. }
  23. func (u *UInt32) SetWord(w uint32) {
  24. u.High = uint16(w >> 16)
  25. u.Low = uint16(w)
  26. }
  27. func (u *UInt32) AsUInt32() *UInt32 {
  28. return u
  29. }
  30. func (u *UInt32) Value() uint32 {
  31. return u.Word()
  32. }
  33. func (u *UInt32) SetValue(val uint32) {
  34. u.SetWord(val)
  35. }
  36. func (u *UInt32) SetPointer(p unsafe.Pointer) {
  37. u.SetWord(uint32(uintptr(p)))
  38. }
  39. type UInt64 struct {
  40. High UInt32
  41. Low UInt32
  42. }
  43. func (u *UInt64) Quad() uint64 {
  44. return uint64(u.High.Word())<<32 + uint64(u.Low.Word())
  45. }
  46. func (u *UInt64) SetQuad(w uint64) {
  47. u.High.SetWord(uint32(w >> 32))
  48. u.Low.SetWord(uint32(w))
  49. }
  50. func (u *UInt64) AsUInt32() *UInt32 {
  51. return &u.Low
  52. }
  53. func (u *UInt64) Value() uint64 {
  54. return u.Quad()
  55. }
  56. func (u *UInt64) SetValue(val uint64) {
  57. u.SetQuad(val)
  58. }
  59. func (u *UInt64) SetPointer(p unsafe.Pointer) {
  60. u.SetQuad(uint64(uintptr(p)))
  61. }