-
Notifications
You must be signed in to change notification settings - Fork 0
/
sh_v1m128.inc
executable file
·153 lines (124 loc) · 4.38 KB
/
sh_v1m128.inc
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
#if (defined __GNUC__) || (!defined X64)
#ifdef X64
#pragma message "Using __int128 type for int64*int64/int64 operations"
// if __int128 available, just use it
typedef unsigned __int128 hword;
static qword muldivR( qword a, qword b, qword range ) { return (hword(a)*b)/(hword(range)+1); }
static qword mulRdiv( qword a, qword c, qword range ) { return (hword(a)*(hword(range)+1))/c; }
#else
// if there's no __int128 and no x64 asm support, then use this
#include "muldiv_x86.inc"
#endif
#else
#ifdef X64
// if __int128 isn't available, use x64 asm
// #include "muldiv_x86.inc"
#include "muldiv_x64_asm.inc"
#else
#endif
#endif
template< int rc_ProcMode >
struct Rangecoder_SH1m128 : Coroutine {
enum {
SCALElog = 15,
SCALE = 1<<SCALElog
};
enum {
NUM = 8,
// sTOP = 0x0100000000000000ULL,
// Thres = 0xFF00000000000000ULL,
MSByte= 24+32
};
static const qword sTOP = 0x0100000000000000ULL;
static const qword Thres = 0xFF00000000000000ULL;
// int rc_ProcMode; // 0=encode, 1=decode;
qword low;
qword Carry;
qword code;
qword range;
uint FFNum;
uint Cache;
void rc_Process( qword cumFreq, qword freq, qword totFreq ) {
qword tmp = mulRdiv(cumFreq,totFreq,range); // + 1;
if( rc_ProcMode ) code-=tmp; else {
//lowc+=tmp;
qword old=low; low+=tmp; if( low<old ) Carry++;
}
range = mulRdiv(cumFreq+freq,totFreq,range)-1 - tmp;
if( rc_ProcMode )
while( range<sTOP ) {
range = (range<<8)+0xFF; // (range+1)*256-1=range*256+255
uint c=get(); FFNum += (c==-1);
(code<<=8)+=byte(c);
}
else
while( range<sTOP ) range=(range<<8)+0xFF, ShiftLow();
}
void rc_Arrange( qword totFreq ) {}
qword rc_GetFreq( qword totFreq ) {
qword r = muldivR(code,totFreq,range);
//printf( "GetFreq: %I64X\n", r );
return r;
}
void rc_BProcess( qword freq, int& bit ) {
qword x[] = { 0, freq, SCALE };
if( rc_ProcMode ) {
qword count = rc_GetFreq( SCALE );
bit = (count>=freq);
}
rc_Process( x[bit], x[1+bit]-x[bit], SCALE );
}
void ShiftLow( void ) {
if( low<Thres || Carry ) {
if( Cache!=-1 ) put( Cache+Carry );
for (;FFNum != 0;FFNum--) put( Carry-1 ); // (Carry-1)&255;
Cache = low>>MSByte;
Carry = 0;
} else FFNum++;
low<<=8;
}
void rcInit( void ) {
range = -1;
low = 0;
FFNum = 0;
Carry = 0;
Cache = -1;
}
void rc_Init() {
rcInit();
//rc_ProcMode = _rc_ProcMode;
if( rc_ProcMode==1 ) {
for(int _=0; _<NUM; _++) (code<<=8)+=get();
}
}
void rc_Quit( void ) {
if( rc_ProcMode==0 ) {
uint i, n = NUM+1;
// cache .. FF x FFNum .. low
qword high=low+range,High=(high<low),tmp=high; high++; High|=(high<tmp);
qword llow=low+range/2; Carry|=(llow<low);
qword mask,nmask,nlow;
mask=0xFFU; nlow=llow|mask; if( (Carry<High) || (nlow<high) ) llow=nlow,n--;
mask=0xFFFFU; nlow=llow|mask; if( (Carry<High) || (nlow<high) ) llow=nlow,n--;
mask=0xFFFFFFU; nlow=llow|mask; if( (Carry<High) || (nlow<high) ) llow=nlow,n--;
mask=0xFFFFFFFFU; nlow=llow|mask; if( (Carry<High) || (nlow<high) ) llow=nlow,n--;
mask=0xFFFFFFFFFFU; nlow=llow|mask; if( (Carry<High) || (nlow<high) ) llow=nlow,n--;
mask=0xFFFFFFFFFFFFU; nlow=llow|mask; if( (Carry<High) || (nlow<high) ) llow=nlow,n--;
mask=0xFFFFFFFFFFFFFFU; nlow=llow|mask; if( (Carry<High) || (nlow<high) ) llow=nlow,n--;
mask=0xFFFFFFFFFFFFFFFFU; nlow=llow|mask; if( (Carry<High) || (nlow<high) ) llow=nlow,n--;
low = llow;
if( low==0xFFFFFFFFFFFFFFFFU ) FFNum=0; // they're also FFs
//printf( "Carry=%I64X\n", Carry );
//printf( "%02X %i %016I64X\n", Cache+Carry, FFNum+Carry, low );
//printf( "%02X %i %016I64X\n", Cache+High, FFNum+High, high );
// write one byte less if possible, last byte is usually partial anyway
if( (n==0) ) if( FFNum==0 ) Cache=-1; else FFNum--;
if( n>0 ) n--;
if( Cache!=-1 ) put( Cache+Carry );
for( i=0; i<FFNum; i++ ) put( 0xFF+Carry );
for( i=0; i<n; i++ ) put( low>>MSByte ), low<<=8;
// qword old=low; low+=range; if( low<old ) Carry++;
// for( i=0; i<n+1; i++ ) ShiftLow();
}
} // quit
};