()
| 76 | } |
| 77 | |
| 78 | func (e *encoder) buildWriteFuncs() { |
| 79 | e.funcs.writeVarint = e.M.Function(e.T.Void, "write_varint", e.T.CtxPtr, e.T.BufPtr, e.T.Uint64). |
| 80 | LinkOnceODR() |
| 81 | e.C.Build(e.funcs.writeVarint, func(s *compiler.S) { |
| 82 | buf, val := s.Parameter(1), s.Parameter(2) |
| 83 | // while (i >= 0x80) { |
| 84 | // bytes[length] = static_cast<uint8>(i | 0x80); |
| 85 | // i >>= 7; |
| 86 | // ++length; |
| 87 | // } |
| 88 | // bytes[length] = static_cast<uint8>(i); |
| 89 | i := s.LocalInit("i", val.Cast(e.T.Uint64)) |
| 90 | length := s.LocalInit("length", s.Scalar(uint32(0))) |
| 91 | bytes := s.Local("bytes", e.T.TypeOf([10]byte{})) |
| 92 | s.While(func() *codegen.Value { |
| 93 | return s.GreaterOrEqualTo(i.Load(), s.Scalar(uint64(0x80))) |
| 94 | }, func() { |
| 95 | v, idx := i.Load(), length.Load() |
| 96 | bytes.Index(0, idx).Store(s.Or(v, s.Scalar(uint64(0x80))).Cast(e.T.Uint8)) |
| 97 | i.Store(s.ShiftRight(v, s.Scalar(uint64(7)))) |
| 98 | length.Store(s.Add(idx, s.Scalar(uint32(1)))) |
| 99 | }) |
| 100 | bytes.Index(0, length.Load()).Store(i.Load().Cast(e.T.Uint8)) |
| 101 | e.AppendBuffer(s, buf, s.Add(length.Load(), s.Scalar(uint32(1))), bytes.Index(0, 0)) |
| 102 | }) |
| 103 | |
| 104 | e.funcs.writeZigzag = e.M.Function(e.T.Void, "write_zigzag", e.T.CtxPtr, e.T.BufPtr, e.T.Uint64). |
| 105 | LinkOnceODR() |
| 106 | e.C.Build(e.funcs.writeZigzag, func(s *compiler.S) { |
| 107 | buf, val := s.Parameter(1), s.Parameter(2) |
| 108 | // (n << 1) ^ (n >> 63) |
| 109 | val = val.Cast(e.T.Int64) |
| 110 | lhs := s.ShiftLeft(val, s.Scalar(int64(1))) |
| 111 | rhs := s.ShiftRight(val, s.Scalar(int64(63))) |
| 112 | zigzag := s.Xor(lhs, rhs) |
| 113 | e.writeVarint(s, buf, zigzag) |
| 114 | }) |
| 115 | } |
| 116 | |
| 117 | // mgEncode returns the mangling function for an encode method on the given |
| 118 | // class. |
no test coverage detected