Fix signed addition and subtraction.

This commit is contained in:
2020-02-07 16:03:49 -06:00
parent 89c297525a
commit 6dd32647b5
3 changed files with 65 additions and 32 deletions

View File

@@ -567,8 +567,58 @@ generateSignedCryptonumConversions source otherSizes = concatMap convert otherSi
tuName = mkIdent ("U" ++ show target) tuName = mkIdent ("U" ++ show target)
sEntries = toLit (source `div` 64) sEntries = toLit (source `div` 64)
tEntries = toLit (target `div` 64) tEntries = toLit (target `div` 64)
sTop = toLit ((source `div` 64) - 1)
extensions = map (\ x ->
let xLit = toLit x
in [stmt| res.contents.value[$$(xLit)] = extension; |])
[(source `div` 64)..((target `div` 64) - 1)]
in case compare source target of in case compare source target of
LT -> [ LT -> [
[item|
impl<'a> From<&'a $$sName> for $$tsName {
fn from(x: &$$sName) -> $$tsName {
let mut res = $$tsName::zero();
res.contents.value[0..$$(sEntries)].copy_from_slice(&x.contents.value);
let extension = if x.contents.value[$$(sTop)] & 0x8000_0000_0000_0000 == 0 {
0
} else {
0xFFFF_FFFF_FFFF_FFFFu64
};
$@{extensions}
res
}
}
|],
[item|
impl From<$$sName> for $$tsName {
fn from(x: $$sName) -> $$tsName {
$$tsName::from(&x)
}
}
|],
[item|
impl<'a> TryFrom<&'a $$sName> for $$tuName {
type Error = ConversionError;
fn try_from(x: &$$sName) -> Result<$$tuName,ConversionError> {
if x.is_negative() {
Err(ConversionError::NegativeToUnsigned)
} else {
Ok($$tuName::from(&x.contents))
}
}
}
|],
[item|
impl TryFrom<$$sName> for $$tuName {
type Error = ConversionError;
fn try_from(x: $$sName) -> Result<$$tuName,ConversionError> {
$$tuName::try_from(&x)
}
}
|]
] ]
EQ -> [ EQ -> [
[item| [item|
@@ -618,33 +668,5 @@ generateSignedCryptonumConversions source otherSizes = concatMap convert otherSi
|] |]
] ]
GT -> [ GT -> [
[item|
impl From<$$tuName> for $$sName {
fn from(x: $$tuName) -> $$sName {
$$sName::from(&x)
}
}
|],
[item|
impl<'a> From<&'a $$tuName> for $$sName {
fn from(x: &$$tuName) -> $$sName {
panic!("from1")
}
}
|],
[item|
impl From<$$tsName> for $$sName {
fn from(x: $$tsName) -> $$sName {
$$sName::from(&x)
}
}
|],
[item|
impl<'a> From<&'a $$tsName> for $$sName {
fn from(x: &$$tsName) -> $$sName {
panic!("from2")
}
}
|]
] ]

View File

@@ -35,7 +35,7 @@ safeSignedSubtractOps :: File
safeSignedSubtractOps = File { safeSignedSubtractOps = File {
predicate = \ me others -> (me + 64) `elem` others, predicate = \ me others -> (me + 64) `elem` others,
outputName = "safe_ssub", outputName = "safe_ssub",
isUnsigned = True, isUnsigned = False,
generator = declareSafeSignedSubtractOperators, generator = declareSafeSignedSubtractOperators,
testCase = Just generateSafeSignedTests testCase = Just generateSafeSignedTests
} }
@@ -53,7 +53,7 @@ unsafeSignedSubtractOps :: File
unsafeSignedSubtractOps = File { unsafeSignedSubtractOps = File {
predicate = \ _ _ -> True, predicate = \ _ _ -> True,
outputName = "unsafe_ssub", outputName = "unsafe_ssub",
isUnsigned = True, isUnsigned = False,
generator = declareUnsafeSignedSubtractOperators, generator = declareUnsafeSignedSubtractOperators,
testCase = Just generateUnsafeSignedTests testCase = Just generateUnsafeSignedTests
} }
@@ -166,7 +166,7 @@ declareSafeSignedSubtractOperators bitsize _ =
type Output = $$dname; type Output = $$dname;
fn sub(self, rhs: &$$sname) -> $$dname { fn sub(self, rhs: &$$sname) -> $$dname {
panic!("sub") $$dname{ contents: &self.contents - &rhs.contents }
} }
} }
@@ -254,7 +254,7 @@ declareUnsafeSignedSubtractOperators bitsize _ =
impl<'a> SubAssign<&'a $$sname> for $$sname { impl<'a> SubAssign<&'a $$sname> for $$sname {
fn sub_assign(&mut self, rhs: &Self) { fn sub_assign(&mut self, rhs: &Self) {
panic!("sub_assign") self.contents -= &rhs.contents;
} }
} }

View File

@@ -0,0 +1,11 @@
{
"folders": [
{
"path": "/Users/awick/projects/cryptonum/generation"
},
{
"path": "/Users/awick/projects/cryptonum/src"
}
],
"settings": {}
}