小编neo*_*003的帖子

长期未加签名

我想获得在c中添加两个无符号64位整数的进位位。如果需要,我可以使用x86-64 asm。码:

#include <stdio.h>

typedef unsigned long long llu;

int main(void){
  llu a = -1, b = -1;
  int carry = /*carry of a+b*/;
  llu res = a+b;
  printf("a+b = %llu (because addition overflowed), carry bit = %d\n", res, carry);
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

c x86-64 integer-overflow addition extended-precision

12
推荐指数
2
解决办法
326
查看次数

编译器忽略运算符的新分配

我在C ++中编写一个512位整数。对于整数,我使用new关键字从堆中分配内存,但是编译器(MINGW上的g ++版本8.1)似乎错误地对其进行了优化。即编译器命令是:

g++ -Wall -fexceptions -Og -g -fopenmp -std=c++14 -c main.cpp -o main.o

g++ -o bin\Debug\cs.exe obj\Debug\main.o -O0 -lgomp

码:

#include <iostream>
#include <cstdint>
#include <omp.h>

constexpr unsigned char arr_size = 16;
constexpr unsigned char arr_size_half = 8;
void exit(int);

struct uint512_t{
    uint32_t * bytes;
    uint512_t(uint32_t num){
        //The line below is either (wrongfully) ignored or (wrongfully) optimized out
        bytes = new(std::nothrow) uint32_t[arr_size];
        if(!bytes){
            std::cerr << "Error - not enough memory available.";
            exit(-1);
        }
        *bytes = num;
        for(uint32_t …
Run Code Online (Sandbox Code Playgroud)

c++ g++ new-operator

0
推荐指数
2
解决办法
61
查看次数