unitrix/constant/
mod.rs

1//! 类型级整数表示模块
2//!
3//! 本模块提供了一种在类型系统中表示整数的方法,使用二进制补码形式。
4//! 正数和负数有不同的表示规则,详见下面的文档说明。
5mod basic;
6pub(super) use basic::*;
7
8pub mod not;//没有共享内容
9
10mod neg;
11pub use neg::*;
12
13mod add1;
14pub use add1::*;
15
16pub mod sub1;
17pub use sub1::*;
18
19mod add;
20pub use add::*;
21
22mod sub;
23pub use sub::*;
24
25mod mul;
26pub use mul::*;
27
28mod div;
29pub use div::*;
30
31pub(super) mod shl;
32//pub use shl::*;
33
34pub(super) mod shr;
35
36pub(super) mod standardization;
37
38/// 类型级整数别名
39/// 
40/// 将编译时常量 N 转换为对应的类型表示
41/// 例如:`Const<42>` 会展开为 `B0<B1<B0<B1<B0<Z0>>>>>`
42pub type Const<const N: i32> = <Constant<N> as ToInt>::Output;
43
44/// 用于在泛型上下文中表示整数的包装类型
45pub struct Constant<const N: i32>;
46
47/// 将整数常量转换为类型表示的 trait
48pub trait ToInt {
49    /// 对应的类型表示
50    type Output;
51}
52
53/* ========== 正数表示规则 ==========
54 * 1. 最高有效位(MSB)在内,最低位(LSB)在外,依次嵌套
55 * 2. 最内层必须是 Z0,表示正号,也表示本位和更高位是0
56 * 3. 数值部分使用 B0/B1 表示二进制位
57 * 
58 
59 * 例如 +5 (101):
60 * 
61 *   Z0 (符号位和终止符,表示本位为0,更高位也是0)
62 *   B1 <- MSB
63 *   B0
64 *   B1 <- LSB
65 * 表示为: B1<B0<P1>>
66 */
67impl ToInt for Constant<0> {
68    type Output = Z0;  // 0 的特殊表示
69}
70
71impl ToInt for Constant<1> {
72    type Output = P1;
73}
74
75impl ToInt for Constant<2> {
76    type Output = B0<P1>;
77}
78
79impl ToInt for Constant<3> {
80    type Output = P1;
81}
82
83impl ToInt for Constant<4> {
84    type Output = B0<B0<Z0>>;
85}
86
87impl ToInt for Constant<5> {
88    type Output = B1<B0<Z0>>;
89}
90
91impl ToInt for Constant<6> {
92    type Output = B0<P1>;
93}
94
95impl ToInt for Constant<7> {
96    type Output = B1<P1>;
97}
98
99impl ToInt for Constant<8> {
100    type Output = B0<B0<B0<Z0>>>;
101}
102
103impl ToInt for Constant<9> {
104    type Output = B1<B0<B0<Z0>>>;
105}
106
107impl ToInt for Constant<10> {
108    type Output = B0<B1<B0<Z0>>>;
109}
110
111impl ToInt for Constant<11> {
112    type Output = B1<B1<B0<Z0>>>;
113}
114
115impl ToInt for Constant<12> {
116    type Output = B0<B0<P1>>;
117}
118
119impl ToInt for Constant<13> {
120    type Output = B1<B0<P1>>;
121}
122
123impl ToInt for Constant<14> {
124    type Output = B0<B1<P1>>;
125}
126
127impl ToInt for Constant<15> {
128    type Output = B1<B1<P1>>;
129}
130
131impl ToInt for Constant<16> {
132    type Output = B0<B0<B0<B0<Z0>>>>;
133}
134
135impl ToInt for Constant<17> {
136    type Output = B1<B0<B0<B0<Z0>>>>;
137}
138
139impl ToInt for Constant<18> {
140    type Output = B0<B1<B0<B0<Z0>>>>;
141}
142
143impl ToInt for Constant<19> {
144    type Output = B1<B1<B0<B0<Z0>>>>;
145}
146
147impl ToInt for Constant<20> {
148    type Output = B0<B0<B1<B0<Z0>>>>;
149}
150
151impl ToInt for Constant<21> {
152    type Output = B1<B0<B1<B0<Z0>>>>;
153}
154
155impl ToInt for Constant<22> {
156    type Output = B0<B1<B1<B0<Z0>>>>;
157}
158
159impl ToInt for Constant<23> {
160    type Output = B1<B1<B1<B0<Z0>>>>;
161}
162
163impl ToInt for Constant<24> {
164    type Output = B0<B0<B0<P1>>>;
165}
166
167impl ToInt for Constant<25> {
168    type Output = B1<B0<B0<P1>>>;
169}
170
171impl ToInt for Constant<26> {
172    type Output = B0<B1<B0<P1>>>;
173}
174
175impl ToInt for Constant<27> {
176    type Output = B1<B1<B0<P1>>>;
177}
178
179impl ToInt for Constant<28> {
180    type Output = B0<B0<B1<P1>>>;
181}
182
183impl ToInt for Constant<29> {
184    type Output = B1<B0<B1<P1>>>;
185}
186
187impl ToInt for Constant<30> {
188    type Output = B0<B1<B1<P1>>>;
189}
190
191impl ToInt for Constant<31> {
192    type Output = B1<B1<B1<P1>>>;
193}
194
195impl ToInt for Constant<32> {
196    type Output = B0<B0<B0<B0<B0<Z0>>>>>;
197}
198
199impl ToInt for Constant<33> {
200    type Output = B1<B0<B0<B0<B0<Z0>>>>>;
201}
202
203impl ToInt for Constant<34> {
204    type Output = B0<B1<B0<B0<B0<Z0>>>>>;
205}
206
207impl ToInt for Constant<35> {
208    type Output = B1<B1<B0<B0<B0<Z0>>>>>;
209}
210
211impl ToInt for Constant<36> {
212    type Output = B0<B0<B1<B0<B0<Z0>>>>>;
213}
214
215impl ToInt for Constant<37> {
216    type Output = B1<B0<B1<B0<B0<Z0>>>>>;
217}
218
219impl ToInt for Constant<38> {
220    type Output = B0<B1<B1<B0<B0<Z0>>>>>;
221}
222
223impl ToInt for Constant<39> {
224    type Output = B1<B1<B1<B0<B0<Z0>>>>>;
225}
226
227impl ToInt for Constant<40> {
228    type Output = B0<B0<B0<B1<B0<Z0>>>>>;
229}
230
231impl ToInt for Constant<41> {
232    type Output = B1<B0<B0<B1<B0<Z0>>>>>;
233}
234
235impl ToInt for Constant<42> {
236    type Output = B0<B1<B0<B1<B0<Z0>>>>>;
237}
238
239impl ToInt for Constant<43> {
240    type Output = B1<B1<B0<B1<B0<Z0>>>>>;
241}
242
243impl ToInt for Constant<44> {
244    type Output = B0<B0<B1<B1<B0<Z0>>>>>;
245}
246
247impl ToInt for Constant<45> {
248    type Output = B1<B0<B1<B1<B0<Z0>>>>>;
249}
250
251impl ToInt for Constant<46> {
252    type Output = B0<B1<B1<B1<B0<Z0>>>>>;
253}
254
255impl ToInt for Constant<47> {
256    type Output = B1<B1<B1<B1<B0<Z0>>>>>;
257}
258
259impl ToInt for Constant<48> {
260    type Output = B0<B0<B0<B0<P1>>>>;
261}
262
263impl ToInt for Constant<49> {
264    type Output = B1<B0<B0<B0<P1>>>>;
265}
266
267impl ToInt for Constant<50> {
268    type Output = B0<B1<B0<B0<P1>>>>;
269}
270
271impl ToInt for Constant<51> {
272    type Output = B1<B1<B0<B0<P1>>>>;
273}
274
275impl ToInt for Constant<52> {
276    type Output = B0<B0<B1<B0<P1>>>>;
277}
278
279impl ToInt for Constant<53> {
280    type Output = B1<B0<B1<B0<P1>>>>;
281}
282
283impl ToInt for Constant<54> {
284    type Output = B0<B1<B1<B0<P1>>>>;
285}
286
287impl ToInt for Constant<55> {
288    type Output = B1<B1<B1<B0<P1>>>>;
289}
290
291impl ToInt for Constant<56> {
292    type Output = B0<B0<B0<B1<P1>>>>;
293}
294
295impl ToInt for Constant<57> {
296    type Output = B1<B0<B0<B1<P1>>>>;
297}
298
299impl ToInt for Constant<58> {
300    type Output = B0<B1<B0<B1<P1>>>>;
301}
302
303impl ToInt for Constant<59> {
304    type Output = B1<B1<B0<B1<P1>>>>;
305}
306
307impl ToInt for Constant<60> {
308    type Output = B0<B0<B1<B1<P1>>>>;
309}
310
311impl ToInt for Constant<61> {
312    type Output = B1<B0<B1<B1<P1>>>>;
313}
314
315impl ToInt for Constant<62> {
316    type Output = B0<B1<B1<B1<P1>>>>;
317}
318
319impl ToInt for Constant<63> {
320    type Output = B1<B1<B1<B1<P1>>>>;
321}
322
323impl ToInt for Constant<64> {
324    type Output = B0<B0<B0<B0<B0<B0<Z0>>>>>>;
325}
326
327/* ========== 负数表示规则 ==========
328 * 1. 最高有效位(MSB)在内,最低位(LSB)在外,依次嵌套
329 * 2. 最内层必须是 N1,表示负数的负号位,也表示本位及更高位为1
330 * 3. 数值部分使用补码表示,使用 B0/B1 表示二进制位
331 * 
332 * 例如 -5 (补码 1011):
333 *   N1 <- MSB (符号位和终止符,也表示本位为1,更高位都是1)
334 *   B0
335 *   B1
336 *   B1 <- LSB
337 * 表示为: B1<B1<B0<N1>>>
338 */
339impl ToInt for Constant<-1> {
340    type Output = N1;
341}
342
343impl ToInt for Constant<-2> {
344    type Output = B0<N1>;
345}
346
347impl ToInt for Constant<-3> {
348    type Output = B1<B0<N1>>;
349}
350
351impl ToInt for Constant<-4> {
352    type Output = B0<B0<N1>>;
353}
354
355impl ToInt for Constant<-5> {
356    type Output = B1<B1<B0<N1>>>;
357}
358
359impl ToInt for Constant<-6> {
360    type Output = B0<B1<B0<N1>>>;
361}
362
363impl ToInt for Constant<-7> {
364    type Output = B1<B0<B0<N1>>>;
365}
366
367impl ToInt for Constant<-8> {
368    type Output = B0<B0<B0<N1>>>;
369}
370
371impl ToInt for Constant<-9> {
372    type Output = B1<B1<B1<B0<N1>>>>;
373}
374
375impl ToInt for Constant<-10> {
376    type Output = B0<B1<B1<B0<N1>>>>;
377}
378
379impl ToInt for Constant<-11> {
380    type Output = B1<B0<B1<B0<N1>>>>;
381}
382
383impl ToInt for Constant<-12> {
384    type Output = B0<B0<B1<B0<N1>>>>;
385}
386
387impl ToInt for Constant<-13> {
388    type Output = B1<B1<B0<B0<N1>>>>;
389}
390
391impl ToInt for Constant<-14> {
392    type Output = B0<B1<B0<B0<N1>>>>;
393}
394
395impl ToInt for Constant<-15> {
396    type Output = B1<B0<B0<B0<N1>>>>;
397}
398
399impl ToInt for Constant<-16> {
400    type Output = B0<B0<B0<B0<N1>>>>;
401}
402
403impl ToInt for Constant<-17> {
404    type Output = B1<B1<B1<B1<B0<N1>>>>>;
405}
406
407impl ToInt for Constant<-18> {
408    type Output = B0<B1<B1<B1<B0<N1>>>>>;
409}
410
411impl ToInt for Constant<-19> {
412    type Output = B1<B0<B1<B1<B0<N1>>>>>;
413}
414
415impl ToInt for Constant<-20> {
416    type Output = B0<B0<B1<B1<B0<N1>>>>>;
417}
418
419impl ToInt for Constant<-21> {
420    type Output = B1<B1<B0<B1<B0<N1>>>>>;
421}
422
423impl ToInt for Constant<-22> {
424    type Output = B0<B1<B0<B1<B0<N1>>>>>;
425}
426
427impl ToInt for Constant<-23> {
428    type Output = B1<B0<B0<B1<B0<N1>>>>>;
429}
430
431impl ToInt for Constant<-24> {
432    type Output = B0<B0<B0<B1<B0<N1>>>>>;
433}
434
435impl ToInt for Constant<-25> {
436    type Output = B1<B1<B1<B0<B0<N1>>>>>;
437}
438
439impl ToInt for Constant<-26> {
440    type Output = B0<B1<B1<B0<B0<N1>>>>>;
441}
442
443impl ToInt for Constant<-27> {
444    type Output = B1<B0<B1<B0<B0<N1>>>>>;
445}
446
447impl ToInt for Constant<-28> {
448    type Output = B0<B0<B1<B0<B0<N1>>>>>;
449}
450
451impl ToInt for Constant<-29> {
452    type Output = B1<B1<B0<B0<B0<N1>>>>>;
453}
454
455impl ToInt for Constant<-30> {
456    type Output = B0<B1<B0<B0<B0<N1>>>>>;
457}
458
459impl ToInt for Constant<-31> {
460    type Output = B1<B0<B0<B0<B0<N1>>>>>;
461}
462
463impl ToInt for Constant<-32> {
464    type Output = B0<B0<B0<B0<B0<N1>>>>>;
465}
466
467impl ToInt for Constant<-33> {
468    type Output = B1<B1<B1<B1<B1<B0<N1>>>>>>;
469}
470
471impl ToInt for Constant<-34> {
472    type Output = B0<B1<B1<B1<B1<B0<N1>>>>>>;
473}
474
475impl ToInt for Constant<-35> {
476    type Output = B1<B0<B1<B1<B1<B0<N1>>>>>>;
477}
478
479impl ToInt for Constant<-36> {
480    type Output = B0<B0<B1<B1<B1<B0<N1>>>>>>;
481}
482
483impl ToInt for Constant<-37> {
484    type Output = B1<B1<B0<B1<B1<B0<N1>>>>>>;
485}
486
487impl ToInt for Constant<-38> {
488    type Output = B0<B1<B0<B1<B1<B0<N1>>>>>>;
489}
490
491impl ToInt for Constant<-39> {
492    type Output = B1<B0<B0<B1<B1<B0<N1>>>>>>;
493}
494
495impl ToInt for Constant<-40> {
496    type Output = B0<B0<B0<B1<B1<B0<N1>>>>>>;
497}
498
499impl ToInt for Constant<-41> {
500    type Output = B1<B1<B1<B0<B1<B0<N1>>>>>>;
501}
502
503impl ToInt for Constant<-42> {
504    type Output = B0<B1<B1<B0<B1<B0<N1>>>>>>;
505}
506
507impl ToInt for Constant<-43> {
508    type Output = B1<B0<B1<B0<B1<B0<N1>>>>>>;
509}
510
511impl ToInt for Constant<-44> {
512    type Output = B0<B0<B1<B0<B1<B0<N1>>>>>>;
513}
514
515impl ToInt for Constant<-45> {
516    type Output = B1<B1<B0<B0<B1<B0<N1>>>>>>;
517}
518
519impl ToInt for Constant<-46> {
520    type Output = B0<B1<B0<B0<B1<B0<N1>>>>>>;
521}
522
523impl ToInt for Constant<-47> {
524    type Output = B1<B0<B0<B0<B1<B0<N1>>>>>>;
525}
526
527impl ToInt for Constant<-48> {
528    type Output = B0<B0<B0<B0<B1<B0<N1>>>>>>;
529}
530
531impl ToInt for Constant<-49> {
532    type Output = B1<B1<B1<B1<B0<B0<N1>>>>>>;
533}
534
535impl ToInt for Constant<-50> {
536    type Output = B0<B1<B1<B1<B0<B0<N1>>>>>>;
537}
538
539impl ToInt for Constant<-51> {
540    type Output = B1<B0<B1<B1<B0<B0<N1>>>>>>;
541}
542
543impl ToInt for Constant<-52> {
544    type Output = B0<B0<B1<B1<B0<B0<N1>>>>>>;
545}
546
547impl ToInt for Constant<-53> {
548    type Output = B1<B1<B0<B1<B0<B0<N1>>>>>>;
549}
550
551impl ToInt for Constant<-54> {
552    type Output = B0<B1<B0<B1<B0<B0<N1>>>>>>;
553}
554
555impl ToInt for Constant<-55> {
556    type Output = B1<B0<B0<B1<B0<B0<N1>>>>>>;
557}
558
559impl ToInt for Constant<-56> {
560    type Output = B0<B0<B0<B1<B0<B0<N1>>>>>>;
561}
562
563impl ToInt for Constant<-57> {
564    type Output = B1<B1<B1<B0<B0<B0<N1>>>>>>;
565}
566
567impl ToInt for Constant<-58> {
568    type Output = B0<B1<B1<B0<B0<B0<N1>>>>>>;
569}
570
571impl ToInt for Constant<-59> {
572    type Output = B1<B0<B1<B0<B0<B0<N1>>>>>>;
573}
574
575impl ToInt for Constant<-60> {
576    type Output = B0<B0<B1<B0<B0<B0<N1>>>>>>;
577}
578
579impl ToInt for Constant<-61> {
580    type Output = B1<B1<B0<B0<B0<B0<N1>>>>>>;
581}
582
583impl ToInt for Constant<-62> {
584    type Output = B0<B1<B0<B0<B0<B0<N1>>>>>>;
585}
586
587impl ToInt for Constant<-63> {
588    type Output = B1<B0<B0<B0<B0<B0<N1>>>>>>;
589}
590
591impl ToInt for Constant<-64> {
592    type Output = B0<B0<B0<B0<B0<B0<N1>>>>>>;
593}
594
595/*
596pub type AddOne<A> = <A as Add1>::Output;
597
598pub type SubOne<I> = <I as Sub1>::Output;
599
600/// 除法运算的类型别名:`Quot<A, B> = <A as Div<B>>::Output`
601pub type Quot<A, B> = <A as Div<B>>::Output; */