source: proto/s2k/trunk/demo/lib/cast.py @ 4012

Last change on this file since 4012 was 4012, checked in by ksherdy, 5 years ago

Added cast support to demo lib.

File size: 810 bytes
Line 
1#
2# Ken Herdy
3# January 7, 2014
4#
5# cast.py
6#
7# s2k cast expressions.
8#
9# These are smoke-and-mirrors Python implementations of s2k operations
10# for demo purposes only.
11#
12
13from bitutil import *
14
15def sext(fw1, strm, fw2, lgth): 
16        a_bstr = strm_to_bstr(strm,lgth)
17        strm = zext_bstr(fw1, a_bstr, fw2)
18        return bstr_to_strm(strm)
19
20def sext_bstr(fw1, a_bstr, fw2): 
21        s = ""
22        for i in range(0,len(a_bstr),fw):
23                sign = a_bstr[i+fw-1] 
24                s += sign * (fw2-fw1)
25                s += a_bstr[i:i+(fw2-fw1)]
26        return s
27
28def zext(fw1, strm, fw2, lgth): 
29        a_bstr = strm_to_bstr(strm,lgth)
30        strm = zext_bstr(fw1, a_bstr, fw2)
31        return bstr_to_strm(strm)
32
33def zext_bstr(fw1, a_bstr, fw2): 
34        s = ""
35        for i in range(0,len(a_bstr),fw1):
36                s += '0' * (fw2-fw1)
37                s += a_bstr[i:i+(fw2-fw1)]
38        return s
39
40def bitcast(fw1, bstr, fw2): 
41  return bstr
Note: See TracBrowser for help on using the repository browser.