Repository: pytorch
Issue: Inconsistencies when casting to integral types
Labels: triaged,module: type promotion,module: arm,module: int overflow

### 🐛 Describe the bug

Inconsistencies when casting to integral types

```python
import torch

print("== Casting way out of bounds float to integral types")
print(torch.tensor(1e20, dtype=torch.float).to(dtype=torch.bool))
print(torch.tensor(1e20, dtype=torch.float).to(dtype=torch.uint8))  # seems to be clamping
print(torch.tensor(1e20, dtype=torch.float).to(dtype=torch.int8))  # nonsense
print(torch.tensor(1e20, dtype=torch.float).to(dtype=torch.int16))  # nonsense
print(torch.tensor(1e20, dtype=torch.float).to(dtype=torch.int32))  # seems to be clamping
print(torch.tensor(1e20, dtype=torch.float).to(dtype=torch.int64))  # seems to be clamping

print("\n== Casting slightly out of bounds values")
print("==== uint8")
print(torch.tensor(256, dtype=torch.int64).to(dtype=torch.uint8))  # seems to wraparound
print(torch.tensor(256, dtype=torch.float).to(dtype=torch.uint8))  # seems to wraparound

print("==== int8")
print(torch.tensor(128, dtype=torch.int64).to(dtype=torch.int8))  # seems to wraparound
print(torch.tensor(128, dtype=torch.float).to(dtype=torch.int8))  # seems to wraparound

print("==== int16")
print(torch.tensor(32768, dtype=torch.int64).to(dtype=torch.int16))  # seems to wraparound
print(torch.tensor(32768, dtype=torch.float).to(dtype=torch.int16))  # seems to wraparound

print("==== int32")
print(torch.tensor(2147483648, dtype=torch.int64).to(dtype=torch.int32))  # seems to wraparound
print(torch.tensor(2150000000, dtype=torch.int64).to(dtype=torch.int32))  # seems to wraparound
print(torch.tensor(3000000000, dtype=torch.int64).to(dtype=torch.int32))  # seems to wraparound
print(torch.tensor(9000000000, dtype=torch.int64).to(dtype=torch.int32))  # seems to wraparound

print(torch.tensor(2147483648, dtype=torch.float).to(dtype=torch.int32)) # seems to be clamping
print(torch.tensor(2150000000, dtype=torch.float).to(dtype=torch.int32)) # seems to be clamping
print(torch.tensor(3000000000, dtype=torch.float).to(dtype=torch.int32)) # seems to be clamping
print(torch.tensor(9000000000, dtype=torch.float).to(dtype=torch.int32)) # seems to be clamping
```

The results we get, show that when casting a very large float to integral doesn't behave the same across all integral types. Also, we can see that casting an int64 value to int32, or the same value as float to int32, behaves differently.
```
== Casting way out of bounds float to integral types
tensor(True)
tensor(255, dtype=torch.uint8)
tensor(-1, dtype=torch.int8)
tensor(-1, dtype=torch.int16)
tensor(2147483647, dtype=torch.int32)
tensor(9223372036854775807)

== Casting slightly out of bounds values
==== uint8
tensor(0, dtype=torch.uint8)
tensor(0, dtype=torch.uint8)
==== int8
tensor(-128, dtype=torch.int8)
tensor(-128, dtype=torch.int8)
==== int16
tensor(-32768, dtype=torch.int16)
tensor(-32768, dtype=torch.int16)
==== int32
tensor(-2147483648, dtype=torch.int32)
tensor(-2144967296, dtype=torch.int32)
tensor(-1294967296, dtype=torch.int32)
tensor(410065408, dtype=torch.int32)
tensor(2147483647, dtype=torch.int32)
tensor(2147483647, dtype=torch.int32)
tensor(2147483647, dtype=torch.int32)
tensor(2147483647, dtype=torch.int32)
```

### Versions

```
Collecting environment information...
PyTorch version: 2.0.1
Is debug build: False
CUDA used to build PyTorch: None
ROCM used to build PyTorch: N/A

OS: macOS 13.5 (arm64)
GCC version: Could not collect
Clang version: 14.0.0 (clang-1400.0.29.202)
CMake version: Could not collect
Libc version: N/A

Python version: 3.9.6 (default, Oct 18 2022, 12:41:40)  [Clang 14.0.0 (clang-1400.0.29.202)] (64-bit runtime)
Python platform: macOS-13.5-arm64-arm-64bit
Is CUDA available: False
CUDA runtime version: No CUDA
CUDA_MODULE_LOADING set to: N/A
GPU models and configuration: No CUDA
Nvidia driver version: No CUDA
cuDNN version: No CUDA
HIP runtime version: N/A
MIOpen runtime version: N/A
Is XNNPACK available: True

CPU:
Apple M1 Pro

Versions of relevant libraries:
[pip3] numpy==1.24.3
[pip3] torch==2.0.1
[conda] Could not collect
```

cc @ezyang @gchanan @zou3519 @nairbv @mruberry @malfet