-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Introduce
<REAL>_TO_<INT>
functions
This commit introduces the `<REAL>_TO_<INT>` library functions, which make use of our implicit truncation feature.
- Loading branch information
Showing
3 changed files
with
266 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
f = open("real_trunc_int.st", "w") | ||
|
||
types_real = ["REAL", "LREAL"] | ||
types_int = ["SINT", "USINT", "INT", "UINT", "DINT", "UDINT", "LINT", "ULINT"] | ||
|
||
template = """ | ||
(******************** | ||
* | ||
* Converts {0} to {1} | ||
* | ||
********************) | ||
FUNCTION {0}_TRUNC_{1} : {1} | ||
VAR_INPUT | ||
in : {0}; | ||
END_VAR | ||
{0}_TRUNC_{1} := in; | ||
END_FUNCTION | ||
""" | ||
|
||
for real in types_real: | ||
for int in types_int: | ||
f.write(template.format(real, int)) | ||
f.write("\n") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,224 @@ | ||
|
||
(******************** | ||
* | ||
* Converts REAL to SINT | ||
* | ||
********************) | ||
FUNCTION REAL_TRUNC_SINT : SINT | ||
VAR_INPUT | ||
in : REAL; | ||
END_VAR | ||
|
||
REAL_TRUNC_SINT := in; | ||
END_FUNCTION | ||
|
||
|
||
(******************** | ||
* | ||
* Converts REAL to USINT | ||
* | ||
********************) | ||
FUNCTION REAL_TRUNC_USINT : USINT | ||
VAR_INPUT | ||
in : REAL; | ||
END_VAR | ||
|
||
REAL_TRUNC_USINT := in; | ||
END_FUNCTION | ||
|
||
|
||
(******************** | ||
* | ||
* Converts REAL to INT | ||
* | ||
********************) | ||
FUNCTION REAL_TRUNC_INT : INT | ||
VAR_INPUT | ||
in : REAL; | ||
END_VAR | ||
|
||
REAL_TRUNC_INT := in; | ||
END_FUNCTION | ||
|
||
|
||
(******************** | ||
* | ||
* Converts REAL to UINT | ||
* | ||
********************) | ||
FUNCTION REAL_TRUNC_UINT : UINT | ||
VAR_INPUT | ||
in : REAL; | ||
END_VAR | ||
|
||
REAL_TRUNC_UINT := in; | ||
END_FUNCTION | ||
|
||
|
||
(******************** | ||
* | ||
* Converts REAL to DINT | ||
* | ||
********************) | ||
FUNCTION REAL_TRUNC_DINT : DINT | ||
VAR_INPUT | ||
in : REAL; | ||
END_VAR | ||
|
||
REAL_TRUNC_DINT := in; | ||
END_FUNCTION | ||
|
||
|
||
(******************** | ||
* | ||
* Converts REAL to UDINT | ||
* | ||
********************) | ||
FUNCTION REAL_TRUNC_UDINT : UDINT | ||
VAR_INPUT | ||
in : REAL; | ||
END_VAR | ||
|
||
REAL_TRUNC_UDINT := in; | ||
END_FUNCTION | ||
|
||
|
||
(******************** | ||
* | ||
* Converts REAL to LINT | ||
* | ||
********************) | ||
FUNCTION REAL_TRUNC_LINT : LINT | ||
VAR_INPUT | ||
in : REAL; | ||
END_VAR | ||
|
||
REAL_TRUNC_LINT := in; | ||
END_FUNCTION | ||
|
||
|
||
(******************** | ||
* | ||
* Converts REAL to ULINT | ||
* | ||
********************) | ||
FUNCTION REAL_TRUNC_ULINT : ULINT | ||
VAR_INPUT | ||
in : REAL; | ||
END_VAR | ||
|
||
REAL_TRUNC_ULINT := in; | ||
END_FUNCTION | ||
|
||
|
||
(******************** | ||
* | ||
* Converts LREAL to SINT | ||
* | ||
********************) | ||
FUNCTION LREAL_TRUNC_SINT : SINT | ||
VAR_INPUT | ||
in : LREAL; | ||
END_VAR | ||
|
||
LREAL_TRUNC_SINT := in; | ||
END_FUNCTION | ||
|
||
|
||
(******************** | ||
* | ||
* Converts LREAL to USINT | ||
* | ||
********************) | ||
FUNCTION LREAL_TRUNC_USINT : USINT | ||
VAR_INPUT | ||
in : LREAL; | ||
END_VAR | ||
|
||
LREAL_TRUNC_USINT := in; | ||
END_FUNCTION | ||
|
||
|
||
(******************** | ||
* | ||
* Converts LREAL to INT | ||
* | ||
********************) | ||
FUNCTION LREAL_TRUNC_INT : INT | ||
VAR_INPUT | ||
in : LREAL; | ||
END_VAR | ||
|
||
LREAL_TRUNC_INT := in; | ||
END_FUNCTION | ||
|
||
|
||
(******************** | ||
* | ||
* Converts LREAL to UINT | ||
* | ||
********************) | ||
FUNCTION LREAL_TRUNC_UINT : UINT | ||
VAR_INPUT | ||
in : LREAL; | ||
END_VAR | ||
|
||
LREAL_TRUNC_UINT := in; | ||
END_FUNCTION | ||
|
||
|
||
(******************** | ||
* | ||
* Converts LREAL to DINT | ||
* | ||
********************) | ||
FUNCTION LREAL_TRUNC_DINT : DINT | ||
VAR_INPUT | ||
in : LREAL; | ||
END_VAR | ||
|
||
LREAL_TRUNC_DINT := in; | ||
END_FUNCTION | ||
|
||
|
||
(******************** | ||
* | ||
* Converts LREAL to UDINT | ||
* | ||
********************) | ||
FUNCTION LREAL_TRUNC_UDINT : UDINT | ||
VAR_INPUT | ||
in : LREAL; | ||
END_VAR | ||
|
||
LREAL_TRUNC_UDINT := in; | ||
END_FUNCTION | ||
|
||
|
||
(******************** | ||
* | ||
* Converts LREAL to LINT | ||
* | ||
********************) | ||
FUNCTION LREAL_TRUNC_LINT : LINT | ||
VAR_INPUT | ||
in : LREAL; | ||
END_VAR | ||
|
||
LREAL_TRUNC_LINT := in; | ||
END_FUNCTION | ||
|
||
|
||
(******************** | ||
* | ||
* Converts LREAL to ULINT | ||
* | ||
********************) | ||
FUNCTION LREAL_TRUNC_ULINT : ULINT | ||
VAR_INPUT | ||
in : LREAL; | ||
END_VAR | ||
|
||
LREAL_TRUNC_ULINT := in; | ||
END_FUNCTION | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// RUN: (%COMPILE %s && %RUN) | %CHECK %s | ||
FUNCTION main | ||
printf('%d$N', REAL_TRUNC_SINT(5.6)); // CHECK: 5 | ||
printf('%d$N', REAL_TRUNC_USINT(5.6)); // CHECK: 5 | ||
printf('%d$N', REAL_TRUNC_INT(5.6)); // CHECK: 5 | ||
printf('%d$N', REAL_TRUNC_UINT(5.6)); // CHECK: 5 | ||
printf('%d$N', REAL_TRUNC_INT(5.6)); // CHECK: 5 | ||
printf('%d$N', REAL_TRUNC_UINT(5.6)); // CHECK: 5 | ||
printf('%d$N', REAL_TRUNC_DINT(5.6)); // CHECK: 5 | ||
printf('%d$N', REAL_TRUNC_UDINT(5.6)); // CHECK: 5 | ||
printf('%d$N', REAL_TRUNC_LINT(5.6)); // CHECK: 5 | ||
printf('%d$N', REAL_TRUNC_ULINT(5.6)); // CHECK: 5 | ||
|
||
printf('%d$N', REAL_TRUNC_SINT(-5.6)); // CHECK: -5 | ||
printf('%d$N', REAL_TRUNC_INT(-5.6)); // CHECK: -5 | ||
printf('%d$N', REAL_TRUNC_DINT(-5.6)); // CHECK: -5 | ||
printf('%d$N', REAL_TRUNC_LINT(-5.6)); // CHECK: -5 | ||
END_FUNCTION |