liyujie
2025-08-28 786ff4f4ca2374bdd9177f2e24b503d43e7a3b93
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
@ Tremolo library
@-----------------------------------------------------------------------
@ Copyright (C) 2002-2009, Xiph.org Foundation
@ Copyright (C) 2010, Robin Watts for Pinknoise Productions Ltd
@ All rights reserved.
 
@ Redistribution and use in source and binary forms, with or without
@ modification, are permitted provided that the following conditions
@ are met:
 
@     * Redistributions of source code must retain the above copyright
@ notice, this list of conditions and the following disclaimer.
@     * Redistributions in binary form must reproduce the above
@ copyright notice, this list of conditions and the following disclaimer
@ in the documentation and/or other materials provided with the
@ distribution.
@     * Neither the names of the Xiph.org Foundation nor Pinknoise
@ Productions Ltd nor the names of its contributors may be used to
@ endorse or promote products derived from this software without
@ specific prior written permission.
@
@ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
@ "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
@ LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
@ A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
@ OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
@ SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
@ LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
@ DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
@ THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
@ (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
@ ----------------------------------------------------------------------
 
    .text
 
   .global    oggpack_look
   .global    oggpack_adv
   .global    oggpack_readinit
   .global    oggpack_read
 
oggpack_look:
   @ r0 = oggpack_buffer *b
   @ r1 = int             bits
   STMFD    r13!,{r10,r11,r14}
   LDMIA    r0,{r2,r3,r12}
                   @ r2 = bitsLeftInSegment
                   @ r3 = ptr
                   @ r12= bitsLeftInWord
   SUBS    r2,r2,r1        @ bitsLeftinSegment -= bits
   BLT    look_slow        @ Not enough bits in this segment for
                   @ this request. Do it slowly.
   LDR    r10,[r3]        @ r10= ptr[0]
   RSB    r14,r12,#32        @ r14= 32-bitsLeftInWord
   SUBS    r12,r12,r1        @ r12= bitsLeftInWord -= bits
   LDRLT    r11,[r3,#4]!        @ r11= ptr[1]
   MOV    r10,r10,LSR r14        @ r10= ptr[0]>>(32-bitsLeftInWord)
   ADDLE    r12,r12,#32        @ r12= bitsLeftInWord += 32
   RSB    r14,r14,#32        @ r14= 32-bitsLeftInWord
   ORRLT    r10,r10,r11,LSL r14    @ r10= Next 32 bits.
   MOV    r14,#1
   RSB    r14,r14,r14,LSL r1
   AND    r0,r10,r14
   LDMFD    r13!,{r10,r11,PC}
 
look_slow:
   STMFD    r13!,{r5,r6}
   ADDS    r10,r2,r1        @ r10= bitsLeftInSegment + bits (i.e.
                   @ the initial value of bitsLeftInSeg)
   @ r10 = bitsLeftInSegment (initial)
   @ r12 = bitsLeftInWord
   RSB    r14,r12,#32        @ r14= 32-bitsLeftInWord
   MOV    r5,r10            @ r5 = bitsLeftInSegment (initial)
   BLT    look_overrun
   BEQ    look_next_segment    @ r10= r12 = 0, if we branch
   CMP    r12,r10            @ If bitsLeftInWord < bitsLeftInSeg
                   @ there must be more in the next word
   LDR    r10,[r3],#4        @ r10= ptr[0]
   LDRLT    r6,[r3]            @ r6 = ptr[1]
   MOV    r11,#1
   MOV    r10,r10,LSR r14        @ r10= first bitsLeftInWord bits
   ORRLT    r10,r10,r6,LSL r12    @ r10= first bitsLeftInSeg bits+crap
   RSB    r11,r11,r11,LSL r5    @ r11= mask
   AND    r10,r10,r11        @ r10= first r5 bits
   @ Load the next segments data
look_next_segment:
   @ At this point, r10 contains the first r5 bits of the result
   LDR    r11,[r0,#12]        @ r11= head = b->head
   @ Stall
   @ Stall
look_next_segment_2:
   LDR    r11,[r11,#12]        @ r11= head = head->next
   @ Stall
   @ Stall
   CMP    r11,#0
   BEQ    look_out_of_data
   LDMIA    r11,{r6,r12,r14}    @ r6 = buffer
                   @ r12= begin
                   @ r14= length
   LDR    r6,[r6]            @ r6 = buffer->data
   CMP    r14,#0
   BEQ    look_next_segment_2
   ADD    r6,r6,r12        @ r6 = buffer->data+begin
look_slow_loop:
   LDRB    r12,[r6],#1        @ r12= *buffer
   SUBS    r14,r14,#1        @ r14= length
   @ Stall
   ORR    r10,r10,r12,LSL r5    @ r10= first r5+8 bits
   ADD    r5,r5,#8
   BLE    look_really_slow
   CMP    r5,r1
   BLT    look_slow_loop
   MOV    r14,#1
   RSB    r14,r14,r14,LSL r1
   AND    r0,r10,r14
   LDMFD    r13!,{r5,r6,r10,r11,PC}
 
 
look_really_slow:
   CMP    r5,r1
   BLT    look_next_segment_2
   MOV    r14,#1
   RSB    r14,r14,r14,LSL r1
   AND    r0,r10,r14
   LDMFD    r13!,{r5,r6,r10,r11,PC}
 
look_out_of_data:
   @MVN    r0,#0            ; return -1
   MOV    r0,#0
   LDMFD    r13!,{r5,r6,r10,r11,PC}
 
look_overrun:
   @ We had overrun when we started, so we need to skip -r10 bits.
   LDR    r11,[r0,#12]        @ r11 = head = b->head
   @ stall
   @ stall
look_overrun_next_segment:
   LDR    r11,[r11,#12]        @ r11 = head->next
   @ stall
   @ stall
   CMP    r11,#0
   BEQ    look_out_of_data
   LDMIA    r11,{r6,r7,r14}        @ r6 = buffer
                   @ r7 = begin
                   @ r14= length
   LDR    r6,[r6]            @ r6 = buffer->data
   @ stall
   @ stall
   ADD    r6,r6,r7        @ r6 = buffer->data+begin
   MOV    r14,r14,LSL #3        @ r14= length in bits
   ADDS    r14,r14,r10        @ r14= length in bits-bits to skip
   MOVLE    r10,r14
   BLE    look_overrun_next_segment
   RSB    r10,r10,#0        @ r10= bits to skip
   ADD    r6,r10,r10,LSR #3    @ r6 = pointer to data
   MOV    r10,#0
   B    look_slow_loop
 
oggpack_adv:
   @ r0 = oggpack_buffer *b
   @ r1 = bits
   LDMIA    r0,{r2,r3,r12}
                   @ r2 = bitsLeftInSegment
                   @ r3 = ptr
                   @ r12= bitsLeftInWord
   SUBS    r2,r2,r1        @ Does this run us out of bits in the
   BLE    adv_slow        @ segment? If so, do it slowly
   SUBS    r12,r12,r1
   ADDLE    r12,r12,#32
   ADDLE    r3,r3,#4
   STMIA    r0,{r2,r3,r12}
   BX      LR
adv_slow:
   STMFD    r13!,{r10,r14}
 
   LDR    r14,[r0,#12]        @ r14= head
   @ stall
adv_slow_loop:
   LDR    r1,[r0,#20]        @ r1 = count
   LDR    r10,[r14,#8]        @ r10= head->length
   LDR    r14,[r14,#12]        @ r14= head->next
   @ stall
   ADD    r1,r1,r10        @ r1 = count += head->length
   CMP    r14,#0
   BEQ    adv_end
   STR    r1,[r0,#20]        @ b->count = count
   STR    r14,[r0,#12]        @ b->head = head
   LDMIA    r14,{r3,r10,r12}    @ r3 = buffer
                   @ r10= begin
                   @ r12= length
   LDR    r3,[r3]            @ r3 = buffer->data
   ADD    r3,r3,r10        @ r3 = Pointer to start (byte)
   AND    r10,r3,#3        @ r10= bytes to backtrk to word align
   MOV    r10,r10,LSL #3        @ r10= bits to backtrk to word align
   RSB    r10,r10,#32        @ r10= bits left in word
   ADDS    r10,r10,r2        @ r10= bits left in word after skip
   ADDLE    r10,r10,#32
   ADDLE    r3,r3,#4
   BIC    r3,r3,#3        @ r3 = Pointer to start (word)
   ADDS    r2,r2,r12,LSL #3    @ r2 = length in bits after advance
   BLE    adv_slow_loop
   STMIA    r0,{r2,r3,r10}
 
   LDMFD    r13!,{r10,PC}
adv_end:
   MOV    r2, #0
   MOV    r12,#0
   STMIA    r0,{r2,r3,r12}
 
   LDMFD    r13!,{r10,PC}
 
oggpack_readinit:
   @ r0 = oggpack_buffer *b
   @ r1 = oggreference   *r
   STR    r1,[r0,#12]        @ b->head = r1
   STR    r1,[r0,#16]        @ b->tail = r1
   LDMIA    r1,{r2,r3,r12}        @ r2 = b->head->buffer
                   @ r3 = b->head->begin
                   @ r12= b->head->length
   LDR    r2,[r2]            @ r2 = b->head->buffer->data
   MOV    r1,r12,LSL #3        @ r1 = BitsInSegment
   MOV    r12,#0
   ADD    r3,r2,r3        @ r3 = r2+b->head->begin
   BIC    r2,r3,#3        @ r2 = b->headptr (word)
   AND    r3,r3,#3
   MOV    r3,r3,LSL #3
   RSB    r3,r3,#32        @ r3 = BitsInWord
   STMIA    r0,{r1,r2,r3}
   STR    r12,[r0,#20]
   BX      LR
 
oggpack_read:
   @ r0 = oggpack_buffer *b
   @ r1 = int             bits
   STMFD    r13!,{r10,r11,r14}
   LDMIA    r0,{r2,r3,r12}
                   @ r2 = bitsLeftInSegment
                   @ r3 = ptr
                   @ r12= bitsLeftInWord
   SUBS    r2,r2,r1        @ bitsLeftinSegment -= bits
   BLT    read_slow        @ Not enough bits in this segment for
                   @ this request. Do it slowly.
   LDR    r10,[r3]        @ r10= ptr[0]
   RSB    r14,r12,#32        @ r14= 32-bitsLeftInWord
   SUBS    r12,r12,r1        @ r12= bitsLeftInWord -= bits
   ADDLE    r3,r3,#4
   LDRLT    r11,[r3]        @ r11= ptr[1]
   MOV    r10,r10,LSR r14        @ r10= ptr[0]>>(32-bitsLeftInWord)
   ADDLE    r12,r12,#32        @ r12= bitsLeftInWord += 32
   RSB    r14,r14,#32        @ r14= 32-bitsLeftInWord
   ORRLT    r10,r10,r11,LSL r14    @ r10= Next 32 bits.
   STMIA    r0,{r2,r3,r12}
   MOV    r14,#1
   RSB    r14,r14,r14,LSL r1
   AND    r0,r10,r14
   LDMFD    r13!,{r10,r11,PC}
 
read_slow:
   STMFD    r13!,{r5,r6}
   ADDS    r10,r2,r1        @ r10= bitsLeftInSegment + bits (i.e.
                   @ the initial value of bitsLeftInSeg)
   @ r10 = bitsLeftInSegment (initial)
   @ r12 = bitsLeftInWord
   RSB    r14,r12,#32        @ r14= 32-bitsLeftInWord
   MOV    r5,r10            @ r5 = bitsLeftInSegment (initial)
   BLT    read_overrun
   BEQ    read_next_segment    @ r10= r12 = 0, if we branch
   CMP    r12,r10            @ If bitsLeftInWord < bitsLeftInSeg
                   @ there must be more in the next word
   LDR    r10,[r3],#4        @ r10= ptr[0]
   LDRLT    r6,[r3]            @ r6 = ptr[1]
   MOV    r11,#1
   MOV    r10,r10,LSR r14        @ r10= first bitsLeftInWord bits
   ORRLT    r10,r10,r6,LSL r12    @ r10= first bitsLeftInSeg bits+crap
   RSB    r11,r11,r11,LSL r5    @ r11= mask
   AND    r10,r10,r11        @ r10= first r5 bits
   @ Load the next segments data
read_next_segment:
   @ At this point, r10 contains the first r5 bits of the result
   LDR    r11,[r0,#12]        @ r11= head = b->head
   @ Stall
read_next_segment_2:
   @ r11 = head
   LDR    r6,[r0,#20]        @ r6 = count
   LDR    r12,[r11,#8]        @ r12= length
   LDR    r11,[r11,#12]        @ r11= head = head->next
   @ Stall
   ADD    r6,r6,r12        @ count += length
   CMP    r11,#0
   BEQ    read_out_of_data
   STR    r11,[r0,#12]
   STR    r6,[r0,#20]        @ b->count = count
   LDMIA    r11,{r6,r12,r14}    @ r6 = buffer
                   @ r12= begin
                   @ r14= length
   LDR    r6,[r6]            @ r6 = buffer->data
   CMP    r14,#0
   BEQ    read_next_segment_2
   ADD    r6,r6,r12        @ r6 = buffer->data+begin
read_slow_loop:
   LDRB    r12,[r6],#1        @ r12= *buffer
   SUBS    r14,r14,#1        @ r14= length
   @ Stall
   ORR    r10,r10,r12,LSL r5    @ r10= first r5+8 bits
   ADD    r5,r5,#8
   BLE    read_really_slow
   CMP    r5,r1
   BLT    read_slow_loop
read_end:
   MOV    r12,#1
   RSB    r12,r12,r12,LSL r1
 
   @ Store back the new position
   @ r2 = -number of bits to go from this segment
   @ r6 = ptr
   @ r14= bytesLeftInSegment
   @ r11= New head value
   LDMIA    r11,{r3,r6,r14}        @ r3 = buffer
                   @ r6 = begin
                   @ r14= length
   LDR    r3,[r3]            @ r3 = buffer->data
   ADD    r1,r2,r14,LSL #3    @ r1 = bitsLeftInSegment
   @ stall
   ADD    r6,r3,r6        @ r6 = pointer
   AND    r3,r6,#3        @ r3 = bytes used in first word
   RSB    r3,r2,r3,LSL #3        @ r3 = bits used in first word
   BIC    r2,r6,#3        @ r2 = word ptr
   RSBS    r3,r3,#32        @ r3 = bitsLeftInWord
   ADDLE    r3,r3,#32
   ADDLE    r2,r2,#4
   STMIA    r0,{r1,r2,r3}
 
   AND    r0,r10,r12
   LDMFD    r13!,{r5,r6,r10,r11,PC}
 
 
read_really_slow:
   CMP    r5,r1
   BGE    read_end
   LDR    r14,[r11,#8]        @ r14= length of segment just done
   @ stall
   @ stall
   ADD    r2,r2,r14,LSL #3    @ r2 = -bits to use from next seg
   B    read_next_segment_2
 
read_out_of_data:
   @ Store back the new position
   @ r2 = -number of bits to go from this segment
   @ r6 = ptr
   @ r14= bytesLeftInSegment
   @ RJW: This may be overkill - we leave the buffer empty, with -1
   @ bits left in it. We might get away with just storing the
   @ bitsLeftInSegment as -1.
   LDR    r11,[r0,#12]        @ r11=head
 
   LDMIA    r11,{r3,r6,r14}        @ r3 = buffer
                   @ r6 = begin
                   @ r14= length
   LDR    r3,[r3]            @ r3 = buffer->data
   ADD    r6,r3,r6        @ r6 = pointer
   ADD    r6,r6,r14
   AND    r3,r6,#3        @ r3 = bytes used in first word
   MOV    r3,r3,LSL #3        @ r3 = bits used in first word
   BIC    r2,r6,#3        @ r2 = word ptr
   RSBS    r3,r3,#32        @ r3 = bitsLeftInWord
   MVN    r1,#0            @ r1 = -1 = bitsLeftInSegment
   STMIA    r0,{r1,r2,r3}
   @MVN    r0,#0            ; return -1
   MOV    r0,#0
   LDMFD    r13!,{r5,r6,r10,r11,PC}
 
read_overrun:
   @ We had overrun when we started, so we need to skip -r10 bits.
   LDR    r11,[r0,#12]        @ r11 = head = b->head
   @ stall
   @ stall
read_overrun_next_segment:
   LDR    r11,[r11,#12]        @ r11 = head->next
   @ stall
   @ stall
   CMP    r11,#0
   BEQ    read_out_of_data
   LDMIA    r11,{r6,r7,r14}        @ r6 = buffer
                   @ r7 = begin
                   @ r14= length
   LDR    r6,[r6]            @ r6 = buffer->data
   @ stall
   @ stall
   ADD    r6,r6,r7        @ r6 = buffer->data+begin
   MOV    r14,r14,LSL #3        @ r14= length in bits
   ADDS    r14,r14,r10        @ r14= length in bits-bits to skip
   MOVLE    r10,r14
   BLE    read_overrun_next_segment
   RSB    r10,r10,#0        @ r10= bits to skip
   ADD    r6,r10,r10,LSR #3    @ r6 = pointer to data
   MOV    r10,#0
   B    read_slow_loop
 
   @ END